#!/bin/bash

#---------------------------------------------------------------------
#  Scribbler
#  Usage: scribbler add|remove spell from-grimoire to-grimoire [to-section]
#---------------------------------------------------------------------

. /etc/sorcery/config

##  CODEX is already defined in /etc/sorcery/config.
#CODEX=/var/lib/sorcery/codex

##  I would prefer to use my own private/custom grimoire and section names and
##  not necessarily predifined grimore and section names.  Or at least place the
##  spell in the same section name of the new grimoire as the one it is pulled
##  from.  Also, until private is in /etc/sorcery/local/grimoires, nothing done
##  with scribble will have any effect.
#mkdir -p $CODEX/private/assorted



usage ()
{
cat << EOF

Scribbler is a utility for controlling the spells in your private grimoire.

Example: scribbler add bash test stable
Usage:
scribbler add spell from-grimoire to-grimoire [to-section]
	copies a spell from one grimoire to
	another grimoire into the specified section.
	The to-section is the same as the source section if unspecified
	
scribbler remove spell from-grimoire
	removes a spell from a grimoire

EOF
}



##  This script needed some sanity checking...
if [ "${1}" == "" ]; then
	usage
	exit 1
fi

if [ "${1}" == "add" ] && [ "${4}" == "" ]; then
	usage
	exit 1
fi

if [ "${2}" == "" ]; then
	usage
	exit 1
fi

if [ "${3}" == "" ]; then
	usage
	exit 1
fi

if [ "${3}" == "${4}" ] && [ "${5}" == "" ]; then
	message "${PROBLEM_COLOR}The from and to grimoires cannot be the same name unless you specify a to-section.${DEFAULT_COLOR}"
	usage
	exit 1
fi


##  Set and validate the from-grimoire name
FROM_GRIMOIRE_DIRECTORY=$(codex_get_all_grimoires | grep $3)
if [ "${FROM_GRIMOIRE_DIRECTORY}" == "" ]; then
	message "${PROBLEM_COLOR}The grimoire you specified (${SPELL_COLOR}${3}${PROBLEM_COLOR}) is not a valid grimoire.${DEFAULT_COLOR}"
	exit 1
fi

##  Set and validate the spell name
SPELL_DIRECTORY=$(codex_cache_spell_lookup $2 $FROM_GRIMOIRE_DIRECTORY)
if [ "${SPELL_DIRECTORY}" == "" ]; then
	message "${PROBLEM_COLOR}The spell you specified (${SPELL_COLOR}${2}${PROBLEM_COLOR}) is not a valid spell of the specified grimoire (${SPELL_COLOR}${3}${PROBLEM_COLOR}).${DEFAULT_COLOR}"
	exit 1
fi



##  Change to root user.  Psuedo copied from cast.
if [ "${UID}" != 0 ]; then
	echo "Enter the root password, please:"
	exec su -c "${0} ${*}" root
fi

mk_tmp_dirs scribbler

##  Get to the root of the issue
case $1 in

	add)
		##  Set and validate (and possibly create) the to-grimoire name
		TO_GRIMOIRE_DIRECTORY=$(codex_get_all_grimoires | grep $4)
		if [ "${TO_GRIMOIRE_DIRECTORY}" == "" ]; then
			message "${PROBLEM_COLOR}The grimoire you specified (${SPELL_COLOR}${4}${PROBLEM_COLOR}) is not a valid grimoire.${DEFAULT_COLOR}"
			if query "Shall I add it for you?" y; then
				##  Because codex_add_grimoire does not create the directory
				mkdir -p ${CODEX}/${4}
				codex_add_grimoire ${CODEX}/${4} 0
				TO_GRIMOIRE_DIRECTORY=$(codex_get_all_grimoires | grep $4)
			else
				exit 1
			fi
		fi

		codex_set_current_spell $SPELL_DIRECTORY

		##  Validate and create the link
		if [ "${5}" == "" ]; then
			if [ -e ${TO_GRIMOIRE_DIRECTORY}/${SECTION}/${2} ]; then
				message "${PROBLEM_COLOR}The spell you specified (${SPELL_COLOR}${2}${PROBLEM_COLOR}) already exists in the specified grimoire/section (${SPELL_COLOR}${4}/${SECTION}${PROBLEM_COLOR}).${DEFAULT_COLOR}"
			fi

			if ! [ -e ${TO_GRIMOIRE_DIRECTORY}/${SECTION} ]; then
				mkdir -p ${TO_GRIMOIRE_DIRECTORY}/${SECTION}
			fi

			ln -s ${FROM_GRIMOIRE_DIRECTORY}/${SECTION}/${2} ${TO_GRIMOIRE_DIRECTORY}/${SECTION}
			##  An alternate action (maybe this could be an switch: -c to copy rather than link):
#			cp -r ${FROM_GRIMOIRE_DIRECTORY}/${SECTION}/${2} ${TO_GRIMOIRE_DIRECTORY}/${SECTION}
		else
			if [ -e ${TO_GRIMOIRE_DIRECTORY}/${5}/${2} ]; then
				message "${PROBLEM_COLOR}The spell you specified (${SPELL_COLOR}${2}${PROBLEM_COLOR}) already exists in the specified grimoire/section (${SPELL_COLOR}${4}/${5}${PROBLEM_COLOR}).${DEFAULT_COLOR}"
			fi

			if ! [ -e ${TO_GRIMOIRE_DIRECTORY}/${5} ]; then
				mkdir -p ${TO_GRIMOIRE_DIRECTORY}/${5}
			fi

			ln -s ${FROM_GRIMOIRE_DIRECTORY}/${SECTION}/${2} ${TO_GRIMOIRE_DIRECTORY}/${5}
			##  An alternate action (maybe this could be an switch: -c to copy rather than link):
#			cp -r ${FROM_GRIMOIRE_DIRECTORY}/${SECTION}/${2} ${TO_GRIMOIRE_DIRECTORY}/${5}
		fi

		scribe reindex > /dev/null
		;;

	remove)
		codex_set_current_spell ${SPELL_DIRECTORY}
		##  This would need to change if this script were to account for dirs:
		rm ${FROM_GRIMOIRE_DIRECTORY}/${SECTION}/${2}
		scribe reindex > /dev/null
		;;

	*)
		usage
		;;

esac

cleanup_tmp_dir $TMP_DIR
exit 0
