#!/bin/bash
# $Id: uninstOld,v 1.16 2005/01/30 01:01:20 Tux Exp $

# Copyright (C) 2003-2005 Olivier Borowski
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# Goal :
#	delete files and settings used by previous versions of this driver
# Params:
#	none

# 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=/lib/modules/`uname -r`
USV=/usr/src/linux-`uname -r`
DISTRIB="generic"
if test -f /etc/mandrake-release || test -f /etc/mandrakelinux-release ; then
	DISTRIB="Mandrake"
elif test -f /etc/slackware-version ; then
	DISTRIB="Slackware"
fi
BUILD_MODULE=1
PPP_OPTIONS_DIR=/etc/ppp/peers
RUN_DEPMOD=0

# === 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" ] ; then
	rpm -e adiusbadsl 2>/dev/null
	rpm -e eagle-usb 2>/dev/null
	if [ -f $LMV/kernel/drivers/usb/adiusbadsl.o ] ; then
		rm -f $LMV/kernel/drivers/usb/adiusbadsl.o
		RUN_DEPMOD=1
	fi
	if [ -d $LMV/kernel/3rdparty/adiusbadsl ] ; then
		rm -Rf $LMV/kernel/3rdparty/adiusbadsl
		RUN_DEPMOD=1
	fi
	if [ -d $LMV/kernel/3rdparty/eagle-usb ] ; then
		rm -Rf $LMV/kernel/3rdparty/eagle-usb
		RUN_DEPMOD=1
	fi
	# remove old /etc/ppp/peers/adsl (Mdk < 10)
	if [ -f $PPP_OPTIONS_DIR/adsl ] ; then
		if grep -q adictrl $PPP_OPTIONS_DIR/adsl ; then
			rm -f $PPP_OPTIONS_DIR/adsl
		fi
	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
if [ -f $LMV/usb/adiusbadsl.o ] ; then
	rm -f $LMV/usb/adiusbadsl.o
	RUN_DEPMOD=1
fi
if [ -f $LMV/kernel/drivers/usb/adiusbadsl.o ] ; then
	rm -f $LMV/kernel/drivers/usb/adiusbadsl.o
	RUN_DEPMOD=1
fi
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 &&
	rm -f $DIR/startmire
done

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

# === kernel module ===
# the module is unloaded after it was removed from the disk
# why? on some distribs, rmmod will fail but the user only
# has to reboot his computer to solve this problem
if [ $BUILD_MODULE -eq 1 ] ; then
	# sync disk in case rmmod crash the kernel
	sync
	# unload old driver
	if lsmod | grep -q "adiusbadsl" ; then
		rmmod adiusbadsl
		sleep 1
		if lsmod | grep -q "adiusbadsl" ; then
			doInUtf8 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 -eq 1 ; then
				doInUtf8 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
					doInUtf8 echo -e $CANT_UNLOAD_EU_MSG
					exit 1
				fi
			fi
		fi
	fi
fi

if [ $RUN_DEPMOD -eq 1 ] ; then
	depmod -a
fi

#***************************************************************************
# $Log: uninstOld,v $
# Revision 1.16  2005/01/30 01:01:20  Tux
# - run "depmod" when neccessary
#
# Revision 1.15  2005/01/16 22:08:17  Tux
# - add license header
#
# Revision 1.14  2005/01/04 21:14:05  Tux
# - switch strings to utf-8
#
# Revision 1.13  2004/11/21 15:32:30  Tux
# - replaced == with -eq
#
# Revision 1.12  2004/11/17 20:58:01  Tux
# - uninstOld should not require Makefile.common
#
# Revision 1.11  2004/10/10 13:54:47  Tux
# - remove startmire
#
# 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
#
#***************************************************************************/
