#!/bin/sh
# $Id: unloadem_linux.sh,v 1.3 2002/11/26 22:37:54 rwb Exp $ 
# SCCSID(@(#)unloadem_linux.sh	1.20) Modified 09:07:35 8/3/01
# Unload the Win4Lin drivers.

# Get the STATICMERGE setting.
SSV_OPTION=noexit
. /etc/mrgssv.sh

cd $STATICMERGE/drivers

# Stop any sessions so that the drivers can be unloaded
killall -15 dosexec > /dev/null 2>&1

# Stop the VNET demon so that vnet drivers can be unloaded
killall -9 vnetd vnetd-2.2 vnetd-2.4 vnetdbg 2>&1 |grep -v 'no process killed'

sleep 1

DEPROCS="`ps -eo comm 2>/dev/null | egrep 'dosexec|vnetd|auserver|xcrt'`"
# Give a bit of time to settle
if [ -n "$DEPROCS" ]
then
	sleep 3
fi

ALL_OK=yes
# Unload the Mmerge driver last
LIST=`ls M*.o | sed -e 's/Mmerge.o//' -e s/Mvnet.*//`
if [ -e Mvnetd.o ]
then
	VNET="Mvnetd.o Mvnet.o Mvnetint.o"
fi
for i in $VNET $LIST Mmerge.o
do
	DN=`echo $i | cut -d. -f1`
	grep "^$DN " /proc/modules > /dev/null 2>&1
	if [ $? -ne 0 ]
	then
		# This driver is not loaded, so try the next one.
		continue
	fi
	if [ -x /sbin/rmmod_ver ]
	then
		/sbin/rmmod_ver `echo $i | cut -f1 -d.`
	else
		rmmod `echo $i | cut -f1 -d.`
	fi
	if [ $? -ne 0 ]
	then
		ALL_OK=no
		echo "Unable to unload $MERGENAME driver $i."
		if [ ! -x /sbin/rmmod_ver ]
		then
		    echo "NOTE: This failure happened because Win4Lin was NOT"
		    echo "installed according to the instructions, and thus"
		    echo "'/sbin/rmmod_ver' (and '/sbin/insmod_ver') did not"
		    echo "get installed from the installation CD."
		    echo "This is needed because the standard 'insmod' and"
		    echo "'rmmod' commands are insufficient to load and unload"
		    echo "the Win4Lin modules on this system type."
		fi
	fi
done
rmmod mki-adapter >/dev/null 2>&1
if [ "$ALL_OK" = "yes" ]
then
	sh "$STATICMERGE"/drivers/tools/rmnode_linux.sh >/dev/null 2>&1
	exit 0
fi
exit 1
