#!/bin/sh

#############################################################################
#                                                                           #
#                PURPOSE                                                    #
#                #######                                                    #
#                                                                           #
#(1)  Removes the entries made to the kernel configuration files            #
#                                                                           #
#############################################################################
ans="n"
echo ""
echo "  Deinstallation Script for Adaptec's ATM S/W   "
echo "******************CAUTION***********************"
echo
echo "This script modifies kernel configuration files. "
echo "This may lead to inconsistancy if any device is  "
echo "installed after installing Adaptec's ATM software."
echo "If you have installed any device after installing"
echo "Adaptec's ATM software , DO NOT run this script. "
echo ""
echo "  Continue Deinstallation? y/n [default n]:"

read ans
if [ -z "$ans" ]
then 
	echo Backing out deinstallation .
    exit
fi

if [ "$ans" = "n" ]
then 
	echo Backing out deinstallation .
	exit
fi

# Cleaning up conf.c
echo Cleaning up /sys/sun/conf.c .

arch=`arch -k`

if [ -f /sys/sun/conf.c -a  "`grep NAATM /sys/sun/conf.c`" ]
then
XXX=`grep -n NAATM /sys/sun/conf.c | cut -d: -f1`

from=`expr $XXX - 2`
to=`expr $XXX + 6`
head "-$from" /sys/sun/conf.c > /tmp/yyyy
tail "+$to" /sys/sun/conf.c > /tmp/zzzz
cat /tmp/yyyy /tmp/zzzz > /tmp/temp
fi


if [ -f /tmp/temp -a  "`grep NALANE /sys/sun/conf.c`" ]
then
XXX=`grep -n NALANE /tmp/temp | cut -d: -f1`

from=`expr $XXX - 2`
to=`expr $XXX + 6`
head "-$from" /tmp/temp > /tmp/yyyy
tail "+$to" /tmp/temp > /tmp/zzzz
cat /tmp/yyyy /tmp/zzzz > /tmp/temp
fi

#Remove ACIP entry from conf.c - ACC
if [ -f /tmp/temp -a  "`grep NACIP /sys/sun/conf.c`" ]
then
XXX=`grep -n NACIP /tmp/temp | cut -d: -f1`

from=`expr $XXX - 2`
to=`expr $XXX + 6`
head "-$from" /tmp/temp > /tmp/yyyy
tail "+$to" /tmp/temp > /tmp/zzzz
cat /tmp/yyyy /tmp/zzzz > /tmp/temp
fi

#Remove AARP entry from conf.c - ACC
if [ -f /tmp/temp -a  "`grep NAARP /sys/sun/conf.c`" ]
then
XXX=`grep -n NAARP /tmp/temp | cut -d: -f1`

from=`expr $XXX - 2`
to=`expr $XXX + 6`
head "-$from" /tmp/temp > /tmp/yyyy
tail "+$to" /tmp/temp > /tmp/zzzz
cat /tmp/yyyy /tmp/zzzz > /tmp/temp
fi

if [ -f /tmp/temp -a  "`grep aatmtab /sys/sun/conf.c`" ]
then
XXX=`grep -n aatmtab /tmp/temp | cut -d: -f1`

from=`expr $XXX - 4`
to=`expr $XXX + 2`
head "-$from" /tmp/temp > /tmp/yyyy
tail "+$to" /tmp/temp > /tmp/zzzz
cat /tmp/yyyy /tmp/zzzz > /tmp/temp
fi

#Remove ACIP entry from switchtable in conf.c -ACC
if [ -f /tmp/temp -a  "`grep aciptab /sys/sun/conf.c`" ]
then
XXX=`grep -n aciptab /tmp/temp | cut -d: -f1`

from=`expr $XXX - 4`
to=`expr $XXX + 2`
head "-$from" /tmp/temp > /tmp/yyyy
tail "+$to" /tmp/temp > /tmp/zzzz
cat /tmp/yyyy /tmp/zzzz > /tmp/temp
fi

#Remove ACIP entry from switchtable in conf.c -ACC
if [ -f /tmp/temp -a  "`grep aarptab /sys/sun/conf.c`" ]
then
XXX=`grep -n aarptab /tmp/temp | cut -d: -f1`

from=`expr $XXX - 4`
to=`expr $XXX + 2`
head "-$from" /tmp/temp > /tmp/yyyy
tail "+$to" /tmp/temp > /tmp/zzzz
cat /tmp/yyyy /tmp/zzzz > /tmp/temp
fi

if [ -f /tmp/temp -a  "`grep alanetab /sys/sun/conf.c`" ]
then
XXX=`grep -n alanetab /tmp/temp | cut -d: -f1`

from=`expr $XXX - 4`
to=`expr $XXX + 2`
head "-$from" /tmp/temp > /tmp/yyyy
tail "+$to" /tmp/temp > /tmp/zzzz
cat /tmp/yyyy /tmp/zzzz > /tmp/temp

mv /tmp/temp /sys/sun/conf.c
rm /tmp/yyyy /tmp/zzzz

fi


# cleaning up /sys/$arch/conf/files
echo Cleaning up /sys/"$arch"/conf/files.

if [ -f /sys/$arch/conf/files -a  "`grep aatm /sys/$arch/conf/files`" ]
then
sed "/aatm/D" /sys/$arch/conf/files > /tmp/temp
fi


if [ -f /tmp/temp -a  "`grep alane /sys/$arch/conf/files`" ]
then
sed "/alane/D" /tmp/temp > /sys/$arch/conf/files
fi

#Clean up ACIP entry from conf/files - ACC
if [ -f /sys/$arch/conf/files -a  "`grep acip /sys/$arch/conf/files`" ]
then
sed "/acip/D" /sys/$arch/conf/files > /tmp/temp
fi

#Clean up AARP entry from conf/files - ACC
if [ -f /tmp/temp -a  "`grep aarp /sys/$arch/conf/files`" ]
then
sed "/aarp/D" /tmp/temp > /sys/$arch/conf/files
fi

# Cleaning up Kernel configuration file  

while [ 1 ]
do
echo -n "Enter the present kernel configuration file :"
read config

if [  -f "/sys/$arch/conf/$config" ]
then
echo Cleaning up /sys/"$arch"/conf/"$config".

	sed "/aatm/D" /sys/$arch/conf/$config > /tmp/temp     
	sed "/alane/D" /tmp/temp > /sys/$arch/conf/$config
#Remove acip and aarp entries too. -ACC
	sed "/acip/D" /sys/$arch/conf/$config > /tmp/temp     
	sed "/aarp/D" /tmp/temp > /sys/$arch/conf/$config
	break
else
	echo -n "No such file"
	echo 
fi
done

rm /tmp/temp

#Cleaning up rc.local

cp /etc/rc.local /tmp/temp

if [ -f /etc/rc.local -a  "`grep \"Not Starting Adaptec AATM NIC.\" /etc/rc.local`" ]
then
XXX=`grep -n "Not Starting Adaptec AATM NIC." /etc/rc.local | cut -d: -f1`
from=`expr $XXX - 5`
to=`expr $XXX + 6`
head "-$from" /etc/rc.local > /tmp/yyyy
tail "+$to" /etc/rc.local > /tmp/zzzz
cat /tmp/yyyy /tmp/zzzz > /tmp/temp
fi


if [ -f /tmp/temp -a  "`grep \"Not Starting Adaptec ATM LAN Emulation.\" /tmp/temp`" ]
then
XXX=`grep -n "Not Starting Adaptec ATM LAN Emulation." /tmp/temp | cut -d: -f1`
from=`expr $XXX - 4`
to=`expr $XXX + 6`
head "-$from" /tmp/temp > /tmp/yyyy
tail "+$to" /tmp/temp > /tmp/zzzz
cat /tmp/yyyy /tmp/zzzz > /tmp/temp
fi

if [ -f /tmp/temp -a  "`grep \"Not Starting Adaptec ATM SNMP Agent.\" /tmp/temp`" ]
then
XXX=`grep -n "Not Starting Adaptec ATM SNMP Agent." /tmp/temp | cut -d: -f1`
from=`expr $XXX - 4`
to=`expr $XXX + 6`
head "-$from" /tmp/temp > /tmp/yyyy
tail "+$to" /tmp/temp > /tmp/zzzz
cat /tmp/yyyy /tmp/zzzz > /tmp/temp
fi

#remove AARP entry from rc.local
if [ -f /tmp/temp -a  "`grep \"Not Starting Adaptec ATM ARP Server.\" /tmp/temp`" ]
then
XXX=`grep -n "Not Starting Adaptec ATM ARP Server." /tmp/temp | cut -d: -f1`
from=`expr $XXX - 4`
to=`expr $XXX + 6`
head "-$from" /tmp/temp > /tmp/yyyy
tail "+$to" /tmp/temp > /tmp/zzzz
cat /tmp/yyyy /tmp/zzzz > /tmp/temp
fi

#remove ACIP entry from rc.local
if [ -f /tmp/temp -a  "`grep \"Not Starting Adaptec ATM Classical IP.\" /tmp/temp`" ]
then
XXX=`grep -n "Not Starting Adaptec ATM Classical IP." /tmp/temp | cut -d: -f1`
from=`expr $XXX - 4`
to=`expr $XXX + 6`
head "-$from" /tmp/temp > /tmp/yyyy
tail "+$to" /tmp/temp > /tmp/zzzz
cat /tmp/yyyy /tmp/zzzz > /tmp/temp
fi

mv /tmp/temp /etc/rc.local

# Removing the device nodes created

rm /dev/alane > /dev/null 2>&1
rm /dev/aatm > /dev/null  2>&1

#remove device nodes for acip and aarp too.-ACC
rm /dev/acip > /dev/null 2>&1
rm /dev/aarp > /dev/null  2>&1

#Removing object files .
echo Cleaning up /sys/$arch/OBJ

if [ -f /sys/$arch/OBJ/aatm.o ]
then
	rm /sys/$arch/OBJ/aatm.o
fi
if [ -f /sys/$arch/OBJ/alane.o ]
then
	rm /sys/$arch/OBJ/alane.o
fi
if [ -f /sys/$arch/OBJ/acip.o ]
then
	rm /sys/$arch/OBJ/acip.o
fi
if [ -f /sys/$arch/OBJ/aarp.o ]
then
	rm /sys/$arch/OBJ/aarp.o
fi

echo
echo "The entries of the devices added by Adaptec have been removed."
echo "Important -- If you have added any more devices after installing"
echo "             Adaptec software, please make appropriate changes"
echo "             to the kernel configuration files and then rebuild"
echo "             the kernel ."
#echo "             However if the last devices installed were Adaptec's"
#echo "             there should be no problem in installing any more devices"

#Following script was added for rebuilding the kernel once
#Adaptec's AATM software has been de-installed -ACC

build_ans="n"
echo ""
echo "To remove Adaptec's ATM drivers from the system"
echo "you should rebuild the kernel with old configuration"
echo "file (ie. configuration before AATM)."
echo "Do you wish to rebuild the kernel now ?y/n [default=n]:"

read build_ans

if [ -z "$build_ans" ]
then 
    echo "Not building the kernel."
	exit
fi
if [ "$build_ans" = "n" ]
then
	echo "Not building the kernel."
	exit
fi

#Rebuild the kernel.
while [ 1 ]
do
echo
echo -n "Enter the old kernel configuration file [GENERIC]:"
read configfile

if [ -z "$configfile" ]
then
	configfile="GENERIC"
fi
 
if [  -f "/sys/$arch/conf/$configfile" ]
then
	cd /sys/$arch/conf
	/usr/etc/config $configfile
	(cd /sys/$arch/$configfile ;
	/bin/make )
	if [ "$?" != "0" ]
	then
			echo "Failed Making Kernel !!!"
			exit 1
	fi
while [ 1 ]
do
	echo
	echo "Moving /sys/$arch/$configfile/vmunix to /vmunix"
	echo "The current kernel /vmunix will be saved as /vmunix.old"
	echo -n "[yes, no] "
	read ans
	if [ "$ans" = "yes" ]
	then
		echo
		echo "Saving current kernel /vmunix as /vmunix.old"
		if [ -f /vmunix.old ]
		then
echo "File /vmunix.old exists, Please enter another file name, or Press return"
echo "to over write /vmunix.old."
			read oldvmunix
			if [ -z "$oldvmunix" ]
			then
				oldvmunix="/vmunix.old"
			fi
		fi
		mv /vmunix /$oldvmunix
		mv /sys/$arch/$configfile/vmunix /
		echo "Please reboot the system using \"reboot\" or \"shutdown\" command"
		break
	elif [ "$ans" = "no" ]
	then
		echo
		echo "To bringup $configfile, Save /vmunix to /vmunix.old"
		echo "copy /sys/$arch/$configfile/vmunix to /vmunix "
		echo "and then reboot the system"
		echo
		break
	else
		echo
		echo "Please answer \"yes\" or \"no\"."
		echo
	fi
done
    break
else
    echo -n "No such file"
fi
done

