#	Copyright (c) 2000, 2002 Sun Microsystems, Inc.
#		All Rights Reserved
#
#	@(#)checkinstall.base 1.3 03/05/02 (c) Sun Microsystems, Inc.
#
# checkinstall script for PatchPro
#

PATH=/usr/sadm/bin:${PATH}	# use admin utilities

PKGPARAM=/usr/bin/pkgparam
UPGR_SCRIPT=/tmp/upgr.$PKGINST
UPGR_ADMIN=/tmp/admin.upgr.$PKGINST
DEFAULT_ADMIN=/var/sadm/install/admin/default

NOUPGRMSG="A version of SUNWppro is already installed. At the \
	administrator's request, this will not be upgraded."
UPGRCHK_NOTICE="Conflict approval questions may be displayed. The \
	listed files are the ones that will be upgraded. Please \
	answer \"y\" to these questions if they are presented. \
	If this is part of an automated installation, be sure to \
	provide an install administration file  containing \
	\"conflict=nocheck\". See man pages for pkgadd(1M) and \
	admin(4)."
DEPENDENCIES_MSG="Could not find required packages SUNWapcy or SUNWsdb"

#
# Determine if this product is already installed
#
Product_Is_Installed() {
	/usr/bin/pkginfo -q SUNWppro.\*
	if [ $? -eq 0 ]; then
		OldPkg=`/usr/bin/pkginfo SUNWppro.\* | \
		    /usr/bin/nawk ' { print $2 } '`
		OldVers=`/usr/bin/pkgparam $OldPkg VERSION`
		OldBD=`/usr/bin/pkgparam $OldPkg BASEDIR`
		return 0
	else
		return 1
	fi
}

#
# Determine if upgrade is OK
#
Upgrade_OK() {
	if [ "$UPGRADE_OK" = "true" ]; then
		return 0
	else
		return 1
	fi
}

#
# Set everything up for the upgrade
#
Prepare_Upgrade() {
	# Craft administrative defaults file from current default
	/usr/bin/nawk '
		/conflict/	{ print "conflict=nocheck"; next; }
		/action/	{ print "action=nocheck"; next }
		{ print; } ' < ${DEFAULT_ADMIN} > ${UPGR_ADMIN}

	# Compose the upgrade script
	echo "PATH=\"/usr/sadm/bin:$PATH\"" > $UPGR_SCRIPT
	echo "sleep 3" >> $UPGR_SCRIPT
	echo "echo Now cleaning up old instance of $PKG" >> \
	    $UPGR_SCRIPT
	if [ ${PKG_INSTALL_ROOT} ]; then
		echo "pkgrm -n -a $UPGR_ADMIN -R $PKG_INSTALL_ROOT $OldPkg" >> $UPGR_SCRIPT
	else
		echo "pkgrm -n -a $UPGR_ADMIN $OldPkg" >> $UPGR_SCRIPT
	fi

	echo "rm $UPGR_ADMIN" >> $UPGR_SCRIPT
	echo "rm $UPGR_SCRIPT" >> $UPGR_SCRIPT
	echo "echo Upgrade complete." >> $UPGR_SCRIPT
	echo "exit \$?" >> $UPGR_SCRIPT

	echo "BASEDIR=\"$OldBD\"" >> $ResponseFile
	puttext -l 5 "$UPGRADE_NOTICE"
}

#
# Main section
#
grep quitinstall $1
if [ $? -eq 0 ]; then
	exit 3	# politely suspend at request of request
fi

ResponseFile=$1
. $ResponseFile

# Make the upgrade decision
#if Product_Is_Installed && [ "$UPDATE" != "yes" ]; then
#	if Upgrade_OK; then
#		Prepare_Upgrade
#	else
#		puttext -l 5 "$NOUPGRMSG"
#		exit 3
#	fi
#fi

# Now see if our required packages are in place
DEPENDENCIESFOUND=0
# Find Simple Database
SDBBASEDIR=`${PKGPARAM} SUNWsdb BASEDIR 2> /dev/null`
if [ $? -ne 0 ]; then
    DEPENDENCIESFOUND=1
    SDBBASEDIR=""
fi
echo "SDBBASEDIR=\"$SDBBASEDIR\"" >> $ResponseFile

# Find Authorization Policy
APCYBASEDIR=`${PKGPARAM} SUNWapcy BASEDIR 2> /dev/null`
if [ $? -ne 0 ]; then
    DEPENDENCIESFOUND=1
    APCYBASEDIR=""
fi
echo "APCYBASEDIR=\"$APCYBASEDIR\"" >> $ResponseFile

if [ ${DEPENDENCIESFOUND} -ne 0 ]; then
    puttext -l 5 "$DEPENDENCIES_MSG"
    exit 3
fi

exit 0
