:
#      @(#)ppp0	5.4.1.4 Lachman System V STREAMS TCP  source
#      SCCS IDENTIFICATION
#
#	System V STREAMS TCP - Release 4.0
#
#	Copyright 1987, 1988, 1989 Lachman Associates, Incorporated (LAI)
#
#	All Rights Reserved.
#
#	The copyright above and this notice must be preserved in all
#	copies of this source code.  The copyright above does not
#	evidence any actual or intended publication of this source
#	code.
#
#	This is unpublished proprietary trade secret source code of
#	Lachman Associates.  This source code may not be copied,
#	disclosed, distributed, demonstrated or licensed except as
#	expressly authorized by Lachman Associates.
#
#	System V STREAMS TCP was jointly developed by Lachman
#	Associates and Convergent Technologies.
#
#
: ${OK=0} ${FAIL=1} ${RELINK=2}
PATH=/bin:/usr/bin:/etc:/etc/conf/bin

LOWER_PRD=$1
LOWER_IF=$2
UPPER_PRD=$3
UPPER_IF=$4
CONFIGURE=$5
base=`basename $0`
drv=`echo $base | sed -e 's/[0-9]*$//'`
unit=`expr $base : '.*\(.\)'`
onemore=`expr $unit + 1`

NETCF=/usr/lib/netconfig
PWD=/usr/lib/netconfig/init
UNITS=4		# Maximum number of duplicated scripts
		# This limits the number of ppp lines configurable

##############################################################################
#
# Remove temp files and exit with the status passed as argument
#
cleanup() {
	trap '' 1 2 3 15
	[ "$tmp" ] && rm -f $tmp* 
	exit $1
}

##############################################################################
#
# Remove drivers then call cleanup
#
cleanup2() {
	trap '' 1 2 3 15
	/etc/conf/bin/idinstall -d -e ppp >/dev/null 2>&1
	/etc/conf/bin/idinstall -d -e asyh >/dev/null 2>&1
	cleanup $1
}

##############################################################################
#
# Print an error message
# Usage: Error "message"
# Argument is a quoted error message
#
Error() {
	echo "\nError: $*" >&2
	return 1
}


##############################################################################
#
# Print a warning message
# Usage: warning "message"
# Argument is a quoted warning message
#
warning() {
	echo "\nWarning: $*"
}

##############################################################################
#
# Print a message
# Argument is a quoted message
#
message() {
	echo "$*"
}

##############################################################################
#
# Add pppuser to /etc/passwd
#
add_pppuser() {
	grep "^nppp:" /etc/passwd > /dev/null || {
		who=`who am i | awk '{ print $1 }'`
		[ "$who" = "" ] && who=root
		if [ "$who" = "root" ]
		then
			/usr/lib/tcprt/addpppuser
		else
			message "You are not logged in as root. After this installation is finished please"
			message "log in as root and run the command /usr/lib/tcprt/addpppuser."
		fi
	}
}

##############################################################################
#
# Now bump to enable the next unit
#
bump() {
	dir=`pwd`
	cd $NETCF/info
	newunit=`expr $unit + 1`
	if [ $newunit -lt $UNITS ] ; then
		newfile=${drv}${newunit}
		sed -e '/^NAME=.*'"[^0]"'/s/0\"/'$newunit'\"/p' \
		    -e '/^DESCRIPTION=.*'"[^0]"'/s/0\"/'$newunit'\"/p' ${drv}0 \
		> $newfile
		chown bin $newfile
		chgrp bin $newfile
		chmod 750 $newfile
		cd $NETCF/init
		ln $base ${drv}`expr $unit + 1` > /dev/null 2>&1
		cd $NETCF/remove
		ln $base ${drv}`expr $unit + 1` > /dev/null 2>&1
	fi
	cd $dir
}

##############################################################################
#
# main() 
#
trap "cleanup $FAIL" 1 2 3 15

if [ "$CONFIGURE" = "N" ] ; then
	cleanup $OK
fi

message "\nConfiguring PPP Interface ppp$unit\n"
# Is this the first ppp line?
n_ppps=`netconfig -s | grep "#ppp" | wc -l`
if [ $n_ppps -eq 0 ] ; then
	for i in ppp asyh
	do
		message "Installing the $i driver into the link kit ... \c"     
		cd /usr/lib/tcprt/ID/$i
		/etc/conf/bin/idcheck -p $i
		if [ $? -lt 16 ] ; then
			/etc/conf/bin/idinstall -a -e -k $i >/dev/null 2>&1
		else 
			/etc/conf/bin/idinstall -u -e -k $i >/dev/null 2>&1
		fi 
		echo "done."
	done

	# Addxuser here
	add_pppuser

	[ ! -f /tmp/tcprt_upgrade ] && {
		/bin/cp /usr/lib/ppp/ppphosts /etc/ppphosts
		/bin/cp /usr/lib/ppp/pppauth /etc/pppauth
	}

	/bin/chown bin /etc/ppphosts
	/bin/chgrp bin /etc/ppphosts
	/bin/chmod 644 /etc/ppphosts
	/bin/chown root /etc/pppauth
	/bin/chgrp root /etc/pppauth
	/bin/chmod 600 /etc/pppauth
	bump		# Add extra script
        cleanup $RELINK	# Ensure kernel is relinked by netconfig
else
	bump		# Add extra script
	cleanup $OK
fi
