#!/bin/sh

if [ x"$LISTSERV_EXIT_STATUS_FILE" = x"" ]; then
echo Do not invoke this script by hand. Use the script "go" 
exit
fi

errorcode=
loop=1

stoploop(){
loop=
}

# start execution loop (15.0)
while [ $loop ]; do

# remove any extant crash.status file (15.0)
rm -f $LISTSERV_EXIT_STATUS_FILE

# start LISTSERV 
./lsv

# evaluate the exit status file (15.0)

if [ -s $LISTSERV_EXIT_STATUS_FILE ]
then
    # LISTSERV was stopped with a STOP command or otherwise aborted cleanly.
    #xfields=`awk '{ print NF }' $LISTSERV_EXIT_STATUS_FILE`
    errorcode=`awk '/^[^0]/{print $1}' $LISTSERV_EXIT_STATUS_FILE`
    action=`awk '/^0 /{if ($3 == "REBOOT") { print "echo >>> RE-IPLing LISTSERV." } else { print "stoploop"}}  /^[^0]/{ print "stoploop" }' $LISTSERV_EXIT_STATUS_FILE`


    if [ x"$action" = x"" ]
    then
    	loop=
    fi

    $action

else
    echo No $LISTSERV_EXIT_STATUS_FILE found! 
    loop=
fi

done

if [ $errorcode ]
then
	echo -n "Abnormal exit: "
	cat $LISTSERV_EXIT_STATUS_FILE
fi
