#!/bin/bash
# 
## 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

RECORD=`pwd`/BLDrecord.txt

MSG="If you cannot resolve the problems, please send $RECORD to discuss@linmodems.org"
echo " " >> $RECORD
echo "#===== autoload section=======" >> $RECORD

# Device File System status check
if test  -e /dev/.devfsd ; then
 DEVFS=active
else
 DEVFS=
fi
echo DEVFS=$DEVFS >> $RECORD
echo

# Check for other /dev/modem links
if [ -L /dev/modem ] ; then
  MODEM=`ls -l /dev/modem | cut -d'>' -f2- | cut -d" " -f2-`
  if test -n "$DEVFS" ; then
     # Necessary for devfs file system nomenclature
     MODEM=/dev/$MODEM
  fi
  echo MODEM=$MODEM >> $RECORD
  echo DEV_MODEM_LINK_ORIG=$MODEM >> $RECORD
  echo "The modem symbolic link is: /dev/modem -> $MODEM"
  echo $MODEM > /tmp/modem.txt
  if ! grep LT0  /tmp/modem.txt >/dev/null  ; then
     echo   "This will conflict with support for the Lucent modem."
     echo   "Please : rm -f /dev/modem"
     echo   "	and  assign another symbolic link to $MODEM, such as:" 
     echo   "    ln -s $MODEM /dev/modem2"
     echo   "Then rerun: $0"
     echo   "Aborting on this trial."
     echo  $MSG >> $RECORD
     echo
     exit 1
  else
     echo The current modem port is: $MODEM
     rm /tmp/modem.txt &> /dev/null
  fi
else
  echo DEV_MODEM_LINK_ORIG= >> $RECORD
fi
# Above block should be worked out BETTER for multi-modem situation

# Identify the install ENVIRironment
if [ "`basename $0`" = "autoload" ] || [ "`basename $0`" = "Autoload" ] ; then
  #                  Just to aid editing in the presence of autoload link  
  ENVIR=autoload
else
  ENDAT=/tmp/suffix
  echo $0 | cut -d' ' -f2- > $ENDAT
  if grep "postinst" $ENDAT >/dev/null ; then
    ENVIR=deb
  elif grep ".rpm" $ENDAT >/dev/null ; then
    ENVIR=rpm
  else
    echo "The installation ENVIRironment cannot be identified! "
    echo "ENVIR=not_dentified " >> $RECORD
    echo $MSG
    echo $MSG >> $RECORD
    echo
    exit 1
  fi
rm $ENDAT 2>/dev/null
fi
echo ENVIR=$ENVIR >> $RECORD

echo
if [ "$DEVFS" = "active" ] ;  then
  ##       Set relevant variables   

   # Check for update command
  if test -x /usr/sbin/update-devfsd ; then
    UPDATE="/usr/sbin/update-devfsd"
  elif test -x /usr/bin/killall ; then
    UPDATE="killall -HUP devfsd"
  else
    echo An update command for the device file system cannot be identified!
    echo $MSG
    echo UPDATE=not_identified >> $RECORD
    echo $MSG >> $RECORD
    echo $MSG
    echo
    exit 1
  fi 
  echo UPDATE=$UPDATE >> $RECORD

  # Identify DEVFS target files
  if test -f /etc/devfsd.conf ; then
    TARGET=/etc/devfsd.conf
  elif test -f /etc/devfs/devfsd.conf ; then
    TARGET=/etc/devfs/conf.d/ltmodem
  else
    echo "While presence of /dev/.devfsd reveals a Device File System,"
    echo "	its configuration file cannnot be found!  Aborting!"
    echo DEVFS configuration file cannnot be found >> $RECORD
    echo $MSG
    echo $MSG >> $RECORD
    echo
    exit 1
  fi
    echo TARGET=$TARGET >> $RECORD

  # Define the DONOR file
  if [ "$ENVIR" = "autoload" ] || [ "$ENVIR" = "deb" ] ; then
    DONOR=source/debian/etc_devfs_conf.d_ltmodem
  elif [ "$ENVIR" = "rpm" ] ; then
    DONOR=/etc/devfs/conf.d/ltmodem
  else
    echo Cannot identify donor files
    echo DEVFS_DONOR_FILE=not_defined >> $RECORD
    echo $MSG
    echo $MSG >> $RECORD
    echo
    exit 1
  fi
  echo DEVFS_DONOR_FILE=$DONOR >> $RECORD
fi     

# Start actions after above checks

# rmmod ltmodem lt_serial lt_modem 2>/dev/null
rmmod ltmodem lt_serial lt_modem 2>/dev/null

umask 007

# case of ./autoload without device file system
if test -z "$DEVFS" ; then
  rm -f /dev/modem /dev/ttyLT0
  mknod /dev/ttyLT0 c 62 64 && \
     echo "   Made modem device node    /dev/ttyLT0"
  chown root /dev/ttyLT0
  getent group dialout >/dev/null 2>/dev/null && \
    (chgrp dialout /dev/ttyLT0; echo "     group ownership set to dialout") || \
    (chgrp uucp /dev/ttyLT0; echo "     group ownership set to uucp")
  umask 022
  ln -s /dev/ttyLT0 /dev/modem && \
  echo "   with symbolic link    /dev/modem --> /dev/ttyLT0"
  echo "   DEVICE_NODE=/dev/ttyLT0"  >> $RECORD
  echo "SYM_LINK1=/dev/modem"  >> $RECORD 
  echo With permissions to group: dialout  >> $RECORD
fi


### The device file system cases
if [ "$DEVFS" = "active" ] ; then

  # First remove old support lines in case of future updates
  if [ "$TARGET" = "/etc/devfsd.conf" ] 
  then
    # Mandrake 8.1 type case
    grep -v LT0 $TARGET > "$TARGET".tmp
    mv "$TARGET".tmp $TARGET
  elif [ "$ENVIR" = "autoload" ] && [ "$TARGET" = "/etc/devfs/conf.d/ltmodem" ] ; then
    # autoload in Debian like ENVIRironment
    rm $TARGET &>/dev/null
  else
    if ! [ "$ENVIR" = "deb" ] ; then
      echo Please report your Linux distribution to discuss@linmodems.org 
      # with the ltmodem-ver.deb removals are done by a prior  cleanup == preinst
    fi
  fi
  # If the following are not done, broken symbolic links remain 
  rm -f /dev/modem   &>/dev/null 
  rm -f /dev/ttySLT0 &>/dev/null
  rm -f /dev/ttyLT0  &>/dev/null
 
  # now reinstall support
  if [ "$ENVIR" = "autoload" ] ; then
    # the autoload case    
    if [ "$TARGET" = "/etc/devfs/conf.d/ltmodem" ] ; then
    # Typical debian like situation
      mkdir -p /etc/devfs/conf.d/
      cp $DONOR $TARGET
    elif  [ "$TARGET" = "/etc/devfsd.conf" ] ; then
    # Mandrake 8.1 like case
      cat $DONOR >> $TARGET
    else
      echo Process for devfs support not adequately defined.
      echo Process for devfs support not adequately defined. >> $RECORD
      echo $MSG
      echo Exiting
      echo
      exit 1 
    fi
  # the RPM installer case 
  elif [ "$ENVIR" = "rpm" ] ; then
    if [ "$TARGET" = "/etc/devfsd.conf" ] ; then 
    # Mandrake 8.1 under rpm case
      mv $TARGET "$TARGET".mdk
      cat "$TARGET".mdk $DONOR >> $TARGET
    elif [ "$TARGET" = "/etc/devfs/conf.d/ltmodem" ] ; then
    # Case currentlty not known, but just in case
      echo TARGET = $DONOR
    else
      echo TARGET not defined
      echo TARGET=not_defined >> $RECORD 
    fi
  # Debian installer case, no action necessary
  elif [ "$ENVIR" = "deb" ] ; then
    echo "$TARGET in place"
  else
    echo "Something is flawed if you see this message"       
    echo "Please alert discuss@linmodems.org"
    echo Aborting
    echo Could not install device file system support >> $RECORD
    echo $MSG >> $RECORD
    echo
    exit 1
  fi
  DEVICE_NODE=/dev/tts/LT0
  echo "Support is installed for maintenance of the modem device node:"
  echo 	"	$DEVICE_NODE"
  echo "by the Device File System, together with its symbolic links;"
  echo "	/dev/modem  and /dev/ttySLTO"
  echo Informing the System with:	
  echo "	$UPDATE"   
  $UPDATE
  echo DEVICE_NODE=$DEVICE_NODE  >> $RECORD
  echo SYM_LINK1=/dev/modem  >> $RECORD
  echo SYM_LINK2=/dev/ttySLT0  >> $RECORD          
  echo Display device node support lines with:  >> $RECORD 
  echo "	grep LT0 $TARGET" >> $RECORD 
fi
echo " "  >> $RECORD
sleep 2
echo
##############  echo End device node section
#!/bin/sh

ALIASLINES='alias char-major-62 lt_serial
alias /dev/tts/LT0 lt_serial
alias /dev/modem lt_serial'

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
  echo "Could not identify your distribution's way of automatically loading modules"
  exit 1
fi

echo
echo "Writing modules autoloading lines to $MCFILE:"
if grep "alias char-major-62" $MCFILE >/dev/null
then
  if ! grep 'alias char-major-62 *lt_serial' $MCFILE >/dev/null || ! grep 'options lt_modem' $MCFILE >/dev/null
  then
      if grep 'alias char-major-62 *lt' $MCFILE >/dev/null
      then
        echo "Old ltmodem entry found in $MCFILE, replacing it"
        cp $MCFILE $MCFILE.ltmodem~
        sed 's/alias char-major-62 *lt.*/# lt_drivers: autoloading and insertion parameter usage \
        `"$ALIASLINES"` \
        # options lt_modem vendor_id=0x115d device_id=0x0420 Forced=3,0x130,0x2f8 \
        # section for lt_drivers ends/' $MCFILE.ltmodem~ >$MCFILE
      else
        echo "$MCFILE already contains an entry for char-major-62"
        echo "and it's obviously not for ltmodem - weird. Aborting"
        exit 1
      fi
  else
    echo "$MCFILE already contains the entry for the ltmodem, not changed."
  fi
else
  cp $MCFILE $MCFILE.ltmodem~
  echo "" >> $MCFILE
  echo "# lt_drivers: autoloading and insertion parameter usage" >> $MCFILE
  echo "$ALIASLINES" >> $MCFILE
  echo "# The next line is a syntax example, rarely needed, to automate parameter usage during lt_drivers insertion. See documentation for details " >> $MCFILE 
  echo "# options lt_modem vendor_id=0x115d device_id=0x0420 Forced=3,0x130,0x2f8" >> $MCFILE
  echo "# section for lt_drivers ends" >> $MCFILE
fi

# Identify UPDATE Modules command
if test $MCFILE = "/etc/modutils/aliases"
then
  UPDATE_MOD="update-modules"
else
  UPDATE_MOD="depmod -a"
fi
echo MCFILE=$MCFILE >> $RECORD
echo >> $RECORD
echo UPDATE_MOD=$UPDATE_MOD >> $RECORD 
TEST=/tmp/depmod_all
echo Updating with:"	$UPDATE_MOD"
echo Capturing output to $TEST and parsing.
$UPDATE_MOD &> $TEST
if grep depmod: $TEST >/dev/null ; then
  echo >> $RECORD
  cat  $TEST   >> $RECORD
fi
  
# Dependecies check
echo
grep lt_ $TEST > $TEST.LT
if grep depmod $TEST.LT >/dev/null
  then
  DEPMOD_LT=failed
  echo DEPMOD_LT=failed >> $RECORD
  echo --------------------
  grep lt_ $TEST.LT
  echo --------------------
  echo An imperfect FLAVOR matching between the current `uname -r` kernel
  echo and the kernel-headers used in compiling is responsible.
  echo Within 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 Within DOCs/ , read Flavor.txt and Compile_Properly.txt >> $RECORD
  echo
  echo Then re-compile the drivers to correct this defect.
  echo
  echo While allowing this script to run to completion in case you are running tests,
  echo it is Quite Likely that the modem drivers will not be functional!!
  echo $MSG >> $RECORD
  echo
  sleep 5
else    
  echo The modem drivers dependencies are properly resolved.
  echo DEPMOD_A=passed >> $RECORD
fi

grep -v lt_ $TEST > $TEST.a
if grep depmod $TEST.a >/dev/null ; then
  echo Checking for other dependency problems:
  echo -------------------
  cat $TEST.a 
  echo ------------------
  if grep "modules." $TEST.a  >/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 ELF $TEST.a | cut -d/ -f5` "
    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." 
    echo
    sleep 5
  fi
fi

grep -v ELF $TEST.a > $TEST.b
if grep "modules." $TEST.b  >/dev/null ;  then
  echo
  cat $TEST.b
  echo Corrective action is recommended for these residual dependency problems.
fi
rm $TEST.* &>/dev/null
 
echo
if [ "$ENVIR" = "rpm" ] ; then
  echo "A folder has been installed in your Systems documentation,"
  echo "  /usr/share/DOCs/ltmodem/ , and if this installer is not current,"
  echo "can be downloaded from http://www.heby.de/ltmodem/ as DOCs.tgz " 
fi
echo "Browse the DOCs/ for advice on configuration and driver usage issues."
echo "	in particular read LastAdvice for dialout configuration instructions"
echo "	and scan Ltmodem.html for issues relevant to your system."
echo 	
echo "To get online, the Port to be specified in your dialout scripts is:"
if [ "$DEVFS" = "active" ] ; then 
  echo "	/dev/modem "
  echo "the symbolic link to /dev/tts/LT0"
else 
  echo "/dev/ttyLT0 or its symbolic, /dev/modem"
fi
echo  >> $RECORD

echo The following block has been written to /etc/modules.conf: >> $RECORD
# grep lt_ /etc/modules.conf
echo " /etc/modules.conf" >> $RECORD
echo #-------------------------- >> $RECORD
grep lt_ /etc/modules.conf >> $RECORD
echo #-------------------------- >> $RECORD
echo   >> $RECORD
sleep 5
echo A summary of installation actions and diagnostics is contained in:  $RECORD
echo There is also a pedantic pedagogical tour of the modem issues provide by:
echo "	./checkout"
echo Skip it if you are experienced.
echo 
sleep 3 

echo "	Otherwise, get On-Line and Enjoy!!"
echo "	Relate problems to discuss@linmodems.org"
echo "	Gratitude too,  to discuss@linmodems.org"
echo

if [ "$ENVIR" = "autoload" ] ; then
  ln -sf utils/checkout checkout &> /dev/null
fi
