#!/bin/bash
#
# Usage: build_drivers_rpm [-t <target>] [<forcedversion>]
#
#-t <target>  alters the platform the rpm will be built for.
#   You can use this option to build an rpm for a different CPU to the one in
#   the computer you're using to build the rpm. eg ./build_rpm -t i486 should
#   build an rpm for a 486 machine. More information can be found in 'man rpm',
#   the argument you give ./build_rpm -t is passed to the --target option that's
#   discussed in the BUILD OPTIONS section of the rpm man page.
#
#<forcedversion> can be used to build an rpm for a kernel other than the one
#   you're currently running. To do this you first need to have the
#   kernel-headers for the kernel you want to build the rpm for installed, and
#   have a sym link from /usr/src/linux to the directory the headers are in.
#   It's also necessary to ensure that there are no other kernel headers that
#   might be picked up during the automated search for header files in /usr/src.
#
#
#
VER=8.26a9
BN=`basename $0`
export BN=$BN
mkdir -p /tmp/ltmodem
TMPM=/tmp/ltmodem/tmpfile

#get target option if it exists

while [ -n "$1" ]; do
case $1 in
	-t )
		shift
		export TARGET="--target $1"
		CPU=$1
		shift
		;;
	-t* )
		export TARGET="--target `echo -n $1 | cut -c3-`"
		shift
		;;
	-n )	# check for number of drivers to make
		shift
		export MAKECOUNT=$1
		shift
		;;
	* )
		break
		;;
esac
done

if test -n "$TARGET" ; then
  CPU=`echo $TARGET  | cut -d' ' -f2`
  FV=$1
  export FV=$FV
  echo Forced_version=$FV
  echo
  sleep 1
fi

if test -z "$CPU" ; then
  CPU_LIST="386 486 586 686 k6 athlon elan crusoe"
  echo `uname -m` > $TMPM
  for x in $CPU_LIST
  do
    if grep $x $TMPM >/dev/null ; then  
      CPU=`cat $TMPM`
      # echo Trapped CPU=$CPU
    fi
  done
else
  CPU=`uname -m`
  echo CPU is not recognized: $CPU
  echo Please report this to discuss@linmodems.org
fi
export CPU=$CPU
# echo CPU=$CPU
SYS=`uname -r`
cat<<END

 This $0 first runs build_module and then assembles a driver Installer.rpm 
 If new to compiling, please first run:    utils/ksp.sh
 to set up your kernel-sources properly. A 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 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

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

 A PREFERRED implementation of $0 is
                    $0 -t CPU
 where CPU is taken from the last section of your kernel-package name such as:
               kernel-$SYS.$CPU.rpm
 in which case             CPU ==  $CPU   and the installer name will be
    ltmodem-kv-$SYS-$VER-$CPU.rpm
 without a CPU specification, the default name will be your system CPU:  $CPU
    ltmodem-kv-$SYS-$VER-$CPU.rpm
 as a Best Guess.

 Advanced Usages
 ---------------
 To adjust to a  PC with a different processor, the nomemclature is
                    $0 -t CPU
 where CPU is one of the following (see the kernel-sources configure.help file):
   i386, i486, i586, i686, k6, athlon, elan, crusoe, etc.

 To use kernel-headers deliberately NOT matching the current kernel-`uname -r`,
 use nomenclature such as
                    $0 -t CPU target_version
 with target_version the "uname -r" of the matching kernel-headers.
 Read $BN as a text file for more information on these usages. 

 Abort with Ctrl-C at any time, or  

END

read -p "To continue,	 Enter"

export FAST=1

if [ ! -x "`type -p rpm`" ]; then
echo "Requires rpm to build an rpm"
exit 1
fi

#if ! rpm -q rpm-build 2>&1 >/dev/null ; then
#echo "Requires rpm-build package to build an rpm"
#exit 3
#fi

#do the main common build_module

# source build_module
.  build_module

if [ $TEST = 0 ]; then
  cd source
  # conserving capability to build Original unitary ltmodem.rpm
  chmod a+w ltmodem.spec
  cp ltmodemdrv.spec ltmodem.spec
fi

make -e rpmdrv | tee rpmbuild.log
FILE=`grep "^Wrote:" rpmbuild.log | grep -v "\.src" | cut -d " " -f2`

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

if [ -n "$FILE" ]; then
  BASEFILE=`basename $FILE`
  rm -f rpmbuild.log
  rm -f $BASEFILE
  ln -s $FILE
else
  echo rpm divers build failed
  echo Exiting
  echo exit
fi


echo 
DEPENDS=winmodemlt
echo Starting $DEPENDS.rpm section. First checking for a $DEPENDS.rpm
BUILD=`ls "$DEPENDS"* 2>/dev/null | grep rpm`
if test -n "$BUILD" ; then
  echo The ltmodem.rpm dependent package $BUILD is already present.
  echo "Hence skipping its assembly. "
  echo
else
  echo The package $DEPENDS.rpm is needed to support $BASEFILE installation.
  echo Preparing to build.
  echo
  sleep 3
  cd source
#  rm $FILE
  make -e winmodemltrpm | tee rpmbuild.log
  FILE=`grep "^Wrote:" rpmbuild.log | grep -v "\.src" | cut -d " " -f2`
  if [ $TEST = 0 ]; then
    cd ..
  fi

  if [ -n "$FILE" ]; then
    BASEFILE2=`basename $FILE`
    rm -f rpmbuild.log
    rm -f $BASEFILE2
    ln -s $FILE
  fi
fi

ls -l *.rpm
cat <<END

Install the generated RPMs successively with the commands:
     rpm -i $BASEFILE2
Followed by:
     rpm -i $BASEFILE
or
     rpm -Uvh $BASEFILE

END
echo
