#!/bin/bash

. $MODULES_DIRECTORY/config
. $MODULES_DIRECTORY/main.dml

#
# It loades modules list from a file. Currently we have modules list
# hardcoded so it does nothing.
#

load_modules_list()
{
	return 0;
}

show_menu()
{
	eval `echo "$DIALOG_main_menu" | $DML`
	module=$res
	return 0;
}

execute_module()
{
	case $module in
		1)
			/bin/bash $MODULES_DIRECTORY/adduser
		;;
		2)
			/bin/bash $MODULES_DIRECTORY/deluser
		;;
		3)
			/bin/bash $MODULES_DIRECTORY/moduser
		;;
		4)
			/bin/bash $MODULES_DIRECTORY/configure
		;;
		5)
			return 0;
		;;
	esac
	return 1;
}

#
# Function checking if BOFH is run as root and if every directory and file
# mentioned in configfile exists.
#

sanity_checks() {
	
	if [ "`id -u`" -ne 0 ]; then
	        echo $"You need to run BOFH as root (uid=0)."
		exit 255
	fi
	
	for i in $CONFIG_DIRECTORY/config $MODULES_DIRECTORY/{adduser,adduser.dml,common.dml,config,configure,configure.dml,deluser,deluser.dml,functions,main.dml,moduser,moduser.dml}; do
		if [ ! -f $i ]; then
			echo $"Your installation of BOFH is incomplete. File $i is missing."
			exit 255
		fi
	done

	if [ ! -d $CFG_ARCHIVE ]; then
		echo $"Archive directory $CFG_ARCHIVE doesn't exist or is not a directory."
		exit 255
	fi

	for i in $DMLEXEC $USERADD $USERDEL $USERMOD $CHPASSWD $TAR; do
		if [ ! -x $i ]; then
			echo $"File $i mentioned in config doesn't exist or is not executable."
			exit 255
		fi
	done
}

sanity_checks
RETVAL=$?

if [ $RETVAL -gt 0 ]; then
	exit 255;
fi

clear

load_modules_list

finish=0

while [ $finish -eq 0 ]; do

	show_menu
	execute_module
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then finish=1; fi

done

