#pragma ident   "@(#)pkgadd 1.4     00/01/19 SMI"
#
# Copyright (c) 1999 by Sun Microsystems, Inc.
# All right reserved.
#
# Package Add utility script 
#
#     $1          $2          $3     $4      $5    
# adminfile responsefiledir pkgdir pkgname logfile 
#
#                     $6         $7       $8          $9
#                    origdir installdir pkgutildir basereloc
#
#
SWERROR=99
PKG_NONABI_SYMLINKS=true
export PKG_NONABI_SYMLINKS
if [ $# = 5  -o $# = 8  -o $# = 9 ]
then 
	# Use a temporary location for the pkgadd admin file
	# based on the logfile location.  Needed so any CDrom drive
	# resident admin file is not used for the pkgadd -a option. 
	# Doing so would leave an open admin file descriptor that points
	# to the CDrom.  Any daemon process started by a package 
	# postinstall script then would then inherit this CDrom resident
        # file descriptor and would make the CDrom in drive unmountable. 

	admin=$5_pkgadd_$4_admin
	/usr/bin/cp $1 $admin
	ropt=""

        if [ $# = 8 ]
	then
		# relocate 
		$8/relocate $6 $7 $2 $4  $5 >> $5 2>&1
		status=$?
		if [ $status != 0 ]
		then
			/usr/bin/rm $admin
			exit $status
		fi

		ropt="-r /tmp/$4"

		# Run pkgadd
	        /usr/sbin/pkgadd -n -a $admin $ropt -d $3  $4 >> $5 2>&1
		status=$?
	        $8/unrelocate $6 $7 $2 $4  $5 >> $5 2>&1
  	elif [ $# = 9 ]
	then
		# do basedir relocation
		$8/baserelocate $7 $2  $4 $5 >> $5 2>&1
		status=$?
		if [ $status != 0 ]
		then
			/usr/bin/rm $admin
			exit $status
		fi

		ropt="-r /tmp/$4"

		# Run pkgadd
	        /usr/sbin/pkgadd -n -a $admin $ropt -d $3  $4 >> $5 2>&1
		status=$?
		$8/unrelocate $6 $7 $2 $4  $5 >> $5 2>&1

	else
		#
		# If /tmp/response.d exists, that is our response dir
		# due to a custom install
		#
		rdir=$2

		if [ -f /tmp/response.s/$4 ]
		then
			rdir=/tmp/response.s
		fi

		ropt="-r $rdir/$4"


		# Don't re-install SUNWuiu8, which is a common package between products.

		if [ "$4" = "SUNWuiu8" ]	# This is a hack so that if SUNWuiu8
		then				# is already installed, we don't try
			pkginfo -q SUNWuiu8	# to install it a second time.
			if [ $? = 0 ]
			then
				/usr/bin/echo "Package SUNWuiu8 skipped - already installed." >> $5
				/usr/bin/rm $admin
				exit 0
			fi
		fi


		# Run pkgadd
		/usr/sbin/pkgadd -n -a $admin $ropt -d $3  $4 >> $5 2>&1
		status=$?
	fi
	/usr/bin/rm $admin
	exit $status
else 
	exit $SWERROR
fi
