#!/bin/sh
# $Id: unloadem_linux.sh,v 1.9 2004/01/01 18:33:59 lreiter 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

LOGFILE="$VARMERGE"/log/unloadem.log
rm -f "$LOGFILE"
exec >$LOGFILE 2>&1

if [ -f "$VARMERGE/MERGE_CHROOT" ]
then
	echo "Unloadem found MERGE_CHROOT, doing nothing"
	exit
fi

cd $STATICMERGE/drivers

# Stop any sessions so that the drivers can be unloaded
( set -x ; killall -15 dosexec 2>/dev/null )

# Stop the mergevncd daemon, if running
if [ -f /var/run/mergevncd.pid ]
then
	MERGEVNCD_PID=`cat /var/run/mergevncd.pid`
	(set -x ; kill -15 $MERGEVNCD_PID 2>/dev/null)
else
	MERGEVNCD_PID=""
fi

# Stop the cdahubd daemon gracefully
killall -15 cdahubd 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

for i in 1 2 3; do
	DEPROCS="`ps -eo comm 2>/dev/null \
| egrep 'dosexec|vnetd|auserver|xcrt|mergevncd|cdahubd'`"
	# Give a bit of time to settle
	if [ -n "$DEPROCS" ]
	then
		sleep 1
	fi
done
if [ "$MERGEVNCD_PID" != "" ]
then
	(set -x ; kill -9 $MERGEVNCD_PID 2>/dev/null)
	rm -f /var/run/mergevncd.pid	# just in case mergevncd doesn't
fi

killall -9 cdahubd 2>&1 |grep -v 'no process killed'

ALL_OK=yes

UNLOAD_LINUX24_MODULES()
{
    # 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

	MODNAME="`echo $i | cut -f1 -d.`"
	if [ -x /sbin/rmmod_ver ]
	then
	    (set -x ; /sbin/rmmod_ver "$MODNAME" )
	else
	    (set -x ; rmmod "$MODNAME" )
	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
    (set -x ; rmmod mki-adapter )
}

UNLOAD_LINUX26_MODULES()
{
    (set -x ; rmmod Win4Lin )
    if [ $? -ne 0 ]
    then
	ALL_OK=no
	echo "Unable to unload $MERGENAME driver Win4Lin"
    fi
    (set -x ; rmmod mki-adapter )
}

LINUX_VER="`uname -r`"
case "$LINUX_VER" in
  2.4*)
    UNLOAD_LINUX24_MODULES
    ;;
  2.6*)
    UNLOAD_LINUX26_MODULES
    ;;
  *)
    echo "Unknown Linux version $LINUX_VER"
    ALL_OK=no
    ;;
esac

if [ "$ALL_OK" = "yes" ]
then
	( set -x ; sh "$STATICMERGE"/drivers/tools/rmnode_linux.sh )
	exit 0
fi
exit 1
