#!/bin/ksh -p

# Its important to export any variables we want the menu layer to 
# know about, here.
# We can then reference then as regular shell-type $VARs in the menu
#

# Some docs on FMLI are at
# http://uw7doc.sco.com/SDK_charm/CTOC-_Introduction_to_FMLI.html



#Unfortunately, only the top level menu seems to see this.

export MENUDIR URL SITE DYNAMICDIR

MENUDIR=`dirname $0`
if [ "$MENUDIR" = "." ] ; then 
	MENUDIR=$PWD
fi

# Make a safe place for us to create our dynamically generated menus.
# We do NOT want to use our MENUDIR. That might be read-only.
DYNAMICDIR=${DYNAMICDIR:-/var/run/pkgadm}

if [[ -h $DYNAMICDIR ]] ; then
	print ERROR: SECURITY BREACH ATTEMPT
	ls -ld $DYNAMICDIR
	exit
fi
if [ ! -d $DYNAMICDIR ] ; then
	mkdir $DYNAMICDIR
fi

if [ ! -d $DYNAMICDIR ] ; then
	print ERROR: $DYNAMICDIR does not exist. Cannot continue
	exit
fi

if [[ -h $DYNAMICDIR ]] ; then
	print ERROR: SECURITY BREACH ATTEMPT
	ls -ld $DYNAMICDIR
	exit
fi

# This is really ugly, I'm sorry. But fmli is stupid. It wont open
# up the menu the first time I create it. The file has to already
# exist, it seems.
cd $MENUDIR
for f in Tmpl.* ; do
   	menulist="$menulist Menu.${f#Tmpl.}"
done
cd $DYNAMICDIR
touch $menulist
if [[ $? -ne 0 ]] ; then
	print ERROR: no permission to write in $DYNAMICDIR/
	exit
fi
cd $MENUDIR

############################################################

usage(){
	print "Usage:"
	print "  $0                     - Select site in menu"
	print "  $0 ftp://ftp.site/dir  - defines remote archive"
	print "  $0 file:///dir/path    - defines 'local' archive"
	print "  $0 /dir/path           - defines cdrom base"
}


################################################################
# Load up config file
CONFFILE=/etc/pkg-get.conf

if [[ ! -f $CONFFILE ]] ; then
	print ERROR: $CONFFILE not present
	print Creating a default file
	cat >$CONFFILE <<EOF
# Shared Configuration file for "pkg-get"
# 'man pkg-get' for details on that program, or download it from 
# http://www.bolthole.com/solaris/

url=ftp://$MASTERSITE/pub/freeware
# if you are behind a firewall, set these as appropriate
#http_proxy=http://your-proxy:8080
#ftp_proxy=http://your-proxy:8023
#export http_proxy ftp_proxy
EOF
fi


############################################################

if [ "$1" != "" ] ; then
	case "$1" in
		# URL: is okay as source
		file:*|ftp:*)
			URL="$1"
			SITE=${URL##*//}
			SITE=${SITE%%/*}
		;;
		/*)
			URL="$1"
			SITE="[Local Filesystem]"
		;;
		*)
			if [ ! -d "$1" ] ; then
				usage
				exit 1
			fi
			SITE='[local directory]'
	esac
else
	# No site specified.
	# default to first site in config file
	. $CONFFILE
	
	SITE=${url##*//}
	SITE=${SITE%%/*}
	URL=$url

	

fi


# supposed to do this to make sure curses is "really" working, or something
tput init

echo PWD is $PWD
		
fmli -i $MENUDIR/init.config $MENUDIR/Menu.pkg

# Remove dynamically generated menus, and possibly failed download.
#  Clutter is bad. Particularly in-memory clutter.
cd $DYNAMICDIR ; rm *

