#!/bin/sh
#
#pragma ident  "@(#)ispifc	1.8 96/05/10 SMI"
#
# Copyright (c) 1994 by Sun Microsystems, Inc.
#
# ISDN PPP "early" ifconfig script to support /etc/defaultrouter
# 
#

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

PATH=$PATH:$BINDIR
export PATH

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

mode=$1

 
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}'`
}

defrouters=""

check_if_default_router()
{
for router in $defrouters
do
	if [ "X$1" = "X$router" ]; then
		return 0
	fi	
done
return 1
}

case "$mode" in
'start')

	# check if any defaulrouters have been defined
	if [ -f /etc/defaultrouter ]; then
        	defrouters=`grep -v \^\# /etc/defaultrouter | awk '{print $1}' `

		if [ -z "$defrouters" ]; then
			exit 0
		fi 
	fi

	# check if one of the is interfaces is one of the defaulrouters 
	# if yes, plumb up all is interfaces before inet starts so that
	# creating the default route won't fail.
	#
	interfaces=`nawk '/^[ \t]*ifconfig/ { \
		print $4; print $5; }' < $CONFFILE`

	# if defrouters exists, the system won't come up as a router
	# and this regardless of the number of isptp devices plumbed up. 
	for i in $interfaces
	do
		check_if_default_router $i
		if [ $? -eq 0 ]; then
			cat $CONFFILE | grep $i |  \
			nawk '/^[ \t]*ifconfig/ { system($0) }'
		#echo "isppp: plumbed $i default route"
		fi
	done

	exit 0
       	;; 

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

esac  
