#!/bin/sh -x
#
#ident "@(#)checkinstall   1.8     99/09/26 SMI"
#
# Copyright (c) 1999 by Sun Microsystems, Inc.
#

#
# figure out CLASSES to install
#
CLASSES="none preserve"
COPY_CONFIG="false"
PATH=/usr/sadm/bin:$PATH

UPGR_SCRIPT=/tmp/upgr.$$
UPGR_ADMIN=/tmp/admin.$$

IS_AN_UPGRADE="false"

UPGRADE_NOTICE="This is an upgrade. 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."

# This constructs a trailer script for removing the old version
# of this package after this new version is installed
mk_new_trailer() {	# $1 is the old packages instance
	# Now make the script that will run after this install is done.
	echo "PATH=/usr/sadm/bin:$PATH; export PATH" > $UPGR_SCRIPT
	echo "echo" >> $UPGR_SCRIPT
	echo "puttext \"After installation of ${PKGINST}, the prior version ($1) will be removed in order to complete the upgrade. Please stand by.\"" >> $UPGR_SCRIPT

	if [ ${PKG_INSTALL_ROOT} ]; then
		echo "pkgrm -n -a ${UPGR_ADMIN} -R $PKG_INSTALL_ROOT $1" >> $UPGR_SCRIPT
	else
		echo "pkgrm -n -a ${UPGR_ADMIN} $1" >> $UPGR_SCRIPT
	fi

	echo "rm $UPGR_ADMIN" >> $UPGR_SCRIPT
	echo "echo Upgrade of $PKG is complete." >> $UPGR_SCRIPT
	echo "rm $UPGR_SCRIPT" >> $UPGR_SCRIPT
	echo "exit 0" >> $UPGR_SCRIPT
}

# This constructs a trailer script for removing the old version
# of this package after this new version is installed. This particular
# function deals with the very old DiskSuite packages that don't have
# smart postremove scripts.
mk_old_trailer() {	# $1 is the old packages instance
	echo "PATH=/usr/sadm/bin:$PATH; export PATH" > $UPGR_SCRIPT

	echo "echo" >> $UPGR_SCRIPT
	echo "puttext \"After installation of ${PKGINST}, the prior version(s) ($*) will be removed in order to complete the upgrade. Please stand by.\"" >> $UPGR_SCRIPT

	for Pkginst in $*; do
		PKGDIR=${PKG_INSTALL_ROOT}/var/sadm/pkg/$Pkginst/install

		# This is a desperate measure but well controlled. Since
		# this is an old package with no understanding of upgrade
		# the only way to safely remove it is to delete the scripts
		# that will do the damage.
		echo "if [ -d $PKGDIR ]; then" >> $UPGR_SCRIPT
		echo "rm -rf $PKGDIR/preremove || exit 1" >> $UPGR_SCRIPT
		echo "rm -rf $PKGDIR/postremove || exit 1" >> $UPGR_SCRIPT
		echo "rm -rf $PKGDIR/r.preserve || exit 1" >> $UPGR_SCRIPT
		echo "fi" >> $UPGR_SCRIPT
	done

	if [ ${PKG_INSTALL_ROOT} ]; then
		echo "pkgrm -n -a ${UPGR_ADMIN} -R $PKG_INSTALL_ROOT $*" >> $UPGR_SCRIPT
	else
		echo "pkgrm -n -a ${UPGR_ADMIN} $*" >> $UPGR_SCRIPT
	fi

	echo "rm $UPGR_ADMIN" >> $UPGR_SCRIPT
	echo "echo Upgrade of $PKG is complete." >> $UPGR_SCRIPT
	echo "rm $UPGR_SCRIPT" >> $UPGR_SCRIPT
	echo "exit 0" >> $UPGR_SCRIPT
}

# This determines the version of the package we're replacing and
# sets up the environment for that.
setup_upgrade() {	# $1 is old pkginst

	InstanceCount=0
	# Count installed instances
	for inst in $1; do
		InstanceCount=`expr $InstanceCount + 1`
	done

	# More than one installed instance means lots of cleanup.
	# Use the brute force method for multiple pkgrm's.
	if [ $InstanceCount -gt 1 ]; then
		OldVCode=41
	else
		OldVCode=`pkgparam -R ${PKG_INSTALL_ROOT:-/} $1 VERSION | cut -d, -f 1 | sed s/\\\\.//g | cut -c1-3`
	fi

	case "${OldVcode}" in
		421)
			#check if its beta. If so set the classes 
			# since locations of files have changed.
			pkgparam -R ${PKG_INSTALL_ROOT:-/} $1 VERSION | \
					grep -i beta > /dev/null 2>&1
			if [ $? = 0 ]; then
				CLASSES="${CLASSES} legacy"
				COPY_CONFIG="true"
			fi
			;;
		
		*)
			COPY_CONFIG="true"
			CLASSES="${CLASSES} legacy"
		  	;; 
	esac

	echo 'mail=
instance=unique
partial=nocheck
runlevel=nocheck
idepend=nocheck
rdepend=nocheck
space=ask
setuid=nocheck
conflict=nocheck
action=nocheck
basedir=default
' > ${UPGR_ADMIN}

	if [ $OldVCode -lt 421 ]; then
		mk_old_trailer "$1"
	else
		mk_new_trailer "$1"
	fi
}

this_arch=`uname -p`
CLASSES="${CLASSES} ${this_arch}"

for i in ${CLASSES} ; do
	CLASSES="${CLASSES} ${i}Os54"
done


# Determine if this is an upgrade
if [ "$UPDATE" != "yes" ]; then	# It's not just a fix
	# This isn't an update of a same-version package
	# Now check if the new pkg exists.
	pkginfo -R ${PKG_INSTALL_ROOT:-/} -q ${PKG}\*

	if [ $? -eq 0 ]; then	# it's an upgrade
        	# A different version of SUNWmdg is already installed
		# It would be great to ask permission to upgrade here
		# but we can't interact.
			OldPkginsts=`pkginfo -R ${PKG_INSTALL_ROOT:-/} -x ${PKG}.\* | nawk ' /SUNW/{printf(" %s", $1) } '`

		setup_upgrade "$OldPkginsts"

                # Get the last package's base directory
		for inst in $OldPkginsts; do
			OldPkginst=$inst
		done

                OldBD=`pkgparam -R ${PKG_INSTALL_ROOT:-/} $OldPkginst BASEDIR`
                puttext -l8 "$UPGRADE_NOTICE"
		IS_AN_UPGRADE="true"
        fi
fi

#
# export environment variables to install
#
cat >$1 <<THE_END
CLASSES="${CLASSES}"
IS_AN_UPGRADE="${IS_AN_UPGRADE}"
UPGR_SCRIPT="${UPGR_SCRIPT}"
BASEDIR="${OldBD:-$BASEDIR}"
COPY_CONFIG="${COPY_CONFIG}"
THE_END

exit 0

