#!/bin/bash
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2004,2007 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 

# "@(#)71   1.6   src/csm/hw/hs/startHDWR_SVR, csm.hardware, csm_rfish, rfishs001b 7/8/05 15:14:51"

# Start the hdwr_svr
#

if [ -f /var/hsc/log/.DEBUG_NETC ]; then
   export NETC_DEBUG_TRACE=NETC_DEBUG_TRACE_ON
fi


# check for backup CSM
# if it is backup CSM, just exit and do nothing
if [ -f /opt/csm/csmbin/ha_daemon_start ]
then
    /opt/csm/csmbin/ha_daemon_start
    if [ $? -eq 1 ]
    then
        exit 1
    fi
fi


# Check if hdwr_svr is a already a subsystem before trying to stop it.
# 
`LC_ALL=C /usr/bin/lssrc -s hdwr_svr >/dev/null 2>&1`
if [[ $? = 0 ]]
      then
           echo "Subsystem hdwr_svr is already active, attempting to restart hdwr_svr."
        /usr/bin/stopsrc -s hdwr_svr

        #
        # Make sure hdwr_svr has stopped. It will most likely stop within 20
        # seconds, if not sooner.
        #
        subsys_checked=0
        hdwr_svr_stopped=`LC_ALL=C /usr/bin/lssrc -s hdwr_svr | /bin/grep inoperative`
        rc=$?

        while [[ $rc != 0 && $subsys_checked -lt 20 ]]
          do
            ((subsys_checked=$subsys_checked+1))
            sleep 1
            hdwr_svr_stopped=`LC_ALL=C /usr/bin/lssrc -s hdwr_svr | /bin/grep inoperative`
            rc=$?
        done

        if [[ $subsys_checked = 20 ]]
          then

          if [[ -n `LC_ALL=C /usr/bin/lssrc -s hdwr_svr | /bin/grep active` ]]; then
              echo "Unable to stop hwsagent, forcing hdwr_svr to terminate."
              # Kill hdwr_svr daemon if still running
             ps_result=`/bin/ps -ef | /bin/grep hdwr_svr | /bin/grep opt | /bin/awk {'print $2'}`
             kill -9 $ps_result

          #   /usr/bin/stopsrc -s hdwr_svr >/dev/null
          fi

          #
          # Make sure hdwr_svr has stopped. It will most likely stop within 20
          # seconds, if not sooner.
          #
          subsys_checked=0
          hdwr_svr_stopped=`LC_ALL=C /usr/bin/lssrc -s hdwr_svr | /bin/grep inoperative`
          rc=$?

          while [[ $rc != 0 && $subsys_checked -lt 20 ]]
            do
              ((subsys_checked=$subsys_checked+1))
              sleep 1
              hdwr_svr_stopped=`LC_ALL=C /usr/bin/lssrc -s hdwr_svr | /bin/grep inoperative`
              rc=$?
          done

          if [[ $subsys_checked = 20 ]]
            then
            echo "Unable to stop hdwr_svr subsystem."
              exit 1
          fi
        fi
     
        # hdwr_svr subsystem stopped, remove subsystem.
        #
        /usr/bin/rmssys -s hdwr_svr >/dev/null 2>&1
fi

# Create hdwr_svr subsystem 
# Specify SIGTERM (15) and SIGKILL (9) as the signorm and sigforce signals.
#
/usr/bin/mkssys -s hdwr_svr -p /opt/hsc/hdwr_svr/hdwr_svr -u 0 -S -n 15 -f 9 -R
rc=$?
if [ $rc != 0 ]
    then
    echo "Unable to create hdwr_svr subsystem."
fi


# Start hdwr_svr subsystem
#
/usr/bin/startsrc -s hdwr_svr
rc=$?
if [ $rc != 0 ]
    then
    echo "Unable to start hdwr_svr subsystem."
fi

/opt/hsc/hdwr_svr/reg_peer_event 1>/var/hsc/log/reg_peer_event.out 2>&1


