#!/bin/sh
#
#pragma ident  "@(#)isppp	1.29 96/06/07 SMI"
#
# Copyright (c) 1994 by Sun Microsystems, Inc.
#
# ISDN PPP start/stop script
#

BASEDIR=/opt/SUNWisdn
CONFDIR=/etc/opt/SUNWisdn
LOGDIR=/var/opt/SUNWisdn

BINDIR=$BASEDIR/bin
LIBDIR=$BASEDIR/lib
CONFFILE=$CONFDIR/isppp.cf
LOGFILE=$LOGDIR/log
PARADIGM=$BINDIR/paradigm
binding_domain=al4
DAEMON=$BINDIR/ispppd
SECURED=$BINDIR/secured

PATH=$PATH:$BINDIR
export PATH

REQ_FILES="$DAEMON $LIBDIR/al4ip.so $CONFFILE"

mode=$1
unload_isdn() {
	isdnids=`modinfo |grep isdn | awk '{print $1}'`
	iscmid=`modinfo |grep isdn | grep cm |awk '{print $1}'`

	# unload cm module last, get device busy
	for id in ${isdnids}; do
		if [ $id != $iscmid ]
		then
			modunload -i $id
		fi
	done
	modunload -i $iscmid
}

 
if [ -f $PARADIGM ]; then
        binding_domain=`grep -v \^\# $PARADIGM | awk '{print $1}' `
        if [ -z "$binding_domain" ]; then
            binding_domain=al4
        fi
fi

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

for FILE in $REQ_FILES; do
	if [ ! -f $FILE ]; then
		echo "$0: ISDN PPP has been installed but"
		echo "not configured correctly"
		echo "$0: $FILE not found"
		echo "$0: please refer to the ISDN documentation"
		exit 1
	fi
done

# check if ispppd is running
check_if_running() {
	pid=`/bin/ps -ef | egrep ispppd | egrep -v egrep |awk '{print $2}'`
}

case "$mode" in
'start')
# Start ispppd
	check_if_running
	if test -n "$pid"; then
		echo "ispppd is already running. Please invoke isppp stop"
		exit 1
	fi

	#  execute the "ifconfig" lines.
	#  execute the "route" lines.
	nawk '/^[ \t]*ifconfig/ { system($0) }' < $CONFFILE 
	nawk '/^[ \t]*route/ { system($0) }' < $CONFFILE 

	$DAEMON -b ${binding_domain}ip.so -d 1 -P $LIBDIR &
	sleep 1	 # allow parsing to complete
       	;; 

'debug')
# Start ispppd with -d 9
	check_if_running
	if test -n "$pid"; then
		echo "ispppd is already running. Please invoke isppp stop"
		exit 1
	fi

	#  execute the "ifconfig" lines.
	nawk '/^[ \t]*ifconfig/ { system($0) }' < $CONFFILE
	#  execute the "route" lines.
	nawk '/^[ \t]*route/ { system($0) }' < $CONFFILE 

	$DAEMON  -b ${binding_domain}ip.so -d 9 -P $LIBDIR &
	sleep 1	 # allow parsing to complete
       	;; 

'stop')
# Stop ispppd
	check_if_running
	if test -n "$pid"; then
		kill -KILL $pid
		sleep 1	 # allow kill to complete
		if test -f $CONFFILE; then
		    if [ `uname -r` = "5.3" ]; then
		    # 2.3 has a different semantic	
        	    nawk '/^[ \t]*ifconfig/ { system("ifconfig " $2 " down") 
				}' < $CONFFILE
		    else
        	    nawk '/^[ \t]*ifconfig/ { system("ifconfig " $2 " unplumb") 
				}' < $CONFFILE
		    unload_isdn
			
		    # bug id #1260220:
		    # unload other autoloaded module such as ipf and caused
		    # the system hard hang.
		    #modunload -i 0
		    #modunload -i 0
		    fi
		    echo "ispppd stopped, ppp interfaces unplumbed"
		else
		    echo "ispppd stopped"
		fi
        else
                echo "ispppd is not running"
		exit 1
	fi
       	;; 


*)	# usage
	echo "usage: $0 start|debug|stop"
	exit 1
	;;

esac  
