#!/sbin/sh
#------------------------------------------------------------------#
#
# HPUX_ID: @(#) $Revision: 18.3 $ generated on Fri Jun  1 01:11:43 MDT 2001
#
# (c)Copyright 1983-2001 Hewlett-Packard Co.,  All Rights Reserved.
#
#------------------------------------------------------------------#
# SD checkinstall script template for 11.X patches.                #
#------------------------------------------------------------------#
# This file is optional for all 11.X patches.  The purpose of this #
# script is to ensure that the target system has no patch specific #
# conditions which would cause either an installation failure or a #
# runtime failure.  There must be no side effects from running     #
# this script, since it is run before there is any commitment to   #
# installation.                                                    #
#------------------------------------------------------------------#

_PATCHID=PHSS_23546

UTILS="/usr/lbin/sw/control_utils"

if [ ! -f $UTILS ]; then
    echo "ERROR:   Cannot find $UTILS"
    exit 1
fi

. $UTILS
exitval=$SUCCESS

#####
##### Insert any code you may require at this point in the script.
##### Pay attention to the exitval variable and set appropriately
##### in your code.  If you are unsure as to what types of operations
##### are legal in this file and what types aren't, consult the 
##### "Guidelines for SD Control Scripts" document.
#####
##### START_CUSTOMIZATION_HERE
##################### begin checkinstall ###########################

##
## Check the status of /opt/graphics/common/lbin/timd.  This daemon
## must be in a quiescent state for install to proceed.  Find the
## timd daemon process id and send timd a "kill -16" signal.  timd
## will write its status report in /var/tmp/timd_status.  If timd
## is not busy, the report will contain:
##
##    Received a kill -16, TIM is gracefully exiting
##
## If timd is busy, the report will contain:
##
##    At least one process is open for texturing on Visualize-48
##    Please remove the process at PID: 5252
##

TIMD_STATUS_FILE="${SW_ROOT_DIRECTORY}var/tmp/timd_status"

TIMD_PPID=`ps -ef | \
	grep timd | \
	grep daemon | \
	sed -e "s/^[ 	][ 	]*//g" | \
	sed -e "s/[ 	][ 	]*/:/g" | \
	cut -f2 -d:`
	
if [ "X$TIMD_PPID" = "X" ]
then
	##
	## No timd process id to be found so it is assumed to
	## not be currently running.  Exit with success status
	## so installation will proceed.
	##

	exitval=$SUCCESS

else
	##
	## The timd daemon is running so it needs to signaled
	## to write its status out to the status file.  Only
	## by checking the contents can it be determined if the
	## timd is busy or has exited gracefully.
	##

	for NEXT_TIMD_PPID in ${TIMD_PPID}
	do
            TIMD_CLIENT_PPID=""
	    TIMD_QUIESCENT=""

	    rm -f ${TIMD_STATUS_FILE} 
	    ####kill -16 ${TIMD_PPID}
	    kill -16 ${NEXT_TIMD_PPID}
	    sleep 1

	    if [ -f $TIMD_STATUS_FILE  ]
	    then
		TIMD_CLIENT_PPID=`\
			grep "PID:" $TIMD_STATUS_FILE |\
			sed -e "s/[ 	][ 	]*//g" |\
			cut -f2 -d:`
		TIMD_QUIESCENT=`grep "gracefully" $TIMD_STATUS_FILE`
		rm -f ${TIMD_STATUS_FILE}

	    else
		echo "ERROR:   The TIM daemon status file, ${TIMD_STATUS_FILE},"
		echo "         was not found despite the signal 16 being"
		echo "         sent to the TIM daemon, ${NEXT_TIMD_PPID}"
		echo "         to report its current"
		echo "         state of being in use or quiescent.  Installation"
		echo "         of ${PRODUCT}.${FILESET} will be disallowed."
		exitval=$FAILURE
	    fi

	    if [ "X$TIMD_CLIENT_PPID" != "X" ]
	    then
		echo "ERROR:   The TIM daemon, ${NEXT_TIMD_PPID} is currently"
		echo "         in use by process id"
		echo "         ${TIMD_CLIENT_PPID}.  This process must be killed or shut down"
		echo "         before ${PRODUCT}.${FILESET} may be installed"
		echo "         on the system.  Until the TIM daemon reports that"
		echo "         it is not in use by any texture mapping client"
		echo "         process, ${PRODUCT}.${FILESET} may not be"
		echo "         installed nor will any other product or fileset"
		echo "         which has a dependency on ${PRODUCT}.${FILESET}."
		exitval=$FAILURE
	    fi
	done
fi
###################### end checkinstall ############################

exit $exitval
