#!/bin/bash
echo
echo " Build a Debian installer package for ltmodem 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) Do change /usr/src/linux --> 
#     when done to avoid later confusion.


VER=5.99b
case $VER in
	*RELEASE* ) 
		VER=5.95a1
		;;
	* )
	;;
esac

if [ $UID -ne 0 ]; then
echo "$0 must be run by Root/Superuser."
fi

echo
echo "  This is ltmodem-$VER compiler and Debian package builder."

# Depends: debhelper, debconf-utils, perl, rpm, dpkg-dev, make, cpio


SYS=`uname -r`

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 7

while [ -n "$1" ]; do
case "$1" in
	"--help")
	cat <<END
	Usage: $0 [--help] [target]
	target is optional and will override the default target e.g. i586
END
	exit 2;
	;;
	*)
	TO=--target=$1
	echo target deb overriden as $1
	;;
esac
shift;
done

## Getting FV, Forced kernel-header-version
FV=$1
if test -n "$FV" ; then
FORCED="--with-force=yes"
echo "Your running kernel is version-$SYS, but the kernel-header resources"
echo "  for compiling will result kernel-version-$FV drivers."
fi

###  Removing old makings and expanding the source package
BASE=source
rm -rf $BASE
tar zxf $BASE.tar.gz

cd $BASE

if [ -f Makefile ]
then
  if ! make debian
  then
    echo "make debian  --is not a supported command."
  fi
else
  if ! ./configure && make debian
  then
    echo "The configuration utility is absent!"
  fi
echo
echo Yes, Debian is the best! 
echo
sleep 2
make debian | grep -v echo
fi

cd ..

# Removing symbolic links to ltinst2 & autoload, to avoid miss use. 
MISUSE="ltinst2 autoload" 
for FILE in $MISUSE
do
  if [ -L $FILE  ]  ;  then
   rm $FILE
  fi
done


echo
echo The debian installer is:
ls -l *.deb
if [ -f ltrecord.txt ] ; then
    echo
    echo "save and terminate the   ltrecord.txt by entering:"     
    echo "    exit"
    echo "ltrecord.txt will be overwritten unless renamed, perhaps ltrecord1.txt"  
fi

echo
