#! /bin/sh
# ************************************************************************
# *                                                                      *
# *   eciadsl-remove-dabusb v0.1 - dabusb destroyer script               *
#                           by FlashCode and Crevetor (c) 14/04/2002     *
# *                                                                      *
# *          For any support, contact one of us :                        *
# *           - FlashCode: flashcode@flashtux.org                        *
# *           - Crevetor : areversat@tuxfamily.org                       *
# *                                                                      *
# *          License : GPL  -  http://eciadsl.flashtux.org               *
# *                                                                      *
# ************************************************************************

# 30/12/2002 Benoit PAPILLAULT <benoit.papillault@free.fr>
#
#            Since depmod -a generate the file 
#            /lib/modules/`uname -r`/modules.usbmap and since this file is used
#            by USB hotplug, simply renaming dabusb.o do not work, the file
#            must be deleted (since it can no longer be used).
#
# 2003/10/03 If hotplug has support for blacklist, simply tell it to ignore dabusb

# <CONFIG>
BIN_DIR="/usr/local/bin"
ETC_DIR="/etc"
CONF_DIR="/etc/eciadsl"
PPPD_DIR="/etc/ppp"
VERSION=""
# </CONFIG>

if [ "$1" == "--version" -o "$1" == "-v" ]
then
	echo "$VERSION"
	exit 0
fi

if [ $UID -ne 0 ]
then
	echo "you must be root to run this script"
	exit 0
fi

lsmod | grep -q dabusb
if [ $? -eq 0 -o "$1" == "--force" ]
then
	module=$(modprobe -l | grep dabusb)
	if [ -n "$module" ]
	then
	    rm -f $module
	    if [ $? -eq 0 ]
		then
			echo -e "\n$module removed"
			depmod -a
		else
			echo -e "\n$module cannot be removed"
		fi
	else
		echo -e "\ncouldn't find a dabusb module to remove"
	fi
	module=$(lsmod | grep dabusb)
	if [ -n "$module" ]
	then
	    modprobe -r $module && \
			sleep 1
	    lsmod | grep -q dabusb && \
	        echo "dabusb successfully unloaded" || \
	        echo "couldn't unload dabusb module, unplug your modem first"
	else
		echo "dabusb module not loaded"
	fi
else
    echo "dabusb is not loaded"
fi

# check if vanilla hotplug is installed,
# and if blacklist support is enabled (>=2002)
HOTPLUG=/etc/hotplug
HOTPLUG_HAS_BLACKLIST=0
test -f $HOTPLUG/blacklist && \
	HOTPLUG_HAS_BLACKLIST=$(($HOTPLUG_HAS_BLACKLIST+1))
grep -q blacklist $HOTPLUG/hotplug.functions 2>/dev/null && \
	HOTPLUG_HAS_BLACKLIST=$(($HOTPLUG_HAS_BLACKLIST+1))
if [ $HOTPLUG_HAS_BLACKLIST -gt 0 ]
then
	grep -q "^dabusb$" $HOTPLUG/blacklist
	if [ $? -eq 0 ]
	then
		echo "dabusb already in hotplug's blacklist"
	else
		echo -e "\n#dabusb disabled: avoids conflict w/ eciadsl usermode driver\ndabusb" >> $HOTPLUG/blacklist
		echo "dabusb added to hotplug's blacklist"
	fi
else
	echo "no hotplug blacklist support detected or hotplug not installed?"
fi
