#! /bin/sh -efu

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

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

moodledir='/var/www/webapps/moodle'
configfilename='config.php'
configfile="$moodledir/$configfilename"
domainsdir="/var/lib/moodle"
delimiter='.'
fullpath=

. shell-error

TEMP=`getopt -n "$PROG" -o d:,f:,m: -l delimiter:,file:,full-path -- "$@"`
eval set -- "$TEMP"

while :; do
	case "$1" in
		--)
			shift
			break
			;;
		-d|--delimiter)
			shift
			delimiter="$1"
			;;
		-f|--file)
			shift
			configfile="$1"
			;;
		--full-path)
			fullpath='yes'
			;;
		*) fatal "Unrecognized option: $1"
			;;
	esac
	shift
done

if [ ! -r "$configfile" ]; then
	fatal "No reading $configfile!"
fi

dbtype=`mt-getCFG ${configfile:+--file="$configfile"} dbtype 2>/dev/null ||:`
dbname=`mt-getCFG ${configfile:+--file="$configfile"} dbname 2>/dev/null ||:`
dataroot=`mt-getCFG ${configfile:+--file="$configfile"} dataroot 2>/dev/null ||:`

if [ -z "$fullpath" ]; then
	datadirname=`echo $dataroot \
		| sed "s@^$domainsdir/@@
			s/\//_/g"`
else
	datadirname="$dataroot"
fi

echo -e "$dbtype$delimiter$dbname$delimiter$datadirname"
