#!/bin/sh
#
# Usage: rules [binary|clean]
#

if [ ! -f VERSION -o ! -f Makefile ]; then
	echo "This command must be run from the main source directory!" >&2
	exit 1
fi

RPMS="BUILD RPMS SOURCES SPECS SRPMS tmp"
DIR=`pwd`/tmp
NAME=cntlm-`cat VERSION`

if [ "$1" = "binary" ]; then
	rm -f cntlm*.rpm 2>/dev/null
	for i in $RPMS; do mkdir -p $DIR/$i; done			# Create new rpm build structure

	make tbz2
	mv $NAME.tar.bz2 $DIR/SOURCES
	cp rpm/cntlm.* $DIR/SOURCES					# Prepare build environment

	rpmbuild \
		--define "_topdir $DIR" \
		--define "_sourcedir %_topdir/SOURCES" \
		--define "_builddir %_topdir/BUILD" \
		--define "_buildrootdir %_topdir/BUILD" \
		--define "_rpmdir %_topdir/RPMS" \
		--define "_specdir %_topdir/SPECS" \
		--define "_srcrpmdir %_topdir/SRPMS" \
		-ba $DIR/SOURCES/cntlm.spec

	cp $DIR/SRPMS/*rpm . 2>/dev/null
	cp $DIR/RPMS/*/cntlm*rpm . 2>/dev/null
elif [ "$1" = "clean" ]; then
	for i in $RPMS; do rm -rf $DIR/$i; done				# Clean the whole mess, keep packages
	rmdir $DIR 2>/dev/null || true
fi
