#!/bin/bash
# $Id: uninstOld,v 1.3 2004/02/21 01:25:02 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"

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

# === 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.9.3+?
	rpm -e adiusbadsl 2>/dev/null
	rm -Rf $LMV/kernel/3rdparty/adiusbadsl
	rm -Rf $LMV/kernel/3rdparty/eagle-usb
fi
# remove 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

# === old+CVS ===
# 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 [ $BUILD_MODULE == 1 ] ; then \
	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 $CANT_UNLOAD_EU_MSG
			exit 1
		fi
	fi
fi
