#!/bin/sh
#
# urpmb - user rpm builder, v0.6
#
# (c) 2003 Michael Reinsch <mr@uue.org>
# licenced under GPL (see http://www.gnu.org/licenses/gpl.html)
#

while getopts "s" opt ; do
	case "$opt" in
		s) sign=$opt ;;
	esac
done
shift $((OPTIND - 1))
specfile=$1
shift
buildopts=$*

if [[ -z "$specfile" || ! -e $specfile ]] ; then
	cat << EOF

 Usage: $0 [<options>] <specfile> [<rpmbuild_options>]

 urpmb expects all required sources and patches to build the rpm(s) from the
 specified specfile to be in the same directory as the specfile.

 urpmb will create a directory called RPMS in the current directory where
 all created *.rpm files will be placed.

 options:
  -s   sign the package

 rpmbuild_options: (passed to rpmbuild)
  -ba  build binary and source packages (default)
  -bb  build binary package
  -bp  executes the "%prep" stage from the spec file
  -bc  do the "%build" stage from the spec file
  -bi  do the  "%install"  stage  from  the spec file
  -bl  do a "list check"
  -bs  build just the source package

EOF
	exit
fi

if [[ ! `echo "$buildopts" | grep -q -e "-b[a|b|p|c|i|l|s] "` ]] ; then
	buildopts="$buildopts -ba"
fi
if [[ -n "$sign" ]] ; then buildopts="$buildopts --sign" ; fi

if [[ ! -e ~/.rpmrc ]] ; then
	echo "creating ~/.rpmrc"
	cat << EOF > ~/.rpmrc
buildarchtranslate: i386: i586
buildarchtranslate: i486: i586
buildarchtranslate: i586: i586
buildarchtranslate: i686: i586
EOF
fi

if [[ ! -e ~/.rpmmacros ]] ; then
	echo "creating ~/.rpmmacros"
	myname="$(getent passwd $USER|awk -F: '{print $5}')"
	myemail="$USER@$(hostname)"
	cat << EOF > ~/.rpmmacros
%_target		linux

%_signature		gpg
%_gpg_name		$myname <$myemail>
%_gpg_path		~/.gnupg

%distribution		Mandrake Linux
%packager		$myname <$myemail>
EOF
	echo "You might want to edit ~/.rpmmacros before you continue"
	exit
fi

if [[ ! -e ./RPMS ]] ; then
	echo "creating build dir"
	mkdir ./RPMS
	ln -s /var/tmp ./RPMS/BUILD
fi

# let's see what files the spec file will produce...
prodfiles=`rpm -q --specfile $specfile`
[[ $? != 0 ]] && exit
if `echo $prodfiles | grep -q "plf$"` ; then
        vendor="plf"
elif `echo $prodfiles | grep -q "mdk$"` ; then
        vendor="mdk"
elif `echo $prodfiles | grep -q "jpp$"` ; then
        vendor="jpp"
fi

if [[ -n "$vendor" ]] ; then
	echo "seems to be a $vendor package"

	if [[ "$vendor" = "plf" ]] ; then
		vendor="vendor Penguin Liberation Front"
	elif [[ "$vendor" = "mdk" ]] ; then
		vendor="vendor MandrakeSoft"
	elif [[ "$venor" = "jpp" ]] ; then
		vendor="vendor JPackage Project"
	else
		# this also sux, we have to specify something here,
        	# but we want the default!
		vendor="___vendor NoOne"
	fi
fi

specdir="$(pushd $(dirname $specfile) > /dev/null ; pwd ; popd > /dev/null)"
curdir="$(pwd)"

# this sux, but def="$def --define \"...\"" doesn't work :(
srcdir="_sourcedir $specdir"
topdir="_topdir $curdir/RPMS"
rpmdir="_rpmdir %_topdir"
tmpdir="_tmppath %_topdir/BUILD"
srcrpmdir="_srcrpmdir %_rpmdir"
form="_build_name_fmt %%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm"

echo rpmbuild $buildopts $specfile
rpmbuild --define "$srcdir" --define "$topdir" --define "$rpmdir"\
	--define "$tmpdir" --define "$srcrpmdir" --define "$form"\
	--define "$vendor" $buildopts $specfile

