#!/bin/bash 
# build script
#
# $Id: build.sh 694 2004-11-21 20:00:02Z joergs $
#
# builds RPM in an clean chroot build environment.
# uses the Suse build script.
#
# without parameter, it checks the directory for the files
# SOURCES or SPECSOURCES. 
# The files definied in there are copied to the 
# build system.
# 
# when used with parameter, 
# this parameter is expected to be a source rpm
#
# config file:
# /usr/local/etc/build
# lists the sources for the different distributions
#
# the rest is controlled by environment variables
#
# build.sh uses sudo su. /etc/sudoers needs a line like:
# %users  ALL=(ALL)       NOPASSWD: /bin/su
# for automatic building

CONFIG_FILE=/usr/local/etc/build
BUILD_CMD=build
BUILDUSER=$USER

[ -z "$BUILD_MODE" ] && BUILD_MODE="--verify"
[ -z "$BUILD_DIST" ] && BUILD_DIST="9.1-i386"
[ -z "$BUILD_ROOT" ] && BUILD_ROOT="/var/tmp/buildsystem.$USER.$BUILD_DIST"
[ -z "$BUILD_PREPARE_SPEC" ] && BUILD_PREPARE_SPEC="no"

[ -z $DEST_DIR ] && DEST_DIR="/tmp/build.$BUILDUSER"
BUILDTEMP=$DEST_DIR/buildsrc/
BUILDSCRIPT=$BUILDTEMP/buildscript.$$

[ -z "$BUILD_USES_SVN" ] && BUILD_USES_SVN="yes"



if [ -r $CONFIG_FILE ]; then
	source $CONFIG_FILE
	MOD_BUILD_DIST=`echo $BUILD_DIST | sed "s/[-+.]/_/g"`
	#echo MOD_BUILD_DIST: $MOD_BUILD_DIST
	#RPMS_PATHS=$[BUILD_RPMS_$MOD_BUILD_DIST]
	#echo RPMS_PATHS: $RPM_PATHS
	#if [ -n "$[BUILD_RPMS_$MOD_BUILD_DIST]" ]; then
	#	BUILD_RPMS=$[BUILD_RPMS_$MOD_BUILD_DIST]
	#fi
	#echo OUTPUT: $[BUILD_RPMS_$MOD_BUILD_DIST]
	#:$BUILD_RPMS
	case "$BUILD_DIST" in
		"9.0-i386")	
			BUILD_RPMS_TEST=$BUILD_RPMS_9_0_i386 ;;
		"sles8-i386")	
			BUILD_RPMS_TEST=$BUILD_RPMS_sles8_i386 ;;
		"sles8-i386+update")	
			BUILD_RPMS_TEST=$BUILD_RPMS_sles8_i386_update ;;
		"9.1-i386")	
			BUILD_RPMS_TEST=$BUILD_RPMS_9_1_i386 ;;
		"sles9-i386")	
			BUILD_RPMS_TEST=$BUILD_RPMS_sles9_i386 ;;
	esac
	if [ "$BUILD_RPMS_TEST" ]; then
		if [ "$BUILD_RPMS" ]; then
			BUILD_RPMS=$BUILD_RPMS:$BUILD_RPMS_TEST
		else
			BUILD_RPMS=$BUILD_RPMS_TEST
		fi
	fi
fi

echo BUILD_RPMS: $BUILD_RPMS

ERROR=0

# Hier gehts los:

if [ -z "$1" ]; then
	# without parameter, we are in source directory
	SPECFILE=$(ls *.spec | awk '{print $1}')
	PACKAGE=$(awk '/^Name:/ {print $2}' <$SPECFILE)
	VERSION=$(awk '/^Version:/ {print $2}' <$SPECFILE)
else
	# parameter is src.rpm file
	PACKAGE=`echo $1 | sed 's/-[0-9]*\..*.src.rpm//'`
	VERSION=`echo $1 | sed 's/.*-\([0-9]*\..*\)-.*.src.rpm/\1/'`
fi

echo $PACKAGE $VERSION $BUILD_DIST $BUILD_MODE

rm -rf ${BUILDTEMP}/src/${PACKAGE};
mkdir -p ${BUILDTEMP}/src/${PACKAGE}
# ${BUILDTEMP}/${BUILD_DIST};

if [ "$1" ]; then
	cp -a "$1" ${BUILDTEMP}/src/${PACKAGE}
	cd ${BUILDTEMP}/src/${PACKAGE}; rpm2cpio $1 | cpio -i
else
	cp -a $SPECFILE ${BUILDTEMP}/src/${PACKAGE}
	
	# uses Subversion (svn) to generate more information for the RPM
	if [ "$BUILD_USES_SVN" = "yes" ]; then
		echo "receiving subversion information ..."
		# set RELEASE number (last modification in this directory)
		# requires subversion (but connection to server isn't neccessary)
		SVN_BUILD_RELEASE=$(svn info . | sed -n 's/^Last Changed Rev: \([0-9]*\)/\1/p')
		# in case of local modification a "m" is added to the release number
		SVN_BUILD_MODIFIED=$(svn status -v | grep "^ *M" | wc -l)
		if [ $SVN_BUILD_MODIFIED -gt 0 ]; then
			SVN_BUILD_RELEASE=${SVN_BUILD_RELEASE}m
		else # check if RPM has been build already
			EXPECTED_DEST_RPM_FILENAME=${DEST_DIR}/${BUILD_DIST}/${PACKAGE}-${VERSION}-${SVN_BUILD_RELEASE}.*.rpm
			if [ -f $EXPECTED_DEST_RPM_FILENAME ]; then
				echo
				echo "Build of \"${PACKAGE}\" unnecessary. Current RPM already at " $EXPECTED_DEST_RPM_FILENAME
				echo "verify if 'svn update' has been performed on the source directory"
				echo
				exit
			fi			
		fi
		if [ "$SVN_BUILD_RELEASE" ]; then
			sed "s/^Release:.*/Release: $SVN_BUILD_RELEASE/g" $SPECFILE > ${BUILDTEMP}/src/${PACKAGE}/$SPECFILE
		fi
		# changelog header
		DATE=`unset LANG; date +"%a %b %d %Y"`
		echo -e "* $DATE -\n" > Changes
		echo "- Subversion status:" >> Changes
		# additional infos for the Changes file (see if everything is checked in)
		svn status -v | grep -v '^? ' >> Changes
		echo >> Changes
		echo "- ChangeLog:" >> Changes
		# requires connection to the subversion server
		svn log -v . > ChangeLog.new && mv ChangeLog.new ChangeLog
		cat ChangeLog >> Changes
		
		# if there is no changelog section in the spec file,
		# also append the Changes there
		if ! grep -q "%changelog" $SPECFILE ; then
			# paste "Changes" into SPEC file. 
			echo "%changelog" >> ${BUILDTEMP}/src/${PACKAGE}/$SPECFILE
			# without trailing *, except for the initial
			echo -n "*" >> ${BUILDTEMP}/src/${PACKAGE}/$SPECFILE
			cat Changes | sed 's/\(^[\*]\+\)//g' >> ${BUILDTEMP}/src/${PACKAGE}/$SPECFILE
		fi
	fi
	# just make sure, that the Changes file exist
	touch Changes
	
	# if "SOURCES" file exist, create tar file on the fly
	if [ -r "SOURCES" ]; then
		# create a tar file from the sources
		tar cvjf ${BUILDTEMP}/src/${PACKAGE}/$PACKAGE-$VERSION.tar.bz2 --files-from SOURCES
	
	elif [ -r "SPECSOURCES" ]; then
		# SPECSOURCES have to list the files that are referenced in the .spec file
		# these ´files are copied to the build environment
		cp -av `cat SPECSOURCES` ${BUILDTEMP}/src/${PACKAGE}
	
	else 
		# legacy method: 
		# call the existing makefile to generate a tar file	
		test -e Makefile && make
		TARFILE=$(ls  *.tar.[gb]z*| awk '{print $1}')
		cp -av $TARFILE ${BUILDTEMP}/src/${PACKAGE}
	fi
fi

# create buildscript
cat > $BUILDSCRIPT <<EOF
#!/bin/bash
# local buildscript
#. /work/src/bin/.profile
export BUILD_ROOT=$BUILD_ROOT
export BUILD_DIST=$BUILD_DIST
export BUILD_RPMS=${DEST_DIR}/${BUILD_DIST}:$BUILD_RPMS
export BUILD_PREPARE_SPEC=$BUILD_PREPARE_SPEC

cd ${BUILDTEMP}/src/${PACKAGE}
$BUILD_CMD $BUILD_MODE
EOF

chmod a+x $BUILDSCRIPT
sudo su -c $BUILDSCRIPT
rm -f $BUILDSCRIPT

mkdir -p ${DEST_DIR}/src
# delete all old versions of this package
rm -f ${DEST_DIR}/src/${PACKAGE}-*.src.rpm

if !(cp -a ${BUILD_ROOT}/usr/src/packages/SRPMS/${PACKAGE}-${VERSION}-*.src.rpm ${DEST_DIR}/src 2> /dev/null); then 
	ERROR=1
fi

mkdir -p ${DEST_DIR}/${BUILD_DIST}
# delete all old versions of this package
rm -f ${DEST_DIR}/${BUILD_DIST}/${PACKAGE}-*.rpm

if !(cp -a ${BUILD_ROOT}/usr/src/packages/RPMS/*/${PACKAGE}*${VERSION}-*.*.rpm ${DEST_DIR}/${BUILD_DIST} ); then 
	ERROR=2
fi

echo
if [ $ERROR -ne 0 ]; then

	echo "ERROR while building packet $PACKAGE";
else
	echo "Build of \"${PACKAGE}\" completed!"
	echo "Packages can be found in ${DEST_DIR}"
fi
echo

exit $ERROR
