:
#      @(#)ppp0	5.4.1.3 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`

tmp=/tmp/ppp$$
NETCF=/usr/lib/netconfig
LKPERM=/etc/perms/inst			# OS (link kit) permlist

##############################################################################
#
# Test to see if link kit is installed
#
chklinkkit() {
	cd /
	fixperm -i -d LINK $LKPERM 2> /dev/null
	case $? in
	0)	return 0
		;;
	3|4)	echo "The Link Kit is not installed." >&2
		cleanup $FAIL
		;;
	5)	echo "The Link Kit is only partially installed." >&2
		echo "Attempting to remove ppp package anyway." >&2
		return 0
		;;
	*)	echo "Error testing for Link Kit. Exiting."
		cleanup $FAIL
		;;
	esac
}

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

##############################################################################
#
# Update configuration files
#
update_config() {
	# Take the ifconfig out of /etc/tcp
	ed -s /etc/tcp <<-EOF > /dev/null 2>&1
	/[ 	]*ifconfig[ 	]*$base/
	d
	w
	q
	EOF

	# Remove from /etc/strcf's pppip function
	ed -s /etc/strcf <<-EOF > /dev/null 2>&1
	/^[ 	]*ppplinkint[ 	]*\$1[ 	]*$base/
	d
	w
	q
	EOF
}

##############################################################################
#
# Figure out which kernel we're running
#
which_kernel() {
	if [ -x /bin/ps ] ; then
		/bin/su root -c "/bin/ps -e" > /tmp/ps.err$$ 2>&1
		if grep "not the booted" /tmp/ps.err$$ > /dev/null ; then
			PS="/bin/ps -n /unix.old"
		else
			PS="/bin/ps"
		fi
		rm -f /tmp/ps.err$$
	else
		PS=""
	fi
}

##############################################################################
#
# Kill pppd
#
kill_pppd() {
	if [ -n $PS ] ;then
		pid=`$PS -ef | /bin/grep pppd | \
		/bin/grep -v grep | /usr/bin/awk '{ print $2 }'`
		if [ -n "$pid" ]; then
			PIDS="$PIDS $pid"
		fi

		if [ -n "$PIDS" ] ; then
			echo "\nKilling pppd procs ..."
			/bin/kill -9 $PIDS 2> /dev/null 1>&2
		fi
	fi
}

##############################################################################
#
# Update /etc/strcf configuration file
#
comment_pppip() {
	ed -s /etc/strcf <<-EOF > /dev/null 2>&1
	/pppip[ 	]*ip/
	.s/^/#/
	w
	q
	EOF
}

##############################################################################
#
# Imitate egrep since it is only available in the extended utilities
# This function has limited functionaliy: Only one file name may be
# specified. Only the -v option flag is currently supported
# Will default to the real egrep if it exists.
#
Egrep() {
	if [ -x /usr/bin/egrep ] ; then
		egrep "$@"
		return $?
	else
		case $1 in
			-v) shift
			    _reg_expr=$1
			    cat $2 | awk "BEGIN { _exit=1 } \
					  ! /$_reg_expr/ \
					  { print \$0 ; _exit=0 } \
					  END { exit _exit }"
			    _exit=$?
			    ;;
			 *) _reg_expr=$1
			    cat $2 | awk "BEGIN { _exit=1 } \
					  /$_reg_expr/ \
					  { print \$0 ; _exit=0 } \
					  END { exit _exit }"
			    _exit=$?
			    ;;
		esac
	return $_exit
	fi
}

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

# Update /etc/tcp and /etc/strcf and /etc/ppphosts
update_config

# Check to see how many ppp drivers are still installed
n_ppp_drvs=`netconfig -s | Egrep "ppp[0-9]*" | wc -l`
if [ $n_ppp_drvs -eq 1 ] ; then
	# Figure out which kernel we're running
	which_kernel
	# Ckeck that the link kit is installed
	chklinkkit
	# Kill pppd
	kill_pppd
	# Save ppphosts and pppauth
	mv /etc/ppphosts /usr/lib/ppp/ppphosts
	mv /etc/pppauth /usr/lib/ppp/pppauth
	# Comment out the call to pppip in /etc/strcf
	comment_pppip
	# Remove the PPP drivers
	for i in ppp asyh
	do
		echo "Removing $i driver ... \c"
		idinstall -d -e $i >/dev/null 2>&1
		echo "done."
	done
	rm -f /dev/ppp /dev/ppcid /dev/asyh
	# Remove all but X0 init, remove, info scripts
        rm -f $NETCF/init/${drv}[1-9] $NETCF/init/${drv}[1-9][0-9]
        rm -f $NETCF/remove/${drv}[1-9] $NETCF/remove/${drv}[1-9][0-9]
        rm -f $NETCF/info/${drv}[1-9] $NETCF/info/${drv}[1-9][0-9]
	echo "$base removal complete.\n"
	cleanup $RELINK	# Ensure kernel is relinked by netconfig
fi
echo "$base removal complete.\n"
cleanup $OK
