#!/bin/csh -f
#
#      $Id: list_menu,v 1.1 1993-02-20 00:11:17 clyne Exp $
#
#########################################################################
#									#
#			   Copyright (C)  1992				#
#	     University Corporation for Atmospheric Research		#
#			   All Rights Reserved				#
#									#
#########################################################################
#
#	File:		list_menu
#
#	Author:		John Clyne
#			National Center for Atmospheric Research
#			PO 3000, Boulder, Colorado
#
#	Date:		Thu Nov 12 14:58:35 MST 1992
#
#	Description:	Present a numbered list selection. Multiple
#			choices may be made. Each command line arg
#			is an item in the list. If list_menu exits
#			with zero exit status a space-separated bit 
#			vector is written to stdout. If bit one is set then
#			item one (command line are number 1) was selected. If
#			bit two is set then item two is selected.... 
#
#	Usage:		list_menu [-t <title>] item1 item2 item3 ...
#
#	Environment:
#
#	Files:
#
#
#	Options:

onintr cleanup

set title = ""	# title for menu

if ($#argv < 1) then
	echo "Usage: $0 [-t title] item1 item2 item3..." > /dev/tty
	exit 1
endif

#
# strip off title option if present
#
if ("$argv[1]" == "-t") then
	shift
	set title = "$argv[1]"
	shift
endif

#
# chosen is a bit vector indicating which items have been selected.
#
set chosen = ($argv)		# alloc space for chosen

@ none_index = $#argv + 1	# item number for "None of the above"
@ all_index = $#argv + 2	# item number for "All of the above"

set done = 0
while (! "$done")

	clear > /dev/tty
	set i = 1
	cat <<EOF > /dev/tty


	$title


	Select the numbered item(s) from the following list (Multiple 
	item numbers should be separated by spaces):



EOF

	#
	#	display the menu
	#
	while ($i <= $#argv)
		set chosen[$i] = 0
		echo "	$i.  $argv[$i]" > /dev/tty
		@ i++
	end
	echo "" > /dev/tty
	echo "	$i.  None of the above" > /dev/tty
	@ i++
	echo "	$i.  All of the above" > /dev/tty
	echo "" > /dev/tty
	echo "	0.  Return without making a selection" > /dev/tty

	echo "" > /dev/tty
	echo "" > /dev/tty
	echo "" > /dev/tty
	echo -n "    Choice(s): " > /dev/tty

	#
	# get the selection(s)
	#
	set answer = $<

	if ("$answer" == "0") then 
		exit 1

	else if ("$answer" == $none_index) then	# none of the above
		set i = 1
		while ($i <= $#argv)
			set chosen[$i] = 0
			@ i++
		end
	else if ("$answer" == "$all_index") then	# all of the above
		set i = 1
		while ($i <= $#argv)
			set chosen[$i] = 1
			@ i++
		end
	else 
		

		#
		# match each item input against the list we were given
		#
		foreach j ($answer)
			set i = 1
			while ($i <= $#argv)
				if ("$j" == "$i") then
					set chosen[$i] = 1
				endif
				@ i++
			end
		end
	endif

	set done2 = 0
	while (! $done2)

		echo "" > /dev/tty
		echo "" > /dev/tty
		echo "" > /dev/tty
		echo "You chose the following items:" > /dev/tty
		echo "" > /dev/tty
		set i = 1
		while ($i <= $#argv)
			if ("$chosen[$i]") then
				echo "	$argv[$i]" > /dev/tty
			endif
			@ i++
		end
		echo "" > /dev/tty
		echo -n "Is this correct [y/n](y) ? " > /dev/tty
		set answer = $<

		if ("$answer" == "y" || "$answer" == "") then 
			set done = 1
			set done2 = 1
		else if ("$answer" == "n") then
			set done2 = 1

		endif
	
	end
end


#
#	print out the bit vector
#
set i = 1
while ($i <= $#argv)
	if ("$chosen[$i]") then
		echo -n "1 "
	else
		echo -n "0 "
	endif
	@ i++
end
	echo ""
exit 0


cleanup:
exit 1
