#!/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.31a10
case $VER in
        *RELEASE* )
                ;;
        * )
        ;;
esac

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

# FV == Forced Version
SYS=`uname -r`
export SYS
if test -n "$FV" ; then
 TARGET_VERSION=$FV
else
 TARGET_VERSION=$SYS
fi

MAJOR=`echo $TARGET_VERSION | cut -d. -f1-2`
export MAJOR
KVER=`echo  $TARGET_VERSION | cut -d- -f1`
export KVER
KEXT=`echo  $TARGET_VERSION | cut -d- -f2`
export KEXT
# for rpm Distro  -CPU.rpm with CPU exported from build_rpm
if test -z "$CPU" ; then
  CPU=`uname -m`
fi
if [ "$MAJOR" = "2.2" ] ; then
  cat<<END

 The core DSP code represented by the versions 8.nn  ltmdmobj.o  is not compatible with 2.2.nn source code.
 Please use the ltmodem-6.00c.tar.gz to build drivers and installers for 2.2.nn kernels.

 Exiting

END
  exit
fi

TMPM=./tmpfile
export TMPM=$TMPM

GCC_INPUT=`cat /proc/version`
GCC_PC=`for i in $GCC_INPUT ; do echo $i ; done | grep -A2 gcc | grep -A1 ersion | grep -v  ersion`
GCC_TEST=`echo $GCC_PC | cut -d. -f1`
echo $GCC_TEST > $TMPM
if grep "-" $TMPM >/dev/null ; then
  GCCKNL=`echo $GCC_TEST | cut -d- -f2`
else
  GCCKNL=$GCC_TEST
fi
cat<<EOF>>/dev/null
# MarkS - this prohibition no longer necessary
if [ "$GCCKNL" = "2" ] ; then
  cat<<END

 For Linux kernels assembled with a version 2.9n compiler,
 the ltmodem-8.26a.tar.gz at http://ltmodem.heby.de must be used.
 This kernel was assembled with a compiler of version:
      $GCC_PC

 Exiting

END
  exit
fi
EOF

# 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_Systems(AS) DSP chip modems (but Not the AMR or soft PCI chipsets)
 are supported by the the resources of this kit.
 AS maintainer S. Sarkar develops the code on a Red Hat Linux test bed. 
 The lt_serial.o Serial Interface module comes under GPL and is Open Source.
 The lt_modem.o code is entirely proprietary, though partially Open Source.
 Adaptation to ongoing changes in Linux source code is enabled by the Open Source components.
 Critical AS proprietary information is pre-complied in   ltmdmobj.o,
 whose constituents are functionally not affected by minor Linux source code changes.

 Volunteer maintainers periodically receive code updates from AS. 
 Through the contributions of Many described in CREDITS, there are
 enhancements in utility, ease of installation and documentation.
 Mark Spieth initiated this ltmodem compiler kit series.
 Chris Hebeisen introduced the Configure implementation, bringing Newbie friendliness.
 Marv Stodolsky is culpable for the verbose messaging.
 Within DOCs/, Ltmodem.html has URLs to many particular usage cases.
 Volunteers assemble the ltmodem-version.deb and
 ltmodem-version.rpm Installers for the User Community as Linux source code evolves 
 utiliizing the resources of this compiler kit series.

 If new to compiling, read the ModemCompiling.txt which is being output, to guide
 proper set up your kernel-sources. The kernel-source package as installed does
 NOT in general match an installed kernel, but one of several alternative kernels.
 Thus 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 needed /lib/modules/Kernel_Version/ tree.

END
read -p "To continue,	 Enter"
cat<<END

 This compilation process is likely Not necessary to work through,
 if the only need is modem drivers matching the standard kernel of the
 following Linux distributions: Debian, Red Hat, Mandrake, SuSE and Connectiva

 Volunteers have already compiled most standard drivers. For more current releases,
 those available compatible with your System requirements will be later listed.
 They are available for download as ltmodem-version.rpm and
 ltmodem-version.deb Installers from a repository, http://ltmodem.heby.de
 If your System can use one of these Installers, you will be informed below.
 Many installers for older kernels are available at:
    http://linmodems.technion.ac.il/packages.html

 The compiling process is a learning experience for Newbies.
 We strongly recommend that you DO learn it sometime.
 But getting on the Internet under Linux should have first priority.
 This kit provides several diagnostics aids and good documentation.
 Continuing will do no harm.  Useful diagnostic information
 on your system will be written to BLDrecord.txt,
 with direction to documentation if a failure occurs.

 At a minimum, Do keep the DOCs/ and utils/ folders and become familiar
 with the resources therein.

END
read -p "To continue:  Enter"
echo "================================================================="
AGERE=`echo $VER | cut -c 1-4`
VEREND=`echo $VER  | cut -c 5-9`
cat<<END

 This is the version $VER of the compiler + installer kit
 	$AGERE	corresponds to the version of the core Agere Systems DSP code.
 	    $VEREND  designation that follows reflects NO improvement in DSP code.
 Rather peripheral driver, added diagnostics and installer improvements
 are reflected.  They are progressively implemented as Users report problems
 or the Linux kernel evolves.

 The modem drivers will serve with Linux kernels beginning with 2.2.2, the current
 2.4.nn series, and the 2.5.nn development kernels.
 However support was broken in the early 2.4.n-test series.
 When there is a Red Hat 2.6.nn release, AS will then update the code

 If your PC is being well served with this $VER,
 there is NO Benefit in using a more recent version
 until there is an update in the Agere driver code, say 8.26 to 9.00.

 But if the driver installation for a new kernel should in some way fail,
 please do download the most recent ltmodem-VER.tar.gz
 before sending a query to discuss@linmodems.org,
 because the problem may already be solved.

 This $BN script only compiles drivers,
 with their installation managed by subsequent commands.
 Thus $0 can be rerun for education or amusement,
 without as yet committing changes to your System.

END
read -p "For general instructions:  Enter"
cat<<END
 =================================================================
 All should work automatically for most Linux installations/distributions,
 	IF a complete kernel-headers/ folder is available:
 through a symbolic link:	/lib/modules/KernelVersion/build --> PATH_to/kernel-headers/
 at the classical /usr/src/ position.
 Otherwise a symbolic link will be necessary:
 ln -s PATH_to/kernel-headers/ /usr/src/linux
 Read ModemDriverCompiling.txt if an explanation is needed about kernel-headers.

 To compile modules for a kernel other version than $SYS, such as version 2.2.17-3test, use syntax:
 	$0 2.2.17-3test
 where the version.h in the kernel-headers/ folder has 2.2.17-3test

END
### 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.  Then the process can be recorded  to ltrecord.txt :
echo 
echo "	 $0 $1 $2 $3 $4 | tee ltrecord.txt"
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

# Acquire distribution data
DISTRO_FILES="redhat-release SuSE-release mandrake-release conectiva-release  \
   bluepoint-release  slackware-version  gentoo-release debian_version knoppix_version fedora-release"
   # 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
    if [ -n "$DISTR" ] && [ "$DISTRO" = "fedora-release" ] ; then
      FEDORA=1
    else
      DISTR=`ls /etc/$i | cut -d/ -f3 | cut -d"-" -f1`
    fi
    if [ "$DISTR" = "debian_version" ] ; then
       DISTR=debian
    fi
    if [ "$DISTR" = "knoppix_version" ] ; then
       DISTR=knoppix
    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
cat<<END>$RECORD

 Together with information included in DOCs/
 this report may enable you to solve problems.
 But if further help is needed, send $RECORD to discuss@linmodems.org
 Please use the following in the email Subject Line:
   SUBJECT=Lucent modem, $DISTR $DVERSION $SYS
 $SUBJECT 
 DISTRO=$DISTRO 
 DISTR=$DISTR
 DVERSION=$DVERSION
 ACTION="$0 $1 $2 $3 $4 $5
 WHOAMI=`whoami`
 TARGET_CPU=$CPU
 `date` 
 `uname -a`
 
END
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
else
  echo " No false /usr/include/  symbolic link">>$RECORD
fi
if [ "$TARGET_VERSION" = "2.6.5-1.358" ] ; then
  cat<<END | tee -a $RECORD
          
 Fedora 2 with kernel version-2.6.5-1.358, is not supported by this ltmodem resource.
 It has an undersirable feature for compiling, which is improved in later kernel updates.
 There are two options Lucent/Agere DSP chipset support:
 1) The easiest is to update the System with any more recent fedora 2 kernel-image package.
 Kernel-headers needed for compiling will be co-installed into
 	/lib/modules/KernelVersion/build/ 
 Then build_module from the ltmodem-8.31aN.tar.gz at http://ltmodem.heby.de will succeed.
 2) To get online under kernel 2.6.5-1.358, 
 Install and configure a kernel-source-2.6.5 package through "make bzImage", including:
 	cd /usr/src/linux
 	make mrproper
	cp /boot/config-2.6.5-1.358 .config 
		and do NOT forget the "." before config
	cp Makefile Makefile.orig
		Edit the 4th line ONLY of the Makefile to:
		EXTRAVERSION  =  -2.6.5-1.358
	make oldconfig
	make bzImage
 which will include assembly of needed kernel-headers	 
 Then use the 2.6.5-1.358 proficient ltmodem-2.6-alk-6.tar.bz2  served from:
 	http://linmodems.technion.ac.il/packages/ltmodem/kernel-2.6/
 Read its documentation carefully as manually port creation and some line editing is needed.
            
END
  echo 
  echo Read BLDrecord.txt, now aborting.
  echo
  exit
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.30a, 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, log into a console with:   su - 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
  echo CPU='"'$CPU'"' >> $BASE/SETTINGS
  # Accomodating SuSE nomenclature in ltmodem.spec
  if [ "$DISTR" = "SuSE" ] && [ "$CPU" = "athalon" ] ; 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
  ln -s ../$TMPM $TMPM
  echo Setting  $TMPM link within $BASE/ folder.
  ls -l $TMPM
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" -o "$DISTRO" = "mandrake-release" ] ; 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 /usr/src/linux-$FVNAME /lib/modules/$FVNAME/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 -I$FVDIR/include -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_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 "KERNEL_DIR :=" Makefile | cut -d' ' -f3`
    HEADERS_IN=$HEADERSIN/include/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
    if test -z "$FAST" ; then
      echo  To continue:
      read -p "   Enter"
    else
      sleep 3
    fi

    if [ "$MAJOR" = "2.4" ] ; then
    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
       cat<<END | tee -a $RECORD

 Sorry, assembly of the needed header file:    modversions.h"
 was not specified during configuration of the kernel sources.
 Withing the section LOADABLE MODULE SUPPORT:
   [*]   Set version information on all module symbols
 which is necessary to specify the assembly of modeversions.h
 Consequently compiling of the drivers will fail at the next step.
 Properly configuring your sources may be aided by for RPM using Distros"
 by:	 utils/srcprep"
 for RPM using Distros
 and for Debian style Distros, the resources of kernel-package.deb

END
      echo MODVERH=$MODVERH   >> $RECORD
    fi # MODversiohns
    fi # 2.4
    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
MAKE=`make -v | grep version | cut -d, -f1 | cut -d' ' -f4`
    if test -z "$MAKE" ; then
      MAKE=`make -v | grep Make | cut -d' ' -f3`
        if test -z "$MAKE" ; then
          MAKE="missing, please install:  make"
        fi
    fi
  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"
    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" >> $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.

END
  cat $TMPM
  cat $TMPM >> $RECORD
  if test -z "$FAST" ; then
    echo  To continue:
    read -p "	Enter"
  else
    sleep 3
  fi
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

cat<<END
 To avoid faulting Systems with an Enviromental setting:	make -e
	make
 is the compiling command used with build_module.
 To modify "make" invocation on your System, if desirable,
 edit the  build_module line below:  MAKEE

END
sleep 3
## MAKEE
make  | grep -v echo 2>&1 |  tee -a $RECORD

echo
echo "Checking for driver products:"
if [ "$MAJOR" = "2.4" ] ; then
  SUF=o
else
  SUF=ko
fi
MODULES="$LT_PROPRIETARY_MODULE.$SUF $LT_SERIAL_MODULE.$SUF"

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 [ -z "$MODVERH" ] && [ "$MAJOR" = "2.4" ] ; 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

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.$SUF"
  DEPMOD2="$DEPMOD -e $LT_PROPRIETARY_MODULE.$SUF $EXTRAMOD $LT_SERIAL_MODULE.$SUF"
  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.$SUF $LT_PROPRIETARY_MODULE.orig
        echo "	../utils/fixscript $LT_PROPRIETARY_MODULE.orig $LT_PROPRIETARY_MODULE.$SUF"
        ../utils/fixscript $LT_PROPRIETARY_MODULE.orig $LT_PROPRIETARY_MODULE.$SUF > fix.txt
      else
        mv $LT_SERIAL_MODULE.$SUF  $LT_SERIAL_MODULE.orig
        echo "	../utils/fixscript $LT_SERIAL_MODULE.orig $LT_SERIAL_MODULE.$SUF"
        ../utils/fixscript $LT_SERIAL_MODULE.orig $LT_SERIAL_MODULE.$SUF > 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*.$SUF $MISFOLDER
rm $MISFOLDER/ltmdmobj.o
ls -l $MISFOLDER
echo


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

LINKS="$LT_PROPRIETARY_MODULE.$SUF $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.$SUF $LT_SERIAL_MODULE.$SUF"
  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 " To continue the ltrecord.txt in the next step, use "
  echo "	./ltinst2 | tee -a ltrecord.txt"
  echo
fi
fi # FAST
rm $TMPM*

echo
