#!/sbin/sh

########
# Product:  Graphics3DCom  FOR 11.11
# Fileset:  COM-RUN
# checkinstall
#
########
#
# (c) Copyright Hewlett-Packard Company 1994,2004
#
########

	set -a       					# export all vars
	exitval=0					# anticipate success
        FAILURE=1         # anticipate FAILURE being set
        SUCCESS=0         # anticipate SUCCESS being set in the first stinking place!


	UTILS="/usr/lbin/sw/control_utils"
	if [[ ! -f $UTILS ]]
	then
		echo "ERROR:    Cannot find UTILS"
		exit 1
	fi
	. $UTILS

################################################################################
#set -x # For debugging purposes.  _Never_ ship uncommented!!

	##
	## If installing to an alternate root, testing of the TIM
	## daemon is moot.  Therefore, if the SW_ROOT_DIRECTORY is
	## something other than "/", this script can just exit with
	## SUCCESS status.
	##
	if [ ${SW_ROOT_DIRECTORY} != "/" ]
	then
		exit $SUCCESS
	fi

	SW_INSTALL_DIR="/"

	##
	## Test to see if either SW_ROOT_DIRECTORY or SW_LOCATION is
	## set to something other than /.  If so, set up SW_INSTALL_DIR
	## so it will not have spurious /s in the path name.
	##
	
	if [ $SW_ROOT_DIRECTORY != / -o $SW_LOCATION != / ]
	then
		if [ $SW_ROOT_DIRECTORY != / ]
		then
			if [ $SW_LOCATION != / ]
			then
				SW_INSTALL_DIR="${SW_ROOT_DIRECTORY}${SW_LOCATION}"
			else
				SW_INSTALL_DIR="${SW_ROOT_DIRECTORY}"
			fi
		else
			if [ $SW_LOCATION != / ]
			then
				SW_INSTALL_DIR="${SW_LOCATION}"
			fi
		fi
	fi

##################### 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

exit $exitval
