#! /bin/sh -efu

#
# Moodle install tools
#
# @package   mt-install
# @copyright 2012 Aleksey Avdeev <solo@altlinux.ru>
# @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v2 or later
#

PROG="${0##*/}"

PATH="/usr/sbin:$PATH"

moodledir='/var/www/webapps/moodle'
configfilename='config.php'
fullpath=

. shell-error

TEMP=`getopt -n "$PROG" -o m:,n: -l moodledir:,configname: -- "$@"`
eval set -- "$TEMP"

while :; do
	case "$1" in
		--)
			shift
			break
			;;
		-m|--moodledir)
			shift
			moodledir="$1"
			;;
		-n|--configname)
			shift
			configfilename="$1"
			;;
		*) fatal "Unrecognized option: $1"
			;;
	esac
	shift
done

configfile="$1"
shift

configfile=`printf '%s\n' "$configfile" \
	| sed -r "s@^[[:space:]]*$moodledir/([^/[:space:]]+)[[:space:]]*\\$@\\1@"`

cd "$moodledir"

if [ -e "$configfilename" ]; then
	if [ ! -L "$configfilename" ];then
		mt-install-configfiletosymlink --config="$configfilename"
	fi

	rm -f "$configfilename"
fi

ln -sf "$configfile" "$configfilename"
