#! /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'
configfile="$moodledir/$configfilename"
passwordhash=
password=
passwordsaltmain=
user=
msg=

. shell-error

TEMP=`getopt -n "$PROG" -o -c:,h:,m:,p:,s:,u:, -l config:,passwordhash:,msg:,password:,passwordsaltmain:,user:, -- "$@"`
eval set -- "$TEMP"

while :; do
	case "$1" in
		--)
			shift
			break
			;;
		-c|--config)
			shift
			configfile="$1"
			;;
		-h|--passwordhash)
			shift
			passwordhash="$1"
			;;
		-m|--msg)
			shift
			msg="$1"
			;;
		-p|--password)
			shift
			password="$1"
			;;
		-s|--passwordsaltmain)
			shift
			passwordsaltmain="$1"
			;;
		-u|--user)
			shift
			user="$1"
			;;
		*) fatal "Unrecognized option: $1"
			;;
	esac
	shift
done

if [ -z "$user" ]; then
	user=`mt-install-lsdefaults \
		sed -rn 's/^adminuser\t//p'`
fi

if [ -n "$passwordsaltmain" ]; then
	mt-setCFG ${configfile:+--file="$configfile"} ${msg:+--msg="$msg"} passwordsaltmain "$passwordsaltmain"
fi

mt-setuserpasswordhash ${configfile:+--file="$configfile"} --user=$user ${passwordhash:+--passwordhash="$passwordhash"} ${password:+--password="$password"}
