#!/bin/ksh
# sample.mnu
# A simple script menu under Unix
# Main logic starts at MAIN LOGIC
# The logo will be displayed at the top of the screen
LOGO="Directory Menu"
LOGO=''
#--------------------------------------------------------------------
# MENU PROMPTS
#-------------------------------------------------------------------
# This program will display choices and write the choice to a file
#syntax: imenu choices=choice1,choice2,choice3,...   title=Select a Subdirectory  filename=filename [-other]
#set the other keyword if you want to allow a user defined response
#If you are using Solaris, use the ksh
#J. Maldjian
#--------------------------------------------------------------------------------
title=`echo Please Select FTP Directory:`

#--------------------------------------------
#Set some OS and debugging specific commands
#--------------------------------------------
tr=`echo tr`
awk=`echo awk`
OS=`uname`
if test $OS == SunOS
	then
	awk='/usr/xpg4/bin/awk'
	tr='/usr/xpg4/bin/tr'
fi


#----------------------------------
# Get the display title
#-----------------------------------
keyword='title'
cnt=0

while true
do
	cnt=`expr $cnt + 1`
	if (test $cnt -gt $#) then break; fi
	title=`echo $@ | $awk  -v pointer=$cnt '{print $pointer}' | grep $keyword `
	if (test ${#title} -gt 0)
	then
		title=`echo $title | $tr ',' ' ' | $tr '=' ' ' | $awk '{print $2}'`
		pos=$cnt
		while true
		do
			let pos=pos+1
			if (test $pos -gt $#) then break; fi
			moretitle=`echo $@ | $awk  -v pointer=$pos '{print $pointer}' `
			moretitle_test=`echo $moretitle | grep '=' `
			if (test ${#moretitle_test} -gt 0) then break; fi
			title=`echo $title $moretitle`
		done
	 break
	fi

done

#----------------------------------
# Get the menu choices
#-----------------------------------
cnt=0
keyword='choices'
while true
do
	pos=1
	cnt=`expr $cnt + 1`
	if (test $cnt -gt $#) then break; fi
	choices=`echo $@ | $awk  -v pointer=$cnt '{print $pointer}' | grep $keyword `
	if (test ${#choices} -gt 0)
	then		
		choices=`echo $choices | $tr ',' ' ' | $tr '=' ' ' `
		while true
		do
			let pos=pos+1
			nextchoice=`echo $choices | $awk  -v pointer=$pos '{print $pointer}' `
			if (test ${#nextchoice} -eq 0) then break; fi
			let assign=pos-1
			 case $assign in
				1) amenu=$nextchoice;;
      				2) bmenu=$nextchoice;;
      				3) cmenu=$nextchoice;;
      				4) dmenu=$nextchoice;;
      				5) emenu=$nextchoice;;
      				6) fmenu=$nextchoice;;
      				7) gmenu=$nextchoice;;
      				8) hmenu=$nextchoice;;
      				9) imenu=$nextchoice;;
				10) jmenu=$nextchoice;;
				11) kmenu=$nextchoice;;
				12) lmenu=$nextchoice;;
				13) mmenu=$nextchoice;;
				14) nmenu=$nextchoice;;
				15) omenu=$nextchoice;;
				16) pmenu=$nextchoice;;
				17) qmenu=$nextchoice;;
				18) rmenu=$nextchoice;;
				19) smenu=$nextchoice;;
				20) tmenu=$nextchoice;;
				21) umenu=$nextchoice;;
				22) vmenu=$nextchoice;;
				23) wmenu=$nextchoice;;
				24) xmenu=$nextchoice;;
				25) ymenu=$nextchoice;;

			esac		
		done
		numchoices=$assign
		#echo choices are $choices
		 break
	fi;
done

#----------------------------------
# Get the output filename
#-----------------------------------
keyword='file'
while true
do
	cnt=`expr $cnt + 1`
	if (test $cnt -gt $#) then break; fi
	filename=`echo $@ | $awk  -v pointer=$cnt '{print $pointer}' | grep $keyword `
	if (test ${#filename} -gt 0)
	then 
		filename=`echo $filename | $tr ',' ' ' | $tr '=' ' ' | $awk '{print $2}'`
	fi

done


#----------------------------------
# Get the value of otherflag
#-----------------------------------
otherflag=`echo $@  | $awk   '/-other/{print}' `
if (test ${#otherflag} -gt 0) then otherflag=1; else otherflag=0; fi
if (test $otherflag -eq 1) 
then 
	alternate_menu="other"; 
	let numchoices=numchoices+1
else
	numchoices='sorry_no_other_entry_here'
fi;
	
#echo other is $otherflag
#echo title is $title




#------------------------------------------------------
# MENU FUNCTION DEFINITIONS
#------------------------------------------------------

# Define a function for invalid menu picks
# The function loads an error message into a variable

badchoice () { MSG="Invalid Selection Please Try Again" ; } 

# For each prompt displayed above, there is a list of 
# commands to execute in response to the user picking the
# associated letter.
# They are defined as functions
# apick () through lpick () where
# apick () corresponds to the menu
# prompt amenu which is selected
# selected by pressing a or # bpick () corresponds to the menu
# prompt bmenu which is selected # pressing b or B and so on.
# Any menu item that is not
# assigned a set of commands, is
# assigned
# the function badchoice () as a default for that pick.
# If the user
# selects a menu key that is assign# to badchoiceThis function
# causes an error message to be
# displayed on the screen.
# To add items to this second
# list, replace badchoice ()
# with the commands to run when
# that letter is pressed.
# The following steps simply define
# the functions, but do not cause
# any shell program steps to be executed.


apick () { if (test ${#amenu} -gt 0) then output_dir=$amenu ; else badchoice; fi }
bpick () { if (test ${#bmenu} -gt 0) then output_dir=$bmenu ; else badchoice; fi }
cpick () { if (test ${#cmenu} -gt 0) then output_dir=$cmenu ; else badchoice; fi }
dpick () { if (test ${#dmenu} -gt 0) then output_dir=$dmenu ; else badchoice; fi }
epick () { if (test ${#emenu} -gt 0) then output_dir=$emenu ; else badchoice; fi }
fpick () { if (test ${#fmenu} -gt 0) then output_dir=$fmenu ; else badchoice; fi }
gpick () { if (test ${#gmenu} -gt 0) then output_dir=$gmenu ; else badchoice; fi }
hpick () { if (test ${#hmenu} -gt 0) then output_dir=$hmenu ; else badchoice; fi }
ipick () { if (test ${#imenu} -gt 0) then output_dir=$imenu ; else badchoice; fi }
jpick () { if (test ${#jmenu} -gt 0) then output_dir=$jmenu ; else badchoice; fi }
kpick () { if (test ${#kmenu} -gt 0) then output_dir=$kmenu ; else badchoice; fi }
lpick () { if (test ${#lmenu} -gt 0) then output_dir=$lmenu ; else badchoice; fi }
mpick () { if (test ${#mmenu} -gt 0) then output_dir=$mmenu ; else badchoice; fi }
npick () { if (test ${#nmenu} -gt 0) then output_dir=$nmenu ; else badchoice; fi }
opick () { if (test ${#nmenu} -gt 0) then output_dir=$omenu ; else badchoice; fi }
ppick () { if (test ${#nmenu} -gt 0) then output_dir=$pmenu ; else badchoice; fi }
qpick () { if (test ${#nmenu} -gt 0) then output_dir=$qmenu ; else badchoice; fi }
rpick () { if (test ${#nmenu} -gt 0) then output_dir=$rmenu ; else badchoice; fi }
spick () { if (test ${#nmenu} -gt 0) then output_dir=$smenu ; else badchoice; fi }
tpick () { if (test ${#nmenu} -gt 0) then output_dir=$tmenu ; else badchoice; fi }
upick () { if (test ${#nmenu} -gt 0) then output_dir=$umenu ; else badchoice; fi }
vpick () { if (test ${#nmenu} -gt 0) then output_dir=$vmenu ; else badchoice; fi }
wpick () { if (test ${#nmenu} -gt 0) then output_dir=$wmenu ; else badchoice; fi }
xpick () { if (test ${#nmenu} -gt 0) then output_dir=$xmenu ; else badchoice; fi }
ypick () { if (test ${#nmenu} -gt 0) then output_dir=$ymenu ; else badchoice; fi }
zpick () { if (test ${#nmenu} -gt 0) then output_dir=$zmenu ; else badchoice; fi }

echo $output_dir #Pin Xie
alternate_pick () { other ; }


other () {
	echo Enter a directory name ; 
	read output_dir ;
}

#------------------------------------------------------
# DISPLAY FUNCTION DEFINITION
#------------------------------------------------------
# This function displays the menu.
# The routine clears the screen, echoes
# the logo and menu prompts
# and some additional messages.
# Note that this definition does
# not cause the function to
# be executed yet, it just defines
# it ready to be executed.

themenu () {
# clear the screenclear
#echo `date`echo
#echo "\t\t\t" $LOGO
echo
echo "\t $title"
echo
if (test ${#amenu} -gt 0) then echo "\t\t\t 1" $amenu; fi;
if (test ${#bmenu} -gt 0) then echo "\t\t\t 2" $bmenu;  fi;
if (test ${#cmenu} -gt 0) then echo "\t\t\t 3" $cmenu;  fi;
if (test ${#dmenu} -gt 0) then echo "\t\t\t 4" $dmenu;  fi;
if (test ${#emenu} -gt 0) then echo "\t\t\t 5" $emenu;  fi;
if (test ${#fmenu} -gt 0) then echo "\t\t\t 6" $fmenu;  fi;
if (test ${#gmenu} -gt 0) then echo "\t\t\t 7" $gmenu;  fi;
if (test ${#hmenu} -gt 0) then echo "\t\t\t 8" $hmenu;  fi;
if (test ${#imenu} -gt 0) then echo "\t\t\t 9" $imenu;  fi;
if (test ${#jmenu} -gt 0) then echo "\t\t\t 10" $jmenu;  fi;
if (test ${#kmenu} -gt 0) then echo "\t\t\t 11" $kmenu;  fi;
if (test ${#lmenu} -gt 0) then echo "\t\t\t 12" $lmenu;  fi;
if (test ${#mmenu} -gt 0) then echo "\t\t\t 13" $mmenu;  fi;
if (test ${#nmenu} -gt 0) then echo "\t\t\t 14" $nmenu;  fi;
if (test ${#omenu} -gt 0) then echo "\t\t\t 15" $omenu;  fi;
if (test ${#pmenu} -gt 0) then echo "\t\t\t 16" $pmenu;  fi;
if (test ${#qmenu} -gt 0) then echo "\t\t\t 17" $qmenu;  fi;
if (test ${#rmenu} -gt 0) then echo "\t\t\t 18" $rmenu;  fi;
if (test ${#smenu} -gt 0) then echo "\t\t\t 19" $smenu;  fi;
if (test ${#tmenu} -gt 0) then echo "\t\t\t 20" $tmenu;  fi;
if (test ${#umenu} -gt 0) then echo "\t\t\t 21" $umenu;  fi;
if (test ${#vmenu} -gt 0) then echo "\t\t\t 22" $vmenu;  fi;
if (test ${#wmenu} -gt 0) then echo "\t\t\t 23" $wmenu;  fi;
if (test ${#xmenu} -gt 0) then echo "\t\t\t 24" $xmenu;  fi;
if (test ${#ymenu} -gt 0) then echo "\t\t\t 25" $ymenu;  fi;
if (test ${#zmenu} -gt 0) then echo "\t\t\t 26" $zmenu;  fi;


if (test ${#alternate_menu} -gt 0) then echo "\t\t\t $numchoices" $alternate_menu;  fi;
#echo "\t\tExit"
echo
echo $MSG
echo
echo Select by pressing the number and then ENTER ;
}
 

#------------------------------------------------------
# MAIN LOGIC
#------------------------------------------------------
# Every thing up to this point has been to define
# variables or functions.
# The program actually starts running here.

# Clear out the error message variable
MSG=

# Repeat the menu over and over
# Steps are: Display the menu
#'read' a line of input from the key board
#Clear the error message
#Check the answer for a or A or b or B and dispatch
#    to the appropriate program or function or exit
#If the entry was invalid call the badchoice () function
#    to initialize MSG to an error message
#This error message is used when setting up the menu
#    for a menu pick that is valid but has no command
#    associated with it.

while  true
do
#display the menu
  themenu

#read a line of input from the keyboard
  read answer


#Clear any error message
  MSG=

#Execute one of the defined functions based on the
#    letter entered by the user.

#If the choice was E through L, the pre-defined
#    function for that pick will execute badchoice ()
#    which loads an error message into MSG  

 # case $answer in
 #     a|A) apick;;
 #     b|B) bpick;;
 #     c|C) cpick;;
 #     d|D) dpick;;
 #     e|E) epick;;
 #     f|F) fpick;;
 #     g|G) gpick;;
 #     h|H) hpick;;
 #     i|I) ipick;;
 #     j|J) jpick;;
 #     k|K) kpick;;
 #     l|L) lpick;;


 case $answer in
	$numchoices) alternate_pick;if (test ${#MSG} -eq 0) then break; fi ;;
	1) apick; if (test ${#MSG} -eq 0) then break; fi ;;
	2) bpick; if (test ${#MSG} -eq 0) then break; fi ;;
	3) cpick; if (test ${#MSG} -eq 0) then break; fi ;;
	4) dpick; if (test ${#MSG} -eq 0) then break; fi ;;
	5) epick; if (test ${#MSG} -eq 0) then break; fi ;;	
	6) fpick;if (test ${#MSG} -eq 0) then break; fi ;;
	7) gpick;if (test ${#MSG} -eq 0) then break; fi ;;
	8) hpick;if (test ${#MSG} -eq 0) then break; fi ;;
	9) ipick;if (test ${#MSG} -eq 0) then break; fi ;;
	10) jpick;if (test ${#MSG} -eq 0) then break; fi ;;
	11) kpick;if (test ${#MSG} -eq 0) then break; fi ;;
	12) lpick;if (test ${#MSG} -eq 0) then break; fi ;;
	13) mpick;if (test ${#MSG} -eq 0) then break; fi ;;
	14) npick;if (test ${#MSG} -eq 0) then break; fi ;;
	15) opick;if (test ${#MSG} -eq 0) then break; fi ;;
	16) ppick;if (test ${#MSG} -eq 0) then break; fi ;;
	17) qpick;if (test ${#MSG} -eq 0) then break; fi ;;
	18) rpick;if (test ${#MSG} -eq 0) then break; fi ;;
	19) spick;if (test ${#MSG} -eq 0) then break; fi ;;
	20) tpick;if (test ${#MSG} -eq 0) then break; fi ;;
	21) upick;if (test ${#MSG} -eq 0) then break; fi ;;
	22) vpick;if (test ${#MSG} -eq 0) then break; fi ;;
	23) wpick;if (test ${#MSG} -eq 0) then break; fi ;;
	24) xpick;if (test ${#MSG} -eq 0) then break; fi ;;
	25) ypick;if (test ${#MSG} -eq 0) then break; fi ;;
	26) zpick;if (test ${#MSG} -eq 0) then break; fi ;;





#      If the user selects =91x=92 to exit then break out
#      of this loop
#      x|X) break;;
 
#If the entry was invalid call the badchoice function
#    to initialize MSG to an error message
        *) badchoice;;
 
  esac

#     Do it again until the user enters =91x=92.


done
echo "output_dir = " $output_dir
if (test ${#filename} -gt 0)
	then 
#	echo output written to $filename
	echo $output_dir > $filename
#	echo $output_dir
fi
