#!/bin/bash
# $Id: uninstOld,v 1.10 2004/07/25 20:19:09 Tux Exp $
# Name: uninstOld
# Goal: delete files and settings used by previous versions of this driver
# Author: Tux
# Params:
#	$1 = module path (ex: /lib/modules/`uname -r`)
#	$2 = kernel source path (ex: /usr/src/linux-`uname -r`)
#	$3 = distrib name (ex: Mandrake)
#	$4 = distrib version (ex: 9.2)
#	$5 = rmmod eagle-usb module? (0/1)

if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ] || [ -z "$5" ] ; then
	echo "syntax: uninstOld \$MODULESDIR \$KERNELSRCDIR \$DISTRIB \$DISTVER \$BUILD_MODULE"
	exit 1
fi

# Messages:
CANT_UNLOAD_ADI_MSG="\nadiusbadsl module has been suppressed but it can't be unloaded.\nYou could reboot your computer to do that.\nExiting...\n"
CANT_UNLOAD_EU_MSG="\nCan't unload eagle-usb module.\nYou could reboot your computer to do that.\nExiting...\n"
UNPLUG_MSG="\n\nPlease unplug your modem and press 'Enter'"

# Paths:
LMV=$1
USV=$2
DISTRIB=$3
DISTVER=$4
BUILD_MODULE=$5

# === distribution specific ===
if [ "$DISTRIB" = "Slackware" ] && grep -q "eagle-usb" /etc/rc.d/rc.local ; then
	# remove autostart service
	if [ ! -e "/etc/rc.d/rc.local.old" ] ; then
		cp /etc/rc.d/rc.local /etc/rc.d/rc.local.old
	fi
	grep -v "eagle-usb" /etc/rc.d/rc.local > /etc/rc.d/rc.local.tmp
	mv -f /etc/rc.d/rc.local.tmp /etc/rc.d/rc.local
fi
if [ "$DISTRIB" = "Mandrake" ] && [ `expr $DISTVER = 9.1` == 1 ] ; then
	# 1.0.2cc5
	rpm -e adiusbadsl 2>/dev/null
	rm -f $LMV/kernel/drivers/usb/adiusbadsl.o
fi
if [ "$DISTRIB" = "Mandrake" ] && [ `expr $DISTVER = 9.2` == 1 ] ; then
	# 1.0.4
	rpm -e adiusbadsl 2>/dev/null
	rm -Rf $LMV/kernel/3rdparty/adiusbadsl
fi
if [ "$DISTRIB" = "Mandrake" ] && [ `expr $DISTVER = 10` == 1 ] ; then
	# 1.9.3+?
	rpm -e adiusbadsl 2>/dev/null
	rpm -e eagle-usb 2>/dev/null
	rm -Rf $LMV/kernel/3rdparty/adiusbadsl
	rm -Rf $LMV/kernel/3rdparty/eagle-usb
fi
if [ "$DISTRIB" = "Mandrake" ] && [ `expr $DISTVER = 10.1` == 1 ] ; then
	# 1.9.5+
	rpm -e eagle-usb 2>/dev/null
	rm -Rf $LMV/kernel/3rdparty/eagle-usb
fi
# remove old Mandrake's /etc/ppp/peers/adsl (Mdk < 10)
if [ -f /etc/ppp/peers/adsl ] ; then
	if grep -q adictrl /etc/ppp/peers/adsl ; then
		rm -f /etc/ppp/peers/adsl
	fi
fi
# remove old testconnec entries in crontab
if [ -f /etc/crontab ] && grep -q testconnec /etc/crontab ; then
	grep -v testconnec /etc/crontab > /etc/crontab.temp
	if [ ! -e /etc/crontab.old ] ; then
		mv -f /etc/crontab /etc/crontab.old
	fi
	mv -f /etc/crontab.temp /etc/crontab
fi

# === < 1.0.4e ===
if [ -f /etc/inittab ] && grep -q adsl.inittab /etc/inittab ; then
	grep -v adsl.inittab /etc/inittab > /etc/inittab.temp
	if [ ! -e /etc/inittab.old ] ; then
		mv -f /etc/inittab /etc/inittab.old
	fi
	mv -f /etc/inittab.temp /etc/inittab
	RES="`whereis telinit | cut -d ':' -f2`"
	if [ ! -z "$RES" ] ; then
		telinit Q
	fi
fi
if [ -f /etc/hotplug/usb.usermap.local ] ; then
	grep -v adiusb /etc/hotplug/usb.usermap.local > /etc/hotplug/usb.usermap.local.temp &&
	mv -f /etc/hotplug/usb.usermap.local.temp /etc/hotplug/usb.usermap.local
fi
if [ -f /var/lock/subsys/adiusbadsl ] && [ -f /etc/init.d/adiusbadsl ] ; then
	/etc/init.d/adiusbadsl stop
fi
rm -f $LMV/usb/adiusbadsl.o &&
rm -f $LMV/kernel/drivers/usb/adiusbadsl.o &&
rm -f /usr/sbin/adsl.inittab &&
rm -f /etc/rc.d/init.d/adiusbadsl &&
rm -f /etc/rc.d/rc0.d/*adiusbadsl &&
rm -f /etc/rc.d/rc1.d/*adiusbadsl &&
rm -f /etc/rc.d/rc2.d/*adiusbadsl &&
rm -f /etc/rc.d/rc3.d/*adiusbadsl &&
rm -f /etc/rc.d/rc4.d/*adiusbadsl &&
rm -f /etc/rc.d/rc5.d/*adiusbadsl &&
rm -f /etc/rc.d/rc6.d/*adiusbadsl
rm -f /var/run/adiusbadsl

# === 1.0.4e ===
rm -Rf /etc/analog &&
rm -f /usr/sbin/startadsl &&
rm -f /usr/sbin/stopadsl &&
rm -f /usr/sbin/startmire &&
rm -f /usr/sbin/eagleConfig &&
rm -Rf /usr/lib/hotplug/adiusbadsl &&
rm -f /etc/hotplug/usb/adiusbdsp &&
rm -f /etc/ppp/options.adsl &&
rm -f /etc/ppp/options.mire &&
rm -f /etc/eagleusb.conf

# === 1.9.0 => 1.9.6 ===
rm -f /etc/eagle-usb/dsp_code_*.bin &&
rm -f /etc/eagle-usb/tmp

# === 1.9.x on /usr/sbin ===
for DIR in /usr/sbin /usr/local/sbin ; do
	rm -f $DIR/eagleconfig &&
	rm -f $DIR/eaglectrl &&
	rm -f $DIR/eaglediag &&
	rm -f $DIR/eaglestat &&
	rm -f $DIR/fctStartAdsl &&
	rm -f $DIR/fctStopAdsl &&
	rm -f $DIR/pppoa
done

# === hotplug ===
update-usb.usermap &>/dev/null

# === kernel module ===
if [ $BUILD_MODULE == 1 ] ; then
	# unload old driver
	if lsmod | grep -q "adiusbadsl" ; then
		rmmod adiusbadsl
		sleep 1
		if lsmod | grep -q "adiusbadsl" ; then
			echo -e $CANT_UNLOAD_ADI_MSG
			exit 1
		fi
	fi
	# unload current driver
	if lsmod | sed s/_/-/g | grep -q "eagle-usb" ; then
		# module name depends on kernel and distrib versions
		rmmod eagle-usb ; sleep 1
		rmmod eagle_usb ; sleep 1
		if lsmod | sed s/_/-/g | grep -q "eagle-usb" ; then
			# ask the user for unplugging the modem
			if test $BUILD_MODULE == 1 ; then
				echo -e $UNPLUG_MSG
				read DUMMY
				rmmod eagle-usb ; sleep 1
				rmmod eagle_usb ; sleep 1
				if lsmod | sed s/_/-/g | grep -q "eagle-usb" ; then
					echo -e $CANT_UNLOAD_EU_MSG
					exit 1
				fi
			fi
		fi
	fi
fi

#***************************************************************************
# $Log: uninstOld,v $
# Revision 1.10  2004/07/25 20:19:09  Tux
# - now remove files in /usr and /usr/local
#
# Revision 1.9  2004/07/12 21:21:03  Tux
# - some distribs require underscore instead of minus in "rmmod eagle-usb"
#
# Revision 1.8  2004/07/02 19:11:38  Tux
# - uninstall now remove Slackware autostart service too
#
# Revision 1.7  2004/05/30 01:56:19  Tux
# - added update-usb.usermap
#
# Revision 1.6  2004/05/11 19:45:01  Tux
# - support for Mandrake cooker
#
# Revision 1.5  2004/04/03 16:18:46  Tux
# - eagle-usb rpm uninstall (including Mdk10 packages)
#
# Revision 1.4  2004/03/22 21:26:52  Tux
# - remove DSP found on old locations (ex: /etc/eagle-usb/)
# - ask to unplug modem when necessary
#
#***************************************************************************/
