#!/bin/bash

# Terminate a running PPP connection, restore /etc/hosts to reflect a local
# network, and reconfigure the web server to allow access from the local
# network.  This script is provided "As is" and all responsibility for its
# use is assumed by the user.  Copyright Mark A. Martin 1999.

# Variables.

RESTORE_HOSTS=/usr/sbin/restore_hosts
RESTORE_NAME=/etc/httpd/conf/restore_name

# Determine the ppp device in use.  This assumes that there is only one
# ppp device in use.

DEVICE=`/bin/netstat -i | /bin/grep ppp | /bin/awk '{print $1}'`

#
# If the ppp0 pid file is present then the program is running. Stop  it.
if [ -r /var/run/$DEVICE.pid ]; then

       # If the ping daemon is running, kill it.

       echo -e "\nTerminating the ping daemon..."
       if ps aux | grep pingd | grep -v grep > /dev/null; then
           kill `ps aux | grep pingd | grep -v grep | awk '{print $2}'`
       fi

       # Kill the ppp daemon.

       echo "Terminating the PPP connection..."
       kill -INT `cat /var/run/$DEVICE.pid`
#
# If unsuccessful, ensure that the pid file is removed.
#
       if [ ! "$?" = "0" ]; then
               echo "removing stale $DEVICE pid file."
               rm -f /var/run/$DEVICE.pid
               exit 1
       fi

       # Alter /etc/hosts to reflect a local configuration.

       echo "Rewriting /etc/hosts for the local network..."
       $RESTORE_HOSTS

       # Reconfigure the web server to allow local access.

       echo "Reconfiguring the web server for local network..."
       $RESTORE_NAME

#
# Success. Terminate with proper status.
#
       echo -e "$DEVICE link terminated\n"
       exit 0
fi
#
# The link is not active
#
echo "$DEVICE link is not active"
exit 1
