#!/bin/bash 
###############################################################################
#
#	PPPSETUP  --  Script to set up pppd
#  	History:
#	  7/21/95 RSL         Script created (v1.0)
#         9/15/96 RSL         Added code to get the ISP's domainname (v1.1)
#         9/19/96 RSL         Revamped interface to use the 'dialog' 
#                             program. (v1.2)
###############################################################################

###############################################################################
#         5/3/99 KR           Added pap, chap, ms-chap, & 
#                             modem init string options. (v2.16)
#                             Creates /etc/ppp/options, & pap, chap, 
#			      secrets files.
#                             More guidance.
#                             Monitor connection.
###############################################################################

if [ ! "$UID" = "0" ]; then
echo
echo "NOTICE: * $LOGNAME * You need to be 'root' to run this script."
echo "You could login: root"
echo "You could also try one of these: # sudo pppsetup"
echo "                                 # su -c pppsetup"
echo
exit 1
fi

if ! type -all "dialog" >/dev/null 2>&1 ; then
echo
echo "Can't find 'dialog', i can't run without 'dialog' on your system."
echo "You can get a compiled elf version of 'dialog' from here."
echo "http://www.tux.org/pub/people/kent-robotti/index.html"
echo "ftp://ftp.tux.org/pub/people/kent-robotti 'dialog-0.9a-15.elf.tar.gz'"
echo
exit 1
fi

if [ ! -d /tmp ]; then
mkdir /tmp
chmod 1777 /tmp
fi

mkdir -p /tmp/ppptmp || exit 1
chmod 600 /tmp/ppptmp

TMP=/tmp/ppptmp

VERSION="2.16"

echo "PPPSETUP $VERSION ..." > $TMP/txtTEMP01
echo >> $TMP/txtTEMP01
echo "Written by Robert S. Liesenfeld <xunil@bitstream.net> <IRC:Xunil>" >> $TMP/txtTEMP01
echo "Changes for $VERSION by Kent Robotti <krobot@erols.com>" >> $TMP/txtTEMP01
echo >> $TMP/txtTEMP01
echo "This script will ask a few questions, ISP phone number," >> $TMP/txtTEMP01
echo "IP address, username, password, for connecting to your" >> $TMP/txtTEMP01
echo "service provider and establishing a PPP connection." >> $TMP/txtTEMP01
echo >> $TMP/txtTEMP01
echo "Press [Enter] to continue with pppsetup." >> $TMP/txtTEMP01

dialog --backtitle "PPPSETUP $VERSION ..." --textbox "$TMP/txtTEMP01" 14 70 

if [ -s /usr/doc/pppsetup/pppsetup.txt ]; then
dialog --title "READ" --clear --textbox "/usr/doc/pppsetup/pppsetup.txt" 22 76	
fi

stty erase "^?" 2>/dev/null 

while [ -z "$PHONENUM" ]
do
echo "What is the phone number of your (I)nternet (S)ervice (P)rovider?" > $TMP/txtTEMP$$ 
echo  >> $TMP/txtTEMP$$ 
echo "Example: 6661776" >> $TMP/txtTEMP$$ 
echo  >> $TMP/txtTEMP$$ 
echo "(Note: in the USA, use *70,6661776 [comma required!] to turn off" >> $TMP/txtTEMP$$ 
echo " call waiting.)" >> $TMP/txtTEMP$$ 
echo  >> $TMP/txtTEMP$$ 
	
dialog --backtitle "PHONE NUMBER ..." --inputbox "`cat $TMP/txtTEMP$$`" 13 70 2> $TMP/rspTEMP$$  

if [ $? = 1 ]; then
rm -rf $TMP
clear 2>/dev/null || echo
echo "PPP configuration cancelled."
exit
fi 

PHONENUM="`cat $TMP/rspTEMP$$`"
 
if [ -z "$PHONENUM" ]; then
clear 2>/dev/null || echo
rm -rf $TMP
echo "PPP configuration cancelled."
echo "No phone number."
exit
fi
done

echo "Where is your modem /dev/ttyS?" > $TMP/txtTEMP$$ 
dialog --backtitle "MODEM DEVICE ..." --menu "`cat $TMP/txtTEMP$$`" 11 46 4 \
ttyS0 "= (COM1: under DOS)" \
ttyS1 "= (COM2: under DOS)" \
ttyS2 "= (COM3: under DOS)" \
ttyS3 "= (COM4: under DOS)" \
2> $TMP/rspTEMP$$

MODEM="`cat $TMP/rspTEMP$$`"

if [ -z $MODEM ]; then
clear 2>/dev/null || echo
rm -rf $TMP
echo "PPP configuration cancelled."
exit 
fi

echo "What's the maximum baud rate of your modem?" > $TMP/txtTEMP$$
dialog --backtitle "MAXIMUM MODEM BAUD RATE ..." --menu "`cat $TMP/txtTEMP$$`" 12 62 5 \
460800 "460KBps  - ISDN modem..." \
230400 "230KBps  - 56Kbps modem... or ISDN modem..." \
115200 "115KBps  - 28.8, 33.6, or 56Kbps modem..." \
57600  "57.6KBps - 28.8, 33.6, or 56Kbps modem..." \
38400  "38.4KBps - 9600 - 33.6 modem..." 2> $TMP/rspTEMP$$

BAUDRATE="`cat $TMP/rspTEMP$$`"

if [ -z $BAUDRATE ]; then
clear 2>/dev/null || echo
rm -rf $TMP
echo "PPP configuration cancelled."
exit 
fi

echo 'The default modem init string will be: "AT\&FW2H0" OK' > $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "If you want to change it put your init string in the box below." >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "If you use \ in the init string, put it twice." >> $TMP/txtTEMP$$
echo 'Example: "AT\&F\\K3\\N3W2H0" OK' >> $TMP/txtTEMP$$
echo "M = No sound: W2 or S95=1 = Show DCE CONNECT speed: 28800 etc." >> $TMP/txtTEMP$$
echo 'Put "" around each init string with "&" in it.' >> $TMP/txtTEMP$$
echo 'Put OK after each init string. Example: ATZ OK "AT\&F1MS95=1H0" OK' >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "Just press [Enter] on a empty box to accept the default above." >> $TMP/txtTEMP$$

dialog --backtitle "MODEM INIT STRING ..." --inputbox "`cat $TMP/txtTEMP$$`" 18 74 2> $TMP/rspTEMP$$

if [ $? = 1 ]; then
rm -rf $TMP
clear 2>/dev/null || echo
echo "PPP configuration cancelled."
exit 
fi 

if [ -s $TMP/rspTEMP$$ ]; then
INIT="`cat $TMP/rspTEMP$$`"
else
INIT="below"
fi

echo "What is the IP address of your Internet provider's nameserver?" > $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "You must have a correct IP address in the /etc/resolv.conf file." >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "You can put a second IP address like this." >> $TMP/txtTEMP$$
echo "IP address + IP address" >> $TMP/txtTEMP$$
echo "Example: 207.132.116.5 + 306.119.175.62" >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "Note: Your service provider's technical support can provide you" >> $TMP/txtTEMP$$
echo "with this information.  Example: 207.132.116.5" >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "If you already have a IP address in /etc/resolv.conf and want" >> $TMP/txtTEMP$$
echo "to use it, just press enter on a empty box and skip this." >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$

dialog --backtitle "DNS IP ADDRESS ..." --inputbox "`cat $TMP/txtTEMP$$`" 20 70 2> $TMP/ipTEMP$$

if [ $? = 1 ]; then
rm -rf $TMP
clear 2>/dev/null || echo
echo "PPP configuration cancelled."
exit 
fi 

DNSIP="$TMP/ipTEMP$$"

echo "Does your service provider use PAP or CHAP authentication?" > $TMP/txtTEMP$$
echo "A lot of service providers today use PAP or CHAP." >> $TMP/txtTEMP$$
echo "If you're presented with a Login: and Password: prompt when" >> $TMP/txtTEMP$$
echo "you connect to your service provider, you might need to use" >> $TMP/txtTEMP$$
echo "the SCRIPT option." >> $TMP/txtTEMP$$
echo "You should ask your service provider if they use PAP or CHAP" >> $TMP/txtTEMP$$
echo "authentication, this could save you a lot of wasted time." >> $TMP/txtTEMP$$
echo "If in doubt try PAP/CHAP." >> $TMP/txtTEMP$$

dialog --backtitle "PAP/CHAP or SCRIPT? ..." --menu "`cat $TMP/txtTEMP$$`" 16 66 2 \
PAP/CHAP "AUTHENTICATION" \
SCRIPT "Create Chat Script For Login" 2> $TMP/papTEMP

if [ $? = 1 ]; then
rm -rf $TMP
clear 2>/dev/null || echo
echo "PPP configuration cancelled."
exit 
else
PAP="`cat $TMP/papTEMP`"
fi

if [ ! -d /etc/ppp ]; then 
mkdir -p /etc/ppp
fi

if [ ! "$PAP" = "SCRIPT" ]; then   
echo "Put your Login = Username in the box below." > $TMP/txtTEMP1
echo >> $TMP/txtTEMP1
echo "Sometimes they want your username or number followed" >> $TMP/txtTEMP1
echo "by their @domain name like this below." >> $TMP/txtTEMP1
echo "Examples: jerry@foo.boo.com   1234567@foo.boo.com" >> $TMP/txtTEMP1
echo >> $TMP/txtTEMP1
echo "Sometimes there's a \, if so put it twice." >> $TMP/txtTEMP1
echo "Example: something\\\something" >> $TMP/txtTEMP1
echo >> $TMP/txtTEMP1
echo "Sometimes the username is two words." >> $TMP/txtTEMP1
echo "Example: jerry donut" >> $TMP/txtTEMP1
echo >> $TMP/txtTEMP1
echo "Usally it's just your username like this below." >> $TMP/txtTEMP1
echo "Example: jerry" >> $TMP/txtTEMP1
echo >> $TMP/txtTEMP1

dialog --title "PAP or CHAP LOGIN? ..." --inputbox "`cat $TMP/txtTEMP1`" 21 60 2> $TMP/rspTEMP1

if [ $? = 1 ]; then
rm -rf $TMP
clear 2>/dev/null || echo
echo "PPP configuration cancelled."
exit 
fi

if [ ! -s $TMP/rspTEMP1 ]; then   
rm -rf $TMP
clear 2>/dev/null || echo
echo "PPP configuration cancelled."
echo "No username for pap or chap given."
exit
fi 
fi

if [ -s $TMP/rspTEMP1 ]; then 
AUTH1="`cat $TMP/rspTEMP1`"
echo "What's your password?" > $TMP/txtTEMP2
echo "Example: Xpi9u87T" >> $TMP/txtTEMP2
dialog --title "PAP or CHAP PASSWORD? ..." --inputbox "`cat $TMP/txtTEMP2`" 9 52 2> $TMP/rspTEMP2

AUTH2="`cat $TMP/rspTEMP2`"
END=""

elif [ "$PAP" = "SCRIPT" ]; then  
END='""'
echo "If you reached this part of the script, it should be because you" > $TMP/txtTEMP$$
echo "skipped the PAP/CHAP part of this script." >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "In other words your service provider is not using PAP or CHAP." >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "I need to know what your service provider prints to your screen" >> $TMP/txtTEMP$$
echo "when you dial them, and what you respond with." >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$

echo "If you're not sure what to expect when you connect to your" >> $TMP/txtTEMP$$
echo "service provider, you can use 'minicom' to dial them and" >> $TMP/txtTEMP$$
echo "see what they print to the screen." >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$

echo "Usally when you connect to your service provider you see a" >> $TMP/txtTEMP$$
echo "Username: or Login: prompt, and you respond with your name." >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "Then there's a Password: prompt and you respond with your" >> $TMP/txtTEMP$$
echo "password." >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "If you're not sure if it's Login: or login:, just put ogin:" >> $TMP/txtTEMP$$
echo "you don't have to spell the whole thing out, the end part" >> $TMP/txtTEMP$$
echo "of what to expect should be enough." >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo 'If they put this for example "User ID:", you could just put ID:' >> $TMP/txtTEMP$$
echo 'or you could spell the whole thing out and put "" around it.' >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "What text should I wait for?" >> $TMP/txtTEMP$$
echo "ID:" >> $TMP/txtTEMP$$
echo "What text should I send?" >> $TMP/txtTEMP$$
echo "jerry" >> $TMP/txtTEMP$$
echo "What text should I wait for?" >> $TMP/txtTEMP$$
echo '"User ID:"' >> $TMP/txtTEMP$$
echo "What text should I send?" >> $TMP/txtTEMP$$
echo "jerry" >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "Sometimes you may want to wait for nothing, but send something." >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "What text should I wait for?" >> $TMP/txtTEMP$$
echo '""' >> $TMP/txtTEMP$$
echo "What text should I send?" >> $TMP/txtTEMP$$
echo "something" >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo '"" = wait for nothing.' >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "If you don't want your password printed out put \q in front of it." >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "What text should I wait for?" >> $TMP/txtTEMP$$
echo "Password:" >> $TMP/txtTEMP$$
echo "What text should I send?" >> $TMP/txtTEMP$$
echo "\qsecret" >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "ogin: = Login:   name: = Username:   word: = Password:" >> $TMP/txtTEMP$$

echo >> $TMP/txtTEMP$$
echo "This below would end up looking like this in the /etc/ppp/" >> $TMP/txtTEMP$$
echo "ppp.chatscript file created by pppsetup when you're done." >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "ogin: jerry" >> $TMP/txtTEMP$$
echo "word: \qsecret" >> $TMP/txtTEMP$$

echo >> $TMP/txtTEMP$$
echo "Basic example." >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "What text should I wait for?" >> $TMP/txtTEMP$$
echo "ogin:" >> $TMP/txtTEMP$$
echo "And what text should I send?" >> $TMP/txtTEMP$$
echo "jerry" >> $TMP/txtTEMP$$
echo "What text should I wait for?" >> $TMP/txtTEMP$$
echo "word:" >> $TMP/txtTEMP$$
echo "And what text should I send?" >> $TMP/txtTEMP$$
echo "\qsecret" >> $TMP/txtTEMP$$
echo "What text should I wait for?" >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "( End by pressing Enter on an empty = blank wait for? box. )" >> $TMP/txtTEMP$$

dialog --title "CHAT SCRIPT" --textbox "$TMP/txtTEMP$$" 22 72

MESSAGE=' '
YOUSAY=' '
while [ ! "$MESSAGE" = "" -a ! "$YOUSAY" = "" ]
do

echo "     End by pressing Enter on a empty = blank box." > $TMP/txtTEMP$$
echo "            What text should I wait for?" >> $TMP/txtTEMP$$
dialog --backtitle "EXPECT ..." --inputbox "`cat $TMP/txtTEMP$$`" 9 60 2> $TMP/rspTEMP$$	

if [ $? = 1 ]; then
rm -rf $TMP
clear 2>/dev/null || echo
echo "PPP configuration cancelled."
exit 
fi

MESSAGE="`cat $TMP/rspTEMP$$`"

if [ -z "$MESSAGE" ]; then
continue
fi

dialog --backtitle "SEND ..." --inputbox "And what text should I send?" 8 60 2> $TMP/rspTEMP$$

if [ $? = 1 ]; then
rm -rf $TMP
clear 2>/dev/null || echo
echo "PPP configuration cancelled."
exit 
fi

YOUSAY="`cat $TMP/rspTEMP$$`"

echo "$MESSAGE $YOUSAY" >> $TMP/sayTEMP1
	
if [ -z "$YOUSAY" ]; then
clear 2>/dev/null || echo
rm -rf $TMP
echo "PPP configuration cancelled."
echo "Nothing to send."
exit 
else	
continue
fi
done
fi

if [ ! -s "$DNSIP" ]; then
:
elif grep + $DNSIP >/dev/null 2>&1 ; then
echo "nameserver `cat $DNSIP | cut -d' ' -f1`" > /etc/resolv.conf
echo "nameserver `cat $DNSIP | cut -d' ' -f3`" >> /etc/resolv.conf
else
echo "nameserver `cat $DNSIP`" > /etc/resolv.conf
fi

if [ -x /usr/sbin/pppd ]; then
PATH1="/usr/sbin/pppd"
else
PATH1="pppd"
fi

if [ -x /usr/sbin/chat ]; then
PATH2="/usr/sbin/chat"
else
PATH2="chat"
fi

echo "ABORT ERROR" > /etc/ppp/ppp.chatscript 
echo "ABORT BUSY" >> /etc/ppp/ppp.chatscript 
echo 'ABORT "NO CARRIER"' >> /etc/ppp/ppp.chatscript
echo 'ABORT "NO DIALTONE"' >> /etc/ppp/ppp.chatscript
echo "REPORT CONNECT" >> /etc/ppp/ppp.chatscript
if [ "$INIT" = "below" ]; then
echo '"" "AT\&FW2H0"' >> /etc/ppp/ppp.chatscript
echo "TIMEOUT 5" >> /etc/ppp/ppp.chatscript
echo 'OK "'ATD$PHONENUM'"' >> /etc/ppp/ppp.chatscript
else
echo '"" '$INIT'' >> /etc/ppp/ppp.chatscript
echo '"'ATD$PHONENUM'"' >> /etc/ppp/ppp.chatscript
fi
echo "TIMEOUT 60" >> /etc/ppp/ppp.chatscript
echo 'CONNECT '$END'' >> /etc/ppp/ppp.chatscript

chown root.root /etc/ppp/ppp.chatscript 2>/dev/null
chmod 600 /etc/ppp/ppp.chatscript 2>/dev/null

if [ -s $TMP/sayTEMP1 ]; then
cat $TMP/sayTEMP1 >> /etc/ppp/ppp.chatscript
echo >> /etc/ppp/ppp.chatscript 
else
echo >> /etc/ppp/ppp.chatscript 
fi

echo "#!/bin/sh" > /etc/ppp/ip-up
echo "#" >> /etc/ppp/ip-up
echo "# This file was created when you ran pppsetup: `date '+%a %b %e %Y' 2>/dev/null`" >> /etc/ppp/ip-up
echo "#" >> /etc/ppp/ip-up
echo "# This file /etc/ppp/ip-up is run by pppd when there's a" >> /etc/ppp/ip-up
echo "# successful ppp connection." >> /etc/ppp/ip-up
echo "#" >> /etc/ppp/ip-up
echo "# Put any commands you want run after a successful connection" >> /etc/ppp/ip-up
echo "# in this file." >> /etc/ppp/ip-up
echo "#" >> /etc/ppp/ip-up
echo "# The companion file is /etc/ppp/ip-down, it's run when the PPP" >> /etc/ppp/ip-up
echo "# connection ends." >> /etc/ppp/ip-up
echo "#" >> /etc/ppp/ip-up
echo "# The environment is cleared before executing this script" >> /etc/ppp/ip-up
echo "# so the path must be reset." >> /etc/ppp/ip-up
echo "#" >> /etc/ppp/ip-up
echo "PATH=/usr/bin:/usr/sbin:/usr/X11R6/bin:/sbin:/bin" >> /etc/ppp/ip-up
echo "export PATH" >> /etc/ppp/ip-up

echo >> /etc/ppp/ip-up
echo "# This will print to the screen the local & remote IP address when you" >> /etc/ppp/ip-up
echo '# make a successful ppp connection.  $4 = Local IP $5 = Remote IP' >> /etc/ppp/ip-up
echo "#" >> /etc/ppp/ip-up
echo "# The CARRIER speed at which you connected will be reported, if it's in" >> /etc/ppp/ip-up
echo '# the /etc/ppp/report-chat file.  You also need the programs "grep"' >> /etc/ppp/ip-up
echo '# and "cut" for this to work.' >> /etc/ppp/ip-up
echo "# You may have to add W2 or S95=1 to your modem init string to" >> /etc/ppp/ip-up
echo "# get your modem to report the DCE = CARRIER speed." >> /etc/ppp/ip-up
echo "# Example: AT&FW2 or AT&FS95=1" >> /etc/ppp/ip-up

echo >> /etc/ppp/ip-up
echo 'S="`grep CONNECT /etc/ppp/report-chat 2>/dev/null | cut -b24-40 2>/dev/null`"' >> /etc/ppp/ip-up
echo >> /etc/ppp/ip-up
echo 'if (echo "$S $4 > $5 $1" | /usr/X11R6/bin/xmessage -display $HOSTNAME:0.0 -file - 2>/dev/null); then' >> /etc/ppp/ip-up
echo ":" >> /etc/ppp/ip-up
echo "else" >> /etc/ppp/ip-up
echo 'echo "$S $4 > $5 $1" >/dev/tty0' >> /etc/ppp/ip-up
echo "fi" >> /etc/ppp/ip-up

echo >> /etc/ppp/ip-up
echo "# If you want to ping the other end to keep the connection open." >> /etc/ppp/ip-up
echo "# The output from ping will goto >/dev/null, you won't see it." >> /etc/ppp/ip-up
echo '# Ping -i 60 = send ping every 60 seconds to remote = $5.' >> /etc/ppp/ip-up

echo >> /etc/ppp/ip-up
echo '#(ping -i 60 $5 &) >/dev/null 2>&1' >> /etc/ppp/ip-up

echo >> /etc/ppp/ip-up
echo "# If you want sendmail to send any mail in /var/spool/mqueue when" >> /etc/ppp/ip-up
echo "# you connect, remove the # below." >> /etc/ppp/ip-up

echo >> /etc/ppp/ip-up
echo "#sendmail -q" >> /etc/ppp/ip-up

echo >> /etc/ppp/ip-up
echo "# If you want fetchmail to get your mail when you connect and check" >> /etc/ppp/ip-up
echo "# every 300 seconds = 5 minutes for mail, remove the # below." >> /etc/ppp/ip-up

echo >> /etc/ppp/ip-up
echo "#fetchmail -d 300" >> /etc/ppp/ip-up

echo >> /etc/ppp/ip-up
echo "# End..." >> /etc/ppp/ip-up

chown root.root /etc/ppp/ip-up 2>/dev/null
chmod 4755 /etc/ppp/ip-up 2>/dev/null

ipdown() {
cat <<EOF
#!/bin/sh
#
# This file was created when you ran pppsetup: `date '+%a %b %e %Y' 2>/dev/null`
#
# This script is run by pppd when the PPP connection is ended.
#
# The companion file is /etc/ppp/ip-up, it's run when the PPP
# connection is started.
#
# The environment is cleared before executing this script
# so the path must be reset.
#
PATH=/usr/bin:/usr/sbin:/usr/X11R6/bin:/sbin:/bin
export PATH

# Stop ping if you started it in /etc/ppp/ip-up.

#killall -9 ping 2>/dev/null

# Stop the fetchmail daemon if you started it in /etc/ppp/ip-up
# with 'fetchmail -d 300'.

#fetchmail -q

# End...
EOF
}

ipdown > /etc/ppp/ip-down
chown root.root /etc/ppp/ip-down 2>/dev/null
chmod 4755 /etc/ppp/ip-down 2>/dev/null

echo "#!/bin/sh" > /usr/sbin/ppp-go
echo "#" >> /usr/sbin/ppp-go
echo 'if [ "$1" = "" ]; then' >> /usr/sbin/ppp-go
echo "killall -9 pppd 2>/dev/null" >> /usr/sbin/ppp-go
echo "rm -f /var/lock/LCK..* /var/run/ppp*.pid" >> /usr/sbin/ppp-go
echo "rm -f /etc/ppp/report-chat" >> /usr/sbin/ppp-go
echo '('$PATH1' connect "'$PATH2' -v -r /etc/ppp/report-chat -f /etc/ppp/ppp.chatscript") || exit 1' >> /usr/sbin/ppp-go
echo 'echo "Dialing... Trying to make a PPP connection..."' >> /usr/sbin/ppp-go
echo "exit 0" >> /usr/sbin/ppp-go
echo 'elif [ "$1" = "-l" ]; then' >> /usr/sbin/ppp-go
echo "clear 2>/dev/null" >> /usr/sbin/ppp-go
echo "tail -n 20 /var/log/messages | cut -d' ' -f6-" >> /usr/sbin/ppp-go
echo "exit 0" >> /usr/sbin/ppp-go
echo "fi" >> /usr/sbin/ppp-go

echo >> /usr/sbin/ppp-go
echo 'echo "USAGE: ppp-go      <Make PPP connection>"' >> /usr/sbin/ppp-go
echo 'echo "USAGE: ppp-go -l   <Show last 20 messages from /var/log/messages,"' >> /usr/sbin/ppp-go
echo 'echo "                    only works if syslogd is running>"' >> /usr/sbin/ppp-go
echo 'echo "                   <If you do not connect with ppp-go, you can"' >> /usr/sbin/ppp-go
echo 'echo "                    look at the messages for the problem>"' >> /usr/sbin/ppp-go

chown root.root /usr/sbin/ppp-go 2>/dev/null
chmod 4755 /usr/sbin/ppp-go 2>/dev/null 
	
echo "lock" > /etc/ppp/options
echo "defaultroute" >> /etc/ppp/options
echo "noipdefault" >> /etc/ppp/options
echo "modem" >> /etc/ppp/options
echo "/dev/$MODEM" >> /etc/ppp/options
echo "$BAUDRATE" >> /etc/ppp/options
echo "crtscts" >> /etc/ppp/options
echo "debug" >> /etc/ppp/options
echo "passive" >> /etc/ppp/options
echo "asyncmap 0" >> /etc/ppp/options
  
if [ "$PAP" = "PAP/CHAP" ]; then        
echo 'user "'$AUTH1'"' >> /etc/ppp/options
echo "remotename *" >> /etc/ppp/options
fi

chown root.root /etc/ppp/options 2>/dev/null
chmod 600 /etc/ppp/options 2>/dev/null

if [ "$PAP" = "PAP/CHAP" ]; then
rm -f /etc/ppp/pap-secrets
echo "# PAP/CHAP authentication file." > /etc/ppp/pap-secrets
echo "# This file should have a permission of 600." >> /etc/ppp/pap-secrets
echo "# chmod 600 /etc/ppp/pap-secrets" >> /etc/ppp/pap-secrets
echo "# Username      Server      Password      IP addresses" >> /etc/ppp/pap-secrets
echo '"'$AUTH1'"   *   "'$AUTH2'"   *' >> /etc/ppp/pap-secrets
echo '*   "'$AUTH1'"   "'$AUTH2'"   *' >> /etc/ppp/pap-secrets

chown root.root /etc/ppp/pap-secrets 2>/dev/null
chmod 600 /etc/ppp/pap-secrets 2>/dev/null
cd /etc/ppp && ln -sf pap-secrets chap-secrets 2>/dev/null 
fi

if [ -f $HOME/.ppprc ]; then
mv $HOME/.ppprc $HOME/.ppprc.off
PPPRC="YES"
fi

echo "=========================================================================" > /etc/ppp/pppsetup.txt
cat $TMP/txtTEMP01 >> /etc/ppp/pppsetup.txt

echo "=========================================================================" > $TMP/txtTEMP$$
echo "These are your PPP configuration files and instructions." >> $TMP/txtTEMP$$
echo "=========================================================================" >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$

echo "If there's a problem check the files below to see if they have" >> $TMP/txtTEMP$$
echo "the correct phone number, username, password, IP address, etc." >> $TMP/txtTEMP$$

echo >> $TMP/txtTEMP$$
echo "/etc/ppp/ppp.chatscript" >> $TMP/txtTEMP$$

echo >> $TMP/txtTEMP$$
echo "/etc/ppp/options file" >> $TMP/txtTEMP$$

if [ "$MODERROR" = "YES" ]; then
echo >> $TMP/txtTEMP$$
echo "WARNING! I could not find the modem device: '/dev/$MODEM'" >> $TMP/txtTEMP$$
echo "ttyS0 = com1 ttyS1 = com2 ttyS2 = com3  ttyS3 = com4" >> $TMP/txtTEMP$$
echo "# /dev/MAKEDEV $MODEM" >> $TMP/txtTEMP$$
fi

echo >> $TMP/txtTEMP$$
echo "/etc/resolv.conf file" >> $TMP/txtTEMP$$

if [ "$PAP" = "PAP/CHAP" ]; then
echo >> $TMP/txtTEMP$$
echo "/etc/ppp/pap-secrets file" >> $TMP/txtTEMP$$
fi

if [ "$PPPRC" = "YES" ]; then
echo >> $TMP/txtTEMP$$
echo "ATTENTION! I found a $HOME/.ppprc file and moved it to" >> $TMP/txtTEMP$$
echo ".ppprc.off, it might interfere with the PPP connection." >> $TMP/txtTEMP$$
fi

echo >> $TMP/txtTEMP$$
echo "If something in the above files is not correct you can change it by" >> $TMP/txtTEMP$$
echo "hand, or run 'pppsetup' again." >> $TMP/txtTEMP$$

echo >> $TMP/txtTEMP$$
echo "=========================================================================" >> $TMP/txtTEMP$$
echo "To connect to your service provider." >> $TMP/txtTEMP$$
echo "=========================================================================" >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "# ppp-go      <Make PPP connection>" >> $TMP/txtTEMP$$
echo "# ppp-go -l   <Show last 20 messages from /var/log/messages," >> $TMP/txtTEMP$$
echo "               only works if syslogd is running>" >> $TMP/txtTEMP$$
echo "              <If you don't connect with ppp-go, you can" >> $TMP/txtTEMP$$
echo "               look at the messages for the problem>" >> $TMP/txtTEMP$$

echo >> $TMP/txtTEMP$$
echo "When you do 'ppp-go' you'll be droped back to #, you'll hear the" >> $TMP/txtTEMP$$
echo "modem dialing, in about 45 seconds you should see a Local and" >> $TMP/txtTEMP$$
echo "Remote IP address printed to the screen, like this below." >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "205.115.312.56 > 216.324.119.81 ppp0" >> $TMP/txtTEMP$$

echo >> $TMP/txtTEMP$$
echo "If you don't make the ppp connection, take a look at the" >> $TMP/txtTEMP$$ 
echo "/var/log/messages and debug files, they should have a" >> $TMP/txtTEMP$$ 
echo "record of the attempted ppp connection." >> $TMP/txtTEMP$$

echo >> $TMP/txtTEMP$$
echo "Sample /var/log/messages file." >> $TMP/txtTEMP$$    
echo >> $TMP/txtTEMP$$
echo "`hostname 2>/dev/null` pppd[562]: Serial connection established." >> $TMP/txtTEMP$$    
echo "`hostname 2>/dev/null` pppd[562]: Using interface ppp0" >> $TMP/txtTEMP$$              
echo "`hostname 2>/dev/null` pppd[562]: Connect: ppp0 <--> /dev/$MODEM" >> $TMP/txtTEMP$$     
echo "`hostname 2>/dev/null` pppd[562]: local IP address 215.87.78.18" >> $TMP/txtTEMP$$      
echo "`hostname 2>/dev/null` pppd[562]: remote IP address 205.94.97.35" >> $TMP/txtTEMP$$     

echo >> $TMP/txtTEMP$$
echo "You don't have a successful PPP connection until you" >> $TMP/txtTEMP$$ 
echo "receive a Local & Remote IP address like above." >> $TMP/txtTEMP$$

echo >> $TMP/txtTEMP$$
echo "If you have the x window system, you could connect in a xterm." >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "`hostname 2>/dev/null` # startx" >> $TMP/txtTEMP$$
echo "`hostname 2>/dev/null` # ppp-go" >> $TMP/txtTEMP$$
echo "`hostname 2>/dev/null` # netscape mosaic etc." >> $TMP/txtTEMP$$

echo >> $TMP/txtTEMP$$
echo "If you run ppp-go in x windows you'll see the Local & Remote IP" >> $TMP/txtTEMP$$
echo "address printed in a little xmessage box, if you have /usr/X11R6/" >> $TMP/txtTEMP$$
echo "bin/xmessage." >> $TMP/txtTEMP$$

echo >> $TMP/txtTEMP$$
echo "You can run 'ifconfig' anytime to see if you're PPP connected." >> $TMP/txtTEMP$$
echo "# ifconfig" >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "You should see something like this if you're PPP connected." >> $TMP/txtTEMP$$
echo "ppp0      Link encap:Point-to-Point Protocol" >> $TMP/txtTEMP$$
echo "          inet addr:105.207.127.86  P-t-P:205.252.116.61  Mask:255.0.0.0" >> $TMP/txtTEMP$$
 
echo >> $TMP/txtTEMP$$
echo "# ppp-off   <To end the ppp connection>" >> $TMP/txtTEMP$$

echo >> $TMP/txtTEMP$$
echo 'There is no support in linux for "winmodems", if you have one' >> $TMP/txtTEMP$$
echo "you will not be able to use it in linux." >> $TMP/txtTEMP$$ 
echo >> $TMP/txtTEMP$$
echo "There is support for plug n play modems, if you have a pnp" >> $TMP/txtTEMP$$
echo 'modem you may need "isapnptools" to get it recognized.' >> $TMP/txtTEMP$$

echo >> $TMP/txtTEMP$$
echo "=========================================================================" >> $TMP/txtTEMP$$
if [ -s /usr/doc/pppsetup/pppsetup-$VERSION.README ]; then
echo "#### Look at: /usr/doc/pppsetup/pppsetup-$VERSION.README. ####" >> $TMP/txtTEMP$$
fi
echo "#### A copy of this text can be found in: /etc/ppp/pppsetup.txt ####" >> $TMP/txtTEMP$$
echo "=========================================================================" >> $TMP/txtTEMP$$

echo >> $TMP/txtTEMP$$
echo "Done ... You can exit now." >> $TMP/txtTEMP$$
echo >> $TMP/txtTEMP$$
echo "End ..." >> $TMP/txtTEMP$$
echo "=========================================================================" >> $TMP/txtTEMP$$

dialog --title "DONE" --clear --textbox "$TMP/txtTEMP$$" 22 78	
	  	  
clear 2>/dev/null || echo

cat $TMP/txtTEMP$$ >> /etc/ppp/pppsetup.txt
chmod 644 /etc/ppp/pppsetup.txt 2>/dev/null 

rm -rf $TMP 2>/dev/null

echo "#### A copy of this text can be found in: /etc/ppp/pppsetup.txt ####" 
if [ -s /usr/doc/pppsetup/pppsetup-$VERSION.README ]; then
echo "#### Look at: /usr/doc/pppsetup/pppsetup-$VERSION.README. ####" 
fi
echo
exit 

