#!/bin/sh
set -e

mkdir -p /tmp/ltmodem
TMPM=/tmp/ltmodem/tmpfile

NEWFILE=ltmodem2
# NEWFILE may be renamed NEWFILE.dpkg-back, awaiting modem drivers installation
if test -f /etc/modutils/$NEWFILE.dpkg-back ; then
  rm -f /etc/modutils/$NEWFILE.dpkg-back
fi

rmmod lt_serial ltmodem lt_modem &>/dev/null

# Device File System status check
if test ! -e /dev/.devfsd 
  then
  # the standard device node case
  if test -c /dev/ttyLT0
    then
      rm -f /dev/ttyLT0 && \
      echo "   Deleted device node    /dev/ttyLT0"
    # Check for symbolic link too
    ls -l /dev/modem 2> $TMPM
    if grep LT0 $TMPM >/dev/null
    then 
      rm -f /dev/modem && \
      echo "   Deleted symbolic link    /dev/modem"
    fi
  fi
else
  DEVFS=active
fi

# Check for update command
if test -x /usr/sbin/update-devfsd
then
  UPDATE="/usr/sbin/update-devfsd"
else
  UPDATE="killall -HUP devfsd"
fi

# Remove any support lines from 
if [ -f /etc/devfsd.conf ] ; then
  # Mandrake 8.1 type case
  TARGET=/etc/devfsd.conf
  grep -v LT0 $TARGET > "$TARGET".tmp
  mv "$TARGET".tmp $TARGET
fi
if [ -f /etc/devfs/conf.d/ltmodem2 ] ; then
  # Debian type case
  rm  /etc/devfs/conf.d/ltmodem2
fi

# If the following are not done, broken symbolic links remain 
rm -f /dev/modem  /dev/ttySLT0  &>/dev/null

if test -n "$DEVFS" ; then
  echo Updating the Device file system with:
  echo "	$UPDATE"
  $UPDATE
fi
echo
echo Lucent DSP modem device node support removed.

# for non-Debian or from tarball installations
MCFILES="/etc/conf.modules /etc/modules.conf /etc/modutils/aliases"
for FILE in $MCFILES
do
  if test -f $FILE
  then
    MCFILE=$FILE
  fi
done

if test -z "$MCFILE" 
then
  echo "Could not identify your distribution's way of automatically loading modules,"
  echo "Exiting."
  echo
  exit 1
fi

if grep lt_ $MCFILE >/dev/null
then
  echo "Removing the following Lucent DSP modem support lines from $MCFILE:"
  echo "-----------------"
  grep lt_ $MCFILE
  echo "-----------------"
else
  echo "Lucent DSP modem support lines are not present in $MCFILE"
  echo "Exiting"
  echo
  exit 1
fi

cp $MCFILE $MCFILE.ltmodem~
grep -v "alias char-major-62 *lt_serial" $MCFILE.ltmodem~ |\
grep -v "alias /dev/tts/LT0 *lt_serial" |\
grep -v "alias /dev/modem *lt_serial" |\
grep -v "alias /dev/ttySLT0 *lt_serial" |\
grep -v " syntax example" |\
grep -v "lt_drivers" | grep -v "options lt_" > $MCFILE
echo Support lines removed:
grep lt_ $MCFILE

echo
if test $MCFILE = "/etc/modutils/aliases"
then
  UPDATE_MOD="update-modules"
else
  UPDATE_MOD="depmod -a"
fi
echo Updating the modules configuration files with:
echo "  $UPDATE_MOD"

$UPDATE_MOD &>  $TMPM

echo Checking dependencies of remaining modules. 
depmod -a &> $TMPM
if grep depmod: $TMPM >/dev/null
then
  echo --------------------------     
  cat $TMPM 
  echo --------------------------
  if grep "modules." $TMPM >/dev/null 
  then
    echo Be only modestly concerned about a warning of the type:
    echo "    something .. ELF ..."
    echo Within /lib/modules/`uname -r`/ the files with names of type:
    echo `grep "cannot read ELF" $TMPM | cut -d/ -f5 | cut -d ' ' -f1` 
    echo are not themselves modules, but rather map the modules.
    echo "The ELF complaint recommends upgrade of the software package:	modutils"
    modprobe -V &> $TMPM.1
    echo While the resident version is: `grep version $TMPM.1 | cut -d' ' -f3`
    echo "	  a minimal version of 2.4.2 is recommended for 2.4.18 kernels and later." 
 
  fi
echo Do presently take corrective action.
echo
fi  
/bin/rm -r /tmp/ltmodem  


 