#!/bin/ksh
##############################################################################
## @(#) ssmenu.sh ver 1.14 Sean McKee (mckee@misslink.net) 11/19/97 16:18:31
##
## Copyright (C) 1996 Sean M McKee (mckee@misslink.net).
## 
## The SS Menu is free software; you can redistribute it and/or
## modify it under the terms of the GNU Library General Public
## License as published by the Free Software Foundation; either
## version 2 of the License, or (at your option) any later version.
## 
## The SS Menu is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
## See the GNU Library General Public License for more details.
## 
## You should have received a copy of the GNU Library General Public
## License along with SS Menu -- see the file COPYING.LIB.
## If not, write to the Free Software Foundation, Inc.,
## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
##
############################################################################
#
#  author		:	Sean McKee
#  create date 	:	Tue Dec 17 14:02:10 CST 1996
#
#  description	: shell menu system main program.
#
#
# SCCS INFORMATION ---------------------------------------------------
#
# Module Name : ssmenu.sh 	SCCS Name : s.ssmenu.sh
# SID Number  : 1.14    Release   : 1	 Level   : 14
# Last Check out date and time	  : 11/19/97 , 16:18:31
#
#############################################################################

MENUPROG="${0}"
BOLD=`tput smso`
OFFBOLD=`tput rmso`
CLS=`tput clear`
REVON=`tput rev`
REVOFF=`tput rmso`
VERSION="Simple Shell Menu Version 1.14"
COMMENT=.comment
TITLE=.title
USERNAME=`logname`
#MAIL=[if not set in the ENV then set here somehow]

##############################################################################
##	CatchTrap	
#
##############################################################################
CatchTrap()
{
	echo "" 1>&2
	echo -n "Signal '${1}' caught, do you want to exit (Y/N)?" 1>&2
	read DoQuit

	if [ "${DoQuit}" = "Y" -o "${DoQuit}" = "y" ]
	then
		exit 1
	else
		exec ${MENUPROG}
	fi

	return 1
}
##############################################################################
##	CheckNewMail	
#
#	NOTE: this function may be changed if your system has a better way to
#			check for new mail.
#
#	return: 0 = NO NEW MAIL, 0 != NEW MAIL
#
##############################################################################
CheckNewMail()
{
	# if you mail file is set
	if [ "${MAIL}" != "" ]
	then
		# if your mail file has something in it
		# then you have new mail
		# else not
		if [ -s ${MAIL} ]
		then
			return 1 
		else
			return 0 
		fi
	fi
	return 0 
}
##############################################################################
##	GetOptCmd	
#
##############################################################################
GetOptCmd()
{
	typeset GOC_option=${1}
	GOC_ret=`grep "^#PROGRAM=" ${GOC_option} 2> /dev/null | cut -c 10-`
	return 1
}
##############################################################################
##	GetOptTitle	
#
##############################################################################
GetOptTitle ()
{
	typeset GOT_option=${1}
	GOT_ret=`grep "^#TITLE=" ${GOT_option} 2> /dev/null | cut -c 8-`
}

##############################################################################
##	DeleteTitle	
#
##############################################################################
DeleteTitle()
{
	rm -f ${TITLE};
}

##############################################################################
#	DeleteComment	
#
##############################################################################
DeleteComment()
{
	rm -f ${COMMENT};
}
##############################################################################
#	AddOption	
#
##############################################################################
AddOption()
{
	typeset AO_option=${1}

	# check to see if this option is a submenu
	if [ -d ${AO_option} ]
	then
		print
		print -n "\t'${AO_option}' is a SubMenu...<ENTER> to continue: "
		read dummy
		return 1
	fi

	# check to see if this option exists
	if [ -f ${AO_option} ]
	then
		print
		print -n "\t'${AO_option}' already exists...<ENTER> to continue: "
		read dummy
		return 1
	fi

	print ${CLS}
	print "\t\t\t---- ADD OPTION ----"
	print 
	print -n "\tEnter Title  : "
	read NewTitle

	print 
	print -n "\tEnter Command: "
	read NewCommand

	eval "echo \"#TITLE=${NewTitle}\" > ${AO_option}"
	eval "echo \"#PROGRAM=${NewCommand}\" >> ${AO_option}"

	return 1
}

##############################################################################
##	EditOption	
#
##############################################################################
EditOption()
{
	typeset EO_option=${1}

	# check to see if this option is a submenu
	if [ -d ${EO_option} ]
	then
		print
		print -n "\t'${EO_option}' is a SubMenu...<ENTER> to continue: "
		read dummy
		return 1
	fi

	print ${CLS}
	print "\t\t\t---- EDIT OPTION ----"
	print 
	GetOptTitle ${EO_option}

	print "\tOld Title        : ${GOT_ret}"
	print -n "\tEnter New Title  : "
	read NewTitle
	typeset NewTitle=${NewTitle:=${GOT_ret}}

	print 
	GetOptCmd ${EO_option}

	print "\tOld Command      : ${GOC_ret}"
	print -n "\tEnter New Command: "
	read NewCommand
	typeset NewCommand=${NewCommand:=${GOC_ret}}

	eval "echo \"#TITLE=${NewTitle}\" > ${EO_option}"
	eval "echo \"#PROGRAM=${NewCommand}\" >> ${EO_option}"

	return 1
}

##############################################################################
##	DeleteOption	
#
##############################################################################
DeleteOption()
{
	typeset DO_option=${1}

	if [ ! -f ${DO_option} ]
	then
		print -n"\t'${DO_option}' is NOT a valid Option...<ENTER> to continue: "
		read dummy
		return 1
	fi

	print -n "\tDelete Option '${DO_option}' (Y/N)?: "
	read DoDelete

	if [ "${DoDelete}" = "Y"  -o "${DoDelete}" = "y" ]
	then
		rm -f ${DO_option}
	fi
	return 1
}
##############################################################################
##	AddSubMenu	
#
##############################################################################
AddSubMenu()
{
	typeset ASM_option=${1}

	# check to see if submenu exists
	if [ -d ${ASM_option} ]
	then
		print
		print -n "\tSubMenu '${ASM_option}' already exists...<ENTER> to continue: "
		read dummy
		return 1
	fi

	# check to see if this option exists
	if [ -f ${ASM_option} ]
	then
		print
		print -n "\tCan't create SubMenu '${ASM_option}', option already exists...<ENTER> to continue: "
		read dummy
		return 1
	fi

	eval "mkdir ${ASM_option} 2> /dev/null"

	print ${CLS}
	print "\t\t\t---- ADD SUBMENU ----"
	print 
	print -n "\tEnter Title  : "
	read NewTitle

	print 
	print -n "\tEnter Comment: "
	read NewComment

	eval "echo \"${NewTitle}\" > ${ASM_option}/${TITLE}"
	eval "echo \"${NewComment}\" > ${ASM_option}/${COMMENT}"

	return 1
}


##############################################################################
##	DeleteSubMenu	
#
##############################################################################
DeleteSubMenu()
{
	typeset DSM_option=${1}

	if [ ! -d ${DSM_option} ]
	then
		print -n "\t'${DSM_option}' is NOT a valid SubMenu...<ENTER> to continue.: "
		read dummy
		return 1;
	fi
	
	print -n "\tDelete SubMenu '${DSM_option}' and *ALL* of it's contents (Y/N)?: "
	read DoDelete

	if [ "${DoDelete}" = "Y"  -o "${DoDelete}" = "y" ]
	then
		rm -rf ${DSM_option};
	fi
	return 1;
}

##############################################################################
##	AddComment	
#
##############################################################################
AddComment()
{
	DeleteComment
	EditComment
}
##############################################################################
##	AddTitle	
#
##############################################################################
AddTitle()
{
	DeleteTitle
	EditTitle
}
##############################################################################
##	EditTitle	
#
##############################################################################
EditTitle()
{
	if [ ! -f ${TITLE} ]
	then
		touch ${TITLE}
	fi
	typeset OldTitle=`cat ${TITLE} 2> /dev/null`

	print 
	print -n "Enter New Title: "
	read NewTitle

	eval "echo \"${NewTitle}\" > ${TITLE}"
}
##############################################################################
##	EditComment	
#
##############################################################################
EditComment()
{
	if [ -f ${COMMENT} ]
	then
		touch ${COMMENT}
	fi
	typeset OldComment=`cat ${COMMENT} 2> /dev/null`

	print 
	print -n "Enter New Comment: "
	read NewComment

	eval "echo \"${NewComment}\" > ${COMMENT}"
}
##############################################################################
##	EditSubMenu	
#
##############################################################################
EditSubMenu()
{
	typeset EO_subMenu=${1}
	print -n "\tGo To Submenu To Edit...<ENTER> to continue: "
	read dummy
}
##############################################################################
##	EditMenu	
#
##############################################################################
EditMenu()
{
	print;
	print -n "\t(A)dd, (M)odify, (D)elete, or (Q)uit: "
	read mode

	case "${mode}" in
		a|A)	;;
		m|M)	;;
		d|D)	;;
		q|Q)	return 1;;
		*)	return 1;;
	esac

	print -n "\t(T)itle, (C)omment, (O)ption, (S)ubmenu, or (Q)uit: "
	read option

	case "${option}" in
		t|T)
			case "${mode}" in
				a|A)
					AddTitle
					;;
				m|M)
					EditTitle
					;;
				d|D)
					DeleteTitle
					;;
				*)	return 1;;
			esac
			;;

		c|C)
			case "${mode}" in
				a|A)
					AddComment
					;;
				m|M)
					EditComment
					;;
				d|D)
					DeleteComment
					;;
				*)	return 1;;
			esac
			;;

		o|O)	
			print -n "\tEnter Option # [1-9], or (Q)uit: ";
			read option

			# 'q' to quit
			if [ "${option}" = "q" -o "${option}" = "Q" ]
			then
				return 1
			fi

			# if option is a directory then quit
			# else edit option
			if [ -d ${option} ]
			then
				print -n "\t'${option}' is a SubMenu...<ENTER> to continue "
				read dummy
				return 1
			else

				case "${mode}" in
					a|A)
						AddOption ${option} ;
						;;
					m|M)
						EditOption ${option} ;
						;;
					d|D)
						DeleteOption ${option} ;
						;;
					*)	
						return 1;
						;;
				esac
			fi
			;;

		s|S)
			print -n "\tEnter SubMenu # [1-9], or (Q)uit: ";
			read option

			# 'q' to quit
			if [ "${option}" = "q" -o "${option}" = "Q" ]
			then
				return 1
			fi
			case "${mode}" in
				a|A)
					AddSubMenu ${option} ;
					;;
				m|M)
					EditSubMenu ${option} ;
					;;
				d|D)
					DeleteSubMenu ${option} ;
					;;
				*)	
					return 1;
					;;
			esac
			;;

		q|Q)
			return 1;
			;;
		*)
			return 1;
			;;
	esac
	return 1
}
##############################################################################
##	PrintHeader	
#
##############################################################################
PrintHeader()
{
	print ${REVOFF}
	print ${CLS}
	print "  ${REVON}                       ${VERSION}                     ${REVOFF}"
	GetDispStr "${TITLE}"
	# default menu title
	if [ "${printline}" = "" ]
	then
		printline="                               (Untitled Menu)"
	fi
	print "${printline}"

	print "    ${REVON}=====================================================================${REVOFF}"
	GetDispStr "${COMMENT}"

	# default comment
	if [ "${printline}" = "" ]
	then
		printline="                          Please Select An Option."
	fi
	print "${printline}"
	print ""
}

##############################################################################
##	GetInput	
#
##############################################################################
GetInput()
{
	input=S
	goodInput=0
	read input
	
	case "${input}" in
		# choose option 1 - 9
		[1-9])
			# if option is a submenu (directory) then go to that
			# directory.
			if [ -d ${input} ]
			then
				cd ${input}
			else

				# if option is a menu choice (file) then get the 
				# program name and run it.
				if [ -f ${input} ]
				then
					# get command
					GOC_ret=""
					GetOptCmd ${input}
					execline=${GOC_ret}

					# save current menu directory
					retdir=`pwd`

					# execute command
					eval "${execline}"
	
					# return to current menu directory
					cd ${retdir};
				fi
			fi
			;;

		# exit menu
		x|X|q|Q)
			# if NOT at the top (main) menu then 
			# return to previous menu (directory) and run the menu
			# program from there.
			if [ ! -f ./.top ]
			then
				cd ..;
				exec ${MENUPROG};
			fi
			goodInput=1;
			;;

		# run default shell
		shell)
			# save current menu directory
			retdir=`pwd`;

			# start default shell
			eval "${SHELL:-sh}";

			# return to current menu directory
			cd ${retdir};
			;;

		# Edit menu title or comment
		e|E)
			EditMenu;
			;;

		# Print Version Info
		v|V|Version|version|VERSION)
			print;
			print -n "\t${REVON}${VERSION}... <ENTER> to return.${REVOFF}";
			read option;
			;;

		# invalid option
		*)
			echo "\007";
			echo -n "\t\t${REVON}*** Invalid Entry...try again ***${REVOFF}";
			sleep 1;
			;;
	esac

	return ${goodInput}
}
##############################################################################
##	PrintMenuOptions	
#
##############################################################################
PrintMenuOptions()
{
	options=`ls -d [1-9] 2> /dev/null`
	for i in ${options}
	do
		if [ -d ${i} ]
		then
			printline=`head -1 ${i}/${TITLE} 2> /dev/null`
			if [ "${printline}" = "" ]
			then
				printline="(Untitled Sub Menu)"
			fi
		fi

		if [ -f ${i} ]
		then
			GOT_ret=""
			GetOptTitle ${i}
			printline=${GOT_ret}
		fi

		if [ "${printline}" = "" ]
		then
			printline="(Untitled Option)"
		fi

		print "\t\t${i} - ${printline}"
	done
	print
	if [ -f ./.top ]
	then
		print "\t\tX - Exit Menuing System"
	else
		print "\t\tX - Exit to Previous Menu"
	fi
	print ${REVOFF}
	print

	# check for new mail
	CheckNewMail
	NewMailFlag=${?}
	# if you have new mail 
	# then print a message
	if [ ${NewMailFlag} -ne 0 ]
	then
		echo "\t\t${REVON}YOU HAVE UNPROCESS MAIL${REVOFF}"
	fi

	echo -n "\t\tEnter Selection, ${USERNAME} ==>> "
}
##############################################################################
##	GetDispStr	
#
##############################################################################
GetDispStr()
{
	GDSFName=$1

	printline=""
	printline=`head -1 ${GDSFName} 2> /dev/null`

	linelen=0
	linelen=${#printline}
	if [ ${linelen} -ne 0 ]
	then
		pad=`expr \( \( 80 - ${linelen} \) \/ 2 \)`
	else
		pad=0
		printline=""
	fi
	count=1
	padstr=""
	while [ ${count} -lt ${pad} ]
	do
		padstr=${padstr}" "
		count=`expr ${count} + 1`
	done
	printline="${padstr}${printline}"
}

##############################################################################
#	main	
#
##############################################################################

# if arg 1 is '-init' then start new menu here
if [ "${1}" = "-init" ]
then
	echo "Creating Top Level Menu Marker..."
	eval "touch .top"
	exit 0
fi

set noclobber off

# define traps
for aSig in  1 2 3 15
do
	trap "CatchTrap ${aSig}" ${aSig}
done

good=0
while [ ${good} -eq 0 ]
do
	PrintHeader
	PrintMenuOptions

	GetInput
	good=${?}
done

print ${REVOFF}
exit 1
