#!/bin/bash

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

# If the build is a BASE layer build, there is nothing to check.
# If the build is a TL layer build, check only for CoreTel.
# If the build is a FULL layer build, check for both CoreTel and voicemail.

determine() {
    if [ -f /etc/APPS ]; then
      echo starting full stage components
      /bin/bash /etc/rc.d/init.d/hc_sanity3 FULL

    elif [ -f /etc/FULL ]; then
      echo starting full stage components
      /bin/bash /etc/rc.d/init.d/hc_sanity3 FULL

    elif [ -f /etc/TL ]; then
      echo starting telephony layer stage components
      /bin/bash /etc/rc.d/init.d/hc_sanity3 TL

    elif [ -f /etc/BASE ]; then
      echo starting base stage components
      /bin/bash /etc/rc.d/init.d/hc_sanity3 BASE

    else
      echo starting full stage components
      /bin/bash /etc/rc.d/init.d/hc_sanity3 FULL
    fi
}

full()
{
    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
            /nn/bin/AddNnuDiagLog -mStartupSequence -lStartupSequence.log -wR -sI -v10907 "System Startup - Telephony and Voicemail active"
        else
            /nn/bin/AddNnuDiagLog -mStartupSequence -lStartupSequence.log -wR -sI -v10934 "hc_sanity3 passed - previous condition failed"
        fi
    else
        /nn/bin/AddNnuDiagLog -mStartupSequence -lStartupSequence.log -wR -sI -v10935 "hc_sanity3 failed"
    fi
}

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

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

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

    if [ $RC1 -eq 0 ] && [ $result -eq 1 ];then
        touch /var/run/hc_sanity3

        if  [ -f /var/run/hc_sanity1 ]  &&
            [ -f /var/run/hc_sanity2 ]; then
            /nn/bin/AddNnuDiagLog -mStartupSequence -lStartupSequence.log -wR -sI -v10911 "System Startup - Telephony active"
        else
            /nn/bin/AddNnuDiagLog -mStartupSequence -lStartupSequence.log -wR -sI -v10934 "hc_sanity3 passed - previous condition failed"
        fi
    else
        /nn/bin/AddNnuDiagLog -mStartupSequence -lStartupSequence.log -wR -sI -v10935 "hc_sanity3 failed"
    fi
}


base() {
    echo `date` hc_sanity3 stage bypassed in BASE layer > /var/run/hc_sanity3
    /nn/bin/AddNnuDiagLog -mStartupSequence -lStartupSequence.log -wR -sI -v10912 "System Startup - Telephony and Voicemail are unavailable in BASE layer"
}

case "$1" in
  FULL)
        full
        ;;
  TL)
        tl
        ;;
  BASE)
        base
        ;;
  *)
        determine
        ;;
esac

exit $?

