acoeur/bin/_commands/choice.sh

51 lines
907 B
Bash
Executable File

#!/bin/sh
. ./_commands/_extract.sh
dir=$1 file=$2
page=${file%.choice}
values=$(extract "$dir" "$page.md" ac_choices)
choose() {
dir=$1 file=$2 target=$3
user=${dir#*/users/}
user=${user%%/*}
target=${target#*(}
target=${target%)*}
echo "Choice => $target.md for user $user"
echo cp "../content$target.md" "../content/users/$user${target%/*}/"
cp "../content$target.md" "../content/users/$user${target%/*}/"
# Delete former result files
rm "$dir/*.result"
# Generate a result file
echo "$target" > "$dir/$page.result"
}
index=0
target=$(cat "$dir/$file")
for value in $values
do
if [ $index = "${target#ac_choice=}" ]
then
choose "$dir" "$file" "$value"
break
fi
index=$((index+1))
done
if [ -z "${dir##*/content/users/*}" ]
then
# Remove the originating file
rm "$dir/$page.md"
# And destination directory
rm -rf "${dir%/content/*}/public/${dir#*/content/}/$page/"
fi