#!/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:$PATH
export PATH
#
# Find pid for dpd
#
DPDPID=
if [ -f ${DPLOG_DIR}/Pid/dpd ] ; then
    DPDPID=`cat /var/adm/dp/Pid/dpd`
fi
if [ -z "$DPDPID" ] ; then
    HUP=0;
    KILL=0;
fi
#
# Shut down if requested..
#
if [ $KILL -eq 1 ] ; then
    kill $DPDPID
    sleep 1
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 5
	fi
    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
	sleep 5
	for DPIF in $DPIFS ; do
	    ${DPBIN_DIR}/dpconfig $DPIF unplumb detach
	done
	sleep 1
    fi
    case "$OS" in
     4.*) 
	cd /tmp
		
	if [ -f $DPLOG_DIR/ID.dp ] ; then
	    modunload -id `cat $DPLOG_DIR/ID.dp` -exec $DPMODULE_DIR/predpunload
	    rm -f  $DPLOG_DIR/ID.dp
	fi
	if [ -f $DPLOG_DIR/ID.vjc ] ; then
	    modunload -id `cat $DPLOG_DIR/ID.vjc`
	    rm -f $DPLOG_DIR/ID.vjc
	fi
	if [ -f $DPLOG_DIR/ID.pppasync ] ; then
	    modunload -id `cat $DPLOG_DIR/ID.pppasync`
	    rm -f $DPLOG_DIR/ID.pppasync
	fi
	;;
     *)
	ID=`modinfo | awk '{ if ($6 == "pppasync") 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 ] ; 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.*)
	PATH=$PATH:/usr/sbin:/usr/etc
	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/ppp_async.o -exec $DPMODULE_DIR/postasyncload
	modload $DPMODULE_DIR/vjc.o -exec $DPMODULE_DIR/postvjcload
	modload $DPMODULE_DIR/dp.o -exec $DPMODULE_DIR/postdpload
	;;
     *)
	modload -p drv/dp
	modload -p strmod/vjc
	modload -p strmod/pppasync
    esac
fi
#
# dpconfig and ifconfig the interfaces
#
if [ $CONFIG -eq 1 ] ; then
    if [ -f $DPCONF_DIR/hostnames ] ; then
	(
	    while read if addrs
	    do
		${DPBIN_DIR}/dpconfig $if attach plumb
		ifconfig $if $addrs netmask + -trailers up
	    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
	    ndd -set /dev/ip ip_forwarding 1
	    defroute="`netstat -rn | grep default`"
	    if [ -z "$defroute" -a -f /etc/defaultrouter ]; then
		defroute="`cat /etc/defaultrouter`"
		if [ -n "$defroute" ]; then
		    /usr/sbin/route add default $defroute 1
		fi
	    fi
	    if [ -x /usr/sbin/gated -a -f /etc/gated.conf -a ! -f /etc/gated.pid ] ; then
		/usr/sbin/gated
	    fi
	fi
    fi
fi
#
# Start things up, if requested
#
if [ $START -eq 1 ] ; then
    $DPBIN_DIR/dpd $*
fi
exit 0
