#!/bin/sh

readonly tmpfile=/var/tmp/pkgseltmp-$$
readonly ADMINDIR=/var/lib/dpkg
readonly LIBDIR=/root
readonly dpkg="dpkg --set-selections --admindir=$ADMINDIR"
readonly tst=""
# readonly LIBDIR=.
# readonly dpkg="cat"
# readonly tst="echo"

build_menu () {
	TITLE=$1
	shift
	TEXT=$1
	shift
	echo -n "result=\$(whiptail --clear \
		--backtitle \"Debian Installation\" --title \"$TITLE\" " >$tmpfile
	if [ "$type" = "tasks" -a "$status" = "select" ]; then
		echo -n "--checklist \"$TEXT\" 0 76 0 " >>$tmpfile
	else
		echo -n "--menu \"$TEXT\" 0 0 0 " >>$tmpfile
		echo -n "$@ \" \" \"\" " \ >>$tmpfile
	fi
	for i in $LIBDIR/$type/* ; do
		name=$(basename $i); 
		if [ $name != CVS ]; then
			descr=$(head -1 $i)
			if [ "$type" = "tasks" -a "$status" = "select" ]; then
				echo -n "\"$name\" \"$descr\" off " >>$tmpfile
			else
				echo -n "\"$name\" \"$descr\" " >>$tmpfile
		        fi
		fi
	done
	echo -n "2>&1 1>/dev/tty)" >>$tmpfile

	result=" "
	while [ "$result" = " " ]; do
		. $tmpfile
	done
	rm -f $tmpfile
}

build_select_profiles_menu () {
	build_menu "Please Select Profile" \
	  "Please select the kind of installation you want:" \
	  "List" "\"List which packages will be installed by each profile\"" \
	  "T.List" "\"List which packages will be installed by each task\"" \
	  "\" \"" "\" \"" \
	  "Custom" "\"Make your own profile: select the tasks for this system.\""
}

build_list_profiles_menu () {
	build_menu "Please Select Profile" \
	  "Please select the profile to list:" \
	  "Back" "\"Back to the previous dialog box.\""
}

build_select_tasks_menu () {
	build_menu "Please Select Software Categories" \
	  "Please select the tasks you will perform with this system:
Note: the following selections install only a minimal subset of 
the hundreds of packages in the Debian distribution. You may 
select others later using 'dselect'."
}

build_list_tasks_menu () {
	build_menu "Please Select Software Categories" \
	  "Please select the task to list:" \
	  "Back" "\"Back to the previous dialog box.\""
}

build_list_box () {
	TITLE="Package list"
	TEXT="The packages you have selected are:"
	echo -n "whiptail --clear \
		--backtitle \"Debian Installation\" \
		--title \"$TITLE\" --msgbox \"$TEXT" >$tmpfile
	echo >>$tmpfile
	echo >>$tmpfile
	tail +2 $LIBDIR/$type/$result | awk -- '{print $1}'>>$tmpfile
	echo -n "\" 0 0 2>&1 1>/dev/tty" >>$tmpfile

	. $tmpfile
	rm -f $tmpfile
}

status="select"
type="profiles"
result=" "
while [ "$result" = " " ]; do
	build_$status\_$type\_menu
	case $result in
		" ")
			echo "Can't happen!"
			exit 2
			;;
	 	"")
			if [ "$type" = "profiles" -a "$status" = "select" ]; then 
					exit 1
			else
				status="select"
				type="profiles"
				result=" "
			fi
			;;
		List)
			status="list"
			result=" "
			;;
		Back)
			status="select"
			type="profiles"
			result=" "
			;;
		Custom)
			type="tasks"
			result=" "
			;;
		"T.List")
			status="list"
			type="tasks"
			result=" "
			;;
		*)
			if [ "$status" = "select" ]; then 
				$tst cat rev_task |$dpkg 
				for i in $result; do
					# Strip quotes.
					a=$(eval echo $i)
					$tst tail +2 $LIBDIR/$type/$a |$dpkg 
				done
				exit 0
			else
				build_list_box
				result=" "
			fi
			;;
	esac
done
