#!/bin/sh
NVUSER="`whoami`"
NVSN="`hostname`"
if [ ! "$NVUSER" = "root" ]; then
	clear
	dialog --title "User not correct!" --msgbox "You are not a \
supperuser, please place this file in the /root directory & run it AS \
ROOT!." 6 60
	exit
fi

error_mesg () {
	dialog --title "Error" --msgbox "You \
didn't enter a value, please enter a value or chose <Cancel>" 6 52
}
old_config () {
	NVDS="`cat ~/.ppp.config | grep NVDS | cut -f2 -d=`"
	NVMODEM="`cat ~/.ppp.config | grep NVMODEM | cut -f2 -d=`"
	NVDNS="`cat ~/.ppp.config | grep NVDNS | cut -f2 -d=`"
	NVUSER="`cat ~/.ppp.config | grep NVUSER | cut -f2 -d=`"
	NVPASSWD="`cat ~/.ppp.config | grep NVPASSWD | cut -f2 -d=`"
	NVINIT="`cat ~/.ppp.config | grep NVINIT | cut -f2 -d=`"
	NVPHONE="`cat ~/.ppp.config | grep NVPHONE | cut -f2 -d=`"
	NVTIMEOUT="`cat ~/.ppp.config | grep NVTIMEOUT | cut -f2 -d=`"
}

default_config () {
	NVDS="NetVision.net.il"
	NVMODEM="/dev/cua1"
	NVDNS="194.90.1.5"
	NVUSER="NVUSER"
	NVPASSWD="NVPASSWD"
	NVINIT="AT&F&C1&D2&K3\\N3%C0"
	NVTIMEOUT="60"
}

creat_ppp_config () {
	echo domain NetVision.net.il > /etc/resolv.conf
	echo nameserver $NVDNS >> /etc/resolv.conf
	echo '""' "$NVINIT OK ATDT$NVPHONE name: $NVUSER word: $NVPASSWD" > ~/.ppp.scr
	echo "#!/bin/sh" > ~/ppp
	echo echo -n \"Checking if you\'r connected... \" >> ~/ppp
	echo TMP=\"\`ps "|" grep pppd "|" grep -v grep "|" cut -b 1-5\`\" >> ~/ppp
	echo if [ \"\$TMP\" = \"\" ] >> ~/ppp
	echo then echo \"no\" >> ~/ppp
	echo else echo \"yes\" >> ~/ppp
	echo echo -n \"killing ppp... \" >> ~/ppp
	echo kill -9 \$TMP >> ~/ppp
	echo sleep 1 >> ~/ppp
	echo echo \". done\" >> ~/ppp
	echo "fi" >> ~/ppp
	echo echo -n \"Clearing the modem\'s buffer.\" >> ~/ppp
	echo "more $NVMODEM > /dev/null &" >> ~/ppp
	echo "sleep 1" >> ~/ppp
	echo echo -n \".\" >> ~/ppp
	echo sleep 1 >> ~/ppp
	echo echo -n \".\" >> ~/ppp
	echo sleep 1 >> ~/ppp
	echo echo \". done\" >> ~/ppp
	echo kill \`ps "|" grep \"more $NVMODEM\" "|" grep -v grep "|" cut -b 1-5\` >> ~/ppp
	echo /usr/sbin/pppd /dev/modem 38400 defaultroute crtscts connect \'/usr/sbin/chat -f ~/.ppp.scr\' >> ~/ppp
	echo TMP=\"\" >> ~/ppp
	echo NVCOUNT=\"1\" >> ~/ppp
	echo echo -n \""          |"\" >> ~/ppp
	echo while [ "!" \"\$NVCOUNT\" = "$NVTIMEOUT" ] >> ~/ppp
	echo do >> ~/ppp
	echo NVCOUNT=\"\`expr \$NVCOUNT + 1\`\" >> ~/ppp
	echo echo "-n" \""-"\" >> ~/ppp
	echo done >> ~/ppp
	echo echo \""|"\" >> ~/ppp
	echo echo -n \"Connecting.\" >> ~/ppp
	echo NVCOUNT=\"1\" >> ~/ppp
	echo while [ \"\$TMP\" = \"\" ] >> ~/ppp
	echo do >> ~/ppp
	echo sleep 1 >> ~/ppp
	echo TMP=\"\`/sbin/ifconfig "|" grep P-t-P "|" cut -f2 -d \':\' "|" cut -f1 -d \' \'\`\" >> ~/ppp
	echo echo -n \".\" >> ~/ppp
	echo NVCOUNT=\"\`expr \$NVCOUNT + 1\`\" >> ~/ppp
	echo if [ \"\$NVCOUNT\" = \"$NVTIMEOUT\" ] >> ~/ppp
	echo then echo \"\" >> ~/ppp
	echo exit >> ~/ppp
	echo fi >> ~/ppp
	echo done >> ~/ppp
	echo echo \".\" >> ~/ppp
	echo echo \"Connected, your IP address is : \$TMP\" >> ~/ppp
	echo echo \"127.0.0.1 localhost\" ">" /etc/hosts >> ~/ppp
	echo echo \"\$TMP $NVSN.$NVDS $NVSN\" ">>" /etc/hosts >> ~/ppp
	chmod og-rwx ~/ppp;chmod u+rwx ~/ppp
	echo NVDS=$NVDS > ~/.ppp.config
	echo NVMODEM=$NVMODEM >> ~/.ppp.config
	echo NVDNS=$NVDNS >> ~/.ppp.config
	echo NVUSER=$NVUSER >> ~/.ppp.config
	echo NVPASSWD=$NVPASSWD >> ~/.ppp.config
	echo NVINIT=$NVINIT >> ~/.ppp.config
	echo NVPHONE=$NVPHONE >> ~/.ppp.config
	echo NVTIMEOUT=$NVTIMEOUT >> ~/.ppp.config
}

submenu_login () {
	while [ 0 ]; do
		dialog --title "Login" --menu "Select Login or Password" \
10 60 3 \
"Username" "Enter your username here" \
"Password" "Enter your password here" \
"Return" "Return to main menu" 2> /tmp/NVsel
		NVSEL="`cat /tmp/NVsel`"
		if [ "$NVSEL" = "Username" ]; then
		while [ 0 ]; do
			dialog --title "Username" --inputbox "Please enter \
your useranem here :" 8 60 2> /tmp/NVsel
			if [ "$?" = "0" ]; then
					NVSEL="`cat /tmp/NVsel`"
					if [ ! "$NVSEL" = "" ]; then
						NVUSER=$NVSEL
						break
					else	error_mesg
					fi
			else break
			fi
		done
		fi
		if [ "$NVSEL" = "Password" ]; then
		while [ 0 ]; do
			dialog --title "Password" --inputbox "Please enter \
your password here :" 8 60 2> /tmp/NVsel
			if [ "$?" = "0" ]; then
				NVSEL="`cat /tmp/NVsel`"
				if [ ! "$NVSEL" = "" ]; then
					NVPASSWD=$NVSEL
					break
				else	error_mesg
				fi
			else break
			fi
		done
		fi
		if [ "$NVSEL" = "" -o "$NVSEL" = "Return" ]; then
			break
		fi
	done
}

submenu_setup () {
	while [ 0 ]; do
		dialog --title "Setup modem & phone number" \
--menu "Select modem or phone number" 12 60 5 \
"Modem" "Select you modem port" \
"Init" "Enter modem init string" \
"Phone" "Enter the phone number" \
"Timeout" "Entering the timeout" \
"Return" "Return to main menu" 2> /tmp/NVsel
		NVSEL="`cat /tmp/NVsel`"
		if [ "$NVSEL" = "" -o "$NVSEL" = "Return" ]; then
			break
		fi
		if [ "$NVSEL" = "Modem" ]; then
		dialog --title "Modem setup" --menu "Select your modem's \
port" 11 50 4 "cua0" "Modem on COM 1" "cua1" "Modem on COM 2" \
"cua2" "Modem on COM 3" "cua3" "Modem on COM 4" 2> /tmp/NVsel
		NVSEL="`cat /tmp/NVsel`"
			if [ ! "$NVSEL" = "" ]; then
				NVMODEM="/dev/$NVSEL"
			fi
		fi
		if [ "$NVSEL" = "Init" ]; then
		while [ 0 ]; do
			dialog --title "Modem init string" --inputbox \
"Please enter the modem init string(*NOTE* do NOT type\n\
any space what so ever, VERY IMPORTANT!, TYPE ONLY IN\n\
UPPER CASE!). And also if you need to type \\ please\n\
type it twice, Example :\n\
AT&F&C1\\N3 will be --> AT&F&C1\\\\N3" 12 60 2> /tmp/NVsel
			if [ "$?" = "0" ]; then
				NVSEL="`cat /tmp/NVsel`"
				if [ ! "$NVSEL" = "" ]; then
					NVINIT=$NVSEL
					break
				else	error_mesg
				fi
			else break
			fi
		done
		fi
		if [ "$NVSEL" = "Phone" ]; then
		while [ 0 ]; do
			dialog --inputbox "Please enter the phone number" \
8 60 2> /tmp/NVsel
			if [ "$?" = "0" ]; then
				NVSEL="`cat /tmp/NVsel`"
				if [ ! "$NVSEL" = "" ]; then
					NVPHONE=$NVSEL
					break
				else	error_mesg
				fi
			else break
			fi
		done
		fi
		if [ "$NVSEL" = "Timeout" ]; then
		while [ 0 ]; do
			dialog --inputbox "Please enter the Time out in \
seconds" 8 60 2> /tmp/NVsel
			if [ "$?" = "0" ]; then
				NVSEL="`cat /tmp/NVsel`"
				if [ ! "$NVSEL" = "" ]; then
					NVTIMEOUT=$NVSEL
					break
				else	error_mesg
				fi
			else break
			fi
		done
		fi
	done
}

submenu_DNS () {
	while [ 0 ]; do
		dialog --inputbox "Enter the IP adress of the DNS server.\n\
Example : 194.90.1.5" 9 45 2> /tmp/NVsel
		if [ "$?" = "0" ]; then
			NVSEL="`cat /tmp/NVsel`"
			if [ ! "$NVSEL" = "" ]; then
				NVDNS=$NVSEL
				break
			else	error_mesg
			fi
		else break
		fi
	done 
}

submenu_show () {
	TMP="`echo $NVMODEM | cut -b 9-9`"
	TMP="`expr 1 + $TMP`"
	dialog --title "Showing Current Configuration" \
--msgbox "The Current Configuration is :\n\n\
   Server Name   : $NVSN.$NVDS\n\
   Domain Suffix : $NVDS\n\
   DNS Server    : $NVDNS\n\
   Modem Port    : COM $TMP\n\
   Modem Init    : $NVINIT\n\
   Phone number  : $NVPHONE\n\
   Time out      : $NVTIMEOUT\n\
   User Name     : $NVUSER\n\
   Password      : $NVPASSWD" 15 60
}

if [ "`ls -a ~ | grep .ppp.config`" = ".ppp.config" ]; then
	old_config
else
	default_config
fi

while [ 0 ]; do
dialog --title "NetVision's PPP Setup Script (by John Basila)" --menu \
"Welcome to my PPP setup script, this script will help\nyou setup your \
PPP connection via modem.\n*NOTE* : if you have trouble using the arrow keys\
\non your keyboard, you can use '+','-', and TAB insted." 15 60 5 \
"Setup" "Setting up your modem & phone number" \
"Login" "Setting up your username & password" \
"DNS" "Setting your DNS server" \
"Show" "Show Configuration" \
"Exit" "Exit the programe" 2> /tmp/NVsel
	NVSEL="`cat /tmp/NVsel`"
	if [ "$NVSEL" = "" -o "$NVSEL" = "Exit" ]; then
		dialog --title "Exit ?" --yesno "Save configuration?" 5 30
		if [ $? = 0 ]; then
			creat_ppp_config
			dialog --title "Done!" --msgbox "/root/ppp file \
generated, to connect type ppp \nat the command prompt AS ROOT.\
 If you have\nany comment or need help please e-mail \n\
john_b@NetVision.net.il or call 04-8550330 & ask for John" 8 50
			exit
		fi
		dialog --title "Done!" --msgbox "If you have any comment \
or need help please e-mail john_b@NetVision.net.il or call \
04-8550330 & ask for John" 6 60
		exit
	fi
	if [ "$NVSEL" = "Login" ]; then
		submenu_login
	fi
	if [ "$NVSEL" = "Setup" ]; then
		submenu_setup
	fi
	if [ "$NVSEL" = "DNS" ]; then
		submenu_DNS
	fi
	if [ "$NVSEL" = "Show" ]; then
		submenu_show
	fi
done
