#!/bin/sh
LOAD=0
CONFIG=0
START=0
KILL=0
HUP=0
UNLOAD=0
GOTFLAG=0
OS=`uname -r`
while getopts lcuskhr c
do
    GOTFLAG=1
    case $c in
     l)  LOAD=1;;
     c)	 CONFIG=1;;
     s)  START=1;;
     k)	 KILL=1;;
     h)	 HUP=1;;
     r)	 KILL=1; START=1;;
     u)  UNLOAD=1;;
    esac
done
shift `expr $OPTIND - 1`
#
# Read in our configuration
#
. /etc/dp.conf
#
# If no flags, assume -s
#
if [ $GOTFLAG -eq 0 ] ; then
    START=1
fi
#
# In order to unload, kill the daemon
#
if [ $UNLOAD -eq 1 ] ; then
    KILL=1;
fi
#
# Include ifconfig and friends in the path
#
PATH=/usr/sbin:/usr/etc:$PATH
export PATH
#
# Find pid for dpd
#
DPDPID=
if [ -f ${DPLOG_DIR}/Pid/dpd ] ; then
    DPDPID=`cat ${DPLOG_DIR}/Pid/dpd`
fi
#
# Shut down if requested..
#
if [ $KILL -eq 1 ] ; then
    echo DP currently unavailable > $DPCONF_DIR/NoLogin
    if [ -n "$DPDPID" ] ; then
	kill $DPDPID
	sleep 1
    fi
fi
if [ $UNLOAD -eq 1 ] ; then
    #
    # If gated is running, kill it..
    #
    if [ -f /etc/gated.pid ] ; then
	GATED=`cat /etc/gated.pid`
	if [ -n "$GATED" ] ; then
	    kill -TERM $GATED
	    sleep 10
	fi
    fi
    #
    # Remove routes that we installed
    #
    if [ -r $DPCONF_DIR/routes ] ; then
	egrep -v '^[ 	]*#|^[ 	]*$' $DPCONF_DIR/routes | \
	sed -e 's/[ 	]*#.*$//' | \
	(
	    while read type dest gateway metric ; do
		route delete $type $dest $gateway $metric
	    done
	) 2>&1 | grep -v '^delete net.*gateway [^ ]*$'
    fi
    #
    # Tear down IP plumbing..
    #
    DPIFS=`ifconfig -a | grep '^dp' | sed 's/^\(dp[0-9]*\).*/\1/'`
    if [ -n "$DPIFS" ] ; then
	for DPIF in $DPIFS ; do
	    ifconfig $DPIF down
	done
	sleep 1
	for DPIF in $DPIFS ; do
	    DPPF=$DPPID_DIR/$DPIF
	    if [ -f $DPPF ] ; then
		DPPID="`cat $DPPF`"
		if [ -n "$DPPID" ] ; then
		    kill $DPPID
		fi
	    fi
	done
	case "$OS" in
	5.*)
	    sleep 5
	    for DPIF in $DPIFS ; do
		${DPBIN_DIR}/dpconfig $DPIF unplumb detach
	    done
	    sleep 1
	esac
    fi
    case "$OS" in
     4.*) 
	cd /tmp
		
	if [ -f $DPLOG_DIR/ID.dp ] ; then
	    modunload -id `cat $DPLOG_DIR/ID.dp`
	    rm -f  $DPLOG_DIR/ID.dp
	fi
	if [ -f $DPLOG_DIR/ID.async ] ; then
	    modunload -id `cat $DPLOG_DIR/ID.async`
	    rm -f $DPLOG_DIR/ID.async
	fi
	if [ -f $DPLOG_DIR/ID.sync ] ; then
	    modunload -id `cat $DPLOG_DIR/ID.sync`
	    rm -f $DPLOG_DIR/ID.sync
	fi
	if [ -f $DPLOG_DIR/ID.vjc ] ; then
	    modunload -id `cat $DPLOG_DIR/ID.vjc`
	    rm -f $DPLOG_DIR/ID.vjc
	fi
	;;
     5.*)
	ID=`modinfo | awk '{ if ($6 == "pppasync") print $1}'`
	if [ -n "$ID" ] ; then
	    modunload -i $ID
	fi
	ID=`modinfo | awk '{ if ($6 == "pppsync") print $1}'`
	if [ -n "$ID" ] ; then
	    modunload -i $ID
	fi
	ID=`modinfo | awk '{ if ($6 == "vjc") print $1}'`
	if [ -n "$ID" ] ; then
	    modunload -i $ID
	fi
	ID=`modinfo | awk '{ if ($6 == "dp") print $1}'`
	if [ -n "$ID" ] ; then
	    modunload -i $ID
	fi
    esac
fi
#
# Re-read initialization files, if requested
#
if [ $HUP -eq 1 -a -n "$DPDPID" ] ; then
    kill -HUP $DPDPID
fi
#
# If no interfaces on the system, try to dynamically load them.
#
if [ $START -eq 1 ] ; then
    case "$OS" in
     4.*)
	LOADED=`ifconfig -a | grep "dp[0-9]" | wc -l`
	if [ $LOADED -eq 0 ] ; then
	    LOAD=1
	fi
	;;
    esac
fi
if [ $LOAD -eq 1 ] ; then
    case "$OS" in
     4.*) 
	cd /tmp
	modload $DPMODULE_DIR/pppsync.o  -exec $DPMODULE_DIR/postsyncload
	modload $DPMODULE_DIR/pppasync.o -exec $DPMODULE_DIR/postasyncload
	modload $DPMODULE_DIR/vjc.o      -exec $DPMODULE_DIR/postvjcload
	modload $DPMODULE_DIR/dpif.o     -exec $DPMODULE_DIR/postdpload
	;;
     *)
	modload -p drv/dp
	modload -p strmod/vjc
	modload -p strmod/pppasync
	modload -p strmod/pppsync
    esac
fi
#
# dpconfig and ifconfig the interfaces
#
if [ $CONFIG -eq 1 ] ; then
    if [ -f $DPCONF_DIR/hostnames ] ; then
	egrep -v '^[ 	]*#|^[ 	]*$' $DPCONF_DIR/hostnames | \
	sed -e 's/[ 	]*#.*$//' | \
	(
	    while read if addrs
	    do
		case "$OS" in
		 5.*) ${DPBIN_DIR}/dpconfig $if attach plumb
		esac
		(ifconfig $if $addrs netmask + -trailers up 2>&1) | \
		grep -v "Setting netmask of dp"
	    done
	) < $DPCONF_DIR/hostnames
	#
	# Make sure ip forwarding is turned on if we have successfully
	# configured at least one dp interface.  If there is a default
	# route configured, but not installed, install it here.
	# If gated is configured, but not running, start it here.
	#
	numdp=`ifconfig -a | grep "dp[0-9]" | wc -l`
	if [ $numdp -gt 0 ] ; then
	    case "$OS" in
	     5.*) ndd -set /dev/ip ip_forwarding 1
	    esac
	    defroute="`netstat -rn | grep default`"
	    #
	    # Honor /etc/defaultrouter
	    #
	    if [ -z "$defroute" -a -f /etc/defaultrouter ]; then
		defroute="`cat /etc/defaultrouter`"
		if [ -n "$defroute" ]; then
		    route add default $defroute 1
		fi
	    fi
	    #
	    # Start gated if configured and not running
	    #
	    if [ \( -x /usr/sbin/gated -o -x /usr/etc/gated \) -a -f /etc/gated.conf -a ! -f /etc/gated.pid ] ; then
		gated
	    fi
	    #
	    # Add routes specific to dp
	    #
	    if [ -r $DPCONF_DIR/routes ] ; then
		egrep -v '^[ 	]*#|^[ 	]*$' $DPCONF_DIR/routes | \
		sed -e 's/[ 	]*#.*$//' | \
		(
		    while read type dest gateway metric ; do
			route add $type $dest $gateway $metric
		    done
		) 2>&1 | grep -v '^add net.*gateway [^ ]*$'
	    fi
	fi
    fi
fi
#
# Start things up, if requested
#
if [ $START -eq 1 ] ; then
    rm -f $DPCONF_DIR/NoLogin
    $DPBIN_DIR/dpd $*
fi
exit 0
