#!/bin/bash
# Name: uninstOld
# Goal: delete files and settings used by previous versions of this driver
# Author: Tux
# Version: 1.9.3
# Params:
#	$1 = module path (ex: /lib/modules/`uname -r`)
#	$2 = kernel source path (ex: /usr/src/linux-`uname -r`)

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

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

# === distribution specific ===
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.0.4?
	rpm -e adiusbadsl 2>/dev/null
	rm -Rf $LMV/kernel/3rdparty/adiusbadsl
fi
# remove Mandrake /etc/ppp/peers/adsl
if [ -f /etc/ppp/peers/adsl ] ; then
	if grep -q adictrl /etc/ppp/peers/adsl ; then
		rm -f /etc/ppp/peers/adsl
	fi
fi

# === < 1.0.4e ===
if 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 ] && [ -e /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/eagle-usb &&
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

# === old+CVS ===
# unload old driver
if lsmod | grep -q "adiusbadsl" ; then
	rmmod adiusbadsl
	sleep 1
	if lsmod | grep -q "adiusbadsl" ; then
		echo -e "\nadiusbadsl module has been suppressed but it can't be unloaded.\nYou could reboot your computer to do that.\nExiting...\n"
		exit 1
	fi
fi
# unload current driver
if lsmod | sed s/_/-/g | grep -q "eagle-usb" ; then
	rmmod eagle-usb
	sleep 1
	if lsmod | sed s/_/-/g | grep -q "eagle-usb" ; then
		echo -e "\nCan't unload eagle-usb module.\nYou could reboot your computer to do that.\nExiting...\n"
		exit 1
	fi
fi


# one of these two lines should be removed when Makefile-2.4 will properly uninstall the module
#rm -f $LMV/kernel/misc/eagle-usb.o
#rm -f $LMV/misc/eagle-usb.o
#cat $LMV/modules.usbmap | grep -v adiusb > $LMV/modules.usbmap
#rm -Rf $LMV/kernel/drivers/usb/adiusbadsl
#rm -Rf $USV/drivers/usb/adiusbadsl
#grep -v "options adiusbadsl" /etc/modules.conf > /etc/modules.conf.temp
#if [ ! -f /etc/modules.conf.old ] ; then
#	mv /etc/modules.conf /etc/modules.conf.old
#fi
#mv -f /etc/modules.conf.temp /etc/modules.conf
