#	Copyright (c) 2000 Sun Microsystems, Inc.
#		All Rights Reserved
#
#	@(#)checkinstall.base 1.1 02/10/11 (c) Sun Microsystems, Inc.
#
# checkinstall script for PatchPro
#

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

UPGR_SCRIPT=/tmp/upgr.$PKGINST
UPGR_ADMIN=/tmp/admin.upgr.$PKGINST
DEFAULT_ADMIN=/var/sadm/install/admin/default

NOUPGRMSG="A version of SUNWppcfg 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)."
PREVIOUS_PKG_MSG_PREFIX="The PatchPro base configuration package \
	cannot be installed because a proprietary configuration package"
PREVIOUS_PKG_MSG_SUFFIX="is already in place. That package configures \
	PatchPro for some special function such as the maintenance of \
	a storage array or a special computer cluster. Determine if \
	that package is necessary for proper functioning of this host \
	and pkgrm it if it is not."
POLLUTED_PKG_MSG="The PatchPro base configuration package cannot be \
	installed because a proprietary configuration is present but \
	no package is associated with it. It is likely that the \
	/etc/opt/SUNWppro directory should be deleted but first satisfy \
	yourself that the directory isn't there for a good reason."

#
# Determine if this product is already installed
#
Product_Is_Installed() {
	/usr/bin/pkginfo -q SUNWppcfg.\*
	if [ $? -eq 0 ]; then
		OldPkg=`/usr/bin/pkginfo SUNWppcfg.\* | \
		    /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"
}

#
# Determine what the other package is. If this returns "", there is no other package.
#
Get_Other() {
	/usr/sbin/pkgchk -lp /etc/opt/SUNWppro/security/cacerts | \
	/usr/bin/nawk '
		 /following packages:/	{ getline; print $1; };
	'
}

#
# 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; then
	if [ "$UPDATE" != "yes" ]; then
		if Upgrade_OK; then
			Prepare_Upgrade
		
		else
			puttext -l 5 "$NOUPGRMSG"
			exit 3
		fi
	fi
else
	if [ -f /etc/opt/SUNWppro/security/cacerts ]; then
		OTHERPKG=`Get_Other`
		if [ "$OTHERPKG" = "" ]; then
			puttext -l 5 "$POLLUTED_PKG_MSG"
		else
			puttext -l 5 "${PREVIOUS_PKG_MSG_PREFIX}, ${OTHERPKG}, \
				${PREVIOUS_PKG_MSG_SUFFIX}"
		fi
		exit 3
	fi
fi

exit 0
