#!/bin/bash

# Disable the watchdog timer during system bootup.
# Disable the watchdog time if the current runleve is 3 (i.e fresh reboot).
# Create a temp file after disabling the watchdog to indicate that the watchdog is 
# disabled during the startup sequence and any further invocation of this script will
# not attempt to disable it again.

runlevelPrev=`/sbin/runlevel | awk '{print $1}'`
runlevelCurr=`/sbin/runlevel | awk '{print $2}'`

if [[ "$runlevelPrev" = "N" && "$runlevelCurr" = "3" ]]
then
   if [ ! -e /tmp/DisbleWatchdog ]
   then
      if [ -e /nn/bin/watchdog ]
      then
         /nn/bin/watchdog set resetenable 0
         if [ $? -ne 0 ]
         then
            echo "[disableHWwatchdog] Failed to disable the Hardware watchdog."
            /usr/bin/logger "[disableHWwatchdog] Failed to disable the Hardware watchdog."
         else
            echo "[disableHWwatchdog] Disabled the Hardware watchdog."
            /usr/bin/logger "[disableHWwatchdog] Disabled the Hardware watchdog."
            echo "Hardware watchdog disabled by psm startupscript !" > /tmp/DisbleWatchdog 
         fi
      else
         echo "[disableHWwatchdog] /nn/bin/watchdog utility doesn't exist. Not disabling the watchdog."
         /usr/bin/logger "[disableHWwatchdog] /nn/bin/watchdog utility doesn't exist. Not disabling the watchdog."
      fi
   fi
fi



/etc/rc.d/init.d/script.template $1 psm
exitPsm=$?


# Enable the Hardware Watchdog so that if the system is stuck while rebooting, the watchdog will
# reset the system.
# The watchdog will be enabled only when the system switches to runlevel 6.


if [ -e /nn/bin/watchdog ]
then

   runLevel=`/sbin/runlevel | awk '{print $2}'`
   if [ 6 -eq $runLevel ]
   then
      echo "[enableHWwatchdog] Enabling Hardware Watchdog to reset the system after 8mins."

# First ensure that PSM is stopped.
      pidPSM=`/sbin/pidof psm | awk '{print $1}'`
      if [ $pidPSM ]
      then
         echo "[enableHWwatchdog] PSM is running, trying to stop."
         /nn/bin/processcontrol stop psm

# If processcontrol has failed to stop psm then kill it.      
         pidPSM_1=`/sbin/pidof psm | awk '{print $1}'`
         if [ $pidPSM_1 ]
         then
            sleep 1
            /usr/bin/killall -9 psm
         fi
      fi

# Get the watchdog settings from PDR. These values are used to revert back the PDR entries as watchdog
# utility will modify the PDR.
      timeoutPDR=0
      resetenablePDR=""
      timeoutPDR=`/nn/bin/pdrcmd get "nortel networks\ssm persistence\watchdogtimeoutminutes"`
      resetenablePDR=`/nn/bin/pdrcmd get "nortel networks\ssm persistence\resetenable"`

# Enable the HW Watchdog to reset the system after 8mins.
      /nn/bin/watchdog set resetenable 1
      if [ $? -ne 0 ]
      then
         echo "[enableHWwatchdog] Failed to enable the Hardware watchdog."
      else
         echo "[enableHWwatchdog] Enabled the Hardware watchdog."
      fi

      /nn/bin/watchdog set timeout 8
      if [ $? -ne 0 ]
      then
         echo "[enableHWwatchdog] Failed to set the Hardware watchdog timeout value."
      else
         echo "[enableHWwatchdog] Hardware watchdog timeout is set to 8min."
      fi

# Delete the tmp file that was created while disabling the watchdog during startup.
      /bin/rm /tmp/DisbleWatchdog -f 

# Restore the watchdog setting in PDR. If the entry was not present previously then detete the entries created
# by watchdog utility.
      if [ "$timeoutPDR" != "Retrieve Failed" ]
      then
         echo "[enableHWwatchdog] Reverting back the Hardware watchdog timeout PDR entry - $timeoutPDR min."
         /nn/bin/pdrcmd modify "nortel networks\ssm persistence\watchdogtimeoutminutes" $timeoutPDR
      else
         echo "[enableHWwatchdog] PDR didn't contained timeout entry. Deleting the entry created by watchdog"
         /nn/bin/pdrcmd delete "nortel networks\ssm persistence\watchdogtimeoutminutes"
      fi

      if [ "$resetenablePDR" !=  "Retrieve Failed" ]
      then
         echo "[enableHWwatchdog] Reverting back the Hardware watchdog enable PDR entry - $resetenablePDR."
         /nn/bin/pdrcmd modify "nortel networks\ssm persistence\resetenable" $resetenablePDR
      else
         echo "[enableHWwatchdog] PDR didn't contained resetenable entry. Deleting the enrty created by watchdog."
         /nn/bin/pdrcmd delete "nortel networks\ssm persistence\resetenable"
      fi
   fi
else
   echo "[enableHWwatchdog] /nn/bin/watchdog utility doesn't exist. Not enabling the watchdog."
fi

# Return the exit code of PSM.
exit $exitPsm
