#! /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:,full-path -- "$@"`
eval set -- "$TEMP"

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

cd "$moodledir"
origin=`mt-install-getactiveconfig --moodledir="$moodledir" --configname="$configfilename"`

{
	if [ -n "$configfilename" ] && [ "$configfilename" = "$origin" ]; then
		printf '%s\n' "$configfilename"
	fi
	find . -mindepth 1 -maxdepth 1 -name "$configfilename.*" -printf '%P\n'
} | while read -r name
do
	if [ "$name" = "$origin" ]; then
		act='Yes'
	else
		act='no'
	fi

	suffix=`mt-getconfigsuffix --delimiter='\t' ${fullpath:+--full-path} --file="$name"`

	printf '%s\t%s\t%s\n' "$name" "$act" "$suffix"
done
