#!/bin/bash
# setup is a script to assist in setting up an EMU server

# check for /usr/local/emu/conf which is a standard place for
# port configuration files, such as 2345.cfg
EMUCONFDIR=/etc/eemu

umask 077

prompt ()
{
        	ANS=""
		# find out if XPG4 \c option is supported in echo
		junk=`echo "test\c" | egrep '\\c' `
		if [ -z "$junk" ];then
        		echo "$1\c"
		else
			echo -n $1
		fi
        	read ANS
}      

hidprompt ()
{
        	ANS=""
		# find out if XPG4 \c option is supported in echo
		junk=`echo "test\c" | egrep '\\c' `
		if [ -z "$junk" ];then
        		echo "$1\c"
		else
			echo -n $1
		fi
		stty -echo
        	read ANS
		stty echo
		echo
}      

echo
echo
echo "-------------------------------------------------------------"
echo "                       EMU Setup v2.0"
echo "-------------------------------------------------------------"
echo

EMUROOTDIR=/var/lib/eemu

prompt "Would you like to create a configuration file for a new port [y|n] (y)?"
case $ANS in
n|N)
     ;;
*)
	PORT=2345
	echo "Ports already configured on this system:"
	for port in `ls $EMUCONFDIR/*.cfg 2>/dev/null`
	do
		port=`basename $port`
		port=`echo $port | cut -d"." -f1`
		echo $port
	done
	prompt "Port number for EMU to listen on: [$PORT] (Hit RETURN for default) ?"
	if [ "$ANS" != "" ];then
		PORT=$ANS
	fi
	if [ -f $EMUCONFDIR/$PORT.cfg ];then
		echo
		echo "Port $PORT is already configured on this system."
		echo "By going ahead, the current config file will be renamed"
		echo "to $EMUCONFDIR/$PORT.cfg.old and the existing configuration"
		echo "will be replaced with a new one."
		prompt "Do you want to continue [y|n] (y)?"
		case $ANS in
		n|N)
			exit 1
			;;
		*)
			cp $EMUCONFDIR/$PORT.cfg $EMUCONFDIR/$PORT.cfg.old
			;;
		esac
	fi

	EMUBASE=$EMUROOTDIR/$PORT
	EMULOGDIR=/var/log/eemu/$PORT
	EMUOUTDIR=$EMUBASE/out
	EMUSCRIPTSDIR=$EMUBASE/scripts
	EMUDBDIR=$EMUBASE/db
	EMUDBFILE=$EMUDBDIR/db$PORT
	EMSGCMD="/usr/bin/emsg1 -n localhost"
	EMUINPUT=$EMUSCRIPTSDIR/input.sh
	EMUDELETE=$EMUSCRIPTSDIR/delete.sh
	EMUOUTPUT=$EMUSCRIPTSDIR/output.sh
	EMUPASSWORD=change-me-now
	hidprompt "EMU password: [$EMUPASSWORD] (Hit RETURN for default) ?"
	if [ "$ANS" != "" ];then
     	EMUPASSWORD=$ANS
	fi

	prompt "Is it OK to generate a config file in $EMUCONFDIR/$PORT.cfg [y|n] (y)?"
	case $ANS in
	n|N)
		exit 1
		;;
	*)
	# create a config file
	# check that config file can be created (permissions ?)
	touch $EMUCONFDIR/$PORT.cfg 2>/dev/null
	if [ $? != 0 ];then
		echo "Cannot create $EMUCONFDIR/$PORT.cfg"
		exit 1
	fi
	echo "
# EMU configuration file
# please do not comment out any parameter

# command to send messages to the EMU manager. This is used by emucleaner
# and eb/xeb. Change localhost to the node name emu is running on
emsgcmd $EMSGCMD

# password to send messages to emu
password  $EMUPASSWORD

# output sction script, set it to null if no action script
output_script  $EMUOUTPUT

# delete action script, set it to null if no action script
delete_script  $EMUDELETE

# input action script, set it to null if no action script
input_script  $EMUINPUT

# directory where log files are stored. Each received message will create
# an entry in a log file
logdir  $EMULOGDIR

# directory where out files are stored. 
outdir  $EMUOUTDIR

# the DBM file name for the EMU database, a .pag and .dir suffix are appended
dbfile  $EMUDBFILE

# interval (in seconds) at which the browser/console scans for new messages
binterval  10

# interval (in seconds) at which emucleaner scans for expired messages.
# Must be less than 60
cinterval  20

# time (in seconds) the emucleaner waits before it starts its activity.
# this will prevent premature message expiration on system reboots
cbootwait  420

" > $EMUCONFDIR/$PORT.cfg 
	chmod 600 $EMUCONFDIR/$PORT.cfg
	;;
	esac
;;
esac

for d in $EMULOGDIR $EMUSCRIPTSDIR $EMUDBDIR $EMUOUTDIR
do
	if [ ! -d $d ];then
		echo "... creating $d"
		mkdir -p  $d
	fi
done
