#!/bin/bash

. /etc/rc.d/init.d/functions
. /etc/rc.d/init.d/init_functions
. /etc/profile.d/SetNNEnvVar.sh

# Check that owcimomd and certain FCAPS providers have started correctly and that previous indicators have been set
# and then set the LED status.

/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

if [ $RC1 -eq 0 ] && [ $runningProviders -eq $totalProviders ];then
    touch /var/run/hc_sanity4
    
    if  [ -f /var/run/hc_sanity1 ]  &&
        [ -f /var/run/hc_sanity2 ]  &&
        [ -f /var/run/hc_sanity3 ]; then
        /nn/bin/AddNnuDiagLog -mStartupSequence -lStartupSequence.log -wR -sI -v10908 "System Startup - Configuration Management available"
    else
        /nn/bin/AddNnuDiagLog -mStartupSequence -lStartupSequence.log -wR -sI -v10936 "hc_sanity4 passed - previous condition failed"
    fi
else
    /nn/bin/AddNnuDiagLog -mStartupSequence -lStartupSequence.log -wR -sI -v10937 "hc_sanity4 failed"
fi

exit 0
