#!/bin/sh
version=$(cat conf-version)

if [ -f /etc/debian_version ] ; then
	echo "you can't do rpmbuild on an insane system" 1>&2
	exit 1
fi

verbose=0
clean=""
while test $# -gt 0; do
    case "$1" in
    -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'`
	;;
    *) optarg=
	;;
    esac

    case "$1" in
    --verbose)
	verbose=1
    ;;
    --clean)
	clean="--clean"
    ;;
    --release=*)
	release=$optarg
    ;;
    *)
    echo "invalid option [$1]"
    read key
    usage 1
    ;;
    esac

    shift
done

if test -f $HOME/.rpmmacros
then
	topdir=`grep ^%_topdir $HOME/.rpmmacros | awk '{print $2}'`
	if test -n "$topdir"
	then
		rpmbuild=$topdir
	else
		rpmbuild=$HOME/rpmbuild
	fi
else
	rpmbuild=$HOME/rpmbuild
fi

copy_src=0
echo -n "Copy Source Files - "
read key
if [ " $key" = " y" -o " $key" = " Y" ] ; then
	copy_src=1
fi

make -s ezmlm.spec
if [ $? -ne 0 ] ; then
	echo "make failed" 1>&2
	exit 1
fi
if [ $copy_src -eq 1 ] ; then
	cd ..
	if [ -d stage ] ; then
		/bin/rm -rf stage
	fi
	mkdir stage
	if [ $? -ne 0 ] ; then
		exit 1
	fi
	cp -rp ezmlm-x stage/ezmlm-$version
	if [ $? -ne 0 ] ; then
		exit 1
	fi
	/bin/rm -f stage/ezmlm-$version/catChangeLog
	cp ezmlm-x/catChangeLog stage/ezmlm-$version/catChangeLog
	cd stage/ezmlm-$version
	if [ $? -ne 0 ] ; then
		exit 1
	fi
	make -s clean
	make -s distclean
	cd ..
	tar \
		--exclude="ezmlm-$version/.git" \
		--exclude="ezmlm-$version/debian" \
		--exclude="ezmlm-$version/RCS" \
		-cf - ezmlm-"$version" \
			| gzip -c > $rpmbuild/SOURCES/ezmlm-"$version".tar.gz
	cd ..
	/bin/rm -rf stage
else
	cd ..
fi

dist=`uname -r |cut -d . -f4`
if [ -z "$release" ] ; then
	if [ -f /usr/bin/ezmlm ] ; then
		edist=$(rpm -qf /usr/bin/ezmlm|cut -d- -f4|cut -d. -f3)
		if [ " $dist" = " $edist" ] ; then
			eversion=$(rpm -qf /usr/bin/ezmlm|cut -d- -f3)
			if [ "$eversion" = "$version" ] ; then
				release=$(rpm -qf /usr/bin/ezmlm | cut -d- -f4 | cut -d. -f2)
				release=$(expr $release + 1)
			else
				release=1
			fi
		else
			release=1
		fi
	else
		release=1
	fi
fi
echo -n "Build RPM for ezmlm-"$version"-1."$release" (Y/N) - "
read key
if [ " $key" = " Y" -o " $key" = " y" ] ; then
	tmprel=`cat ezmlm-x/conf-release 2>/dev/null`
	if [ ! " $tmprel" = " 1.$release" ] ; then
		echo 1.$release > ezmlm-x/conf-release
		cd ezmlm-x
		make -s ezmlm.spec
		cp ezmlm.spec /tmp
		cd debian
		make -s
	else
		cp ezmlm-x/ezmlm.spec /tmp
	fi
	if [ $verbose -eq 0 ] ; then
		echo "rpmbuild -ba $clean --quiet /tmp/ezmlm.spec"
		rpmbuild -ba $clean --quiet /tmp/ezmlm.spec
	else
		echo "rpmbuild -ba $clean /tmp/ezmlm.spec"
		rpmbuild -ba $clean /tmp/ezmlm.spec
	fi
	if [ $? -eq 0 ] ; then
		/bin/rm -f /tmp/ezmlm.spec
		build_arch=`rpmbuild --showrc|grep "^build arch" | awk '{print $4}'`
		rpm --addsign $rpmbuild/RPMS/$build_arch/ezmlm-"$version"-"1.$release".$dist.$build_arch.rpm
		rpm --addsign $rpmbuild/SRPMS/ezmlm-"$version"-"1.$release".$dist.src.rpm
		echo -n "RPM lint for ezmlm-"$version"-1."$release" (Y/N) - "
		read key
		if [ " $key" = " Y" -o " $key" = " y" ] ; then
			(
			echo $i
			rpmlint $rpmbuild/RPMS/$build_arch/ezmlm-"$version"-"1.$release".$dist.$build_arch.rpm
			echo ------------------------
			echo ezmlm-"$version"-"1.$release".$dist.src.rpm
			rpmlint $rpmbuild/SRPMS/ezmlm-"$version"-"1.$release".$dist.src.rpm
			echo ------------------------
			) 2>&1 | less
		fi
	else
		/bin/rm -f /tmp/ezmlm.spec
	fi
fi
if [ $copy_src -eq 1 ] ; then
	echo -n "Remove Source (Y/N) - "
	read key
	if [ " $key" = " Y" -o " $key" = " y" ] ; then
		/bin/rm -f $rpmbuild/SOURCES/ezmlm-"$version".tar.gz
	fi
fi
