#!/bin/bash
#this script makes a tarball suitable for running rpmbuild -ta
#
#it presumes a lot.
#
#that your package name doesn't contain a space or strange characters.
#
#that = is not in the package name in the configure script.
#
#that = is not in the version number in the configure script.
#
#that : is not in the Summary in a file called ${PACKAGE}.spec.in.
#
#that there is a spec file called ${PACKAGE}.spec.in that turns
#into ${PACKAGE}.spec after a make.
#
#that your ${PACKAGE}.spec file ends with %changelog
#
#that your ${PACKAGE}.spec file has valid entries in the %files section.
#
#that ${PACKAGE} is the name of the binary.
#

if [ ! -x configure ]; then
	autoconf
fi
if [ ! -f Makefile ]; then
	./configure
fi

PACKAGE=`grep "^AM_INIT_AUTOMAKE" configure.in | cut -f 1 -d "," | cut -f 2 -d "("`
VERSION=`grep "^AM_INIT_AUTOMAKE" configure.in | cut -f 2 -d " " | cut -f 1 -d ")"`
SUMMARY=`grep "^Summary" ${PACKAGE}.spec.in | cut -f 2 -d ":"`

#clean up and make the distribution RPM enabled
if [ -f Makefile ]; then
	make clean
	make ${PACKAGE}.spec
	cp -f ${PACKAGE}.spec ${PACKAGE}-${VERSION}.spec
	if [ -f ChangeLog ]; then
		#echo "\n" >> ${PACKAGE}-${VERSION}.spec
		#attempt to generate FSF ChangeLog from CVS with cvs2cl
		#convert the ChangeLog from FSF style to RPM style
		#append it to the end of the spec file.
		cat ChangeLog >> ${PACKAGE}-${VERSION}.spec
	fi
	make distclean
	rm -f *.o
	rm -f ${PACKAGE}
fi
#make it .deb enabled?

#rename the directory so that rpm will work correctly.
mydir=`pwd`
thisdir=`basename ${mydir}`
cd ..
if [ "$thisdir" != "${PACKAGE}-${VERSION}" ]; then
	mv $thisdir ${PACKAGE}-${VERSION}
fi
#tar up the distribution
#don't tar up cvs dirs if there are any.
#make an excludelist for the CVS dirs and pass it to tar.
find ${PACKAGE}-${VERSION}/ -name CVS -print > ${PACKAGE}.exclude
tar --exclude-from=${PACKAGE}.exclude -zchvf  ${PACKAGE}-${VERSION}.tar.gz ${PACKAGE}-${VERSION}
rm -f ${PACKAGE}.exclude
mv ${PACKAGE}-${VERSION} $thisdir
cd ${thisdir}
rm -f ${PACKAGE}-${VERSION}.spec
./configure
make
