#!/bin/sh

# In case we had to build our own dialog binary, add our path, just in case.
export PATH=/etc/dgap:$PATH

# Point to our empty dialogrc file
DIALOGRC=/etc/dgap/dialogrc
export DIALOGRC

termtype=${TERM:-"linux"}
cols=`tput cols`
cols=`expr "$cols" - 4`

maintitle="                Digi International -  MenuPort Interface"
title1="Move using [UP] [DOWN],[Enter] to Select\n\n\n\n"

while [ 0 ]; do

	rm -f /tmp/menuitem*

	dialog --backtitle "$maintitle" --nocancel \
	--title "Main Menu" \
	--menu "$title1" 19 $cols 5 \
	Config "Launch the Config/Setup Program" \
	Module "Load or Unload the driver module" \
	Diags "Select a Digi diagnostics utility to run" \
	RN "Read the Release Notes" \
	Exit "Exit MPI" \
	2>/tmp/menuitem.$$

	menuitem=`tail -1 /tmp/menuitem.$$`

	opt=$?

	case $menuitem in
	Config)
		/usr/bin/dgap_config
		;;

	Diags)

		while [ 0 ]; do

			rm -f /tmp/mpi.tmp

			dialog --backtitle "$maintitle" --nocancel \
			--title "Load/Unload Driver Menu" \
			--menu "$title1" 20 $cols 4 \
			DPA "Launch the Digi Port Authority Program" \
			Test "Launch the Digi Diagnostics Utility" \
			View "Launch the Digi Port Statistics Program" \
			Back "Go back" \
			2>/tmp/mpi.tmp

			menuitem1=`tail -1 /tmp/mpi.tmp`
			rm -f /tmp/mpi.tmp

			opt=$?
			case $menuitem1 in
			DPA)
				/usr/bin/dpa.dgap
				continue
				;;
			Test)
				/etc/dgap/dgaptest
				continue
				;;
			View)
				/etc/dgap/dgapview
				continue
				;;
			Back)
				break;
				;;
			*)
				break;
				;;
			esac
		done
		;;

	Module)
		rm -f /tmp/mpi.tmp

		dialog --backtitle "$maintitle" --nocancel \
		--title "Load/Unload Driver Menu" \
		--menu "$title1" 20 $cols 4 \
		Load "Load the dgap driver module, if not already loaded." \
		Unload "Unload the dgap driver module, if not already unloaded." \
		Reload "Unload, then Load the dgap driver module." \
		Back "Go back" \
		2>/tmp/mpi.tmp

		menuitem1=`tail -1 /tmp/mpi.tmp`
		rm -f /tmp/mpi.tmp

		opt=$?
		case $menuitem1 in
		Load)
			/etc/init.d/dgap start dialog
			;;
		Unload)
			/etc/init.d/dgap stop dialog
			;;
		Reload)
			/etc/init.d/dgap restart dialog
			;;
		Back)
			;;
		*)
			;;
		esac
		;;

	RN)
		dialog  --textbox /etc/dgap/relnotes.txt 20 $cols 2>/dev/null
		;;
	Exit)
		tput -T${termtype} clear
		rm -f /tmp/menuitem*
		exit
		;;
	esac

	if [ -z $menuitem ]
	then
		tput -T${termtype} clear
		rm -f /tmp/menuitem*
		exit
	fi
done
