#!/bin/sh
#
#Coyote Linux Gateway configuration menu

# Source the coyote linux configuration file
. /etc/coyote/coyote.conf

while [ 1 ]; do

clear
echo
echo "                Coyote Linux Gateway -- Configuration Menu"
echo
echo
echo "  1) Edit main configuration file         2) Change system password"
echo "  3) Edit rc.local script file            4) Custom firewall rules file"
echo "  5) Edit firewall configuration          6) Edit port forward configuration"
echo
echo "  c) Show running configuration           f) Reload firewall"
echo "  r) Reboot system                        w) Write configuration to disk"
if [ "$INETTYPE" = "PPP" ]; then
  echo "  d) Dial PPP connection                  h) Hangup PPP connection"
fi
if [ -e /usr/sbin/iptraf ] ; then
  echo "  i) IpTraf"
fi
echo
echo "  q) Exit Menu                            l) Logout"
echo "  ----------------------------------------------------------------------------"
echo -n "  Selection: "

read OPT


	case $OPT in
		q | Q)	echo
			echo "Exiting menu. To return to the menu, simply type \"menu\" and"
			echo "press enter from the command prompt."
			echo
			break
		    ;;
		1 )	edit /etc/coyote/coyote.conf
		    ;;
		2 )	clear
			passwd
			if [ -d /var/http/htdocs ]; then
    			ROOTPASS=`grep root: /etc/shadow | cut -f 2 -d :`
			    echo "Updating webadmin password..."
			    echo "root:$ROOTPASS" > /var/http/htdocs/cgi-bin/.htpasswd
			fi

			# Update the Coyote Configuration file entry
			ADMIN_AUTH=`grep root: /etc/shadow | cut -f 2 -d ":" | cut -b 5-`
			if [ -r /tmp/coyote.tmp ]; then
				rm /tmp/coyote.tmp
			fi
			touch /tmp/coyote.tmp
			chmod 600 /tmp/coyote.tmp
			cat /etc/coyote/coyote.conf | while read CONFENT; do
				CONFLINE=`echo "$CONFENT" | cut -f 1 -d "="`
				if ! [ "$CONFLINE" = "ADMIN_AUTH" ]; then
					if [ ! -z "$CONFENT" ]; then
						echo "$CONFENT" >> /tmp/coyote.tmp
					fi
				else
					if [ ! -z "$CONFENT" ]; then
						echo "ADMIN_AUTH=${ADMIN_AUTH}" >> /tmp/coyote.tmp
					fi
				fi
			done

			cp /tmp/coyote.tmp /etc/coyote/coyote.conf
			rm /tmp/coyote.tmp
			touch /tmp/need.save

			echo
			echo "Press enter to return to system menu."
			read JUNK
		    ;;
		3 )	edit /etc/rc.d/rc.local
		    ;;
		4 )	edit /etc/coyote/firewall.local
		    ;;
		5 )	edit /etc/coyote/firewall
		    ;;
		6 )	edit /etc/coyote/portforwards
		    ;;
		r | R)  echo
			echo
			echo -n "Are you sure you want to reboot the system? - [Y/N] - "
                        read JUNK
		        if [ "$JUNK" = "Y" -o "$JUNK" = "y" ] ; then
    				reboot
				exit 0;
			fi	
		    ;;
                i | I ) [ -e /usr/sbin/iptraf ] && iptraf -l eth0;
                    ;;
		l | L )	exit 10;
		    ;;
		c | C)  showcfg
			echo
			echo -n "Press enter to return to system menu. "
			read JUNK ;;
		w | W)	clear
			lrcfg.back
			echo
			echo "Backup script complete. Press ENTER to return to menu."
			read JUNK
		    ;;
		f | F)	echo
			/etc/rc.d/rc.firewall
			echo
			echo "Firewall reload complete. Press ENTER to return to menu."
			read JUNK
		    ;;
		h | H)  clear
			/usr/sbin/ppp.hangup
			echo
			echo "Press ENTER to return to menu."
			read JUNK;;
	        d | D)  clear
			/usr/sbin/ppp.dial
			echo
			echo "Press ENTER to return to menu"
			read JUNK;;
	esac

done

exit 1
