#!/bin/bash

# Check that DiaLogger has started correctly and that previous indicators have been set
# and then set the LED status.

sanityLog()
{
    /nn/bin/AddNnuDiagLog "$@"
    if [ $? -ne 0 ];then
        echo `date` $0 sanity script failed to log message using AddNnuDiagLog "$@" >> /var/log/StartupSequence.log
    fi
}

# if  /sbin/pidof monit -eq 0 &&
#     /sbin/pidof crond -eq 0; then
/sbin/pidof DiaLogger 
RC1=$?

# make sure that the network interfaces are up
inetaddr="inet addr"
eth0Status=`/sbin/ifconfig eth0 | grep "$inetaddr"`
eth1Status=`/sbin/ifconfig eth1 | grep "$inetaddr"`
eth2Status=`/sbin/ifconfig eth2 | grep "$inetaddr"`
loStatus=`/sbin/ifconfig lo | grep "$inetaddr"`

ifConfigured=0
if [ "$eth0Status" != "" ] && [ "$eth1Status" != "" ] && 
    [ "$eth2Status" != "" ] && [ "$loStatus" != "" ]; then
    ifConfigured=1
fi

# make sure that /etc/hosts file is configured properly
hostsConfigured=0
if [ -s /etc/hosts ]; then
    hostnameRC=`hostname`
    hostnameiRC=`hostname -i`
    hostnamedRC=`hostname -d`

    if [ "$hostnameRC" != "" ] && [ "$hostnameiRC" != "" ] && [ "$hostnamedRC" != "" ]; then
	hostsConfigured=1
    fi
fi

if [ $RC1 -eq 0 ] && [ -f /var/run/DiaLogger.ini ] &&
    [ $ifConfigured -eq 1 ] && [ $hostsConfigured -eq 1 ]; then
    touch /var/run/hc_sanity2
    
    if  [ -f /var/run/hc_sanity1 ];then
        # Examine the current setting of the Status LED "LED1".
        # If set to Solid Red "(1,0)", use alternate alarm to prevent override of alarm condition.
        # If /proc/nn/led does not exist, treat as alternate alarm so alarm system still notified of startup status.
        cat /proc/nn/led | grep "LED1: (1,0)" > /dev/null
        if [ $? -ne 0 ] && [ -e /proc/nn/led ]; then
            sanityLog -mStartupSequence -lStartupSequence.log -wR -sI -v10906 "System Startup - DiaLogger managing LEDs"
        else
            sanityLog -mStartupSequence -lStartupSequence.log -wR -sI -v10916 "System Startup - DiaLogger managing LEDs. Separate error detected."
        fi
    else
        sanityLog -mStartupSequence -lStartupSequence.log -wR -sI -v10932 "hc_sanity2 passed - previous condition failed"
    fi
else
    sanityLog -mStartupSequence -lStartupSequence.log -wR -sI -v10933 "hc_sanity2 failed"
fi

exit 0
