:
#!/bin/bash


# Prompt for yes or no answer - returns non-zero for no
_getyn()
{
        while   echo -n "$* (y/n) ">&2
        do      read yn rest
                case $yn in
                [yY])   return 0                      ;;
                [nN])   return 1                      ;;
                *)      echo "Please answer y or n" >&2 ;;
                esac
        done
}

# run README file without looping
_read_readme()
{
	if [ ! -f README ]
	then
		echo "*** Missing README file ***"
		return
	fi
	if [ ! -x README ]
	then
		chmod +x README
	fi
	README QUICKLY
}

_show_menu1()
{
	echo " "
	echo "               MAIN MENU"
	echo " "
	echo "1) read README file"
	echo "2) run make"
	echo "3) run it"
	echo ":-)"
	echo "10) tour package"
	echo "11) clean package"
	echo "12) archive package"
	echo ":-)"
	echo "h) help"
	echo "q) quit program"
	echo " "
	echo -n "Select then press <ENTER>: "
}

_do_menu1()
{
	local NOWPICK
	local REST

	NOWPICK=$1
	case $NOWPICK in
	1)
		_read_readme
		;;
	2)
		echo "Wait a few minutes..."
		(cd src; make)
		if [ -f src/nosey ]
		then
			echo moving nosey to bin
			if [ ! -d bin ]
			then
				echo making bin directory
				mkdir bin
			fi
			mv src/nosey bin
		fi
		;;
	3)
		$AGENTS/ratcheck.bsh
		;;
	10)
		$AGENTS/whatisit.bsh -top
		(cd src; $AGENTS/whatisit.bsh src)
		(cd bin; $AGENTS/whatisit.bsh bin)
		(cd agents; $AGENTS/whatisit.bsh agents)
		(cd legal; $AGENTS/whatisit.bsh legal)
		;;
	11)
		_getyn "Clean package?" \
		&& _getyn "*** Are you sure?" && _clean_files
		;;
	12)
		_getyn "Really clean package before archive?" \
		&& _getyn "*** Are you sure?" && _realclean_files
		_getyn "Create manifest for archive?" \
		&& $AGENTS/manifest.bsh create
		_getyn "Proceed creating an archive?" \
		&& (cd ..; $AGENTS/newtar.bsh nosey)
		;;
	:-)
		echo "What are you smiling at laughing boy?"
		read REST
		echo "yeah yeah, sure sure."
		;;
	*)
		echo -n "Invalid selection" $NOWPICK ". Please press <ENTER>"
		read REST
	esac
}

_help_menu1()
{
	local REST

	echo " "
	echo "Type q<ENTER> to quit"
	echo "Pick - read README - to review what its all about"
	echo "Pick - run makes - to prepare the whole thing in one shot"
	echo "Pick - run it - to execute program"
	echo "Most other selections for touring the overall package"
	echo " "
	echo -n "Please press <ENTER>."
	read REST
}

_clean_files()
{
	(cd src; make clean)
}

_realclean_files()
{
	(cd src; make realclean)
}

_choose_items()
{
	local NOWPICK
	local REST
	local SHOW_MENU
	local DO_MENU
	local HELP_MENU

	SHOW_MENU=$1
	DO_MENU=$2
	HELP_MENU=$3

        while  [ 1 ]
        do
		$SHOW_MENU
	        read NOWPICK REST
                case $NOWPICK in
		"")
			;;
		[hH]*)
			$HELP_MENU
			;;
#                [xX]*)   
#			return 1
#			;;
                [qQ]*)   
			exit 1
			;;
                *)
			$DO_MENU $NOWPICK
			;;
                esac
        done
}





#
# main
#
	if [ -z "$BASH_VERSION" ]
	then
		echo "I want to be bash'd!"
		_getyn "Continue running in this shell anyway?" || exit
	fi
	if [ ! -d agents ]
	then
		echo "Oops, missing critical agents subdirectory."
		exit
	fi
	AGENTS=`cd agents; pwd`
	PACKAGE=`pwd`
	export PACKAGE AGENTS

	while ( _choose_items _show_menu1 _do_menu1 _help_menu1)
	do
		echo " "
	done
