#!/bin/sh

echo "[H[J"
echo "[1m              Welcome to The Simplest PPP Configurator! :-)[m"
echo ""
echo "    Your PPP is already preconfigured, so that you can dial manually."
echo "However, you probably noticed that it requires chatting with your modem"
echo "and logging in every time you want to connect. It's simple and it works,"
echo "but it's also annoying."
echo ""
echo "This script will try to configure your PPP so that you can run it in"
echo "background (thus freeing the console), and log in automatically."
echo ""
echo "If you want to continue, press [1mEnter[m, otherwise press [1mCtrl-C[m."
echo ""
read junk
while [ "X${ans}" != "Xy" ]
do

while [ "X${phone}" = "X" ]
do
	echo "[H[J"
	echo "[1m                        Phone Number[m"
	echo ""
	echo "Here you should enter the full phone number you normally"
	echo "use to connect to your provider, with all necessary prefixes"
	echo "attached, e.g.: 01122334455"
	echo ""
	read -p "Please enter the phone number: " phone
done
while [ "X${dev}" = "X" ]
do
	echo "[H[J"
	echo "[1m                        Port Number[m"
	echo ""
	echo "Here you should enter the port number, to which your modem is"
	echo "attached. REMEMBER: COM1 is port 0 in FreeBSD, COM2 - port 1,"
	echo "and so on. You should enter only the number, not the full name"
	echo "of the device."
	echo ""
	read -p "Please enter the port number (0,1,2): " dev
done
while [ "X${timo}" = "X" ]
do
	echo "[H[J"
	echo "[1m                        Idle Timeout[m"
	echo ""
	echo "Here you should enter timeout (in seconds). After this time, if"
	echo "the connection is idle, it's disconnected (to save your money :-)"
	echo ""
	read -p "Please enter the timeout value: " timo
done
while [ "X${user}" = "X" ]
do
	echo "[H[J"
	echo "[1m                        Login Name[m"
	echo ""
	echo "Here you should enter your login name that you normally use"
	echo "to log in to your provider's terminal server."
	echo ""
	read -p "Please enter your login name: " user
done
while [ "X${pass}" = "X" ]
do
	echo "[H[J"
	echo "[1m                        Password[m"
	echo ""
	echo "Here you enter the password that you use to log in to the"
	echo "terminal server."
	echo ""
	echo "[31mWARNING: your password will be stored in readable form on the"
	echo "floppy!!! If you don't like it... well, you must dial in manually."
	echo "In that case abort this script (Ctrl-C). Otherwise, continue.[37m"
	echo ""
	read -p "Please enter your password: " pass
done
echo "[H[J"
while [ "X${chat}" = "X" ]
do
	echo "[1m                     Type of Login Dialog[m"
	echo ""
	echo "What type of login dialog do you expect from the terminal server?"
	echo ""
	echo "1)	[32m......login:[37m ${user}"
	echo "	[32m...password:[37m ********"
	echo "		[36m(terminal server starts PPP here)[37m"
	echo ""
	echo "2)	[32m......login:[37m ${user}"
	echo "	[32m...password:[37m ********"
	echo "	[32m...protocol:[37m ppp"
	echo "		[36m(terminal server starts PPP here)[37m"
	echo ""
	echo "3)	[32m......username:[37m ${user}"
	echo "	[32m......password:[37m ********"
	echo "	[32mportX/..xxx...:[37m ppp"
	echo "		[36m(terminal server starts PPP here)[37m"
	echo ""
	read -p "Choose 1,2 or 3: " chat
	case ${chat} in
	1)
		chat1="TIMEOUT 10 ogin:--ogin: ${user} word: \\\\P"
		;;
	2)
		chat1="TIMEOUT 10 ogin:--ogin: ${user} word: \\\\P otocol: ppp"
		;;
	3)
		chat1="TIMEOUT 10 ername:--ername: ${user} word: \\\\P port ppp"
		;;
	*)	echo "Bad value! Please choose 1,2 or 3."
		echo ""
		unset chat
		;;
	esac
done
echo "[H[J"
echo "[1m     You assigned the following values:[m"
echo ""
echo "	Phone number:	${phone}"
echo "	Port number:	cuaa${dev}"
echo "	Timeout:	${timo} s"
echo "	Login name:	${user}"
echo "	Password:	${pass}"
echo "	Chat pattern:	${chat}"
echo ""
read -p "Are you satisfied with these values? (y/n) " ans
if [ "X${ans}" != "Xy" ]
then
	unset phone
	unset dev
	unset timo
	unset user
	unset pass
	unset chat
fi
done

echo ""
echo -n "Generating /etc/ppp/ppp.conf file..."
rm -f /etc/ppp/ppp.conf
cp /etc/ppp/ppp.conf.template /etc/ppp/ppp.conf
echo "" >>/etc/ppp/ppp.conf
echo "# This part was generated with $0" >>/etc/ppp/ppp.conf
echo "dialup:" >>/etc/ppp/ppp.conf
echo " set phone ${phone}" >>/etc/ppp/ppp.conf
echo " set authkey ${pass}" >>/etc/ppp/ppp.conf
echo " set timeout ${timo}" >>/etc/ppp/ppp.conf
echo " set login \"${chat1}\"" >>/etc/ppp/ppp.conf
echo " set ifaddr 10.0.0.1/0 10.0.0.2/0 255.255.255.0 0.0.0.0" >>/etc/ppp/ppp.conf
echo " delete all" >>/etc/ppp/ppp.conf
echo " add 0 0 HISADDR" >>/etc/ppp/ppp.conf

echo " Done."

echo ""
echo "Ok. Please check the contents of /etc/ppp/ppp.conf, and edit it if"
echo "necessary. When you're satisfied with it, run ppp in background:"
echo ""
echo "	ppp -background dialup"
echo ""
echo "REMEMBER to run [1m/stand/update[m! Otherwise these changes will be lost!"
echo ""
