#!/bin/sh
# <BEGIN COPYRIGHT>
# This file is part of the "Bad Penguin" GNU/Linux distribution
# Copyright (C) 1997-98-99 by Antonio GALLO
# Home Page at ......: http://www.badpenguin.org  http://www.linux.it/~agx/
# e-mail contact ....: mailto:agx@linux.it	mailto:agx@geocities.com
# This program is distributed under the GNU General Public License
# You are not allowed to remove the copyright notice
# <END COPYRIGHT>

# BAD PENGUIN" - Internet Connection Utility by AGX
# 1999-02-23, Ver. 1.0.0 by agx@linux.it
# 2000-02-20, Ver. 1.1.0 by Stefano Falsetto: menu of configured connections
# 2000-02-20, Ver. 1.1.1 by Stefano Falsetto: option to use peer DNS



# Controlla se almeno una connessione e' stata creata
TROVATO=0
for i in /etc/sysconfig/*.internet ; do
  if [ -f $i ]; then
    TROVATO=1
    break
  fi
done
if [ $TROVATO -eq 0 ]; then
  color YELLOW
  echo "Sorry, you did not configured any ISP"
  echo "Please run the 'controlpanel' and under the system configuration menu"
  echo "  choose to configure your Internet connections."
  color WHITE
  USCITA=5 ; exit 5
fi



# Temp dir 
TEMPDIR="$HOME/tmp"
[ ! -d $TEMPDIR ] && mkdir -p $TEMPDIR
TEMPSCRIPT=$TEMPDIR/connect-tempscript.$$
TEMPDATA=$TEMPDIR/connect-tempdata.$$
trap 'rm -f $TEMPSCRIPT $TEMPDATA ; exit $USCITA' 0
USCITA=0



# Display menu of configured isp
connection_menu() {
  # List Internet Connection
  > $TEMPSCRIPT
  for i in /etc/sysconfig/*internet; do
    REALNAME="`basename $i .internet`"
    if [ -f /etc/sysconfig/$REALNAME.internet ]; then
      echo -n "$REALNAME::" >> $TEMPSCRIPT
    fi
  done

  # Ask what to do
  IFS=":"  
  dialog --title "Internet Services Providers connections" --menu "These are your current internet connections.\n" 20 70 12 `cat $TEMPSCRIPT` 2>$TEMPDATA
  REPLY=$?
  if [ $REPLY -ne 0 ] ; then
    echoc RED "Nessuna connessione scelta"
    USCITA=$REPLY ; exit $REPLY
  fi
  ISP="`cat $TEMPDATA`"
}



# Check command line
if [ "$1" = "" -o "$1" = "--menu" ]; then
  echo "Scelta connessione ..."
  connection_menu
fi



# Check command line
case $2 in
  DETAILS|details)
    DETACH=""
    NOWAIT=2
    ;;
  NOWAIT|nowait|DETACH|detach)
    NOWAIT=1
    DETACH=""
    ;;
  *)
    NOWAIT=0
    DETACH="-detach"
    ;;
esac

# Get the Parameters
if [ -f /etc/sysconfig/$PROFILE/modem ]; then
  . /etc/sysconfig/$PROFILE/modem
else
  color YELLOW
  echo "Sorry, you did not configured the modem for your profile"
  echo "Your current profile is $PROFILE"
  echo "Please run the 'controlpanel' and under the hardware menu"
  echo "  choose to configure your modem."
  color WHITE
  USCITA=1 ; exit 1
fi

# Check if connection configuration file exists
if [ -f /etc/sysconfig/$ISP.internet ]; then
  . /etc/sysconfig/$ISP.internet
else
  echoc RED "Sorry, the connection $ISP does not exist"
  USCITA=2 ; exit 2
fi


# Check CHAT file
FILENAME="/etc/ppp/chatscripts/$ISP"
# Check if the chat file was created
if [ ! -f $FILENAME ]; then
  echoc RED "Sorry, the chat script $FILENAME does not exist"
  echoc RED "Please, re-create your internet connection"
  USCITA=2 ; exit 2
fi


# Check if modem is configured
if [ "$MODEM_DEVICE" = "NONE" ]; then
  color YELLOW
  echo "We are sorry but you told us that you didn't have any modem."
  echo "Please use the 'controlpanel' to configure it"
  color WHITE
  USCITA=3 ; exit 3
fi

# SINGLE MODEM CHECK
for i in /$USER/*.connect; do
  if [ -f $i ]; then
    CONN=`basename $i .connect`
    echoc YELLOW "The connection [ $CONN ] is already running"
    echoc YELLOW "Please use the 'disconnect' command first !"
    USCITA=4 ; exit 4
  fi
done


# ---------------------------------------------------------- TRANSLATE VARS

# Preparing Internet Connection ....
# Those depends by your connection type !

# Modem Control Flow
if [ "$MODEM_FLOW" = "HARDWARE" ]; then
  FLOW="crtscts"
elif [ "$MODEM_FLOW" = "SOFTWARE" ]; then
  FLOW="xonxoff"
else
  FLOW=""
fi

# PPP Fixed Addresses
DNS1=""
DNS2=""
WINS1=""
WINS2=""
DNS_FROM_PEER=0

if [ ! "$ISP_DNS1" = "" ]; then
  DNS1="ms-dns $ISP_DNS1"
fi
if [ ! "$ISP_DNS2" = "" ]; then
  DNS2="ms-dns $ISP_DNS2"
fi
# Use DNS from the Peer
if [ "$ISP_DNS1" = "" -a "$ISP_DNS2"="" ]; then
      DNS_FROM_PEER=1
fi
if [ ! "$ISP_WINS1" = "" ]; then
  WINS1="ms-wins $ISP_WINS1"
fi
if [ ! "$ISP_WINS2" = "" ]; then
  WINS2="ms-wins $ISP_WINS2"
fi

# ------------------------------------------------------------ DO CONNECTION

#### LINUX CONNECTION ####

# Restart the network in order to clean up /etc/resolv.conf from
# settings of another ISP connection
service network restart


NAME=""
REMOTENAME=""
if [ ! "$ISP_CONNECTION" = "LINUX" ]; then
  echo "Updating secret files"
  
  NAME="name $ISP_ACCOUNT"
  if [ "$ISP_CONNECTION" = "PAP" ]; then
    echo "$ISP_ACCOUNT	*	$ISP_PASSWORD"	> /etc/ppp/pap-secrets
  fi
  if [ "$ISP_CONNECTION" = "CHAP" -o "$ISP_CONNECTION" = "MSCHAP" ]; then
    if [ ! "$ISP_REMOTENAME" = "" ]; then
      REMOTENAME="remotename $ISP_REMOTENAME"
    fi
    echo "$ISP_ACCOUNT	$ISP_REMOTENAME	$ISP_PASSWORD"	> /etc/ppp/chap-secrets
    echo "$ISP_REMOTENAME	$ISP_ACCOUNT	$ISP_PASSWORD"	>> /etc/ppp/chap-secrets
  fi 
  chown root /etc/ppp/*ap-secrets
  chgrp root /etc/ppp/*ap-secrets    
  chmod 600  /etc/ppp/*ap-secrets
fi

# --------------------------------------------------------- Updating Network

if [ "$ISP_DNS1" != "" ]; then 
  grep "$ISP_DNS1" /etc/resolv.conf> /dev/null
  if [ $? = 1 ]; then
    echo "Adding primary DNS : nameserver $ISP_DNS1"
    echo "nameserver $ISP_DNS1" >> /etc/resolv.conf
  fi
fi

if [ "$ISP_DNS2" != "" ]; then 
  grep "$ISP_DNS2" /etc/resolv.conf > /dev/null
  if [ $? = 1 ]; then
    echo "Adding secondary DNS : nameserver $ISP_DNS2"
    echo "nameserver $ISP_DNS2" >> /etc/resolv.conf
  fi
fi

# --------------------------------------------------------- Configure PPPD
# This should be re-created every time since modem settings could change
# between different profiles, or simply you change them.

# on PAP, CHAP, MSCHAP
# pppd take the option "name <your_name_at_ISP>"
# As PAP is for authenticating computers, technically you need also to
# specify a remote computer name. However, as most people only have one
# ISP, you can use a wild card (*) for the remote host name in the
# secrets file.

PEERNAME="/etc/ppp/peers/$ISP"
echo "$MODEM_DEVICE"	>  $PEERNAME
echo "$MODEM_SPEED"	>> $PEERNAME
echo noipdefault	>> $PEERNAME
echo $DETACH		>> $PEERNAME
echo $NAME		>> $PEERNAME
echo $REMOTENAME	>> $PEERNAME
echo modem		>> $PEERNAME

if [ DNS_FROM_PEER -eq 0 ]; then
  echo $DNS1		>> $PEERNAME
  echo $DNS2		>> $PEERNAME
else
  echo "usepeerdns"	>> $PEERNAME
fi

[ "$WINS1" != "" ] && echo $WINS1		>> $PEERNAME
[ "$WINS2" != "" ] && echo $WINS2		>> $PEERNAME

echo lock		>> $PEERNAME
echo defaultroute	>> $PEERNAME
echo netmask 255.255.255.0 >> $PEERNAME
echo asyncmap 0		>> $PEERNAME
echo mru 552		>> $PEERNAME
echo mtu 552		>> $PEERNAME
echo noipx		>> $PEERNAME
echo proxyarp		>> $PEERNAME
echo $FLOW		>> $PEERNAME
echo lcp-echo-interval 30	>> $PEERNAME
echo ipcp-max-configure 30	>> $PEERNAME
### [ ! "$DETACH" = "" ] && ( echo disconnect \"/usr/sbin/chat -v -f /etc/ppp/hangup.chat\" >> $PEERNAME )
echo connect \"/usr/sbin/chat -v -f $FILENAME\"	>> $PEERNAME


# --------------------------------------------------------- LAUNCH PPPD
if [ "$DETACH" != "" ]; then
  echo
  echoc CYAN "Press CTRL + C to terminate the connection or run the 'disconnect' command"
fi
echo
echoc GREEN "Staring Internet connection on `date`"
> /$USER/$ISP.connect
STARTDATE=`date +%s`

pppd call $ISP
REPLY=$?

# pppd return an error
if [ $REPLY -ne 0 ]; then
	echoc RED "pppd error $REPLY !"
	disconnect
	USCITA=$REPLY ; exit $REPLY
fi

  
# Initial dialing delay
sleep 5

# Wait for the connection
NUMTRY=0
while ! ifconfig | grep ^ppp0 > /dev/null ; do
	echoc CYAN -n "."
	sleep 1
	if [ "`pidof pppd`" = "" ]; then
		echoc RED "pppd is dead !"
		disconnect
		USCITA=1 ; exit 1
	fi
	NUMTRY=$[ NUMTRY + 1 ]
	if [ $NUMTRY -gt 40 ]; then
		echoc RED "connection timeout !"
		disconnect
		USCITA=2 ; exit 2
	fi
done

# Connected !
# Compute connection speed
ISP_SPEED=`\
	grep "REL/V42BIS" /var/log/ppp.log | \
	tail -1 | \
	sed "s/^.*CARRIER \([0-9]\+\).*$/\1/"  `
echoc GREEN "connection estabilished at $ISP_SPEED bps !"
if [ $NOWAIT = 1 ]; then
	USCITA=0 ; exit 0
fi

# Display connection time
while ifconfig | grep ^ppp0 > /dev/null ; do
	clear
	NOWDATE=`date +%s`
	DIFFDATE=$[ NOWDATE - STARTDATE ]
	HDATE=$[ DIFFDATE / 3600 ]
	DIFFDATE=$[ DIFFDATE - ( HDATE * 3600 ) ]
	MDATE=$[ DIFFDATE / 60 ]
	DIFFDATE=$[ DIFFDATE - ( MDATE * 60 ) ]
	SDATE=$DIFFDATE
	echoc CYAN "Connected to $ISP from  h:$HDATE min:$MDATE sec:$SDATE"
        sleep 4
        if [ "`pidof pppd`" = "" ]; then
		break;
	fi
done
echoc GREEN "connection terminated on `date`."
disconnect
USCITA=0
exit 0
