#!/bin/sh
# ------------------------------------------------------------------------------
# ppp           Script for automating the PPP connection process.
#
# Usage:
#               ppp start|up      - Activates the PPP connection.
#               ppp stop|dn       - Terminates the PPP connection.
#               ppp restart       - Restarts the PPP connection.
#               ppp status|?      - Reports connection status.
#               ppp toggle        - Toggles current state connection state.
#               ppp serial        - Execs pppd in shell, for dialin ppp.
#               ppp ttyS*         - Starts ppp over serial port specified.
#
# Notes:        Add chatscript selection argument.
#
# Author:       Sean Blankenship <seanb/at/writeme/dot/com>.
# Modified:     12-24-2000.
# ------------------------------------------------------------------------------
# Configuration Area.
#
XLOCK="/var/lock/ppp.lock"
XTTYS="ttyS1"
XBAUD="115200"
XARGS="modem crtscts lock -detach defaultroute"
XCHAT="/etc/ppp/chatscript"
XGATE="192.168.1.1"
# ------------------------------------------------------------------------------
case "$1" in
        start|up)
                # --------------------------------------------------------------
                # Check for current connection...
                #
                if [ -f "$XLOCK" ]; then
                        echo -e -n "\a"
                        $0 status
                        exit 1
                fi
                # Kill all active pppd connection if present...
                #
                #/sbin/pidof pppd >/dev/null 2>&1; PPPDQ="$?"
                #if [ "$PPPDQ" = "0" ]; then
                #       $0 restart
                #       exit 1
                #fi
                # --------------------------------------------------------------
                # Call pppd and redial if PPP connection is lost.
                # The { } & will keep pppd in background while not detached.
                #
                # /bin/touch $XLOCK >/dev/null 2>/dev/null
                echo $USER > $XLOCK
                {
                while [ -f "$XLOCK" ]; do
                        /usr/sbin/pppd /dev/$XTTYS $XBAUD $XARGS connect "/usr/sbin/chat -f $XCHAT"
                        # If connection fails, reinit in x second(s).
                        if [ -f "$XLOCK" ]; then
                                (
                                        echo "To: root"
                                        echo "From: $0"
                                        echo "Subject: PPP Connection Failure"
                                        /bin/date
                                )>/dev/null 2>/dev/null #|/usr/sbin/sendmail -t
                                sleep 4
                        fi
                done
                } >/dev/null 2>/dev/null &
                ;;
        stop|dn)
                # --------------------------------------------------------------
                # Kill this script | Remove Lock file...
                # Stop connection status checking initiated in start...
                #
                if [ -f "$XLOCK" ]; then
                        rm -f $XLOCK >/dev/null 2>/dev/null
                fi
                # --------------------------------------------------------------
                # Kill the pppd process...
                #
                if [ -r /var/run/ppp0.pid ]; then
                        kill -INT $(cat /var/run/ppp0.pid) >/dev/null 2>&1 &
                        # Check for kill problems...
                        if [ ! "$?" = "0" ]; then
                                # Force remove lock file and return error...
                                rm -f /var/run/ppp0.pid
                                echo -e "Removing stale ppp0.pid file.\n\a"
                                exit 1
                        fi
                fi
                # --------------------------------------------------------------
                # Kill any active pppd connections if present...
                # This is redundant but returns error...
                # This is not a great option b/c other pppd processes
                # may be running. It will only kill the pppd processes
                # that belong to user.
                #
                /sbin/pidof pppd >/dev/null 2>&1; PPPDQ="$?"
                if [ "$PPPDQ" = "0" ]; then
                        killall pppd >/dev/null 2>/dev/null
                        #echo -e "Error killing pppd with ppp0.pid pid\n\a"
                        #exit 1
                fi
                # ---------------------------------------------------------
                # Remove ttyS* lock file if present...
                #
                if [ -f "/var/lock/LCK..$XTTYS" ]; then
                        rm -f /var/lock/LCK..$XTTYS >/dev/null 2>/dev/null
                        #echo -e "Had to remove /var/lock/LCK..$XTTYS\n\a"
                        #exit 1
                fi
                ;;
        restart)
                $0 stop
                $0 start
                ;;
        status|?)
                # --------------------------------------------------------------
                PPPIF="ppp0"
                if /sbin/ifconfig | grep $PPPIF >/dev/null; then
                        PPPIP=`/sbin/ifconfig $PPPIF|awk '/inet addr/ {gsub(".*:","",$2); print $2}'`
                        PPPPE=`/sbin/ifconfig $PPPIF|awk '/inet addr/ {gsub(".*:","",$3); print $3}'`
                        PPPMA=`/sbin/ifconfig $PPPIF|awk '/inet addr/ {gsub(".*:","",$4); print $4}'`
                        echo -e "$PPPIF interface IP.....: $PPPIP"
                        echo -e "$PPPIF peer IP..........: $PPPPE"
                        echo -e "$PPPIF subnet mask......: $PPPMA"
                        echo -e ""
                else
                        echo "$PPPIF interface is down."
                        echo ""
                fi
                ;;
        toggle)
                #----------------------------------------------------------
                if [ -f "$XLOCK" ]; then
                        $0 stop
                else
                        $0 start
                fi
                ;;
        serial)
                # --------------------------------------------------------------
                # Setup ppp over dialin connection. No for console use...
                #
                exec /usr/sbin/pppd -detach 192.168.1.1:192.168.1.200
                #exec /usr/sbin/pppd file /etc/ppp/options.dialin.ttyS1
                ;;
        ttyS*)
                # --------------------------------------------------------------
                # Setup ppp over a serial device...
                #
                /usr/sbin/pppd /dev/$1 115200 defaultroute >/dev/null
                ;;
        *)
                # --------------------------------------------------------------
                echo -e "Usage: $(basename $0) [start|stop|restart|?]\n\a"
                exit 1
esac
exit 0