#!/bin/bash
# Installation script for DIP/PPPD combination of startppp

clear
echo "Installation for startppp script.  This script will ask questions"
echo "relating to your system and ISP."
echo 
echo "You will also need to have the pppd and dip packages already"
echo "installed on your system"
echo
echo
echo "Continue (Y/n)"
read CONT
if [ "$CONT" = "n" ]
  then exit
elif [ "$CONT" = "N" ]
  then exit
fi
if [ "$UID" != "0" ]
  then
  echo 
  echo "NOTICE: You need to be root to run this script!!"
  exit
fi
echo

###########################################################
### User Info Gathering
###########################################################

GATEIP=""
while [ "$GATEIP" = "" ]
  do
  echo "What is your gateway's IP address:"
  read GATEIP
  done
MODEM_PATH=""
while [ "$MODEM_PATH" = "" ]
  do
  echo "What is the full path to your modem:"
  read MODEM_PATH
  if [ ! -a "$MODEM_PATH" ]
    then MODEM_PATH=""
    echo "$MODEM_PATH  file not found"
  fi
  done
ISPLOGIN=""
while [ "$ISPLOGIN" = "" ]
  do
  echo "What is your PPP login name on with your ISP:"
  read ISPLOGIN
  done
ISPPASS=""
while [ "$ISPPASS" = "" ]
  do
  echo "What is the password for $ISPLOGIN:"
  read ISPPASS
  done
ISPPHONE=""
while [ "$ISPPHONE" = "" ]
  do
  echo "What is the phone number for your ISP:"
  read ISPPHONE
  done
PPPNAME="startppp"
  echo "What would you like to have your startup script called?"
  echo "The default is 'startppp' just hit <return> for that:"
  read REPLY
  if [ "$REPLY" != "" ]
  then 
     PPPNAME=$REPLY
  fi
MODEM_INIT="" 
while [ "$MODEM_INIT" = "" ]
  do
  echo "What is your modem init string (default is:  AT&F):"
  read MODEM_INIT
  done 

echo
echo
echo "  Gateway:         $GATEIP"
echo "  Modem path:      $MODEM_PATH"
#echo "  Mail daemon:     $MAIL_D"
echo "  Login:           $ISPLOGIN"
echo "  Password:        $ISPPASS"
echo "  Phone number:    $ISPPHONE"
echo "  PPP startup:	 $PPPNAME"
echo "  Modem string:    $MODEM_INIT"
echo
echo "Does this look correct (Y/n)"
read CONT
if [ "$CONT" = "n" ]
  then exec ./pppconfig
elif [ "$CONT" = "N" ]
  then exec ./pppconfig

fi

###########################################################
### Getting System Information
###########################################################

echo "Getting info for making startppp script...."
DIP=`which bmdial | gawk '{print $1}'`
if [ "$DIP" = "which:" ]
  then
  DIP=""
  while [ "$DIP" = "" ]
    do
    echo "The program 'bmdial' can't be found please enter the FULL path:"
    read DIP
    if [ ! -a $DIP ]
      then
      DIP=""
    fi
    done
fi
PPPD=`which pppd | gawk '{print $1}'`
if [ ! -a $PPPD ]
  then 
  PPPD=""
  while [ "$PPPD" = "" ]
    do
    echo "The PPP daemon can't be found please enter the FULL path:"
    read PPPD
    if [ ! -a $PPPD ]
      then
      PPPD=""
    fi
    done
fi
THREEIP=`echo $GATEIP | gawk -F . '{printf "%s.%s.%s",$1,$2,$3}'`
if [ -f /etc/HOSTNAME ]
then
  HOSTNM=/etc/HOSTNAME
elif [ -f /etc/hostname ]
then
  HOSTNM=/etc/hostname
fi
HOSTNAME=`cat $HOSTNM`
MACHINENAME=`hostname`
if [ -f /etc/DOMAINNAME ]
then
  DOMAIN=`cat /etc/DOMAINNAME`
else
  DOMAIN=`cat /etc/reslov.conf | grep domain | gawk '{proint $2}'`
  echo $DOMAIN > /etc/DOMAINNAME
fi

SETSERIAL=`which setserial`
SENDMAIL=`which sendmail`
MAIL=`which mail`
GREP=`which grep`
KILL=`which kill`
AWK=`which awk`
PS=`which ps`
PING=`which ping`
CAT=`which cat`
TOUCH=`which touch`
RM=`which rm`
CP=`which cp`
BINHOSTNAME=`which hostname`
NSLOOKUP=`which nslookup`
IFCONFIG=`which ifconfig`

if [ ! -a "$MAIL" ]
then
   echo "ERROR: mail was not found, please make sure it is in your path"
   exit
fi
if [ ! -a "$SENDMAIL" ]
then
   echo "ERROR: sendmail was not found, please make sure it is in your path"
   exit
fi
if [ ! -a "$TOUCH" ]
then
   echo "ERROR: touch was not found, please make sure it is in your path"
   exit
fi
if [ ! -a "$CAT" ]
then
   echo "ERROR: cat was not found, please make sure it is in your path"
   exit
fi
if [ ! -a "$CP" ]
then
   echo "ERROR: cp was not found, please make sure it is in your path"
   exit
fi
if [ ! -a "$RM" ]
then
   echo "ERROR: rm was not found, please make sure it is in your path"
   exit
fi
if [ ! -a "$SETSERIAL" ]
then
   echo "ERROR: setserial was not found, please make sure it is in your path"
   exit
fi
if [ ! -a "$GREP" ]
then
   echo "ERROR: grep was not found, please make sure it is in your path"
   exit
fi
if [ ! -a "$KILL" ]
then
   echo "ERROR: kill was not found, please make sure it is in your path"
   exit
fi
if [ ! -a "$AWK" ]
then
   echo "ERROR: awk was not found, please make sure it is in your path"
   exit
fi
if [ ! -a "$PS" ]
then
   echo "ERROR: ps was not found, please make sure it is in your path"
   exit
fi
if [ ! -a "$PING" ]
then
   echo "ERROR: ping was not found, please make sure it is in your path"
   exit
fi
if [ ! -a "$BINHOSTNAME" ]
then
   echo "ERROR: hostname  was not found, please make sure it is in your path"
   exit
fi
if [ ! -a "$NSLOOKUP" ]
then
   echo "ERROR: nslookup was not found, please make sure it is in your path"
   exit
fi

echo "Creating startppp script...."
cat startppp.in | sed s-'%SETSERIAL%'-"$SETSERIAL"-g | \
 sed s-'%PORT%'-"$MODEM_PATH"-g | sed s-'%DIP%'-"$DIP"-g | \
 sed s-'%PPPD%'-"$PPPD"-g | sed s/'%THREE%'/"$THREEIP"/g | \
 sed s-'%HOSTFILE%'-"$HOSTNM"-g | sed s-'%CP%'-"$CP"-g | \
 sed s-'%CAT%'-"$CAT"-g | sed s-'%HOSTNAME%'-"$BINHOSTNAME"-g | \
 sed s-'%IFCONFIG%'-"$IFCONFIG"-g | sed s-'%GREP%'-"$GREP"-g | \
 sed s-'%GAWK%'-"$AWK"-g | sed s-'%NSLOOKUP%'-"$NSLOOKUP"-g | \
 sed s-'%KILL%'-"$KILL"-g | sed s-'%SENDMAIL%'-"$SENDMAIL"-g |\
 sed s-'%MAIL%'-"$MAIL"-g  > startppp.out

cat killppp.in | sed s-'%DIP%'-"$DIP"-g | sed s-'%GREP%'-"$GREP"-g |\
 sed s-'%PS%'-"$PS"-g | sed s-'%GAWK%'-"$AWK"-g | 
 sed s-'%KILL%'-"$KILL"-g  > killppp.out

cat checkppp.in | sed s-'%GREP%'-"$GREP"-g | \
 sed s-'%KILL%'-"$KILL"-g | sed s-'%CAT%'-"$CAT"-g | \
 sed s-'%STARTPPP%'-"$PPPNAME"-g | sed s-'%PING%'-"$PING"-g | \
 sed s-'%GATEWAY%'-"$GATEIP"-g | sed s-'%DIP%'-"$DIP"-g | \
 sed s-'%RM%'-"$RM"-g | sed s-'%IFCONFIG%'-"$IFCONFIG"-g | \
 sed s-'%TOUCH%'-"$TOUCH"-g > checkppp.out
 
cat bmdial.ppp |  sed  s/GATEWAY_ADDRESS/"$GATEIP"/g | \
 sed s-PORT-"$MODEM_PATH"-g | sed s/PHONE_NUMBER/"$ISPPHONE"/g | \
 sed s/LOGIN/"$ISPLOGIN"/g | sed s/PASS/"$ISPPASS"/g | \
 sed s-MODEMINIT-"$MODEM_INIT"-g > ppp.out  

cat config_host.in | sed s/'%THREE%'/"$THREEIP"/g  \
  > config_host.out

echo
echo
echo "Creating a proper /etc/hosts file setup needed for startppp"
echo "If you have modified /etc/hosts any at all you will need to"
echo "read the information again. Your current /etc/hosts will "
echo "be saved to /etc/hosts.old. You will need to add this info "
echo "to /etc/hosts.bak"
mv /etc/hosts /etc/hosts.old
echo "127.0.0.1		localhost" > /etc/hosts
if [ ! -f /etc/hosts.bak ]
then 
   cp /etc/hosts /etc/hosts.bak
fi
echo "127.0.0.1		$HOSTNAME $MACHINENAME" >> /etc/hosts
cp /etc/hosts /etc/hosts.pre

chmod 0644 /etc/hosts
chmod 0644 /etc/hosts.pre
chmod 0644 /etc/hosts.bak

echo
echo
echo "Installing scripts now...."
echo

echo "Adding /usr/sbin/killppp"
cp killppp.out /usr/sbin/killppp
chmod 0700 /usr/sbin/killppp
chown root.root /usr/sbin/killppp

echo "Adding /usr/sbin/checkppp"
cp checkppp.out /usr/sbin/checkppp
chown root.root /usr/sbin/checkppp
chmod 0700 /usr/sbin/checkppp

echo "Adding /usr/sbin/$PPPNAME"
cp startppp.out /usr/sbin/$PPPNAME
chown root.root /usr/sbin/$PPPNAME
chmod 0700 /usr/sbin/$PPPNAME

echo "Adding /root/.ppp.bmdial"
cp ppp.out /root/.ppp.bmdial
chown root.root /root/.ppp.bmdial
chmod 0600 /root/.ppp.bmdial

echo "Adding /etc/ppp/options"
if [ ! -d /etc/ppp ]
  then
  mkdir /etc/ppp
fi
cp options /etc/ppp
chown root.root /etc/ppp/options
chmod 0644 /etc/ppp/options

echo "Adding /usr/sbin/config_host"
cp config_host.out /usr/sbin/config_host
chown root.root /usr/sbin/config_host
chmod 0700 /usr/sbin/config_host

rm startppp.out
rm ppp.out
rm checkppp.out
rm killppp.out
rm config_host.out

echo "Installation DONE!!"
echo 
echo "***NOTE:  If your PPP account doesn't automatically start PPP"
echo "     right away (ie after you give your password you must type"
echo "     'ppp' or something) edit the /root/.ppp.bmdial file.  There"
echo "     are comments in there that tell you what to do."
