#!/bin/bash
#  Build a Debian installer package for modem drivers
# Depends: debhelper, debconf-utils, perl, rpm, dpkg-dev, make, cpio

VER=8.26a9
case $VER in
	*RELEASE* ) 
		VER=6.00test
		;;
	* )
	;;
esac

Ver=0.5
# for winmodemlt

# Check permissions
if [ $UID -ne 0 ]; then
echo The package assembly section of $0 requires root/superuser permission.
echo Alternatively, there will suffice:
echo "   fakeroot $0"
echo "with fakeroot available from a Debian repository as fakeroot-SomeVersion.deb"
        exit 2
fi
cat<<END

 This $0 first runs build_module and then assembles  installer.DEBs, 
     ltmodemdrv-version.deb   for installing drivers
     winmodemlt-version.deb   for complementary utilities and documentation  
 First confirm that the there is matching between versions of the kernel
 and its kernel-headers for compiling drivers as displayed by:
    uname -r
 Otherwise with a niave usage, all processes could be completed well technically.
 BUT the installed drivers might not be compatible with your kernel,
   of ever reside in the right /lib/modules/Kernel_Version/ tree.

 To use kernel-headers deliberately NOT matching the current kernel,
 use nomenclature such as
    $0 target_version
 with target_version the "uname -r" of the matching kernel.

 Also do run ./build_module which steps through informatively 
 before a first use of   $0
 If ./build_modules completes without error, then run $0

END
read -p "To continue,    Enter"

echo
echo Building a Debian installer for ltmodem
export FAST=1

echo "Abort with Ctrl-C anytime, to terminate or restart."
read -p "	or Enter to continue."

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="--with-kernel=/usr/src/kernel-headers-$1"
	FV=$1
	;;
esac
shift;
done

BN=`basename $0`
export BN=$BN
export FV=$FV

# source build_module
.  build_module

if [ $TEST = 0 ]; then
  cd source
fi

cat <<END

 With the successful Configuration trial,
 ready to compile and assemble the Debian package.
 Lines below containing db3, package and SuSE are not relevant,
 being only germane to applications of configure in rpm environments.

END
export CONF_OPTS=$FORCED
echo CONF_OPTS=$CONF_OPTS
echo
cp debian/changelog.ltmodemdrv debian/changelog
cp debian/control.ltmodemdrv debian/control
echo Using debian/rules2 to build the new drivers Only package.
echo Starting debian/rules2 binary
echo
sleep 1
debian/rules2 binary | grep -v echo | grep -v autoload
  
if [ $TEST = 0 ]; then
  cd ..
fi
LTNAME=`ls ltmodem* | grep deb`
tree source/debian/tmp/ > LtmodemTree.txt

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

echo
BASE_DEB=winmodemlt
BUILD=`ls "$BASE_DEB"* 2>/dev/null | grep deb`
if test -n "$BUILD" ; then
  echo The ltmodem.deb dependent package $BUILD is present.
  echo "Hence skipping its assembly."
else
  echo The ltmodem.deb installer is dependent on prior installation of a:
  echo "	$BASE_DEB-SomeVersion.deb"
  echo  Preparing to build a winmodemlt.deb using debian/rules3 binary    
  echo  "Either abort with   CTRL-C,   or to Continue: "
  read -p "	Enter"
  cd source 
  debian/rules3 clean
  cp debian/changelog.winmodemlt debian/changelog
  cp debian/control.winmodemlt debian/control
  cp debian/conffiles.winmodemlt debian/conffiles
  debian/rules3 binary
  cd ..
  tree source/debian/tmp/ > WinmodemltTree.txt
fi
BASENAME=`ls "$BASE_DEB"* | grep 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

ls -l *.deb
cat<<END

 Install the generated DEBs successively with the commands:
     dpkg -i $BASENAME
 Followed by:
     dpkg -i $LTNAME

 The drivers can be functionally installed alone by:
     dpkg --force-i $LTNAME
 But the device node and autoloading lines removal 
 Only accompany removal of $BASE_DEB
 This prevents elimination of modem support for alternative boot kernels,
 when a ltmodem drivers package for a single kernel is removed.
 Additionally, installation of $BASEDEB is strongly recommended.
 It has several useful utilities and 
 the general ltmodem Documentation which is indepdendent of kernel-version.

END
rm -r /tmp/ltmodem
echo
