#
# request script to control dependency installation.
#
# This looks at the PKG_DEPEND list which is a list of packages upon
# which this one depends. Here's what happens.
#    if this is a batch install
#        for each depend package that isn't installed
#           if it is available on the medium
#               add it to $LIST_FILE
#           else
#               issue a WARNING and continue
#           fi
#        done
#    else this is the initiator
#        for each depend package that isn't installed
#           if it is available on the medium
#               if there's no LIST_FILE
#                   create LIST_FILE
#               fi
#               add it to $LIST_FILE
#           else
#               issue a WARNING and continue
#           fi
#        done
#    fi
#    if there's anything in the list file
#                   copy over tralier
#                   create trailer response file
#                   send LIST_FILE to postinstall
#    fi
#

PATH=/usr/sadm/bin:$PATH

TRLR_RESP=/tmp/response.$PKGINST
resp_file=$1

NOPKG_WRN="WARNING: A required $arch package, $pkg version $version, \
        is not available on the medium. This package will need to be \
        installed at a later time."

#
# Assign a name to what may become our dependency list.
#
init_list() {
        # initialize list
        LIST_FILE=/tmp/depend_list.$$
        if [ -f $LIST_FILE ]; then
                rm $LIST_FILE
        fi
	initiate
}

#
# Lay down the trailer script and its associated support files.
#
initiate () {
        # Let postinstall know where list is
        echo "LIST_FILE=$LIST_FILE" >> $resp_file

        # copy the trailer script to the /tmp directory
        echo "TRLR_RESP=$TRLR_RESP" >> $resp_file

        # Create trailer's response file
        echo "BATCH_INSTALL=true" > $TRLR_RESP
        echo "LIST_FILE=$LIST_FILE" >> $TRLR_RESP

        return 0
}

#
# Return whether the indicated package is available on the source medium.
#
is_on_medium () {       # $1=package $2=architecture $3=version 
                        # $4=path to medium
        #  pkginfo -q -d $4 -v $3 -a $2 $1
        pkginfo -q -d $4 -v $3 $1
        if [ $? -eq 0 ]; then
                return 1
        else
                return 0
        fi
}

#
# Return whether the indicated package is installed already.
#
not_installed() {       # $1=package to test $2=architecture $3=version $4=rootdir
	#
	# $4 will be specified on the PKG_DEPEND line in the pkginfo
	# and will either start with ROOTDIR or BASEDIR. If it
	# starts with ROOTDIR, check to see if the specified package
	# is installed in ${BASEDIR}/${ROOTDIR}. Base the return
	# value on this query
	#
	if [ ! -z "$4" ]; then
	  TYPE=`echo $4 | awk -F= '{ print $1 }'`
	  DIRNAME=`echo $4 | awk -F= '{ print $2 }'`
	
	    if [ "${TYPE}" = "ROOTDIR" ]; then
	      ROOTPATH_ARG="-R ${BASEDIR}/${DIRNAME}"
	    else
	      ROOTPATH_ARG=""
	    fi
	fi

        pkginfo ${ROOTPATH_ARG} -v $version -a $arch -q $1
        if [ $? -eq 0 ]; then
	        # Package is present
	        return 0
        else
                return 1
        fi
}

#
# Add a package to the list without repetition. Note that architecture
# and version don't matter here because some mechanism (like a dot
# extension) is required to make the name unique on the common medium.
#
# LIST_FILE contains package name and rootdir pairs in order to support 
# installation of packages into specific root directories.
#
add_to_list() { # $1=package to add $2=rootdir
        if [ ! -f $LIST_FILE ]; then
                echo "$1 $2" > $LIST_FILE
                return 0
        fi 
        while read file_pkg pkg_root ; do
                if [ "$file_pkg" = "$1" -a "$pkg_root" = $2 ]; then
                        return 1
                fi
        done < $LIST_FILE
        if [ $? -eq 1 ]; then
                return 0
        fi
        echo "$1 $2" >> $LIST_FILE

	cat $LIST_FILE
        return 0 
}

#
# parse_pkg_depend will iterate over the values in a 
# PKG_DEPEND* line in the pkginfo file and add the 
# required package to the list of packages to be
# installed if it's not installed yet.
#
parse_pkg_depend() {

	# 
	# handle multiple package dependencies by iterating 
	# over the list of PKG_DEPEND* variables:
	#   PKG_DEPEND, PKG_DEPEND1, PKG_DEPEND2, ...
	# 
	i=""
	PKG_DEP=PKG_DEPEND${i}
	eval PD=\$${PKG_DEP}

	while [ ! -z "${PD}" ]; do
	        for entry in ${PD}; do
	                if [ -z "$pkg" ]; then
	                        if [ -n "$entry" ]; then
	                                pkg=$entry
	                                continue
	                        else
	                                break
	                        fi
	                fi
	                if [ -z "$arch" ]; then
	                        arch=$entry
	                        continue
	                fi
	                if [ -z "$version" ]; then
	                        version=$entry
				continue
	                fi
	                if [ -z "$rootdir" ]; then
	                        rootdir=$entry
	                fi
	                not_installed $pkg $arch $version $rootdir

	                if [ $? -eq 1 ]; then
	                        is_on_medium $pkg $arch $version $INST_DATADIR
	                        if [ $? -eq 1 ]; then
	                                add_to_list $pkg $rootdir
	                        else
	                                puttext "$NOPKG_WRN"
	                        fi
	                fi
	                pkg=""
	                arch=""
	                version=""
			rootdir=""
	        done

		# 
		# "increment" i, order is "", 1, 2, ...
		# 
		if [ "$i" = "" ]; then
			i=1
		else 
			i=`expr $i + 1`
		fi
		PKG_DEP=PKG_DEPEND${i}
		eval PD=\$${PKG_DEP}
	done
}

#
# Get the key environment parameters from the response file and
# put them in this environment.
#
s=`egrep BATCH_INSTALL $resp_file`
if [ -n "$s" ]; then
        BATCH_INSTALL="true"
fi
s=`egrep LIST_FILE $resp_file`
if [ -n "$s" ]; then
        LIST_FILE=`echo $s | sed s/LIST_FILE=//`
fi

# Determine if this was called as part of a batch install
if [ -n "$BATCH_INSTALL" ]; then
        # Confirm that this package is not installed on the system.
        if [ "${UPDATE}" = "yes" ]; then
                # Hey, it's already there.
                exit 3  # suspend
        fi

	parse_pkg_depend()


else    # This may initiate
        init_list

	parse_pkg_depend

        if [ -f $LIST_FILE -a -s $LIST_FILE ]; then
                initiate
        fi
fi

exit 0
