#!/bin/ksh -p

# This script gets passed in a "site" from an FMLI form.
# It will then list all the packages on that "site", in a format
#  suitable for inclusion in the Menu.
#  (or, "ALL filenames" , if $2==ALL
#
# It's up to the enclosing Menu to configure what to do with
# selections
#
# Note that "site" could also be a local CDROM directory .
# It may also be the keyword "installed", in which case, just show
# all packages already installed. (Whether from Sun, or downloaded)
#


CATALOGDIR=/var/pkg-get


# This routine is called when we are listing packages to
# DELETE.
# The other routines are for potential install source sites
installed(){
	# Too easy, eh? :-)
	pkginfo |nawk '{ print "name="$2;
			print "description=\""$3" "$4" "$5" "$6" "$7" "$8" "$9" "$10" "$11" "$12"\""}
	'
	# testing this new hack
}


# Just give the names of the directories that have packages in them
# This... SHOULD be the same as the first colume pkginfo puts out.
# In theory, it might not be. But in practice, it usually is. 
# Oh well, doesnt matter if directory name is different, since
# what pkginfo prints OUT, is what pkgadd expects IN. I hope.
#
dir_shortlist(){

	pkginfo -d $1 | nawk '{ print $2}'
}

# We've been passed in the filesystem path to a locally mounted cdrom
# or something similar. Assume the packages can be listed with
# pkginfo -d $dirname.
# About the same level of difficulty as the "installed" subroutine
dir_list(){
	if [ "$2" == "ALL" ] ; then
		dir_shortlist $1
		return
	fi

	TMPFILE=$DYNAMICDIR/tmplist.$$

	pkginfo -d $1 | nawk '{ print "name="$2;
			print "description=\""$3" "$4" "$5" "$6" "$7" "$8" "$9" "$10" "$11" "$12"\""}
		' >$TMPFILE

	if [[ ! -s $TMPFILE ]] ; then
		print 'name=`gettext "ERROR"`'
		print 'description=`gettext "No packages found"`'
	else
		print 'name=`gettext "ALL"`'
		print 'description=`gettext "ALL packages - Use with caution"`'

		cat $TMPFILE
	fi
	rm $TMPFILE

}

# Quickie utility thingie -- print out JUST THE FILENAMES
# (or at least the initial component thingies) suitable for
# script use. Do NOT output in Menu format
site_shortlist() {
	SITE=$1
	CATALOGFILE=/var/pkg-get/catalog-$SITE
	if [[ ! -f $CATALOGFILE ]] ; then
		print 'errror: no catalog for site $SITE' >/dev/fd/2
		return
	fi
	
	nawk '$1 == "#PKGADM" {next}
		{
			print $1"-"$2;
		}
		' $CATALOGFILE
}


# Trust that the site catalog updater has already been run.
# Just parse the appropriate catalog in /var/pkg-get,
# and output suitable for inclusion in a Menu
site_list_menu() {
	SITE=$1
	if [ "$2" == "ALL" ] ; then
		site_shortlist $SITE
		return
	fi

	CATALOGFILE=/var/pkg-get/catalog-$SITE
	if [[ ! -f $CATALOGFILE ]] ; then
		print 'framemessage="errror: no catalog for site $SITE"'
		return
	fi
	
	print 'name=`gettext "ALL"`'
	print 'description=`gettext "ALL packages - Use with caution"`'

	nawk '$1 == "#PKGADM" {next}
		{
			print "name=\""$1"-"$2"\"";
			print "description="$3;
		}
		' $CATALOGFILE
}


# Ugh. I hate double-code like this.
if [[ -f $CATALOGDIR/catalog-$1 ]] ; then
	site_list_menu $1 $2
	exit
fi

case "$1" in
	installed)
		installed
		;;
	/*)
		dir_list $1 $2
		;;
	file://*|ftp://*)
		# old code. leave in just to be paranoid.
		# we should have been given the site, NOT the URL.
		SITE=${1##*//}
		SITE=${SITE%%/*}
		site_list_menu $SITE $2


		;;
	*)
		print "Unrecognized site type " $1 $2
		print URL is $URL
		;;
esac
