#!/bin/bash
# Written by: Behan Webster

COURSE=$(basename "$(pwd)")

find . -type l -name '[0-9]*' -print0 | xargs -0 --no-run-if-empty rm -f

[[ $1 == clean ]] && exit

CH=0
# shellcheck disable=SC2013
for DIR in $(grep subimport "$COURSE.tex" | egrep -v '^%' | sed -re 's|.*\{(.*)\/}\{index\}|\1|') ; do
	CH=$(( CH + 1 ))
	NAME=$(sed -e 's/^[^_]*_//' <<< "$DIR")
	# shellcheck disable=SC2046
	eval $(printf "ln -s $DIR %02d_%s;\n" $CH "$NAME")
done

