#!/bin/bash
#
# Usage: build_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"
		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

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

END

read -p "To continue:	Enter"

CPUSYS=`uname -m`
if test -n "$TARGET" ; then
  # check for proper nomenclature
  CPU_LIST="i486 i586 i686 ia64 k6 athlon elan crusoe cyrix"
  CPUtest=`echo $TARGET  | cut -d' ' -f2`
  FV=$1
  for x in $CPU_LIST
  do
    if [ "$CPUtest" = "$x" ] ; then  
      CPU=$x 
#      echo Trapped CPU=$CPU
    fi
  done
  if test -z "$CPU" ; then
    cat<<END

  The suggested entry for a processor:  $CPUtest
      is not recognized among the recognized list:
     `echo  $CPU_LIST`
  Continue if you wish or abort with Ctrl-C

END

  read -p "To continue:	Enter"
  CPU=$CPUtest
  fi
else
  CPU=`uname -m`
fi

export FV=$FV
export CPU=$CPU
SYS=`uname -r`

cat<<END

 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.i586.rpm
 in which case             CPU ==  i586   and the installer name will be like
    ltmodem-kv-$SYS-$VER-1.i586.rpm
 without a CPU specification, the default will be your system CPU:  $CPUSYS
END
if test -z "$FV" ; then
  SHOW=$SYS
else
  SHOW=$FV
fi
echo   as a Best Guess. The current setting is:
echo "	ltmodem-kv-$SHOW-$VER-$CPU.rpm"
echo
read -p "To continue,	 Enter"
cat<<END

 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
fi

make -e rpm | 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

cat <<END

Install the generated rpm with the command

rpm -i $BASEFILE

or

rpm -Uvh $BASEFILE

END

else
echo rpm build failed
fi
rm -r /tmp/ltmodem
echo

