#!/bin/sh
#
#ident "@(#)checkinstall   1.4     01/06/06 SMI"
#
# Copyright (c) 1999 by Sun Microsystems, Inc.
#
PATH=/usr/sadm/bin:$PATH

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

IS_AN_UPGRADE="false"
COPY_CONFIG="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 package 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 determines the version of the package we're replacing and
# sets up the environment for that.
setup_upgrade() {	# $1 is old pkginst

#		OldVCode=`pkgparam -R ${PKG_INSTALL_ROOT:-/} $1 VERSION | cut -d, -f 1 | sed s/\\\\.//g | cut -c1-3`

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

		mk_new_trailer "$1"
}

#
# figure out CLASSES to install
#
CLASSES="none preserve"

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

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

#
# check for md entries in the /etc files
#

# -  Base Solaris has the following name_to_major entry
#	md 85
nmtomaj=${PKG_INSTALL_ROOT}/etc/name_to_major
grep '^md ' ${nmtomaj} > /dev/null
if [ $? != 0 ]; then
	echo "$myname: couldn't find \"md\" in ${nmtomaj}"
	exit 2
fi

# -  Base Solaris has the following minor_perm entries
#	md:* 0640 root sys
#	md:admin 0644 root sys
minperm=${PKG_INSTALL_ROOT}/etc/minor_perm
grep '^md:' ${minperm} > /dev/null
if [ $? != 0 ]; then
	echo "$myname: couldn't find \"md\" in ${minperm}"
	exit 2
fi

#This is where we determine if we are going to do an upgrade of an old pkg.
# if we find SUNWmd installed, then we do the following:
# 1. Add legacy to class.
# 2. set COPY_CONFIG to "true" to copy all files in preinstall.

# Determine if this is an upgrade
if [ "$UPDATE" != "yes" ]; then	# It's not just a fix
	pkginfo -R ${PKG_INSTALL_ROOT:-/} -q SUNWmd\*
	if [ $? = 0 ]; then
		CLASSES="${CLASSES} legacy"
		COPY_CONFIG="true"
                puttext -l8 "$UPGRADE_NOTICE"
	fi

	# This isn't an update of a same-version package
	pkginfo -R ${PKG_INSTALL_ROOT:-/} -q ${PKG}\*

	if [ $? = 0 ]; then	# it's an upgrade
        	# A different version of SUNWmdr 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
