# Package Add utility script 
#
#     $1          $2          $3     $4      $5        $6
# adminfile responsefiledir pkgdir pkgname logfile    alternate root directory
#
#                     $7         $8       $9          $10
#                    origdir installdir pkgutildir basereloc
#
#
SWERROR=1010
if [ $# = 5 -o $# = 6  -o $# = 9  -o $# = 10 ]
then 
	ALTROOT=""

	if [ $# != 5 ]
	then
	  ALTROOT="-R $6"
  	fi 

	# 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

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

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

		# Run pkgadd
	        /usr/sbin/pkgadd ${ALTROOT} -a $admin -r /tmp/$4 -d $3  $4 >> $5 2>&1
		status=$?
		$9/unrelocate $7 $8 $2 $4  $5 >> $5 2>&1

	else
		# Run pkgadd
		if [ -f $2/$4 ]
		then
			/usr/sbin/pkgadd ${ALTROOT} -a $admin -r $2/$4 -d $3  $4 >> $5 2>&1
		else
			/usr/sbin/pkgadd ${ALTROOT} -a $admin -d $3  $4 >> $5 2>&1
		fi
		status=$?
	fi
	/usr/bin/rm $admin
	exit $status
else 
	exit $SWERROR
fi
