#!/bin/sh
# Shell script to automatically build an rpm from a cvs snapshot of the
# source and the latest packaging files from a distributions cvs (ie Mandrake)
# Buchan Milne <bgmilne@linux-mandrake.com>
# 25 Sept 2002

PACKAGE=$1
shift

# Some definitions
#file locations:

# These you probably need to change
CVS_TREE=$HOME/downloads/source/cvs
PKG_TREE=$CVS_TREE/$PACKAGE
DIST_TREE=$CVS_TREE/mandrake/
SPEC_DIR=SPECS

# These should (hopefully) be left
RPMDIR=`rpm --eval "%{_topdir}"`
RPMSOURCEDIR=`rpm --eval "%{_sourcedir}"`
RPMSPECDIR=`rpm --eval "%{_specdir}"`
SPEC_TREE=$DIST_TREE/$SPEC_DIR
SPEC_FILE=$SPEC_TREE/$PACKAGE/$PACKAGE.spec
LOGFILE=$RPMDIR/$PACKAGE.log

#check that directories exist:
for DIR in $DIST_TREE  $RPMDIR $RPMSPECDIR $RPMSOURCEDIR; do
	if [ ! -d $DIR ] ; then
		echo "ERROR:$DIR not found, creating"
		mkdir -p $DIR
	fi;
done;
if [ -d $SPEC_TREE/$PACKAGE ] ; then
	echo "Found $SPEC_TREE/$PACKAGE";
else
	echo "The CVS packaging directory for $PACKAGE ($PKG_TREE) was not found"
	echo "Would you like to check this out [(y)/n]: "
	read ANS
	if [ ! $ANS = "n" ];then
		(cd $DIST_TREE;
		cvs co $SPEC_DIR/$PACKAGE;)
	fi;
fi 

#check that important files exist:
#for FILE in $SPEC_FILE;do
	if [ -f $SPEC_FILE ]; then
		echo "Found file $SPEC_FILE"
		echo "Updating `basename $SPEC_FILE` in SPECS"
		cp -f $SPEC_FILE $RPMSPECDIR
	else
		echo "ERROR: $SPEC_FILE not found, exiting"
		exit 1
	fi;
#done;

#find version and release:
#RELEASE=$(grep "%define" $SPEC_FILE|grep rel|cut -d' ' -f3)
#VERSION=$(grep "%define" $SPEC_FILE|grep ver|cut -d' ' -f3)
#echo "Packaging $PACKAGE-$VERSION-$RELEASE"


# Source compression options:
ARCH_SUFFIX="tar.bz2"
ARCH_OPTS="--exclude=CVS -cjf  $RPMSOURCEDIR/$PACKAGE-$VERSION.$ARCH_SUFFIX $PACKAGE-$VERSION"
#ARCH_OPTS="-cjf $RPMSOURCEDIR/$PACKAGE-$VERSION.$ARCH_SUFFIX $PACKAGE-$VERSION"

if [ -d $PKG_TREE ];then
# Check if the top level CVS dir in the package is newer that the tarball:
# This is a bad method, no idea how to improve it ...
	if [ $PKG_TREE/CVS/Entries -nt $RPMSOURCEDIR/$PACKAGE-$VERSION.$ARCH_SUFFIX ];then
		#make a new snapshot from cvs of the package:
		START_DIR=$(pwd)
		echo "UPDATING SOURCE FROM CVS!"
		cd $CVS_TREE
		mv $PACKAGE $PACKAGE-$VERSION
		tar $ARCH_OPTS 
		mv $PACKAGE-$VERSION $PACKAGE
		cd $START_DIR;
	else
		echo "TARBALL IS NEWER THAN CVS, NOT UPDATING!!";
	fi;
fi

#create new snapshots of the patches from distributions tree:
SOURCES=`awk '/^Source/ {print $2}' $SPEC_FILE |grep -v "%{"`
PATCHES=`awk  '/^Patch/ {print $2}' $SPEC_FILE`

for i in $PATCHES $SOURCES;do
	i=$SPEC_TREE/$PACKAGE/$i
	# We have two cases to fix, one where it's bzip2'ed
	# in the spec and not in CVS, one where it's bzip2'ed
	# in CVS but not in the spec
	[ -e $i ] && cp -av $i $RPMSOURCEDIR
	i_nobz2=`echo $i|sed -e 's/.bz2$//'`
	i_bz2=$i.bz2
	[ -e $i_nobz2 ] && bzip2 -kf $i_nobz2  && mv -fv $i $RPMSOURCEDIR
	[ -e $i_bz2 ] && bunzip2 -kf $i_bz2 && mv -fv $i $RPMSOURCEDIR
done
								
rpm -ba $SPEC_FILE $@ 2>&1|tee $LOGFILE

