#!/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.
#
#

echo Build an rpm for ltmodem

export FAST=1

#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
		;;
	* )
		break
		;;
esac
done

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

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

