#!/bin/bash
# Basically, configures needed files and compiles the drivers by:
#   scanning for Lucent DSP PCI card modem
#   accommodates non-version matched compiles
#   configure engine, from Chris Hebeisen
#   make the drivers, Mark Spieth
#   pop-up messages when User flaws are anticipated, Marv Stodolsky
# Feel entirely free to hack out sections.
#   2002Jan24,  MarvS 

# Define version
VER=8.26a9
case $VER in
        *RELEASE* ) 
                VER=6.00b11
                ;;
        * )
        ;;
esac

# INSTALLROOT=`pwd` # leftover from NOISE.tar.gz procedures

RECORD=BLDrecord.txt
# BaseName way be inherited from build_rpm or build_deb
if test -z "$BN" ; then
  BN=`basename $0`
fi
if [ "$BN" = "build_module" ] ; then
  export BN=$BN
  export RECORD=$RECORD
  # for use by called executables
  FV="$1"
  # alternatively FV may be inherited from build_deb or build_rpm
else
  # inheritance from ./build_rpm
  FV=$FV
fi
echo "Within build_module FV=$FV"
# sleep 3


# FV == Forced Version
SYS=`uname -r`
if test -n "$FV" ; then
 TARGET_VERSION=$FV
else
 TARGET_VERSION=$SYS
fi
MAJOR=`echo $TARGET_VERSION | cut -d. -f1-2`
KVER=`echo  $TARGET_VERSION | cut -d- -f1`
KEXT=`echo  $TARGET_VERSION | cut -d- -f2`
# echo TARGET_VERSION=$TARGET_VERSION
# echo MAJOR=$MAJOR
# echo KVER=$KVER
# echo KEXT=$KEXT

if [ "$MAJOR" = "2.2" ] ; then
  echo
  echo "The core DSP code represented by the version 8.22 ltmdmobj.o is not compatible with 2.2.nn source code."
  echo "Please use the ltmodem-6.00c2.tar.gz to build drivers and installers for 2.2.nn kernels."
  echo
  echo Exiting
  echo
  exit 2
fi

mkdir -p /tmp/ltmodem
TMPM=/tmp/ltmodem/tmpfile
echo $TMPM
export TMPM=$TMPM

# FAST is usually exported from build_rpm or build_deb.
# When is exists, FAST=anything as contrasted to FAST=   
#    pauses and "read -p" sections are bypassed.
# FAST=anything

if [ -z "$FAST" ]; then

cat<<END

Lucent/Agere DSP chip modems (but Not the AMR modems)
 are supported by the the resources of this kit.
 Agere Systems, Inc. provides the  proprietary code,
 particularly including DSP (digital signal processing)
 to volunteer Maintainers managing several enhancements.

 Mark Spieth initiated the ltmodem compiler kit series.
 Chris Hebeisen introduced the Configure implementation,
 bringing Newbie friendliness.
 Marv Stodolsky is culpable for the verbose messaging.
 Within DOCs/, see CREDITS and Ltmodem.html for details on
 the contributions of many others.
 Volunteers have assembled the ltmodem-version.deb and
 ltmodem-version.rpm Installers for the Linux community
 utiliizing the resources of this compiler kit series.

 If new to compiling, please first run:    utils/ksp.sh
 to set up your kernel-sources properly. The kernel-source package as installed
 does NOT in general match an installed kernel, but one of several alternative kernels.
 This with a niave usage, all could be completed well technically.
 BUT the installed drivers might not be compatible with your kernel,
 of even reside in the right /lib/modules/Kernel_Version/ tree.

END
read -p "To continue,	 Enter"

echo
echo This compilation process is likely Not necessary to work through,
echo if the only need is modem drivers matching the standard kernel of the
echo following Linux distributions: Debian, Red Hat, Mandrake, SuSE and Connectiva
echo
echo Volunteers have already compiled most standard drivers.
echo Those available as of this $VER release are embedded in utils/scanmodem,
echo and any compatible with your System requirements will be later listed.
echo They are available for download as ltmodem-version.rpm and
echo ltmodem-version.deb Installers from http://www.heby.de/ltmodem/
echo Some additions may have since been deposited at http://www.heby.de/ltmodem/
echo since this $VER release. Do check carefully therein for updates if necessary.
echo
echo The compiling process is a learning experience for Newbies.
echo We strongly recommend that you DO learn it sometime.
echo But getting on the Internet under Linux should have first priority.
echo This kit provides several diagnostics aids and good documentation.
echo Continuing will do no harm.  Useful diagnostic information
echo on your system will be written to BLDrecord.txt,
echo with direction to documentation if a failure occurs.
echo
echo At a minimum, Do keep the DOCs/ and utils/ folders and become familiar
echo with the resources therein.
echo
read -p "To continue:  Enter"
echo "================================================================="
AGERE=`echo $VER | cut -c 1-4`
VEREND=`echo $VER  | cut -c 5-9`
echo
echo "This is the version $VER of the compiler + installer kit "
echo "	$AGERE	corresponds to the version of the core Agere Systems DSP code."
echo "	    $VEREND  designation that follows reflects NO improvement in DSP code."
echo "Rather peripheral driver, added diagnostics and installer improvements"
echo "are reflected.  They are progressively implemented as Users report problems"
echo "or the Linux kernel evolves."
echo
echo "The modem drivers will serve with Linux kernels beginning with 2.2.2, the current "
echo "2.4.nn series, and there is experimental support for the emerging 2.5.nn kernels."
echo
echo "If your PC is being well served with this $VER,"
echo "there is NO Benefit in using a more recent version"
echo "until there is an update in the Agere driver code, say 8.22 to 9.00."
echo
echo "But if the driver installation for a new kernel should in some way fail,"
echo "please do download the most recent ltmodem-VER.tar.gz"
echo "before sending a query to discuss@linmodems.org,"
echo "because the problem may already be solved."
echo
echo "This $BN script only compiles drivers,"
echo "with their installation managed by subsequent commands."
echo "Thus $0 can be rerun for education or amusement,"
echo "without as yet committing changes to your System."
echo
read -p "For general instructions:  Enter"

echo "================================================================="
echo "All should work automatically for most Linux installations/distributions, " 
echo "	IF a complete kernel-headers/ folder is available:"
echo "through a symbolic link:	/lib/modules/KernelVersion/build --> PATH_to/kernel-headers/"
echo "at the classical /usr/src/ position. "
echo "Otherwise a symbolic link will be necessary:"
echo "ln -s PATH_to/kernel-headers/ /usr/src/linux"
echo Read DOCs/Compile-properly.txt and 1ST-READ if an explanation is needed about kernel-headers.
echo
echo "To compile modules for a kernel other version than $SYS, such as version 2.2.17-3test, use syntax:"
echo "  $0 2.2.17-3test"
echo "where the version.h in the kernel-headers/ folder has 2.2.17-3test"
echo
### Restore this section to display the instruction section for:
# echo "To compile multiple driver pairs to support multiple Lucent modems"
# echo "  in the same PC, Enter:   $0 -n N "
# echo "  when N is the number of desired driver pairs."
# echo "This option is only supported when drivers to be compiled"
# echo "  and the running kernel are version matched."  
# echo
### End multi-driver instruction section.

echo If desirable to make a record for trouble shooting purposes,
echo Abort with Ctrl-C. First start a recording script with:
echo "  script ltrecord.txt"
echo "	Then again the command: $0 $1 $2 $3 $4"
echo
read -p "To continue:  Enter"
echo
echo "===================================================================="
echo Diagnostic information is progressively being written to $RECORD .
echo "Often, contents of $RECORD will aid in solving a problem."
echo

fi # end 1st FAST section

echo "Now acquiring distribution data:"
DISTRO_FILES="redhat-release SuSE-release mandrake-release conectiva-release  \
   bluepoint-release  slackware-version  debian_version"
#   redhat MUST proceed mandrake in this listing as Mandrake has an /etc/redhat-release
for i in $DISTRO_FILES
do
  if [ -a /etc/$i ]; then
    DISTRO=$i
    # generating short name
    DISTR=`ls /etc/$i | cut -d/ -f3 | cut -d"-" -f1`
    if [ "$DISTR" = "debian_version" ] ;then
       DISTR=debian
    fi
    DVERSION=`cat /etc/$i`
  fi
done

if [ -z $DISTRO ] ; then
  DISTR="Not_identified"
  echo "The distribution is $DISTR"
  echo "	Please report the identifying /etc/FileName to discuss@linmodems.org"
else
  echo "The Linux distribution is:   $DISTR, $DVERSION  "
fi
export DISTR=$DISTR

# Starting $RECORD
echo > $RECORD 
echo "Linux Newbies should check DOCs/Installers and if still necessary" >> $RECORD 
echo "	http://www.heby.de/ltmodem for already available driver installers"  >> $RECORD
echo >> $RECORD 
echo "Together with information included in DOCs/,"   >> $RECORD
echo "this report may enable you to solve problems."  >> $RECORD
echo "But if further help is needed, send $RECORD to discuss@linmodems.org" >> $RECORD
echo "Please use the following in the email Subject Line:" >> $RECORD
SUBJECT="Lucent modem, $DISTR $DVERSION $SYS"
echo $SUBJECT >> $RECORD
echo DISTRO=$DISTRO >>  $RECORD
echo DISTR=$DISTR >>  $RECORD
echo DVERSION=$DVERSION >> $RECORD
echo ACTION="$0 $1 $2 $3 $4 $5" >> $RECORD
echo WHOAMI=`whoami` >> $RECORD
echo TARGET_CPU=$CPU
date >> $RECORD
uname -a >> $RECORD
if [ "$TARGET_VERSION" != "$SYS" ] ; then
  echo TARGET_VERSION=$TARGET_VERSION
fi
MSG="If you cannot solve the problem, send  $RECORD to: discuss@linmodems.org" 
export MSG=$MSG
echo  >> $RECORD
echo "	End of distribution check."
echo

# starting 2nd FAST section
if [ -z "$FAST" ]; then 

# Calling scanPCI utility
utils/scanmodem
echo
read -p "To continue:	Enter"
echo ==================================================
echo
echo Checking for any preliminary complications.
if [ -L /usr/src/linux ] ; then
  HEADERS_INCLUDE=`ls -l /usr/src/linux | cut -d'>' -f2`
  if [ "$HEADERS_INCLUDE" = "/usr/include" ] ; then
    echo HEADERS_INCLUDE=$HEADERS_INCLUDE >> $RECORD
    echo "	WARNING!!!"
    echo On your system. the symbolic link /usr/src/linux is currently set to /usr/include/
    echo While /usr/include/ does have system headers, BUT only a partial set of All kernel-headers.
    echo This subset is NOT adequate for compiling drivers!!
    echo A complete kernel-header package must either be installed or compiled.
    echo
    echo For some Linux distributions, the package NAMED kernel-headers-SomeVersion
    echo installs to /usr/include/.  These are the incomplete set suitable as System headers.
    echo 
    echo If a complete kernel-header package is provided by your Linux distribution, 
    echo "these headers will be installed as: /usr/src/kernel-headers-$SYS"
    echo Read DOCs/Compile-properly.txt for details.
    echo Read DOCs/Compile-properly.txt >> $RECORD
    echo While it will do no harm to continue, $0 will fail later.
    echo 
    read -p "Abort with Ctrl-C, but to continue:	Enter"
    echo =============================================================
  else
    echo Bad links to /usr/include not evident.
  fi
fi

## Modem drivers will work with SMP kernels beginning with now.
if [ -r /boot/config-$TARGET_VERSION ]  && [ -z "$FV" ] ; then
   CONFIG=/boot/config-`uname -r`
   SMP0=`grep CONFIG_SMP $CONFIG | grep =`
   if test -n "$SMP0" ; then
     echo SMP=$SMP >> $RECORD
     echo Your current kernel has Symmetric Multi Processors enabled.
     echo Beginning with ltmodem version 8.22a, the modem drivers should be SMP competent. 
     echo
   else
     echo The kernel supports only single processor motherboards.
   fi
   echo
   read -p "To continue:	Enter"
   echo ==================================================
else
   echo Kernel config file not recognized within the /boot folder.
fi

echo
if [ "$DISTRO" = "SuSE-release" ] && [ $UID -ne 0 ] ; then
  echo For the SuSE Linux distribution, 
  echo access to kernel-headers requires Root permission.
  echo "So start $0 again, after login as Root or switch to   sudo root"
  echo 
  exit 2
fi

if [ "$DISTRO" = "redhat-release" ] ; then
  echo For the Red hat Linux distribution,
  echo there are four alternative kernel config files
  echo within the folder /usr/src/linux/configs/
  echo The kernel-header set to be used in this driver compile
  echo MUST flavor match of your current kernel as displayed by:
  echo "	uname -r"
  echo Otherwise ./build_module will succeed and ./ltinst2 also.
  echo But there will be a failure during the system update
  echo "	depmod -a	included within ./autoload"
  echo To check this issue if desirable, abort with Ctrl-C
  read -p "or to continue:	Enter"
  echo =================================================
fi  

echo OK thus far.
read -p "To begin acquisition of compiling information:  Enter"
echo
 
fi 
#  end 2nd FAST section

# defines code folder
BASE=source
echo BASE=$BASE  >> $RECORD
if [ -f $BASE.tar.gz ]; then
  if [ -d $BASE ] ; then 
     echo "Removing old makings and expanding a clean source.tar.gz "
  fi
  rm -rf $BASE
  tar zxf $BASE.tar.gz

  # for rpm Distro  -CPU.rpm with CPU exported from build_rpm
  if test -n "$CPU" ; then
    echo CPU='"'$CPU'"' >> $BASE/SETTINGS
  fi
  # Accomodating SuSE nomenclature in ltmodem.spec
  if [ "$DISTR" = "SuSE" ] ; then
    echo KPKG='"'k_deflt'"' >> $BASE/SETTINGS
  else
    echo KPKG='"'kernel'"' >> $BASE/SETTINGS
  fi
  # passed to ltmodem.spec
  if test -n "$FV" ; then
    export FV=$FV
  fi
  source $BASE/SETTINGS 
  echo "# ===SETTINGS===" >> $RECORD
  cat  $BASE/SETTINGS  >> $RECORD
  echo  >> $RECORD
  # copy DOCs/ stuff to the source dir to support later
  #   packaging by build_rpm and build_deb
  cp -a DOCs utils $BASE
  # move to work space
  cd $BASE
  ln -s ../$RECORD $RECORD
  echo Setting  $RECORD link within $BASE/ folder.
  ls -l $RECORD
  TEST=0
else
  TEST=1
fi

## Support for multiple driver pairs
[ -z "$MAKECOUNT" ] && MAKECOUNT=1
# check for number of drivers to make
if [ "$1" = "-n" ]; then
shift
MAKECOUNT=$1
echo MAKECOUNT=$MAKECOUNT  >> $RECORD
shift
[ -z "$MAKECOUNT" ] && MAKECOUNT=1
fi

## Getting driver names from source/SETTINGS
source SETTINGS

## Starting Forced version 
if test -n "$FV" ; then
  shift
  FVNAME=$FV
  if ! [ "$DISTRO" = "debian_version" ] ; then
    FV=`echo -n $FV | cut -d- -f1`
  fi
  # best alternative goes last because later options override earlier ones
  KERNEL_HEAD_ALT="/usr/src/linux /usr/src/kernel-headers-$FV /usr/src/kernel-header+$FV"
  if echo "$FV" | grep "-" >/dev/null
  then
    if [ "$DISTRO" = "mandrake-release" ] ; then
      # kludge for mandrake kernel headers
      KERNEL_HEAD_ALT="$KERNEL_HEAD_ALT /usr/src/linux-`echo "$FV" | cut -d"-" -f1`"
    fi
  fi
  echo "Searching for a kernel-headers-$FV resource."
  KERNEL_HEAD_ALT="  $KERNEL_HEAD_ALT /usr/src/linux-$FV /lib/modules/$FV/build "
  echo KERNEL_HEAD_ALT=$KERNEL_HEAD_ALT 
  for DIR in $KERNEL_HEAD_ALT
  do
      echo -n "  ... in $DIR: "
      if test -r $DIR/include/linux/version.h
      then
        FVDIR=$DIR
        echo "Kernel headers found in $FVDIR"
      else
        echo "nope"
      fi
  done
  if test -z "$FVDIR"
  then
    echo
    echo "ERROR: No kernel headers found."
    echo "If a kernel-headers folder is installed, please set a symbolic link:"
    echo "	ln -s PATH_TO/kernel-headers-SomeVersion/ /usr/src/linux"
    echo "Then restart with command:"
    echo "	/"$BN" SomeVersion "
    echo "If more instruction is needed, within DOCs/ read ForNewbies and Compile_fail.txt"
    echo
    exit 1
  else
    echo "  using $FVDIR"
  fi

  if test -f $FVDIR/include/linux/version.h ; then
cat - > utsrelease.c <<END
#include "$FVDIR/include/linux/autoconf.h"
#include "$FVDIR/include/linux/config.h"
#include "$FVDIR/include/linux/version.h"
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main(int argc ,char** argv){
  printf("%s",UTS_RELEASE);
  return(0);
}
END
     gcc -o utsrelease utsrelease.c
     VH=`./utsrelease`
     rm -f utsrelease*
  fi
  if [ "$FVNAME" = "$VH" ] ; then
     echo "Found kernel-headers-$FV for compiling drivers."
     echo
  else
     echo "Folder for kernel-headers-$FV not found through $FVDIR. Name was $VH"
     echo
     echo $MSG
     echo
     exit 2
  fi

  ## configure force/enable options
  #  --with-force=      force build even if kernel sources don't match
  #                          running kernel yes:[no]
  #  --with-kernel=dir  provide the directory with kernel headers
  #
  FORCED="--with-force=yes --with-kernel=$FVDIR"
  echo FORCED=$FORCED >> $RECORD
  echo "Your running kernel is version-$SYS," 
  echo "  but the modem drivers will be compiled for a $FV kernel."
else
  echo "Following a successful check for matching kernel-headers,"
  echo "the modem drivers will be compiled for the current kernel version: $SYS"
fi  
echo FV=$FV >> $RECORD

if [ -z "$FAST" ]; then
  read -p "To start resource tests:	Enter"
fi	
echo

## Check for configure prerequisites, 1st capture output and parse
CONF_OUT=../conf-report.txt
echo Performing a configure trial and capturing the report to $CONF_OUT.
./configure $FORCED &> $CONF_OUT
# The below  grep -v db3  eliminates an irrelevant rpm related error message
#     which would cause an abort, when parsed subsequently.
if [ "$BN" = "build_module" ]  || [ "$BN" = "build_drivers_deb" ] || [ "$BN" = "build_deb" ] ; then
  cat $CONF_OUT | grep -v db3 > ../conf-report.txt
fi
echo "Parsing the report:"
# Cutting either  error or ERROR from $CONF_OUT
CONF_FAIL=`cat $CONF_OUT  |  grep -i error | cut -d: -f2`

# Alternative actions following parse outcomes
if test -z "$CONF_FAIL" ; then
    # This is the tentative SUCCESS case, contingent upon modversions.h 
    # Display filtered CONF_OUT   
    echo "	No evident problems."
    echo 
    HEADERSIN=`grep "KCFLAGS =" Makefile | cut -dI -f2 | cut -d' ' -f1`
    HEADERS_IN=`echo $HEADERSIN | cut -d' ' -f1`/linux/
    # echo HEADERS_IN=$HEADERS_IN
    # sleep 3
    #   to exclude debian related messages during build_module.
    if [ "$BN" = "build_module" ] ; then
      cat $CONF_OUT | grep -v deb | grep -v package    
    else 
    #    to exclude debian related messages from during build_rpm.
      cat $CONF_OUT | grep -v deb | grep -v fakeroot   
    fi   
    grep using $CONF_OUT  >>  $RECORD
    grep  "are version"  $CONF_OUT  >>  $RECORD 
    
    if grep "using /usr/src/linux" $CONF_OUT > /dev/null ; then
      echo "ls -l /usr/src/linux"  >> $RECORD
      ls -l /usr/src/linux  >> $RECORD
    fi
    if grep $TARGET_VERSION/build $CONF_OUT > /dev/null ; then
      echo "BUILD_LINK=/`ls -l /lib/modules/$TARGET_VERSION/build | cut -d'/' -f2-`"  >> $RECORD
    fi
    rm $CONF_OUT
    if [ "$BN" = "build_rpm" ] ; then
      echo >> $RECORD
      echo ------ Requires: within ltmodem.spec ---------- >> $RECORD
      grep Requires: ltmodem.spec >> $RECORD
      echo >> $RECORD
    fi
    if [ "$BN" = "build_deb" ] ; then
      cp debian/control ../
    fi 
    echo
    echo "The check for compilation tools and general resources was successful."
    echo Within DOCs/ there is an annotated conf-report.txt
    echo "The Makefile and down steam installation scripts have been created."
    echo
    echo "Next checking for the the needed header file:	modversions.h"    
    echo HEADERS_IN=$HEADERS_IN  >> $RECORD
    if [ -f "$HEADERS_IN"modversions.h ] ; then
      MODVERH=yes
      echo Found: "$HEADERS_IN"modversions.h
    else
      echo "Sorry, assembly of the needed header file:    modversions.h" >  $TMPM
      echo was not specified during configuration of the kernel sources. >>  $TMPM
      echo Withing the section LOADABLE MODULE SUPPORT:  >>  $TMPM
      echo " [*]   Set version information on all module symbols" >>  $TMPM
      echo which is necessary to specify the assembly of modeversions.h >>  $TMPM
      echo Consequently compiling of the drivers will fail at the next step. >>  $TMPM
      echo "Properly configuring your sources may be aided by for RPM using Distros"
      echo "by:	 utils/srcprep"
      echo for RPM using Distros
      echo and for Debian style Distros, the resources of kernel-package.deb
      cat $TMPM
      cat $TMPM   >> $RECORD 
    fi
    echo MODVERH=$MODVERH   >> $RECORD 
    echo >> $RECORD
    
    echo
    echo Next checking utilty versions,
    if [ -f  /usr/src/linux/Documentation/Changes ] ; then
      # This block is just a trivia service
      CHANGES=/usr/src/linux/Documentation/Changes
      if [ "$MAJOR" = "2.2" ] ; then
        FILTER="; "
      else 
        FILTER="# "
      fi
      WRITE="../Utility_version_tests.txt"
      echo > $WRITE
      echo From $CHANGES for sources version $MAJOR >> $WRITE
      echo    "Utility               Version_min               Version test" >>  $WRITE
      echo ----------------------------------------------------------------  >>  $WRITE
      grep  $FILTER $CHANGES >> $WRITE
      echo >> $WRITE
    fi
    echo "           Version_min "
    echo " Utility    or range for kernels   " 
    echo "   name     2.2.16       2.4.18          Actual_version   " 
    echo ------------------------------------------------------------------- 
    echo "Gnu C      2.7.2.3      2.95.3-2.9.99   `gcc -dumpversion`"
    echo "Gnu make   3.77         3.79.1          `make -v | grep version | cut -d, -f1 | cut -d' ' -f4`" 
    echo "binutils   2.8.1.0.23   2.9.1.0.25      `ld -v | grep version | cut -d' ' -f4`"
    echo
    echo Next checking utilty versions, with ranges  >> $RECORD
    echo "           Version_min " >> $RECORD
    echo " Utility    or range for kernels   " >> $RECORD 
    echo "   name     2.2.16       2.4.18          Actual_version   " >> $RECORD
    echo ------------------------------------------------------------------- 
    echo "Gnu C      2.7.2.3      2.95.3-3.2.1   `gcc -dumpversion`" >> $RECORD
    echo "Gnu make   3.79.1       3.77            `make -v | grep version | cut -d, -f1 | cut -d' ' -f4`" >> $RECORD
    echo "binutils   2.8.1.0.23   2.9.1.0.25      `ld -v | grep version | cut -d' ' -f4`" >> $RECORD
    echo >> $RECORD

    cat <<END > $TMPM

  The gcc compiler resources available are:
 ----------------------------
 `ls -l /usr/bin/gcc*`
 ----------------------------
 with your System currently using version `gcc -dumpversion`
 Do NOT mix the major versions of compilers for the kernel and modem drivers
 or your System will crash! The following releases are using gcc version 3.2.x :
    Mandrake 9, Redhat 8 and SuSE 8.1 in November 2002. 
 This $BN automatically adjusts its resources to the compiler version used. 
 See the utils/ltrename (by Mark Spieth) called by the Makefile.
 
END
    cat $TMPM
    cat $TMPM >> $RECORD
    echo  To continue:
    read -p "	Enter"
else
# These are the various configure failure cases
  if test -n "$CONF_FAIL" ; then
    grep ERROR $CONF_OUT >> $RECORD
  fi    
  echo >> $CONF_OUT
  echo $CONF_FAIL  
  echo A sample successfull conf_out.txt is in the folder DOCs/

  ERRORMSG="Read in DOCs/ ForNewbies, Compile-Properly.txt and Flavor.txt" 
  if grep error $CONF_OUT | grep cc > /dev/null ; then
    echo Basic utilities required for compiling have not been installed on your System. 
    echo $ERRORMSG 
    echo $ERRORMSG  >> $RECORD
    echo 
  fi
  if grep "ERROR: No kernel headers found." $CONF_OUT  > /dev/null ; then
    echo $ERRORMSG 
    echo $ERRORMSG >> $RECORD
    echo 
  fi

  if [ "$DISTRO" = "redhat-release" ] ; then
    echo >> $RECORD
    echo "For Red Hat Linux, config files corresponding to the running kernel $SYS" >> $RECORD
    echo "	are contained in: /usr/src/linux/configs/" >> $RECORD
    echo "	The appropriate one must be used to specify matching kernel-headers" >> $RECORD
  fi
  if [ "$DISTRO" = "SuSE-release" ] ||  [ $UID -ne 0 ]; then
    echo "For the Linux distribution SuSe and perhaps Others,"
    echo "        the resource test failure may merely be"
    echo "	because Root permission is needed to access kernel-headers/ folders."
    echo "Login in as Root and re-run $0"
    echo "For the Linux distribution SuSe and perhaps Others,"  >> $RECORD
    echo "        the resource test failure may merely be" >> $RECORD
    echo "	because Root permission is needed to access kernel-headers/ folders." >> $RECORD
    echo "Login in as Root and re-run $0" >> $RECORD
    echo  >> $RECORD
  fi

  ls -l /usr/src/linux > header.txt
  if grep include header.txt  > /dev/null ; then
    grep include header.txt  >> $RECORD
    echo 
    echo "While there is a kernel-headers Sub_Set in /usr/include/"
    echo "it is NOT adequate for compiing drivers!!!"
    echo "If the COMPLETE kernel-headers set is NOT in the standard /usr/src/ position," 
    echo "a symbolic link will be necessary:" 
    echo "    ln -s PATH_TO/kernel-headers/ /usr/src/linux" 
    echo "Within DOCs/, ForNewbies, Compile-fail.txt and Flavor provide relevant details." 
    echo
    rm header.txt
  fi
  echo Here is the report from $CONF_OUT
  echo ------------------
  cat $CONF_OUT
  echo ------------------
  echo 
  echo Read instructions at the end of $RECORD
  echo "Properly configuring your sources may be aided by:	 utils/ksp.sh" >> $RECORD
  echo
  exit 2
fi

echo
if [ -z "$FAST" ]; then
  read -p "	To begin compilation of $LT_PROPRIETARY_MODULE.o and $LT_SERIAL_MODULE.o,  Enter"
fi
echo

make -e | grep -v echo

echo
echo "Checking for driver products:"
MODULES="$LT_PROPRIETARY_MODULE.o $LT_SERIAL_MODULE.o"
for i in $MODULES
do
  if [ -f  $i ] ; then
    ls -l $i
    ls -l $i >> $RECORD
  else
    cd ..
    echo
    echo Compilation of $i failed,
    if test -z "$MODVERH" ; then
      echo as expected from absence of modversions.h 
    else
      echo "Do assess whether your utilities are up to date as listed within 1ST-READ!"
    fi
    echo "Read Compile_properly.txt from DOCs/."
    echo $MSG
    echo  >> $RECORD
    echo $MSG >> $RECORD
    echo
    exit 2
  fi
done
echo

### Section for requesting multiple driver pairs.
if test -z "$FV" ; then
echo
  if [ $MAKECOUNT -gt 1 ]; then
    if [ -z "$FAST" ]; then
      read -p "	Ready to make remainder of $MAKECOUNT requested driver pairs."
    fi
    i=1;
    while [ $i -lt $MAKECOUNT ]; do
      make LTNUMBER=$i semiclean
      make -e LTNUMBER=$i
      i=$[$i+1]
    done
  fi
else
  echo "  Only one driver pair can be compiled "
  echo "    when kernel-headers and the running kernel are not version matched."
  echo
fi

if test -n "$FV" ; then    
  echo Skipping depmod tests because drivers and kernel
  echo "   are deliberately not version matched."
  echo
else
  # This depmod section has an integral fixscript
  if [ -z "$FAST" ]; then 
    echo =================================================
    read -p "	Enter, to begin modules dependencies test."
  fi

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

  echo
  if [ -f /lib/modules/$SYS/kernel/drivers/pnp/isa-pnp.o ] ; then
    EXTRAMOD=/lib/modules/$SYS/kernel/drivers/pnp/isa-pnp.o
    echo "  Modular isa-pnp.o is present, and may be needed for the dependency test."
  fi
  echo  EXTRAMOD=$EXTRAMOD >> $RECORD

  echo
  DEPMOD1="$DEPMOD -e $LT_PROPRIETARY_MODULE.o"
  DEPMOD2="$DEPMOD -e $LT_PROPRIETARY_MODULE.o $EXTRAMOD $LT_SERIAL_MODULE.o"
  DEP_SET="1 2"
  for i in $DEP_SET
  do
    if [ "$i" = "1" ] ; then
      DEPTEST=$DEPMOD1
    else
      DEPTEST=$DEPMOD2
    fi
    echo Running: $DEPTEST
    $DEPTEST &> dep.txt
    DEPS=`cat dep.txt | grep "*" | cut -d: -f1`
    if [ -z "$DEPS" ] ; then
      echo " The test succeeded. "
      echo "succeeded:  $DEPTEST" >> $RECORD
    else
      echo "The dependency test failed."
      echo The kernel-headers are not exactly version matched with the current kernel-$SYS.
      echo A purely cosmetic correction with fixscript will be attempted.
      if [ "$i" = "1" ] ; then
        mv $LT_PROPRIETARY_MODULE.o $LT_PROPRIETARY_MODULE.orig
        echo "	../utils/fixscript $LT_PROPRIETARY_MODULE.orig $LT_PROPRIETARY_MODULE."
        ../utils/fixscript $LT_PROPRIETARY_MODULE.orig $LT_PROPRIETARY_MODULE.o > fix.txt
      else
        mv $LT_SERIAL_MODULE.o  $LT_SERIAL_MODULE.orig
        echo "	../utils/fixscript $LT_SERIAL_MODULE.orig $LT_SERIAL_MODULE.o"
        ../utils/fixscript $LT_SERIAL_MODULE.orig $LT_SERIAL_MODULE.o > fix.txt
      fi
      $DEPTEST  &> dep.txt
      DEPS=`cat dep.txt | grep "*" | cut -d: -f1`
      if [ -z "$DEPS" ] ; then
        echo  " The cosmetic masking has been successful."
        echo
      else
        $DEPTEST | grep depmod
        echo
        echo Fixscript treatment was not adequate.
        echo The report is being copied to : dep.txt
        cat dep.txt >>  $RECORD
        echo  >>  $RECORD
        echo "	fixscript_record" >>  $RECORD
        cat fix.txt  >>  $RECORD
        cp dep.txt ../
        echo It is essential to acquire or compile
        echo "	 kernel-headers which version match your kernel."
        echo Read DOCs/Compile-fail.txt for instructions.
        echo $MSG
        echo >>  $RECORD
        echo $MSG >>  $RECORD
        echo 
        exit 1
      fi
    fi 
  done  
  echo
  echo There are occassionally failures in the more global test:
  echo "	depmod -a"
  echo executed after the drivers are installed, reflecting a lack
  echo of exact Flavor matching of the kernel-headers and kernel-$SYS
  echo Thus far this problem has only been encountered for Red Hat Linux
  echo
fi
# End of dependencies test section.
echo
echo "==========================================================================="
echo  >> $RECORD
echo

if [ -z "$FAST" ]; then
  read -p "	Enter, to finish $0"
  echo
fi

if [ $TEST = 0 ]; then

# Storage folder for drivers
if [ -n "$FV" ] ; then
   MISFOLDER=../drivers-$FV
else 
   MISFOLDER=../drivers-$SYS
fi
if test ! -d $MISFOLDER ; then
    mkdir $MISFOLDER
fi
echo Copying newly compiled drivers to folder $MISFOLDER as a backup,
echo "  as the source/ folder will be deleted during next run of:  $BN"
cp lt*.o $MISFOLDER 
rm $MISFOLDER/ltmdmobj.o
ls -l $MISFOLDER 
echo


## Back out of source/
cd ..
# RECORD=BLDrecord.txt

LINKS="$LT_PROPRIETARY_MODULE.o $LT_SERIAL_MODULE.o autoload ltinst2 ltuninst2"
# Removing symbolic links if kernel-headers and kernel-version are mis-matched or not build_module
if test -n "$FV" || test "$BN" != "build_module" ; then
  echo Removing installation links if necessary:
  echo which should NOT be used when the modem drivers 
  echo are Not version or flavor matched with the current kernel-$SYS
  echo or after assembly of Debian or RPM installer packages.
  for FILE in $LINKS
  do
    if [ -L $FILE  ]  ;  then
      rm $FILE  
      echo Removed symbolic link:	$FILE
    fi
  done
  if test -n "$FV" ; then
    echo
    echo Two steps in the complete installation procedure are not implemented
    echo when a forced versioning is implemented : FORCED=$FORCED
    echo is implemnted:
    echo "First, the modem device nodes are not made;"
    echo "Second autoloading of the drivers are not provided for."
    echo "Do browse the files in DOCs/ should you wish to proceed thus By Hand."
    echo
  fi
else
# Making symbolic links for continuing installation, if not present already.
  echo 
  if [ "$BN" != "build_module" ] ; then
    LINKS="$LT_PROPRIETARY_MODULE.o $LT_SERIAL_MODULE.o"
  fi
  echo Making symbolic links: $LINKS 
  for FILE in $LINKS
  do
    if [ ! -L $FILE   -a  -f  $BASE/$FILE ]  ;  then  
      ln -s $BASE/$FILE $FILE  
    fi
  done
fi

## always KEEP ltuninst2 and cleanup links
# echo Verifying cleanup links.
KEEP="cleanup ltuninst2"
for FILE in $KEEP
do
  if [ ! -L $FILE   -a  -f  $BASE/$FILE ]  ;  then
    ln -s $BASE/$FILE $FILE
  fi 
done                                                     

fi #TEST=1

if [ -z "$FAST" ]; then
if [ -n "$FV" ] ; then
  echo For useful methods of packaging drivers read DOCs/Options.txt
else
  echo
  echo "A diagnostic record has been saved to $RECORD and $RECORD.2"
  echo "To install the modem drivers in the Search Path for modules,  run:"
  echo "     ./ltinst2"
  echo "This command does require Root permission."
  echo "For advanced User alternatives to this installation route,"
  echo "involving assembly of RPM or Debian installers, read DOCs/Options.txt"
  echo
fi # FV
# Reminder to save and terminate the   ltrecord.txt
if [ -f ltrecord.txt ] ; then
  echo "The scripting ltrecord.txt can be continued through "
  echo "	./ltinst2  and  ./autoload"
  echo " or stopped and Saved now by:"
  echo "    exit"
fi
fi # FAST
echo
