# 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 [ ! -f "/tmp/$4" ]; then
   echo "creating the response file"
   /usr/bin/touch /tmp/$4

    fi

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

        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

		# Run pkgadd
	        /usr/sbin/pkgadd -a $admin -r /tmp/$4 -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

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

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