acoeur/bin/_commands/count.sh
2022-12-12 21:05:06 +01:00

53 lines
1.2 KiB
Bash
Executable File

#!/bin/sh
dir=$1 file=$2 from=$3
dest_dir=${dir%%/content/users/*}/data/users/${dir##*/content/users/}
dest_file=$dest_dir/counters.yml
counter=${file%%.*}
if [ ! -e "$dir/_index.md" ]
then
echo "Creating $dir/_index.md"
echo "---
---
" > "$dir/_index.md"
fi
if ! grep -q "^ac_errors: " "$dir/_index.md"
then
sed -i "2,/---/s/---/ac_errors: 0\n---/" "$dir/_index.md"
fi
if ! grep -q "^ac_total: " "$dir/_index.md"
then
sed -i "2,/---/s/---/ac_total: 1\n---/" "$dir/_index.md"
fi
if [ ! -e "$dest_file" ]
then
mkdir -p "$dest_dir"
echo "Creating counter file for ${dir#*/content/}/$file, coming from: $from"
echo "$counter: 1" >> "$dest_file"
elif ! grep -q "$counter: " "$dest_file"
then
echo "$counter: 1" >> "$dest_file"
awk -i inplace "/ac_total: / {\$2=++\$2}; {print}" "$dir/_index.md"
else
# Increment counter
awk -i inplace "/$counter: / {\$2=++\$2}; {print}" "$dest_file"
fi
counter_from=$(grep -o "$from: .*" "$dest_file" | cut -d ' ' -f2)
counter_to=$(grep -o "$counter: .*" "$dest_file" | cut -d ' ' -f2)
echo "from $counter_from to $counter_to"
if [ "$counter_from" -lt "$counter_to" ]
then
echo "ERROR!!!"
awk -i inplace "/ac_errors: / {\$2=++\$2}; {print}" "$dir/_index.md"
fi