#!/bin/sh
#
# Because this stuff is still pretty young, you may wish to run the
# server using this script.  It'll notify if the server crashes and
# restart it after a couple of minutes.  It also puts the core in a known
# place and mails you any output generated by the terminal process.
#
# run it as  'run_msql_daemon msqld' to run the mSQL engine.
#
#					bambi@Bond.edu.au

cfg=/etc/msql.acl
BINDIR=/usr/sbin
TMPDIR=/var/log/msql
INST_DIR=/usr/sbin
ADMIN=`grep '^#[ ]*MailTo=' $cfg|sed 's/^#[ ]*MailTo=\(.*\)$/\1/g'`
PROG=msqld
SUPPRESS=`grep '^#[ ]*SuppressMail=' $cfg|sed 's/^#[ ]*SuppressMail=\(.*\)$/\1/g'`
MSQL_TCP_PORT=`grep '^#[ ]*Port=' $cfg|sed 's/^#[ ]*Port=\(.*\)$/\1/g'`
if [ -n "$MSQL_TCP_PORT" ]; then
  export MSQL_TCP_PORT
fi
MINERVA_DEBUG=`grep '^#[ ]*Debug=' $cfg|sed 's/^#[ ]*Debug=\(.*\)$/\1/g'`
if [ -n "$MINERVA_DEBUG" ]; then
  export MINERVA_DEBUG
fi

rm -f $TMPDIR/*

while :
do
	touch $TMPDIR/${PROG}.$$
	chmod 600 $TMPDIR/${PROG}.$$
	${BINDIR}/${PROG} > $TMPDIR/${PROG}.$$ 2>&1
	echo "Program : ${PROG}
Time : `date`
Program Output
--------------

" > $TMPDIR/mail.tmp
	if [ "$SUPPRESS" = "no" -a -x /usr/lib/sendmail ]; then
		cat $TMPDIR/${PROG}.$$ >> $TMPDIR/mail.tmp
		(echo "Subject: Minerva Daemon Crash Report"; cat $TMPDIR/mail.tmp) \
		| /usr/lib/sendmail -t
	fi
	rm -f $TMPDIR/mail.tmp $TMPDIR/${PROG}.$$
	if [ ! -f /var/run/${PROG}/shutdown ]; then
		sleep 15
	else
		rm -f /var/run/${PROG}/shutdown /var/run/${PROG}/${PROG}.pid
		exit 0
	fi
done
