#!/bin/bash

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

# Check that Telephony and voicemail has 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 FULL layer build, check for Telephony and voicemail.
# If the build is a TL layer build, check for Telephony.

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 MscService
        RC1=$?
	/sbin/pidof cfsserver
        RC2=$?
	/sbin/pidof Msm
        RC3=$?
        /sbin/pidof voicemail
        RC4=$?

        if [ -f /var/run/MscService.ini ] && [ -f /var/run/cfsserver.ini ] && [ -f /var/run/Msm.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 ] && [ $RC3 -eq 0 ] && [ $RC4 -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 MscService
        RC1=$?
	/sbin/pidof cfsserver
        RC2=$?
	/sbin/pidof Msm
        RC3=$?

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

    if [ $RC1 -eq 0 ] && [ $RC2 -eq 0 ] && [ $RC3 -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 $?

