#!/bin/sh
#
# /usr/local/bin/abcnet.solaris
#
# Enable/disable Solaris 2.5 Internet access to ABC-Klubben
#
# Version 2.01 1996-05-27
#
# Author: Lars-Ake.Carlsson@Sun.se, adopted for ABC by Bo.Kullmar@abc.se
#

IFCONFIG=/usr/sbin/ifconfig
NETSTAT=/usr/bin/netstat
PING=/usr/sbin/ping
PS=`ps -e`
DAEMON=aspppd
HOSTS=/etc/inet/hosts

# This is the IP address of the default gateway at Swipnet
DEFAULT_GATEWAY="192.36.170.254"

# Dummy address for the local interface
# Note: Must be a unique network and node address
LOCAL_DUMMY="192.36.170.240"

# The Point-To-Point interface name
PTP_INTERFACE="ipdptp0"

# Not James Bond...
BELL="\007"

# Script name
MODE=$1

# Check who's knock'n... (suid $0 would also work)

set `id`
if [ $1 != "uid=0(root)" ];
then
  echo "${BELL}\n$0: this script must be run as root ... fatal error\n"

  exit 1
fi

# NSS       - Active Name Service configuration (DO NOT CHANGE)
# NSS_DNS   - Name Service configuration including DNS
# NSS_NODNS - Name Service configuration without DNS
NSS="/etc/nsswitch.conf"
NSS_DNS="/etc/nsswitch.abc"
NSS_NODNS="/etc/nsswitch.files"

# Check if both NSS files exist, it's safer this way.

if [ ! -f "${NSS_DNS}" ];
then
  echo "${BELL}\nError: ${NSS_DNS} does not exist"

  exit 2
elif [ ! -f "${NSS_NODNS}" ];
then
  echo "${BELL}\nError: ${NSS_NODNS} does not exist"

  exit 3
fi

# Function that send a hangup signal to the daemon
restart_daemon()
{
  trap "" 1 2 3 15			# ignore signals
  
  PID=`echo "${PS}" | egrep \ ${DAEMON}\$ | sed -e 's/^  *//' -e 's/ .*//'`

  if [ "${PID}" ];
  then
    echo "Sending HUP signal to pid ${PID} (${DAEMON})"

    kill -HUP ${PID} >/dev/null 2>&1
  else
    echo "${DAEMON} not running"
    
  fi

  trap 1 2 3 15				# reset signals
}

case "${MODE}" in
  'start')
	 echo "Enabling interface ${PTP_INTERFACE}"
	 ${IFCONFIG} ${PTP_INTERFACE} ${LOCAL_DUMMY} ${DEFAULT_GATEWAY} up

	 echo "Enabling DNS in the Name Service database"
	 cp ${NSS_DNS} ${NSS}

	 DEFGTWY_NAME=`cat ${HOSTS} | egrep ${DEFAULT_GATEWAY} | cut -f2`

	 if [ -z "${DEFGTWY_NAME}" ];
	 then
	   echo "Calling ${DEFAULT_GATEWAY} (unknown name), please wait..."
	 else
	   echo "Calling ${DEFGTWY_NAME} (${DEFAULT_GATEWAY}), please wait..."
	 fi
	 
	 ${PING} ${DEFAULT_GATEWAY} 60 > /dev/null 2>$1

	 while [ $? -ne 0 ];
	 do
	   echo "Wait, trying again\n"
	   ${PING} ${DEFAULT_GATEWAY} 60 > /dev/null 2>$1	
	 done
         echo "${BELL}\nOk!\n"
##	 audioplay $HOME/lib/yankee.au
	 ;;
  'stop')
	 echo "Disabling interface ${PTP_INTERFACE}"
	 ${IFCONFIG} ${PTP_INTERFACE} down

	 echo "Restoring the Name Service database"
	 cp ${NSS_NODNS} ${NSS}

	 restart_daemon
	 ;;
  *)
         echo "${BELL}\nUsage: $0 start|stop\n"

	 exit 5
	 ;;
esac

