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

# Needed for locating DOCS/utils/scanPCI
LT_VERSION=8.22a5
TARGET_VERSION=2.4.19-plip
# is Headers version, which could be different then acting kernel version
KVER=`echo $TARGET_VERSION | cut -d- -f1`
KEXT="_`echo $TARGET_VERSION | cut -d- -f2`"

PWD=`pwd`

if test -z "$TMPM" ; then
mkdir -p /tmp/ltmodem
TMPM=/tmp/ltmodem/tmpfile
fi

# BN can be inherited from parent files
if test -z "$RECORD" ; then
  if [ "`pwd`" = "/" ] ; then
    RECORD=/tmp/ltmodem/LTinstall.txt
  else
    RECORD=LTinstall.txt
  fi
  BN=`basename $0`
  # for test purposes, re-defined later
fi

# Identify the install ENVIRironment and define files
# First the continuation of build_module and ltinst2 case
if [ "$BN" = "autoload" ] ||  [ "$BN" = "Autoload" ] || [ "$BN" = "build_module" ] || [ "$BN" = "lt_autoload" ] ; then
  #  Just to aid test and editing of Autoload in presence of autoload link
  ENVIR=autoload 
  RECORD=BLDrecord.txt
else
# Now RPM and DEB installer cases
  # finishing ENVIR determination if necessary
  if grep -E postinst $RECORD >/dev/null ; then 
    ENVIR=deb
    DOCS=/usr/share/doc/ltmodem-"$TARGET_VERSION"
  elif grep -E -i "rpm" $RECORD >/dev/null ; then
    ENVIR=rpm
    DOCS=/usr/share/doc/ltmodem-kv_"$KVER""$KEXT"-"$LT_VERSION"   
    # in case build_rpm was not run by Root
    chown -R 0:0 /lib/modules/"$TARGET_VERSION"/ltmodem
    chmod  644   /lib/modules/"$TARGET_VERSION"/ltmodem/*
    FAST=1
  else
    echo "The installation ENVIRironment cannot be identified! "
    echo $MSG
    echo
    exit 1
  fi
fi

if [ "$ENVIR" = "autoload" ] ; then
  read -p "To continue:	Enter"
else
  sleep 2
fi
echo

SYS=`uname -r`
CPU=`uname -m`
# Checks for use of ltmodem-version.rpm without supporting installed kernel
if ! test -d /lib/modules/$TARGET_VERSION/kernel  >/dev/null -o [ "$SYS" != "$TARGET_VERSION" ] ; then
  cat <<END > $TMPM
 
 The currently serving modules tree is:    /lib/modules/$SYS/
 The modem drivers have been installeed in /lib/modules/$TARGET_VERSION/ltmodem/   
 Thus they cannot serve under the current kernel: $SYS
 
 This is acceptable ONLY if a kernel + modules for kernel-version $TARGET_VERSION
 will be later installed.

 Otherwise DO get advice on a suitable installer run:   scanmodem
 The desired Installer name is like: ltmodem-kv-$SYS-$LT_VERSION-1.$CPU.rpm

END
 cat $TMPM
 cat $TMPM >> $RECORD
 sleep 10
fi

# Device File System status check
if test  -e /dev/.devfsd ; then
  DEVFS=active
  DEVFSD=`locate devfsd.conf`
  if test -n "$DEVFSD" ; then
    echo DEVFSD=$DEVFSD
    if ! grep "LOOKUP *PPP *MODLOAD" $DEVFSD >/dev/null ; then
      echo "Appended to $DEVFSD: LOOKUP  PPP     MODLOAD" >> $RECORD
      echo >> $DEVFSD
      echo  "LOOKUP  PPP     MODLOAD"  >> $DEVFSD
      echo >> $DEVFSD
    fi
  else
    echo Expected devfs file not found:  devfsd.conf
    echo Expected devfs file not found:  devfsd.conf
  fi
else
 DEVFS=
fi
echo DEVFS=$DEVFS >> $RECORD
echo DEVFSD=$DEVFSD >> $RECORD
echo

# Check for other /dev/modem links
if [ -L /dev/modem ] ; then
  MODEM=`ls -l /dev/modem | cut -d'>' -f2- | cut -d" " -f2-`
  echo MODEM=$MODEM >> $RECORD
  echo DEV_MODEM_LINK_ORIG=$MODEM >> $RECORD
  echo "The modem symbolic link is: /dev/modem -> $MODEM"
  echo $MODEM > $TMPM
  if ! grep LT0  $TMPM >/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

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

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

exit

### The device file system cases
if [ "$DEVFS" = "active" ] ; then
  echo First remove old support lines in case of future updates
  if [ "$TARGET" = "/etc/devfsd.conf" ] ;  then
    echo  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
    echo 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
    echo This is the problem 1
  fi
  echo This is the problem 2
  # If the following two are not done, broken symbolic links remain 
  rm -f /dev/modem  /dev/ttySLT0 /dev/ttyLT0   &> /dev/null ; fi

  # now reinstall support
  if [ "$ENVIR" = "autoload" ] ; then
    echo  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
    echo the rpm case
    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

 
OLDSITE=/lib/modules/`uname -r`/kernel/drivers/char/
if [ -f $OLDSITE/lt_modem.o ] ; then
  rm $OLDSITE/lt_modem.o* $OLDSITE/lt_serial.o* &> /dev/null
  echo Removed modem drivers from $OLDSITE >> $RECORD 
  echo The new lt_drivers site is /lib/modules/`uname -r`/ltmodem/ >> $RECORD  
fi 

echo " "  >> $RECORD
# rm $TMPM.* &> /dev/null

if [ "$ENVIR" = "Reb" ] ; then
  echo There is a documentation and utilities package, DOCs.tgz which can be downloaded from  >> $RECORD  
  echo "	http://www.heby.de/ltmodem"  >> $RECORD 
  echo Diagnostic information and advice was been written to $RECORD
  echo >> $RECORD 
  echo Get Online and Enjoy  >> $RECORD        
  echo Relate problems to discuss@linmodems.org >> $RECORD 
  echo Gratitude too,  to discuss@linmodems.org >> $RECORD 
fi

if [ "$ENVIR" = "autoload" ] ; then
  read -p "To continue:	Enter"
else
  sleep 2
fi
# echo
# echo Diagnostic information was written to $RECORD

##############  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 "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 two lines are syntax examples, only 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" >> $MCFILE
  echo "# options lt_modem Forced=3,0x130,0x2f8,0" >> $MCFILE
  echo "# section for lt_drivers ends" >> $MCFILE
fi

## ppp aliases support section

echo > $TMPM
echo "# This block installed with Lucent modem drivers is for PPP related support" >> $TMPM
if ! grep "alias /dev/ppp *ppp_generic" /etc/modules.conf > /dev/null ; then
  echo "alias /dev/ppp          ppp_generic" >> $TMPM
fi
if ! grep "alias char-major-108 *ppp_generic" /etc/modules.conf > /dev/null ; then
  echo "alias char-major-108    ppp_generic" >> $TMPM  
fi
if ! grep "alias tty-ldisc-3 *ppp_async" /etc/modules.conf > /dev/null ; then
  echo "alias tty-ldisc-3       ppp_async" >> $TMPM  
fi  
if ! grep "alias tty-ldisc-14 *ppp_synctty" /etc/modules.conf > /dev/null ; then
  echo "alias tty-ldisc-14      ppp_synctty" >> $TMPM  
fi
if ! grep "alias ppp-compress-21 *bsd_comp" /etc/modules.conf > /dev/null ; then
  echo "alias ppp-compress-21   bsd_comp" >> $TMPM
fi  
if ! grep "alias ppp-compress-24 *ppp_deflate" /etc/modules.conf > /dev/null ; then 
  echo "alias ppp-compress-24   ppp_deflate" >> $TMPM
fi
if ! grep "alias ppp-compress-26 *ppp_deflate" /etc/modules.conf > /dev/null ; then
  echo "alias ppp-compress-26   ppp_deflate" >> $TMPM 
fi
# if any ppp in $TMPM
if grep ppp $TMPM > /dev/null ; then
  cat $TMPM >> $MCFILE
fi

# check for 2.4.nn /dev/ppp
DEVPPP=`locate /dev/ppp`
if test -z "$DEVPPP" -a -z "$DEVFS" ; then
  cat<<END >> $RECORD

 Made recommended 2.4.nn device:   /dev/ppp c 108 0
 Users of ppp should be added to group dip
 Current members are:  `grep dip /etc/group`

END
  mknod /dev/ppp c 108 0
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 
echo Updating with:"	$UPDATE_MOD"
echo Capturing output to $TMPM and parsing.
$UPDATE_MOD &> $TMPM

# check for ALIASLINES bug
if grep ALIASLINES /etc/modules.conf >/dev/null ; then
  echo ALIASLINES bug present >> $RECORD
  grep -v ALIASLINES $MCFILE > $TMPM   
  mv $TMPM $MCFILE 
  $UPDATE_MOD &> $TMPM
fi


if [ "$ENVIR" = "autoload" ] ; then
  echo
  read -p "To continue:	Enter"
else
  sleep 2
fi


# Dependecies check
if grep depmod: $TMPM >/dev/null ; then
  echo >> $RECORD
  cat  $TMPM   >> $RECORD
fi
  
echo
grep lt_ $TMPM > $TMPM.LT
if grep depmod $TMPM.LT >/dev/null
  then
  DEPMOD_LT=failed
  echo DEPMOD_LT=failed >> $RECORD
  echo --------------------
  grep lt_ $TMPM.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
  if [ "$ENVIR" = "autoload" ] ; then
    read -p "To continue:	Enter"
  else
    sleep 3
  fi
  echo
else    
  echo The modem drivers dependencies are properly resolved.
  echo DEPMOD_A=passed >> $RECORD
fi

grep -v lt_ $TMPM > $TMPM.a
if grep depmod $TMPM.a >/dev/null ; then
  echo Checking for other dependency problems:
  echo -------------------
  cat $TMPM.a 
  echo ------------------
  if grep "modules." $TMPM.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 "cannot read ELF" $TMPM.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 &> $TMPM.vrs
    echo While the resident version is: `grep version $TMPM.vrs | cut -d' ' -f3`
    echo "	  a minimal version of 2.4.2 is recommended for 2.4.18 kernels and later." 
    echo
    if [ "$ENVIR" = "autoload" ] ; then
      read -p "To continue:	Enter"
    else
      sleep 5
    fi
  fi
fi

grep -v ELF $TMPM.a > $TMPM.b
if grep "modules." $TMPM.b  >/dev/null ;  then
  echo
  cat $TMPM.b
  echo Corrective action is recommended for these residual dependency problems.
fi

echo The following block has been written to /etc/modules.conf: >> $RECORD
echo " /etc/modules.conf" >> $RECORD
echo #-------------------------- >> $RECORD
grep lt_ /etc/modules.conf >> $RECORD
echo #-------------------------- >> $RECORD
echo   >> $RECORD

echo
if [ "$ENVIR" = "rpm" ] ; then
  echo "A folder has been installed in your Systems documentation,"
  echo "  $DOCS "
  echo containing much useful guidance and uilities. Updates can be 
  echo "downloaded from http://www.heby.de/ltmodem/ as DOCs.tgz " 
  echo
fi
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  A summary of installation actions with Advice is contained in:  $RECORD
echo Please browse $DOCS to benefit from DOCs/ and utils/  therein. >>  $RECORD 
cat << END

  Browse DOCs/Ltmodem.html for issues that may be relevant to your system and useful URLs
  Also see DOCs/LastAdvice
  Some dialer utilities including KPPP require that Each User set up a configuration,
  as there could be diverse configurations on multi-User systems.
  
  Read DOCs/wvdial.txt for aid in configuring the smart dialup utility, WVDIAL
 
	Otherwise, get Online and Enjoy         
	Relate problems to discuss@linmodems.org
 	Gratitude too,  to discuss@linmodems.org

END
cat $TMPM
cat $TMPM >> $RECORD

echo  If not familar with the ltmodem drivers, run the interactive CHECKOUT utility by executing:
if [ "$ENVIR" = "autoload" ] ; then
  echo
  ln -sf utils/checkout checkout &> /dev/null
  echo "	./checkout"
elif  [ "$ENVIR" = "deb" ] ; then
  if [ -f $DOCS/utils/checkout.gz ] ; then
    gunzip $DOCS/utils/checkout.gz
    chmod +x $DOCS/utils/checkout
    ln -sf $DOCS/utils/checkout checkout &> /dev/null
    echo "	./checkout"
  else
    echo Sorry checkout executable not found.
  fi
elif   [ "$ENVIR" = "rpm" ] ; then
  echo "	$DOCS/utils/checkout" 
  chmod +x $DOCS/utils/checkout
else
  echo No checkout alternative
fi

if  [ "$ENVIR" = "deb" ] ; then
  echo Ending Debian package actions.
fi
  

# cleanup tmp files
if test -d /tmp/ltmodem ; then
  /bin/rm -r /tmp/ltmodem/ 
fi

