#!/bin/bash

echo Build an rpm for ltmodem

VER=5.78e
case $VER in
	*RELEASE* ) 
		VER=5.78-test
		;;
	* )
	;;
esac

BASE=ltmodem-$VER

if [ $UID -ne 0 ]; then
echo $0 must run as root/superuser
fi

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

checkpkg () {
	$* > /dev/null 2>&1 ; [ $? -eq 127 ] && {
		echo Requires $1 package to build. please install
		exit 3
	}
}

checkpkg make -f ""
checkpkg gcc
checkpkg tar

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=--target=$1
	echo target rpm overriden as $1
	;;
esac
shift;
done

rpm -ta --clean $TO $BASE.tar.gz | tee build.log

FILE=`grep "^Wrote:" build.log | grep -v "\.src" | cut -d " " -f2`
BASEFILE=`basename $FILE`
rm -f build.log
rm -f $BASEFILE
ln -s $FILE

cat <<END

Install the generated rpm with the command

rpm -i $BASEFILE

END

