:
#      @(#)sl0	5.5.1.2 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/slip$$
NETCF=/usr/lib/netconfig

# Function declarations

##############################################################################
#
# Remove temp files and exit with the status passed as argument
#
cleanup() {
	trap '' 1 2 3 15
	exit $1
}

##############################################################################
#
# 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
}

##############################################################################
#
# Edit out $SLATTACH in /etc/tcp
#
update_config() {
	SLATTACH_N=`echo "$SLATTACH" | sed -e 's:/:\\\\/:g'`
	ed -s  /etc/tcp <<-EOF > /dev/null 2>&1
	/$SLATTACH_N/
	d
	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

# Figure out which kernel we're running
which_kernel

# Look for /etc/slattach line in /etc/tcp - use slip no. comment
SLATTACH=`Egrep "^[ 	]*/etc/slattach" /etc/tcp | fgrep $base`
if [ -z "$SLATTACH" ] ; then
	echo "\nError: Cannot find $base in configuration files.\n"
	cleanup $FAIL
fi

# Get PID of slattach process
if [ -n $PS ]; then
	SL_PID=`$PS -ef | grep "$SLATTACH" | awk '{print $2}'`
fi

# Kill off the process
if [ -n "$SL_PID" ]
then
       kill -9 $SL_PID >/dev/null 2>&1
fi

SLINK=`$PS -ef | grep "slink" | grep -v "grep slink"`
# Don't do this if tcp is not running
if [ "$SLINK" != "" ] ; then
	# Delete slip entry in routing table
	# Look for entry in routing table
	SL_LINE=`netstat -r | grep $base | awk '{ print $1 } { print $2 }' 2> /dev/null`
	if [ -n "$SL_LINE" ]
	then
	        route delete $SL_LINE >/dev/null 2>&1
	fi
fi

# Update /etc/tcp
update_config

# Check to see how many slip drivers are still installed
n_slip_drvs=`netconfig -s | Egrep "sl[0-9]*" | wc -l`
if [ $n_slip_drvs -eq 1 ] ; then
	# Remove the SLIP drivers
	echo "Removing slip driver ... \c"
	idinstall -d -e slip >/dev/null 2>&1
	/bin/rm -f /dev/slip
	echo "done."
	# 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
