#!/bin/sh
echo
## Check permissions
if [ $UID -ne 0 ]; then
   echo
   echo "Drivers and modules can only be installed with Root permssion!"
   echo "Please log in as root/superuser and rerun $0"
   echo 
   exit 2
fi

echo These script shuts down support for Ethernet and Pcmcia and unloads Non-essential modules.
echo This is a pre-cautionary step when resource conflicts are suspected.
echo The script is obvious and can be easily modified should you choose to do so.
echo
echo Should you wish to avoid shutdown actions,
echo "	Abort now with:	 Ctrl-C"
read -p "	or to continue:	Enter"
echo "========================================================"
echo 

RECORD=Unload.txt
date > $RECORD
ETH=`ifconfig | grep eth | cut -d' ' -f1`
if test -n "$ETH" ; then
   echo There is an "$ETH", shutting it down
   echo ETH=$ETH >> $RECORD
   ifconfig $ETH down > /dev/null
else
   echo Ethernet not detected.
fi
echo

MODULES=`lsmod |  cut -d' ' -f1 | grep -v Module`
# just captures the module list compactly
echo The initial modules list is:
echo $MODULES
echo MODULES_INITIAL=$MODULES >> $RECORD
echo
echo Now attempting removals sequentially with:	modprobe -r ModuleName
MODTMP=$BASE/lsmod.tmp
echo $MODULES > $MODTMP 
# MODTMP == source/lsmod.tmp   needed to support below usage of:  grep
for MODULE in $MODULES
do 
   if grep $MODULE $MODTMP >/dev/null
     then
     modprobe -r $MODULE
     lsmod |  cut -d' ' -f1 | grep -v Module > $MODTMP
     # which regenerates the shortened useful list
   fi
done
# with 1st round of removals which might have included some PCMCIA dependent drivers
echo
echo MODULE_LATTER=`cat $MODTMP`
echo MODULE_LATTER=`cat $MODTMP` >> $RECORD

# Now PCMCIA
if grep pcmcia $MODTMP >/dev/null
  then
  echo Now stopping any pcmcia support with:
  echo "	/etc/init.d/pcmcia stop"
  /etc/init.d/pcmcia stop
  echo
  echo Rerunning module removals.
  MODULES=`lsmod |  cut -d' ' -f1 | grep -v Module`
  echo The residual modules list is:
  echo $MODULES
  echo $MODULES > $MODTMP 
  for MODULE in $MODULES
  do 
    if grep $MODULE $MODTMP >/dev/null
    then
    modprobe -r $MODULE
    lsmod |  cut -d' ' -f1 | grep -v Module > $MODTMP
    fi
  done
fi

echo After removals there remain:
MODULES_FINAL=`lsmod |  cut -d' ' -f1 | grep -v Module` 
echo $MODULES_FINAL
echo MODULES_FINAL=$MODULES_FINAL >> $RECORD
echo
echo For an actions summary, here is $RECORD.
echo
cat  $RECORD
echo
echo Bye
echo

