#/bin/bash

    sleep 5 

    # 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
        # Verify if hpsmh autostart daemon is already running
		PROC=`pidof -x /opt/hp/hpsmh/lbin/smhstartd`
	        if [ "$PROC" = "" ]
        	then
            		# Start the HPSMH autostart daemon
            		/usr/bin/nohup /opt/hp/hpsmh/lbin/smhstartd > /dev/null 2>&1
        	fi
 

        # Verify if HPSMH has timeout. Do this only if SMH is in autostart URL mode.

        TIMEOUT_SMH=`cat /opt/hp/hpsmh/conf/smhpd.xml | grep -i "timeoutsmh"| sed 's/.*<timeoutsmh>\(.*\)<\/timeoutsmh>.*/\1/' | cut -d "<" -f 1`

                        # 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



       		if [ -z "$TIMEOUT_SMH" ] || [ $TIMEOUT_SMH -eq 0 ]
	       	then
            		# Timeout is disabled
            		# Remove file that indicates timeout enabled
			echo -e Timeout is disabled.
			if [ -f /opt/hp/hpsmh/logs/smh.timestamp ]; then
            			/bin/rm /opt/hp/hpsmh/logs/smh.timestamp > /dev/null 2>&1
			fi

        	else

	    		# Timeout is enabled
            		# Create file that indicates timeout enabled
            		/bin/touch /opt/hp/hpsmh/logs/smh.timestamp


           		# Start the HPSMH timeout monitor
           		# Verify if HPSMH timeout monitor is already running
			PROC=`pidof -x /opt/hp/hpsmh/lbin/timeoutmonitor`
            		if [ "$PROC" = "" ]; then
               			/usr/bin/nohup /opt/hp/hpsmh/lbin/timeoutmonitor $TIMEOUT_SMH > /dev/null 2>&1 &
	    		fi
        	fi

    else

	# 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 hpsmh autostart daemon is already running
	PROC=`pidof -x /opt/hp/hpsmh/lbin/smhstartd`
        if ! [ "$PROC" = "" ]
        then
		kill -9 $PROC
        fi
    fi
