#! /sbin/sh 
##################################################################
# This script should not be executed after boot becaue it
# may report system reboot and cause confusion.
#
# availmon always collects data irrespective of whether
# the data is sent outside or not.
#
# availmon's data is always logged into system support
# database.  Please refer to the man page of ESP
# for more information.
# 
# This init script identifies any Controlled shutdowns,
# panics or other unscheduled shutdowns.
##################################################################


COMMAND=$0
AMRVERSIONNUM=2.1
AVAILDIR=/var/adm/avail
SAVEDIR=$AVAILDIR/.save
USRETCDIR=/usr/etc
AMTIME1970=$USRETCDIR/amtime1970
EVENTFILE=$SAVEDIR/event
SUEVENTFILE=$SAVEDIR/suevent
SCRATCHFILE=$AVAILDIR/init.scratch

case $1 in
'start')
    if [ ! -x $USRETCDIR/amstart ] ; then
	echo "\n$CMD: ERROR: Can't find $USRETCDIR/amstart."
	echo "Executable files corrupted.  Please try re-installing eoe"
	logger -t availmon -p err -i cannot find $USERETCDIR/amstart
    else
	$USRETCDIR/amstart 2>&1 &
    fi
    ;;
'stop')

    if [ ! -d $SAVEDIR ] ; then
	exit
    fi

    if [ -x $AMTIME1970 ] ; then
	SINCE1970=`$AMTIME1970`
    else
	SINCE1970=-1
    fi

    if [ -x /usr/etc/eventmond ] ; then
        /usr/etc/eventmond -a off >/dev/null 2>&1 
    fi

    if [ -f $SCRATCHFILE ] ; then
	EVENTCODE=`cat $SCRATCHFILE`
	if [ "$EVENTCODE" -gt 2097197 -o "$EVENTCODE" -lt 2097169 -o "$EVENTCODE" -eq 2097171 ] ; then
	    EVENTCODE=2097171
        fi
	rm -f $SCRATCHFILE
    else
	EVENTCODE=2097169
    fi

    if [ "$EVENTCODE" -ge 2097190 ] ; then
	echo "$EVENTCODE|$SINCE1970" > $SUEVENTFILE
    else
	echo "$EVENTCODE|$SINCE1970" > $EVENTFILE
    fi
    ;;

*)
    echo "$0: This script is normally run during startup/shutdown and shouldnot be run manually"

esac
