#!/bin/bash

# Check that owcimomd and certain FCAPS providers have 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
}

/sbin/pidof owcimomd
RC1=$?

# Determine which FCAPS providers are expected to be started.
fgrep "check process" /etc/monitrc | grep ProviderAgent > /tmp/startup/hc_sanity4.tmp

rm -f /var/log/hc_sanity4.log
cp -a /tmp/startup/hc_sanity4.tmp /var/log/hc_sanity4.log

TIMEOUT=15
count=0

# Loop on the following section until all FCAPS providers are running or a timeout has expired.
while [ "$count" -lt $TIMEOUT ];do
    ls -l /proc/*/exe  2>/dev/null > /tmp/startup/hc_sanity4_processlist.txt

    totalProviders=0
    runningProviders=0
    while read providerName;do
        providerName=${providerName##"check process "}
        let "totalProviders += 1"
        fgrep "/$providerName" /tmp/startup/hc_sanity4_processlist.txt > /dev/null
        if [ $? -eq 0 ]; then
            let "runningProviders += 1"
        fi
    done < /tmp/startup/hc_sanity4.tmp

    echo `date` total providers= $totalProviders >> /var/log/hc_sanity4.log
    echo `date` running providers= $runningProviders >> /var/log/hc_sanity4.log

    if [ $runningProviders -eq $totalProviders ];then
        count=$TIMEOUT
    else
        sleep 5
        let "count += 5"
    fi
done

totalProviders=`expr $totalProviders - 1`


if [ $RC1 -eq 0 ] && [ $runningProviders -ge $totalProviders ];then
    touch /var/run/hc_sanity4
    
    if  [ -f /var/run/hc_sanity1 ]  &&
        [ -f /var/run/hc_sanity2 ]  &&
        [ -f /var/run/hc_sanity3 ]; 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 -v10908 "System Startup - Configuration Management available"
        else
            sanityLog -mStartupSequence -lStartupSequence.log -wR -sI -v10918 "System Startup - Configuration Management available. Separate error detected."
        fi
    else
        sanityLog -mStartupSequence -lStartupSequence.log -wR -sI -v10936 "hc_sanity4 passed - previous condition failed"
    fi
else
    sanityLog -mStartupSequence -lStartupSequence.log -wR -sI -v10937 "hc_sanity4 failed"
fi

exit 0
