#!/bin/bash
# Configures needed files and compiles the drivers.
# 
### Should you wish to compile drivers on this PC for transport to another,
#   follow these steps:
# 1) With the exception of the kernel-headers/ folder to be utilized
# rename other kernel-header resources so that they won't be recognized
# by an automated search for compile resources.
# For examples, folder   linux-2.4.2/ --> Linux-2.4.2/
#               kernel-headers-2.4.0/ --> Kernel-headers-2.4.0/
# 
# 2) Set the ususal symbolic link, for example:
#    ln -s PATHto/kernel-headers-2.2.17 /usr/src/linux
#  Enter  
#    ./build_module 2.2.17
# 3) The drivers will be written to a folder drivers-2.2.17/
# 
# 4) When done, do change /usr/src/linux --> 
#     to avoid later confusion.
#
# The consituent source/configure script has MANY more options,
# in addition to the FORCED. If interested, get info by:
#    source/configure --help  
#
# Implementation of the configure scripting for ltmodem was done by:
#      ********** Christolph Hebeisen *************
#   building on the ltmodem-5.nn series kits initiated by:
#      ********** Mark Spieth *********************
#   Marv Stodolsky has culpability for verbose messaging.  
#

# Open DOCs package
echo
if [ ! -d DOCs  -a -f DOCs.tgz ] ; then
echo Expanding DOCs folder for you.
tar xvfz DOCs.tgz
sleep 3
fi


# Define version
VER=@RELEASE@
case $VER in
	*RELEASE* ) 
		VER=5.99b
		;;
	* )
	;;
esac

## Check permissions
if [ $UID -ne 0 ]; then
	echo $0 must run as root/superuser
	exit 2
fi

echo
echo This is ltmodem-$VER compiler and installer.

SYS=`uname -r`

if [ -z "$FAST" ]; then
echo
echo To make a record for trouble shooting purposes,
echo Start again with:
echo "	script ltrecord.txt"
echo "	$0"
echo Abort with Ctrl-C to terminate now.
echo " or wait a few seconds"
echo
sleep 5
fi

# defines code folder
BASE=source
if [ -f $BASE.tar.gz ]; then
#  Removing old makings and expanding the source package
rm -rf $BASE
tar zxf $BASE.tar.gz

# move to work space
cd $BASE
TEST=0
else
TEST=1
fi

### Getting driver names from source/SETTINGS
echo
echo Drivers to be compiled are:
source SETTINGS
echo " $LT_PROPRIETARY_MODULE.o and $LT_SERIAL_MODULE.o"
echo

## Getting FV, the Forced kernel-header-version, and verifying
FV=$1
if test -n "$FV" ; then
  shift
  FVNAME=$FV
  FV=`echo -n $FV | cut -d- -f1`
  # best alternative goes last because later options override earlier ones
  KERNEL_HEAD_ALT="/usr/src/linux /usr/src/kernel-headers-$FV"
  if echo "$FV" | grep "-" >/dev/null
  then
      # kludge for mandrake kernel headers
      KERNEL_HEAD_ALT="$KERNEL_HEAD_ALT /usr/src/linux-`echo "$FV" | cut -d"-" -f1`"
  fi
  KERNEL_HEAD_ALT="$KERNEL_HEAD_ALT /usr/src/linux-$FV"

  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 "ERROR: No kernel headers found."
    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
     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 "Your running kernel is version-$SYS, but the kernel-header resources"
echo "  for compiling will produce kernel-version-$FV drivers."
fi

echo " Checking for necessary consituents, exclusive of ISA modems:"
echo "    PCI modem, then needed utilities, finally compiling resources."
echo " If all is well,"
echo "    supporting script will be written from the source/*.in files."
[ -z "$FAST" ] && sleep 3
echo
# fakeroot & package only relevant to ./build_deb & ./build_rpm .
if [ "$0" = "./build_module" ] ; then
  ./configure $FORCED | grep -v fakeroot | grep -v package
else 
./configure $FORCED | grep -v fakeroot
fi
echo

echo Trying compilation of $LT_PROPRIETARY_MODULE.o   and   $LT_SERIAL_MODULE.o
echo
make -e
echo

echo With resources provided, as good a make as possible has been done.
echo Now looking for driver products:
if [ -f  $LT_PROPRIETARY_MODULE.o ] ; then
   ls -l $LT_PROPRIETARY_MODULE.o
else
   cd ..
   cp DOCs/Compile_failure .
   echo
   echo Compilation of  $LT_PROPRIETARY_MODULE.o failed!
   echo Read Compile_failure from DOCs.
   echo
   exit 2
fi
 
if [ -f $LT_SERIAL_MODULE.o ] ; then
  ls -l $LT_SERIAL_MODULE.o
else
   cd ..
   cp DOCs/Compile_failure .
   echo
   echo Compilation of  $LT_SERIAL_MODULE.o failed!
   echo Read Compile_failure from DOCs.
   echo
   exit 2
fi
echo

if test -n "$FV" ; then
echo Skipping depmod tests because drivers and kernel 
echo "   are deliberately not version matched."
else
echo Preparing for drivers dependencies check:
  if [ -f /lib/modules/$SYS/kernel/drivers/pnp/isa-pnp.o ] ; then
    EXTRAMOD=/lib/modules/$SYS/kernel/drivers/pnp/isa-pnp.o
    ISA="isa-pnp"
    echo "  Modular isa-pnp.o is present, and needed for the dependency test."
  fi
echo
echo Running test: 
echo "  depmod -e $LT_PROPRIETARY_MODULE.o  $LT_SERIAL_MODULE.o  $EXTRAMOD"
depmod -e $LT_PROPRIETARY_MODULE.o  $LT_SERIAL_MODULE.o $EXTRAMOD | grep symbols
echo
echo Test Ended
echo "If an output above includes:	*** Unresolved symbols,"
echo then the kernel-header resources are NOT version matched 
echo with the running kernel. This may be MERELY a cosmetics problem.
echo Read the DOCs/Linmodem-HowTo concerning usage of the fixscript.
echo OR
echo Alternately, it may be Essential to import or compile kernel-headers 
echo that are version matched with your running kernel.
echo Carefully read related sections of 1ST-READ and files in DOCs/, 
echo If still necessary, ask assistance from the List: 
echo discuss@linmodems.org.
sleep 5
fi

# Special folder for version mismatched drivers 
if [ $TEST = 0 ]; then
if test -n "$FV" ; then
MISFOLDER=../drivers-$FV
echo Copying out version mis-matched drivers to Folder $MISFOLDER
  if test ! -d $MISFOLDER ; then
  mkdir $MISFOLDER
  fi
cp  $LT_PROPRIETARY_MODULE.o $MISFOLDER
cp  $LT_SERIAL_MODULE.o $MISFOLDER
ls  $MISFOLDER 
echo
fi

## Back out of source/
cd ..

LINKS="$LT_PROPRIETARY_MODULE.o $LT_SERIAL_MODULE.o autoload ltinst2"
# Removing symbolic links if kernel-headers and kernel-version are mismatched
if test -n "$FV" ; then
 for FILE in $LINKS
 do
   if [ -L $FILE  ]  ;  then
    rm $FILE
   fi
 done
# Make symbolic links, if not present already
else
 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
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

echo
echo  ============= ending "$0"  =================
echo 
# reminder to stop scripting
if test -n "$FV" ; then
  if [ -f ltrecord.txt ] ; then
  echo "save and terminate the   ltrecord.txt by entering:"     
  echo "    exit"
  echo "ltrecord.txt will be overwritten unless renamed," 
  echo "    perhaps:  ltrecord1.txt"
  fi
else
# Do installation messages only if version matched drivers are compiled.
# Warning to update ppp
KV=`uname -r | cut -c 1-3`
if [ $KV = 2.4 ]; then
echo "Upgrade pppd to at least version 2.4.0 for service under $KV kernels!"
echo "    -----------------------------++++++-----------------+++++--------"
fi
echo "To install the modem drivers and enable their demand loading,  run:"
echo "     ./ltinst2"
echo "     ./autoload"
echo "Copy the following command sets out, before they scroll away,"
echo "  then run them to learn from the outputs," 
echo "  and get information for troubleshooting reports. " 
echo "          grep lt_ /etc/modules.conf "
echo "          modprobe    lt_serial , lsmod "
echo "          modprobe -r lt_serial , lsmod , cat /proc/interrupts "
echo "	  tail -f /var/log/messages & (plus a few ENTER, followed by: ) "
echo "          wvdial & (or whatever starts your Internet dial out) "
echo "          cat /proc/interrupts"
  
echo  
echo Should complete removal of the ltmodem installation be desired, run
echo "                   ./ltuninst2"
echo "                   ./cleanup " 
#  Reminder to save and terminate the   ltrecord.txt
if [ -f ltrecord.txt ] ; then
  echo
  echo "After running test commands," 
  echo "save and terminate the   ltrecord.txt by entering:"
  echo "    exit"
  echo "ltrecord.txt will be overwritten unless renamed, perhaps:  ltrecord1.txt"
fi
fi
echo
fi	#FAST
