#!/bin/bash
#
# RedHat Startup script for the HP System Management Homepage Server
# 
# chkconfig: - 85 15
# description: HP System Management Homepage server.
#	     
# processname: hpsmhd
# pidfile: /var/spool/opt/hp/hpsmh/run/httpd.pid
# config: /opt/hp/hpsmh/conf/smhpd.conf
# config: /opt/hp/hpsmh/conf/smhpd.xml
# 
#
# My parent may set this so unset it
unset LD_ASSUME_KERNEL
unset LD_LIBRARY_PATH
#
## VCA Restart Filter 
#
VCATMP=/opt/hp/hpsmh/logs/vcarestart.flag
HPVCAINSTALLCOMPLETED=`echo "$HPVCAINSTALLCOMPLETED" | tr '[A-Z]' '[a-z]'`
HPVCAINSTALLBYVCA=`echo "$HPVCAINSTALLBYVCA" | tr '[A-Z]' '[a-z]'`
if [ "$HPVCAINSTALLCOMPLETED" = "yes" ]; then
   # unset HPVCAINSTALLBYVCA
   if [ ! -f $VCATMP ]; then
     exit 0
   fi
elif [ "$HPVCAINSTALLBYVCA" = "yes" ]; then
     echo $$ >> $VCATMP
     exit 0
fi
[ -f $VCATMP ] && rm -f $VCATMP
#
## Clean out Shared Memory & scache file
#
PID_LOC=/opt/hp/hpsmh/logs/httpd.pid
S_CACHE=/opt/hp/hpsmh/logs/scache
ERRLOG=/var/spool/opt/hp/hpsmh/logs/error_log
#
## Does a PID file exist?
#
if [ -f "$PID_LOC" ]; then
  PID=`cat $PID_LOC`
  #
  ## Check for nonblank PID number
  #
  if [[ "j$PID" != "j" ]]; then
    kill -0 $PID 2>/dev/null
    STATUS=$?
    if [ "$STATUS" == 0  ]; then  
       echo "hpsmhd PID->$PID is running now!" >> $ERRLOG
    else
       echo "hpsmhd PID->$PID is not running!" >> $ERRLOG
       if [ -f "$S_CACHE" ]; then
          echo "Located invalid shared memory file:" >> $ERRLOG
          rm -rvf "$S_CACHE" >> $ERRLOG 2>&1
       fi
       IPCSHMEM=( $(ipcs -pm) )
       element_count=${#IPCSHMEM[@]}
       # Start 9 elements into the array
       ix=9
       while [ $ix -lt "$element_count" ]
       do    
          SHMID=${IPCSHMEM[$ix]}
          let "ix = $ix + 1"
          OWNER=${IPCSHMEM[$ix]}
          let "ix = $ix + 1"
          CRPID=${IPCSHMEM[$ix]}
          let "ix = $ix + 1"
          LAPID=${IPCSHMEM[$ix]}
          let "ix = $ix + 1"
          if [ "$PID" == "$CRPID" ]; then
            echo "Located invalid shared memory block: $SHMID" >> $ERRLOG
            ipcrm shm "$SHMID" >> $ERRLOG 2>&1
          fi
       done
       IPCSEMPH=( $(ipcs -s) )
       element_count=${#IPCSEMPH[@]}
       # AS 2.1 
       ix=10
       while [ "$ix" -lt "$element_count" ]
       do    
          SEMKY=${IPCSEMPH[$ix]}
          let "ix = $ix + 1"
          SEMID=${IPCSEMPH[$ix]}
          let "ix = $ix + 1"
          OWNER=${IPCSEMPH[$ix]}
          let "ix = $ix + 1"
          SEMPM=${IPCSEMPH[$ix]}
          let "ix = $ix + 1"
          SEMAT=${IPCSEMPH[$ix]}
          let "ix = $ix + 1"
          if [ "$OWNER" == "hpsmh" ]; then
             echo "Located invalid semaphore array: $SEMID" >> $ERRLOG
             ipcrm sem "$SEMID" >> $ERRLOG 2>&1
          fi
       done
       # AS 3.0
       ix=9
       while [ "$ix" -lt "$element_count" ]
       do    
          SEMKY=${IPCSEMPH[$ix]}
          let "ix = $ix + 1"
          SEMID=${IPCSEMPH[$ix]}
          let "ix = $ix + 1"
          OWNER=${IPCSEMPH[$ix]}
          let "ix = $ix + 1"
          SEMPM=${IPCSEMPH[$ix]}
          let "ix = $ix + 1"
          SEMAT=${IPCSEMPH[$ix]}
          let "ix = $ix + 1"
          if [ "$OWNER" == "hpsmh" ]; then
             echo "Located invalid semaphore array: $SEMID" >> $ERRLOG
             ipcrm sem "$SEMID" >> $ERRLOG 2>&1
          fi
       done

    fi
  else
    echo ""  
  fi
else
  echo ""
fi
#
## Source function library.
#
. /etc/rc.d/init.d/functions
#
# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Path to the apachectl script, server binary, and short-form for messages.
httpdstart=/opt/hp/hpsmh/sbin/smhstart < /dev/null > /dev/null 2>&1
httpdctl=/opt/hp/hpsmh/sbin/apachectl
httpd=/opt/hp/hpsmh/sbin/hpsmhd
prog=hpsmhd
RETVAL=0

# Find the installed modules and convert their names into arguments httpd
# can use.
moduleargs() {
	moduledir=/opt/hp/hpsmh
	moduleargs=`
	/usr/bin/find ${moduledir} -type f -perm -0100 -name "*.so" | env -i tr '[:lower:]' '[:upper:]' | awk '{\
	gsub(/.*\//,"");\
	gsub(/^MOD_/,"");\
	gsub(/^LIB/,"");\
	gsub(/\.SO$/,"");\
	print "-DHAVE_" $0}'`
	echo ${moduleargs}
}

# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.

start() { 
    
    # Check the value of AUTOSTART_SMH variable
    AUTOSTART_SMH=`cat /opt/hp/hpsmh/conf/smhpd.xml | grep -i "autostart"| sed  's/<[^<>]*>//' | cut -d "<" -f 1`
    
    if [ -n "$AUTOSTART_SMH" ] && [ $AUTOSTART_SMH == "true" ]
    then
        # HPSMH autostart mode is active
	echo -e HPSMH autostart mode is active.
        # Verify if hpsmh autostart daemon is already running
        PROC=`pidof -x /opt/hp/hpsmh/lbin/smhstartd`
	if [ "$PROC" = "" ]
        then
            # Start the HPSMH autostart daemon
	    # Verify if the port 2301 is in use by another process
            INUSE=$(/bin/netstat -an | /bin/grep 2301 | /bin/grep LISTEN )
            if ! [ "$INUSE" = "" ]
            then
                echo -e Restart the hpsmhd to apply the changes ...
                exit 0
            fi
	    echo -e Starting the HPSMH autostart daemon...
            /usr/bin/nohup /opt/hp/hpsmh/lbin/smhstartd > /dev/null 2>&1
	    sleep 1	 
        fi
        
	# Verify if HPSMH has timeout. Do this only if SMH is in autostart URL mode.
        AUTOSTART_SMH=`cat /opt/hp/hpsmh/conf/smhpd.xml | grep -i "autostart"| sed  's/<[^<>]*>//' | cut -d "<" -f 1`
        if [ -n "$AUTOSTART_SMH" ] && [ $AUTOSTART_SMH == "true" ]
        then
            TIMEOUT_SMH=`cat /opt/hp/hpsmh/conf/smhpd.xml | grep -i "timeoutsmh"| sed 's/.*<timeoutsmh>\(.*\)<\/timeoutsmh>.*/\1/' | cut -d "<" -f 1`
            if [ -z "$TIMEOUT_SMH" ] || [ $TIMEOUT_SMH == 0 ]
            then
                # Timeout is disabled
                # Remove file that indicates timeout enabled
                echo -e Timeout is disabled.
                /bin/rm /opt/hp/hpsmh/logs/smh.timestamp > /dev/null 2>&1
            else
                # Timeout is enabled
                # Create file that indicates timeout enabled
                umask 333
                /bin/touch /opt/hp/hpsmh/logs/smh.timestamp
                umask 002
            fi
        fi

        if [ -n "$AUTOSTART_SMH" ] && [ $AUTOSTART_SMH == "true" ]
        then
            if [ -z "$TIMEOUT_SMH" ] || [ $TIMEOUT_SMH == 0 ]
            then
            echo "The System Management HomePage timeout monitor is currently disabled."
            else
                # Start the HPSMH timeout monitor
                # Verify if HPSMH timeout monitor is already running
		PROC=`pidof -x /opt/hp/hpsmh/lbin/timeoutmonitor`
                if [ "$PROC" = "" ]
                then
                    echo "The System Management HomePage timeout monitor is currently enabled ($TIMEOUT_SMH minutes )."
                    /usr/bin/nohup /opt/hp/hpsmh/lbin/timeoutmonitor $TIMEOUT_SMH > /dev/null 2>&1 &
                fi
            fi
        fi
    
        # Verify if hpsmh is already running
        st=`/etc/init.d/hpsmhd status`
        echo $st | grep -i "run" > /dev/null 2>&1
        if [ $? = 0 ]
        then
	    echo
            echo -e $"hpsmhd already running"
        else
            echo -n $"Starting $prog: "
            # Change HMMO permissions
            INITREE='/opt/compaq'
            # Fix for QXCR1000895417
            # Moving the fixperms file from /etc/init.d location.
            source /opt/hp/hpsmh/bin/fixperms
            if [ -d /var/spool/compaq ]
	    then
                chown -R --silent root:hpsmh /var/spool/compaq
                chmod -R --silent 770 /var/spool/compaq
            fi
            # Check for vcagnet type first
            if [ -d /opt/hp/vcagent ]
            then
                if [ -f /opt/hp/vcagent/hpwebsetup -o -f /opt/hp/vcagent/etc/html/cpqhmmd.ini ]
		then
                    INITREE='/opt/hp/vcagent'
                    source /opt/hp/hpsmh/bin/fixperms
                    if [ -d /var/spool/opt/hp/vcagent ]
		    then
                        chown -R --silent root:hpsmh /var/spool/opt/hp/vcagent
                        chmod -R --silent 770 /var/spool/opt/hp/vcagent
                    fi
                fi
            fi
            sleep 1
            daemon $httpdstart
            RETVAL=$?
            echo
            [ $RETVAL = 0 ] && touch /var/lock/subsys/hpsmhd
            return $RETVAL
        fi

    else
        
	# HPSMH autostart mode is disabled
        # Verify if hpsmh autostart daemon is running
        PROC=`pidof -x /opt/hp/hpsmh/lbin/smhstartd`
	if ! [ "$PROC" = "" ]
        then
            # Kill the HPSMH autostart daemon
            kill -9 $PROC
        fi
        # Verify if the timeoutmonitor is running
        PROC=`pidof -x /opt/hp/hpsmh/lbin/timeoutmonitor`
	if ! [ "$PROC" = "" ]
        then
            # Kill the timeoutmonitor
            kill -9 $PROC
        fi

        # Kill the time-out process
        PROC=`pidof -x /opt/hp/hpsmh/lbin/time_out`
	if ! [ "$PROC" = "" ]
        then
            kill -9 $PROC
        fi

        # Verify if hpsmh is already running
        st=`/etc/init.d/hpsmhd status`
        echo $st | grep -i "run" > /dev/null 2>&1
        if [ $? = 0 ]
        then
            echo
            echo -e $"hpsmhd already running"
        else
            echo -n $"Starting $prog: "
            # Change HMMO permissions
            INITREE='/opt/compaq'
            # Fix for QXCR1000895417
            # Moving the fixperms file from /etc/init.d location.
            source /opt/hp/hpsmh/bin/fixperms
            if [ -d /var/spool/compaq ]
	    then
                chown -R --silent root:hpsmh /var/spool/compaq
                chmod -R --silent 770 /var/spool/compaq
            fi
            # Check for vcagnet type first
            if [ -d /opt/hp/vcagent ]
	    then
                if [ -f /opt/hp/vcagent/hpwebsetup -o -f /opt/hp/vcagent/etc/html/cpqhmmd.ini ]
		 then
                     INITREE='/opt/hp/vcagent'
                     source /opt/hp/hpsmh/bin/fixperms
                     if [ -d /var/spool/opt/hp/vcagent ]
                     then
                         chown -R --silent root:hpsmh /var/spool/opt/hp/vcagent
                         chmod -R --silent 770 /var/spool/opt/hp/vcagent
                     fi
                fi
            fi
            sleep 1
            daemon $httpdstart
            RETVAL=$?
            echo
            [ $RETVAL = 0 ] && touch /var/lock/subsys/hpsmhd
            return $RETVAL
        fi
    fi
}

autostart() {

    # Check current user
    USER_NAME=$(/usr/bin/id -u)
    if [ "$USER_NAME" != 0 ]
    then
        echo "ERROR: Only super user (root) is able to start System Management HomePage."
        echo
        exit 0
    fi

    # Verify if HPSMH has timeout. Do this only if SMH is in autostart URL mode.
    AUTOSTART_SMH=`cat /opt/hp/hpsmh/conf/smhpd.xml | grep -i "autostart"| sed  's/<[^<>]*>//' | cut -d "<" -f 1`
    if [ -n "$AUTOSTART_SMH" ] && [ $AUTOSTART_SMH == "true" ]
    then
        TIMEOUT_SMH=`cat /opt/hp/hpsmh/conf/smhpd.xml | grep -i "timeoutsmh"| sed 's/.*<timeoutsmh>\(.*\)<\/timeoutsmh>.*/\1/' | cut -d "<" -f 1`

        if [ -z "$TIMEOUT_SMH" ] || [ $TIMEOUT_SMH -eq 0 ]
        then
            # Timeout is disabled
            # Remove file that indicates timeout enabled
            echo -e Timeout is disabled.
            /bin/rm /opt/hp/hpsmh/logs/smh.timestamp 2> /dev/null/null
        else
            # Timeout is enabled
            # Create file that indicates timeout enabled
            umask 333
            /bin/touch /opt/hp/hpsmh/logs/smh.timestamp
            umask 002
        fi
    fi

    if [ -n "$AUTOSTART_SMH" ] && [ $AUTOSTART_SMH == "true" ]
    then
        if [ -z "$TIMEOUT_SMH" ] || [ $TIMEOUT_SMH -eq 0 ]
        then
           echo "The System Management HomePage timeout monitor is currently disabled."
        else
           # Start the HPSMH timeout monitor
           # Verify if HPSMH timeout monitor is already running
           PROC=`pidof -x /opt/hp/hpsmh/lbin/timeoutmonitor`   
	   if [ "$PROC" = "" ]; then
              echo "The System Management HomePage timeout monitor is currently enabled ($TIMEOUT_SMH minutes )."
              /usr/bin/nohup /opt/hp/hpsmh/lbin/timeoutmonitor $TIMEOUT_SMH > /dev/null 2>&1 &
           fi
        fi
    fi

    echo "Starting the System Management HomePage server ..."
        st=`/etc/init.d/hpsmhd status`
        echo $st | grep -i "run" > /dev/null 2>&1
        if [ $? = 0 ]
	 then
             echo
             echo -e $"hpsmhd already running"
        else
            echo -n $"Starting $prog: "
            # Change HMMO permissions
            INITREE='/opt/compaq'
            # Fix for QXCR1000895417
            # Moving the fixperms file from /etc/init.d location.
            source /opt/hp/hpsmh/bin/fixperms
            if [ -d /var/spool/compaq ]
	    then
                chown -R --silent root:hpsmh /var/spool/compaq
                chmod -R --silent 770 /var/spool/compaq
            fi
            # Check for vcagnet type first
            if [ -d /opt/hp/vcagent ]
	    then
                if [ -f /opt/hp/vcagent/hpwebsetup -o -f /opt/hp/vcagent/etc/html/cpqhmmd.ini ]
 	        then
                    INITREE='/opt/hp/vcagent'
                    source /opt/hp/hpsmh/bin/fixperms
                    if [ -d /var/spool/opt/hp/vcagent ]
                    then
                        chown -R --silent root:hpsmh /var/spool/opt/hp/vcagent
                        chmod -R --silent 770 /var/spool/opt/hp/vcagent
                    fi
                fi
            fi
            sleep 1
            daemon $httpdstart
            RETVAL=$?
            echo
            [ $RETVAL = 0 ] && touch /var/lock/subsys/hpsmhd
            return $RETVAL
        fi
}

stop() {

    # Verify if the timeoutmonitor is running
    PROC=`pidof -x /opt/hp/hpsmh/lbin/timeoutmonitor`
    if ! [ "$PROC" = "" ]
    then
        kill -9 $PROC
    fi

    # Kill the time-out process
    PROC=`pidof -x /opt/hp/hpsmh/lbin/time_out`
    if ! [ "$PROC" = "" ]
    then
        kill -9 $PROC
    fi
	# Verify if hpsmhd is already stopped
        st=`/etc/init.d/hpsmhd status`
        echo $st | grep -i "stop" > /dev/null 2>&1
        if [ $? = 0 ]
	then
            echo -e $"$prog already stopped"
        else
            echo
            echo -n $"Stopping $prog: "
            killproc $httpd
            RETVAL=$?
        rm -f /opt/hp/hpsmh/session/sessions.dir
        rm -f /opt/hp/hpsmh/session/sessions.pag
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/hpsmhd /var/spool/opt/hp/hpsmhd/run/httpd.pid
        fi
}

reload() {
	echo -n $"Reloading $prog: "
	killproc $httpd -HUP
	RETVAL=$?
	echo
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  autostart)
        autostart
	;;
  stop)
	stop
	;;
  status)
    status $httpd
	RETVAL=$?
	;;
  restart)
	stop
	start
	;;
  condrestart)
	if [ -f /var/spool/opt/hp/hpsmh/run/httpd.pid ] ; then
		stop
		start
	fi
	;;
  reload)
    reload
	;;
  configtest)
	$httpd -t -f /opt/hp/hpsmh/conf/smhpd.conf
	RETVAL=$?
	;;
  graceful)
	$httpd -k $@ -f /opt/hp/hpsmh/conf/smhpd.conf
	RETVAL=$?
	;;
  *)
	echo $"Usage: $prog {start|stop|restart|condrestart|reload|graceful|configtest|status}"
	exit 1
esac

exit $RETVAL
