#!/bin/sh
#
#ident "@(#)checkinstall   1.3     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 SUNWmdr 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_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 constructs a trailer script for removing the old version
# of this package after this new version is installed. This particular
# function deals with the pre-4.2 DiskSuite packages that don't have
# smart remove 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

	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 [ "x$2" = "xtrue" ]; then
		mk_old_trailer "$1"
	else
		mk_new_trailer "$1"
	fi
}

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


# 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 SUNWmd\*

	if [ $? = 0 ]; then
	  CLASSES="${CLASSES} legacy"
	  Oldpkgname="true"
	fi

	pkginfo -R ${PKG_INSTALL_ROOT:-/} -q SUNWmdr\*
	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 [ $? = 0 ]; then	# it's an upgrade
        	# A different version of SUNWmd 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" "$Oldpkgname"
		# 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"

	elif [ "$Oldpkgname" = "true" ] ; then
		OldPkginsts=`pkginfo -R ${PKG_INSTALL_ROOT:-/} -x SUNWmd\* | nawk '/SUNW/{printf(" %s", $1) }'`
		setup_upgrade "$OldPkginsts" "$Oldpkgname"
	# 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}"
THE_END

exit 0
