#!/bin/sh

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

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" \
	DPA "Launch the Digi Port Authority Program" \
	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
		;;
	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
		;;

	DPA)
		/usr/bin/dpa.dgap
		;;
	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
