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

TMPM=0tmpfile
VER=8.31a10
case $VER in
	*RELEASE* ) 
		VER=6.00test
		;;
	* )
	;;
esac

# 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 an installer.deb 
 for the compiled drivers. First verify that your kernel_version
 and kernel-headers versions are matched.  Otherwise with a
 Niave usage, all procedues 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

   Abort with Ctrl-C anytime, to terminate or restart

END
read -p "To continue,    Enter"

export FAST=1

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

  echo "With the successful Configuration trial,"
  echo "ready to compile and assemble the Debian package."
  echo "   Lines below containing db3, package and Suse are not relevant,"
  echo "   being only germane to applications of configure in rpm environments."
  echo
  export CONF_OPTS=$FORCED
  echo CONF_OPTS=$CONF_OPTS
  echo
  debian/rules binary | grep -v echo
  sleep 3
  
if [ $TEST = 0 ]; then
cd ..
fi

# 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
echo The installer package specs are:
ls -l ../*.deb
echo "This package can be used on  any PC with kernel-headers," 
echo "   or derivative kernel, identical to those used in this compilation."
echo "For conversion to non-Debian formats, get information from: man alien."
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
echo Displaying the packaged directory structure:
tree -d source/debian/tmp/ | grep -v DEBIAN
echo
rm -r $TMPM &>/dev/null
