#!/bin/sh

MSG() {
cat <<EOF

# ezsetup
#
# Setup PPP connection with eznet: (2-24-98) Kent Robotti
#
# This script will ask you a few questions to setup a PPP connection
# to your service provider using 'eznet'.
#
# ezsetup -s   <Setup eznet>
# ezsetup -h   <This message>

EOF
}

if [ "$1" = "" -o "$1" = "-h" ]; then
MSG
exit
fi

if [ ! "$UID" = "0" ]; then
echo "You need to be 'root' to run this script."
exit
fi

if [ "$1" = "-s" ]; then

if [ ! -x /usr/bin/eznet ]; then
echo "You don't have '/usr/bin/eznet' installed, i can't continue."
exit
fi

if [ -s /etc/ppp/eznet/eznet.conf ]; then
echo
echo "You have a /etc/ppp/eznet/eznet.conf file, do you"
echo "want to remove it or add a new entry to it?"
echo -n "Should i remove it? (N/y) : "
read ans
if [ "$ans" = "y" -o "$ans" = "Y" ]; then
rm -f /etc/ppp/eznet/* 2>/dev/null
else
ADD="YES"
fi
fi

echo
echo "Example: 1234567"
echo -n "What is your service providers Phone Number? : "
read PN

if [ "$PN" = "" ]; then
echo "Cancelled."
exit
fi

echo
echo "Examples: something.com something.net something.edu"
echo -n "What is your service providers Domain Name? : "
read DN

if [ "$DN" = "" ]; then
echo "Cancelled."
exit
fi

echo
echo "What is the IP address of your service providers nameserver?"
echo "If you have a IP address in /etc/resolv.conf press [Enter] and skip this."
echo "Example: 205.252.116.61"
echo -n "IP address? : "
read IP

echo
echo "Example: jerry"
echo -n "What is your Username? : "
read UN

if [ "$UN" = "" ]; then
echo "Cancelled."
exit
fi

echo
echo "Example: Xpo4Gkh9"
echo -n "What is your Password? : "
read PW

if [ "$PW" = "" ]; then
echo "Cancelled."
exit
fi

echo
echo "Examples: ttyS0 = com1 ttyS1 = com2 ttyS2 = com3 ttyS3 = com4"
echo "Don't include the /dev/ part in your answer."
echo -n "Where is your modem?  ttyS? : "
read MOD

if [ "$MOD" = "" ]; then
echo "Cancelled."
exit
fi

echo
echo "You probably have PPP 2.2.something or the more recent 2.3.something." 
echo "If not sure or NO press [Enter] otherwise put: y"
echo -n "Do you have version 2.2 of PPP? (N/y) : "
read VER

if [ "$VER" = "y" -o "$VER" = "Y" ]; then
ver=2.2
fi

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

if [ -f /etc/ppp/pap-secrets -o -f /etc/ppp/chap-secrets ]; then
rm -f /etc/ppp/pap-secrets
rm -f /etc/ppp/chap-secrets
fi

if [ -f /etc/ppp/ip-up -o -f /etc/ppp/ip-down ]; then
rm -f /etc/ppp/ip-up
rm -f /etc/ppp/ip-down
fi

if [ -s /etc/ppp/options ]; then
mv /etc/ppp/options /etc/ppp/options.off
fi

if [ ! "$IP" = "" ]; then
echo "nameserver $IP" > /etc/resolv.conf
fi

if [ -c "/dev/$MOD" ]; then
ln -sf /dev/$MOD /dev/modem 2>/dev/null
else
DEV="NO"
fi

if ! type -all pidof >/dev/null 2>&1 ; then
if [ -x /sbin/killall5 ]; then
ln -sf /sbin/killall5 /sbin/pidof 2>/dev/null
elif [ -x /bin/killall5 ]; then
ln -sf /bin/killall5 /bin/pidof 2>/dev/null
elif [ -x /usr/sbin/killall5 ]; then
ln -sf /usr/sbin/killall5 /usr/sbin/pidof 2>/dev/null
elif [ -x /usr/bin/killall5 ]; then
ln -sf /usr/bin/killall5 /usr/bin/pidof 2>/dev/null
elif [ -x /sbin/killall ]; then
ln -sf /sbin/killall /sbin/pidof 2>/dev/null
elif [ -x /bin/killall ]; then
ln -sf /bin/killall /bin/pidof 2>/dev/null
elif [ -x /usr/sbin/killall ]; then
ln -sf /usr/sbin/killall /usr/sbin/pidof 2>/dev/null
elif [ -x /usr/bin/killall ]; then
ln -sf /usr/bin/killall /usr/bin/pidof 2>/dev/null
fi
fi

chown root.root /usr/bin/eznet 2>/dev/null
chmod 4755 /usr/bin/eznet 2>/dev/null

/usr/bin/eznet add service=$DN phone=$PN user=$UN password=$PW 

if [ "$ver" = "2.2" ]; then
/usr/bin/eznet change $DN pppversion=2.2
fi

if [ "$ADD" = "YES" ]; then
N="`tail -n 1 /etc/ppp/eznet/eznet.conf | cut -b-1 2>/dev/null`" || N="number?"
OR="or  eznet up $DN"
fi

echo
echo "Done ..."
echo "To make the PPP connection do this."
echo "eznet up $N  $OR"
echo "To end the PPP connection do this."
echo "eznet down"
echo "Your config file is in /etc/ppp/eznet/eznet.conf."
if [ "$DEV" = "NO" ]; then
echo
echo "WARNING: The modem device you chose '/dev/$MOD' doesn't exist."
echo "Create '/dev/MAKEDEV $MOD' and link 'ln -sf /dev/$MOD /dev/modem'."
echo
fi
echo "Done ..."
else
MSG
fi

