#!/bin/bash

# This script creates a ppp connection, updates /etc/hosts to reflect
# the assigned IP address and hostname, and reconfigures the web server
# to allow connections using the hostname.  This script is provided "As is"
# and all responsibility for its use is assumed by the user.
# Copyright Mark A. Martin 1999.

# Set needed variables.

LOCKDIR=/var/lock    # directory for writing lock file.
DEVICE=modem         # modem device
PHONE=9999999        # phone number
OUR_IP_ADDR=0.0.0.0  # i.e. IP address assigned by remote host.
                     # If you have a fixed IP address, enter it here.
PING_TARGET=192.192.192.192  # Address for the keep-alive to ping.

# Auxiliary scripts.

UPDATE_NAME=/etc/httpd/conf/update_name
PINGD=/usr/sbin/pingd

# Check to see if the device is already in use.

if [ -f $LOCKDIR/LCK..$DEVICE ]
then
    echo "PPP device is locked"
    exit 1
fi

function connect {

    # This function makes the connection using the phone number
    # and account information given.

    (
        /bin/stty 38400 -tostop

        # ATM0 (i.e. ATMzero) turns off the speaker on the modem.
        # Different numbers correspond to different speaker modes
        # and ATLn, where n = 0-3, sets the speaker volume.

        # ATDT*70W$PHONE implements tone dialing using *70 to turn off
        # call waiting and W to wait for a second dial tone before
        # dialing $PHONE.

        if /usr/sbin/chat ABORT "NO CARRIER" ABORT BUSY "" ATZ OK ATM0 OK ATDT*70W$PHONE CONNECT "" name: $USER ssword: \\q$PASSWORD ">>" ppp
        then
            /usr/sbin/pppd asyncmap 0 -detach modem crtscts defaultroute mru 1500 $OUR_IP_ADDR: /dev/$DEVICE
            /bin/rm -f $LOCKDIR/LCK..$DEVICE
            return 0
        else
            echo "PPP call failed" 1>&2
            if ps aux | grep pingd | grep -v grep > /dev/null; then
                echo -e "\nTerminating the ping daemon..."
                kill `ps aux | grep pingd | grep -v grep | awk '{print $2}'`
            fi
            return 1
        fi
    ) < /dev/$DEVICE > /dev/$DEVICE &

}

function update_hosts {

    if [ -e /etc/hosts ]; then

        # If /etc/hosts doesn't contain the short version of the hostname,
        # "hostname -s" will fail.

        host=`/bin/hostname -s`
        /bin/mv /etc/hosts /etc/hosts.old
        echo "$ip  $host  $fqdn" > /etc/hosts
        echo "127.0.0.1  localhost" >> /etc/hosts
        return 0

    else
        echo "Couldn't find /etc/hosts."
        return 1
    fi

}

if ! /bin/ps axc | /bin/grep pppd > /dev/null; then

    # Prompt the user for username and password.

    echo -en "\nUsername: "
    read USER
    /bin/stty -echo
    echo -n 'Password: '
    read PASSWORD
    /bin/stty echo
    echo -e "\n"

    echo "Establishing the ppp connection..."
    connect

    # Start the ping daemon to keep the connection alive.

    echo "Starting the ping daemon..."
    $PINGD 120 $PING_TARGET &> /dev/null &
    pingpid=$!

    ###################################################################
    # Do an nslookup for the one node that is expected to be on the   #
    # network, i.e. this one.  Get the interface and IP address from  #
    # ifconfig.  Attempt the nslookup for a specified number of       #
    # retries before failing.                                         #
    ###################################################################

    # Determine the interface and the IP address assuming that there
    # is only one PPP interface in use.

    retries=10

    echo -n "Determining the interface and IP address"
    while [ $retries -gt 0 ]
    do
        iface=`/sbin/ifconfig | /bin/grep ppp | /bin/awk '{print $1}'`
        if [ $iface ]; then
            ip=`/sbin/ifconfig $iface | /bin/grep "inet addr"\
                | /bin/awk '{print $2}' | /bin/awk -F: '{print $2}'`
            let "retries = 0"
            continue
        else
            let "retries = retries - 1"
            n=5
            while [ $n -gt 0 ]
            do
              sleep 1
              echo -n "."
              let "n = n - 1"
            done
        fi
    done

    if [ $iface ]; then
        echo "done"
    else
        echo
        echo "Couldn't determine interface."
        # Maybe run ppp-off instead
        if /bin/ps hp $pingpid; then
            echo -e "\nTerminating the ping daemon..."
            kill $pingpid
        fi
        exit 1
    fi

    if [ $ip ]; then
        echo -e "\nUsing interface $iface with IP address $ip.\n"
    else
        echo "Couldn't determine the IP address."
        # Maybe run ppp-off instead
        echo -e "\nTerminating the ping daemon..."
        kill $pingpid
        exit 1
    fi

    # Test to see if name services are available.
    # If so, reconfigure /etc/hosts and the web server to reflect
    # the assigned host name.

    retries=10

    while [ $retries -gt 0 ]
    do

        echo -n "Trying name services..."
        fqdn=`/usr/bin/nslookup $ip | /bin/grep "Name:" | /bin/awk '{print $2}'`
        if [ $fqdn ]; then

            echo -e "done\n\nHost name: $fqdn\n"

            # Perform reconfiguration and exit.

            # Write a new /etc/hosts file to reflect the new FQDN.

            echo "Updating /etc/hosts..."
            update_hosts

            # Reconfigure the web server to use the correct FQDN for
            # its ServerName.

            echo "Reconfiguring the web server..."
            $UPDATE_NAME $fqdn

            # Write the web page allowing access to the local web server.

            #echo "Notifying potential visitors..."
            #/home/mark/bin/notify_on

            echo -e "Connection established and reconfiguration complete.\n"
            exit 0

        fi

        n=5
        while [ $n -gt 0 ]
        do
          sleep 1
          echo -n "."
          let "n = n - 1"
        done

        let "retries = retries - 1"

    done

    echo -e "\nName services appear to be unavailable."
    exit 1

else

  echo "pppd is already running"

fi
