#!/bin/bash
# Installs modem into the modules search path and sets permisssions

echo
SYS=`uname -r`
RECORD=BLDrecord.txt
BN=`basename $0`
echo  >> $RECORD
echo "#===== $BN section =====" >> $RECORD

## 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 1
fi

DEPMOD_PATH="/bin /usr/bin /usr/sbin /sbin"
for i in $DEPMOD_PATH
do
  if test -x $i/depmod
  then
    DEPMOD=$i/depmod  >> $RECORD
    echo Using $DEPMOD
  fi
done
if test -z "$DEPMOD" ; then
  echo
  echo "A search for the utility depmod Failed! "
  echo "For some Linux distributions, running   $BN   as Root may be necessary" 
  echo
  exit 2
fi

cd source
. SETTINGS

#  Dependency check of all modules before running install
TMPM=depmod_all.txt
$DEPMOD -a &> $TMPM
if grep depmod: $TMPM >/dev/null ; then
  echo DEPMOD_ALL=failed >> ../$RECORD
  grep -v lt_ $TMPM > $TMPM.a
  # The -v excludes old and possible defective lt_drivers from the following  analysis
  if grep depmod: $TMPM.a >/dev/null
  then
    echo Exclusive of the lt_drivers, unmet dependencies are revealed by the test:
    echo "	$DEPMOD -a"
    echo -------------------
    cat $TMPM.a
    echo ------------------
    if grep "modules." $TMPM.a  >/dev/null ; then
      cat $TMPM.a >> ../$RECORD
      echo "	This failure is not by itself lethal" >> ../$RECORD
      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 ELF $TMPM.a | 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 &> /tmp/vrs.txt
      echo While the resident version is: `grep version /tmp/vrs.txt | cut -d' ' -f3`
      rm /tmp/vrs.txt
      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
    sleep 5
  fi
fi    

echo
echo >> ../$RECORD
echo  Proceeding with the modem drivers installation:
make install | grep install > ltinst.tmp
grep m644 ltinst.tmp
LT_MODEM=`grep lt_modem ltinst.tmp | cut -d' ' -f9`
LT_SERIAL=`grep lt_serial ltinst.tmp | cut -d' ' -f9`
echo LT_MODEM=$LT_MODEM  >>  ../$RECORD
echo LT_SERIAL=$LT_SERIAL  >>  ../$RECORD
echo >> ../$RECORD
cd ..
echo 
echo The modem drivers are now installed in the standard modules path:
echo "  $LT_MODEM"
echo "  $LT_SERIAL"
echo with ownership by root.
echo 
if [ -f /lib/modules/$SYS/kernel/drivers/char/$LT_PROPRIETARY_MODULE.o ] ; then
  OLD_FOLD=/lib/modules/$SYS/kernel/drivers/char/
  echo "The new folder for ltmodem drivers is:/lib/modules/$SYS/lt_modem/"
  echo "The ltmodem drivers previously placed in:	 $OLD_FOLD"
  echo are being removed, unless there is a Ctrl-C abort.
  echo This removal is strongly recommended.
  read -p "	To continue the recommended removal:	Enter"
  rm -f $OLD_FOLD/$LT_PROPRIETARY_MODULE.* 
  rm -f $OLD_FOLD/$LT_SERIAL_MODULE.* 
  echo
fi

# Second dependecies check
echo "Informing the System, including a check of dependencies with:"
echo "	$DEPMOD -a"
$DEPMOD -a &> $TMPM
grep lt_ $TMPM > $TMPM.LT
if grep depmod $TMPM.LT >/dev/null
    then
    echo DEPMOD_LT_NEW=failed >> $RECORD
    echo --------------------
    grep lt_ $TMPM.LT
    echo --------------------
    cat  $TMPM.LT >> $RECORD
    echo An imperfect FLAVOR matching between the current `uname -r` kernel
    echo and the kernel-headers used in compiling is responsible.
    echo With DOCs/ , read Flavor.txt and Compile_Properly.txt
    #       Just sending the message into $RECORD
    echo An imperfect FLAVOR matching between the current`uname -r` kernel >>  $RECORD
    echo and the kernel-headers used in compiling is responsible. >>  $RECORD
    echo With DOCs/ , read Flavor.txt and Compile_Properly.txt >>  $RECORD
    echo
    echo Then re-compile the drivers to correct this defect.
    echo If not corrected, this Warning will recur in the next step.
    echo =========================================================
    echo
    sleep 5
    echo Finishing now.
else
    echo The modem drivers passed the dependency test.
    echo DEPMOD_LT_NEW=passed >> $RECORD
fi

echo
echo "The drivers can be removed from the modules search PATH with:"
echo "	 ./ltuninst2"
echo >> $RECORD
echo "The drivers can be removed from the modules search PATH with:	 ./ltuninst2" >> $RECORD

echo
echo "The next installation command is:	./autoload"
if test  -e /dev/.devfsd ; then
  echo "Autoload provides for dynamic creation of modem device nodes under the device file system."
else
  echo Under ./autoload the modem device node is created. 
fi
echo and autoloading of the modem drivers through amendments to /etc/modules.conf
echo "So unless you are a Linux expert, it is strongly recommended to next run:"
echo "	./autoload "
echo 

rm $TMPM* &> /dev/null