#!/bin/sh
#
# %W% %E%
#
# Copyright (c) 1999 Sun Microsystems Inc.
# All Rights Reserved
#

error() {
	echo "$SCRIPT: $ERROR: $*"
	exit 3
}

SCRIPT=`echo $0 | sed 's,.*/,,'`

echo $SCRIPT: Starting ...

# Since the 'incompatibles' relation over patches is symmetric, we've got to
# implement the desired installation configuration valiadtion in here
# installpatch iff patch C installed *and* QFE not installed
# Error if QFE already installed
QFEPID="`/usr/sbin/patchadd -p | sed -n 's,^Patch: \(107778-..\).*,\1,p'`"
[ "$QFEPID" ] &&
	ERRSTR="Incompatible with QFE patch ($QFEPID) - uninstall QFE patch"

JPID="`/usr/sbin/patchadd -p | sed -n 's,^Patch: \(107369-1[67]\),\1,p'`"
[ "$JPID" ] || {
	ERRSTR="Incompatible jumbo patch version - install patch 'C' 107369-16 or 107369-17"
}

[ "$ERRSTR" ] && error "$ERRSTR"

SLEEP_TIME=60
times_up() {
	exit 0
}

trap 'times_up' ALRM
( sleep $SLEEP_TIME ; kill -ALRM $$ >/dev/null 2>&1 ) &
child_pid=$!

echo "$SCRIPT: Do you wish to continue this installation n/[y] ?"
echo "$SCRIPT: (by default, this installation attempt will continue in $SLEEP_TIME seconds)"

read RESP
while [ 1 ]
do
        case $RESP in
                n | no | N | NO | No)
                        kill -9 $child_pid >/dev/null 2>&1
                        exit 1 ;;
                "" | y | yes | Y | Yes | YES)
                        kill -9 $child_pid >/dev/null 2>&1
			break ;;
                *)  echo "yes or no please. \c"
                        read RESP ;;
        esac
done

# Allocate the rootdirectory according to installation location
[ "$ROOTDIR" = / ] && unset ROOTDIR || ROOT="-R $ROOTDIR"

# Should get these from packaging?
ETCCMS=$ROOTDIR/etc/SUNWcms
USRCMS=$ROOTDIR/usr/platform/SUNW,Ultra-4FT/SUNWcms
CMSCFG=$ETCCMS/.config

#
# Certain of the FCS deliverablews cause great grief when installing this
# patch, so adjust the package database in an attempt to circumvent the problem
# Firstly, provide a workround for 41????? and remove the extraneous cmsd
# binary from SUNWcmscu
removef $ROOT SUNWcmscu $USRCMS/lib/cmsd || exit 1
removef $ROOT -f SUNWcmscu || exit 1

# Remove the conflicting symlink from SUNWftcau
removef $ROOT SUNWftcau $ROOTDIR/usr/platform/SUNW,Ultra-4FT/lib/prtdiag >/dev/null 2>&1 || exit 1
removef $ROOT -f SUNWftcau || exit 1

# Ensure the both the CMS conf and rule files are known to the database - let
# patch/pkg management take care of saving/restoration
installf $ROOT -c none SUNWcmsdf $USRCMS/etc/cmsdef.d/cmsdef.conf ||
	error "installf(1m) failed"
installf -f $ROOT -c none SUNWcmsdf || 
	error "installf(1m) failed"

MONLCK=$ETCCMS/.locks/cmsmonitord.lock
CMSLCK=$ETCCMS/.locks/cmsd.lock

for F in $MONLCK $CMSLCK; do
	[ -f "$F" ] || continue

	COUNT=1
	KILLPID=`cat $F`
	while kill $KILLPID > /dev/null 2>&1
	do
		sleep 2
		COUNT=`expr $COUNT + 1`
		[ $COUNT -gt 10 ] || continue

		DAEMON=`echo $F | sed 's,.*/,,; s,\..*,,'`
		echo "$SCRIPT: INFO: Cannot closedown $DAEMON, killing with extremme prejudice ..." > /dev/console
		kill SIGKILL $KILLPID > /dev/null 2>&1 && break
	done
done

exit 0

