#!/bin/sh
#
#ident "@(#)checkinstall   1.2     99/09/26 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"
Oldpkgname="false"

UPGRADE_STOP_NOTICE="For upgrades, strict ordering of pkgs is required. Please\
 install SUNWmdnr before installing this pkg."

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_trailer() {	# $1 is the old package instance
	# 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(s) ($*) 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 $*" >> $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
}

#
# 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

# Things are bit ugly here. We have to handle 2 kinds of upgrades.
# 1. Normal ones for SUNWmdr type pkg.
# 2. Upgrade from SDS 4.2 when pkg names were simply SUNWmd, SUNmdg etc.
# Ah those days of innocence!

# 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

	# check for the existance of SUNWmd. 
	pkginfo -R ${PKG_INSTALL_ROOT:-/} -q SUNWmdn\*
	if [ $? = 0 ]; then
	  CLASSES="${CLASSES} legacy"
	  Oldpkgname="true"
	fi
        pkginfo -R ${PKG_INSTALL_ROOT:-/} -q SUNWmdnr\*
        if [ $? != 0 -a "$Oldpkgname" = "true" ]; then
          # The order is not ahreaded to. Scoot out.
          puttext -l8 "$UPGRADE_STOP_NOTICE"
          exit 3
        fi


	pkginfo -R ${PKG_INSTALL_ROOT:-/} -q ${PKG}\*

	if [ $? -eq 0 -o "$Oldpkgname" = "true" ]; then	# it's an upgrade
        	# A different version of SUNWmdi is already installed
		# It would be great to ask permission to upgrade here
		# but we can't interact.
		if [ "$Oldpkgname" = "true" ]; then
			OldPkginsts=`pkginfo -R ${PKG_INSTALL_ROOT:-/} -x SUNWmdn\* | nawk ' /SUNW/{printf(" %s", $1) } '`
		else
			OldPkginsts=`pkginfo -R ${PKG_INSTALL_ROOT:-/} -x ${PKG}.\* | nawk ' /SUNW/{printf(" %s", $1) } '`
		fi
		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_trailer "$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 environmment variables to install
#
cat >$1 <<THE_END
CLASSES="${CLASSES}"
IS_AN_UPGRADE="${IS_AN_UPGRADE}"
UPGR_SCRIPT="${UPGR_SCRIPT}"
BASEDIR="${OldBD:-$BASEDIR}"
THE_END

exit 0
