#!/bin/bash

# Check that CoreTel and voicemail 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
}

sanitycheck()
{
    TIMEOUT=60
    count=0
    result=0

    while [ "$count" -lt $TIMEOUT ];do
        /sbin/pidof CoreTel
        RC1=$?
        /sbin/pidof voicemail
        RC2=$?

        if [ -f /var/run/CoreTel.ini ] && [ -f /var/run/voicemail.ini ];then
            count=$TIMEOUT
            result=1
        else
            let "count += 5"
            sleep 5
        fi
    done

    if [ $RC1 -eq 0 ] && [ $RC2 -eq 0 ] && [ $result -eq 1 ];then
        touch /var/run/hc_sanity3
    
        if  [ -f /var/run/hc_sanity1 ]  &&
            [ -f /var/run/hc_sanity2 ]; 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.
            cat /proc/nn/led | grep "LED1: (1,0)" > /dev/null
            if [ $? -ne 0 ] && [ -e /proc/nn/led ]; then
                sanityLog -mStartupSequence -lStartupSequence.log -wR -sI -v10907 "System Startup - Telephony and Voicemail active"
            else
                sanityLog -mStartupSequence -lStartupSequence.log -wR -sI -v10917 "System Startup - Telephony and Voicemail active. Separate error detected."
            fi
        else
            sanityLog -mStartupSequence -lStartupSequence.log -wR -sI -v10934 "hc_sanity3 passed - previous condition failed"
        fi
    else
        sanityLog -mStartupSequence -lStartupSequence.log -wR -sI -v10935 "hc_sanity3 failed"
    fi
}

case "$1" in
  start)
        sanitycheck
        ;;
  *)
        echo $"Usage: $0 {start}"
        exit 1
        ;;
esac

exit $?

