#!/bin/sh
#
# This script builds rpms, given an SVN path, RPM basename and SVN revision
# the generated rpms are saved in the 'allrpms' repository, and the names
# are saved in the 'svnrpm' cache.
#
# It prints out the names of the RPMs it generated (to standard output)
# All other info is (should be) printed to stderr

# source function library
. /etc/rc.d/init.d/functions

#
# Include al relevent build environment stuff.
#
export PATH=/usr/local/nbe/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/$MYRELMAN/bin
. /etc/profile.d/nbe.sh

# We need at least to parameters.
if [ $# -lt 2 ]
then
	echo usage: $0 base-rpm-name svn-path svn-revision-number
	exit 1
fi

# Display the rpm we are building.
echo Building $* 1>&2

# Verbose option.
VERBOSE=0
if [ "$1" = "-v" ]
then
	VERBOSE=1
	shift
fi

if [ "$1" = "-f" ]
then
        NBE_FORCE="yes"
        shift
fi
if [ "$NBE_FORCE" = "yes" ]
then
        VERBOSE=1
fi

# Get the needed info. 
RPMBASE=$1
SVNPATH=$2

# If this is set, we want to use a specific svn release to build a rpm.
if [ -z "`echo $3 | tr -d "[:digit:]"`" -a ! $3="" ]
then	
	REV=$3
fi

# Set BASEDIR
BASEDIR=~/current/reltree/allrpms/$MYPROD/$MYPRODREL/$MYDIST/$MYARCH/$MYCUST/$MYBUILD/$MYPRODSTABILITY/

#
# Check if we are dealing with subversion of a src.rpm.
#
if [ `echo $SVNPATH|grep \.src\.rpm$|wc -l` -gt 0 ]
then
	BUILD_TYPE=srpm
else
	BUILD_TYPE=svn
fi

#
# Check if we need to compile a local package or from a remote repository. 
# if SVNPATH is not a full URL, assume it is on our repository
#
if [ $BUILD_TYPE = "svn" ]
then

	if [ `echo $SVNPATH | grep :// | wc -l` -eq 0 ]
  	then
    		SVNPATH=$MYSVNPATH/$SVNPATH
        else    
		# Other svn repos have their own numbering.
		# so our numbers should not be used there.	
                MYREVISION=""
                SVN_REVISION=""
  	fi
fi

# Check if we got the srpm, if not, try to get it from the global srpm directory.
if [ $BUILD_TYPE = "srpm" ]
then

	# Set global SRPMS directory.
	GLOBAL_SRPMS="$HOME/current/reltree/allrpms/External-SRPMS/$MYARCH/SRPMS"
	if [ ! -d $GLOBAL_SRPMS ]
	then
		echo "Global SRPMS directory does not exists : $GLOBAL_SRPMS"
    		exit 1
	fi

	# If the SRPMS directory does not exists, create it.
  	if [ ! -d $BASEDIR/SRPMS ]
  	then
		mkdir -p $BASEDIR/SRPMS
  	fi	

  	# If we dont's got the srpm, copy it from the globals srpms directory.
  	if [ ! -f $BASEDIR/SRPMS/$SVNPATH ] 
  	then
		if [ -f $GLOBAL_SRPMS/$SVNPATH ]
		then
			cp -l "$GLOBAL_SRPMS/$SVNPATH" "$BASEDIR/SRPMS" > /dev/null 2>&1
		else 
			echo "Can't find srpm : $SVNPATH"
    			exit 1
		fi
  	fi 	
fi

#
# Determine revision number from subversion.
#
if [ $BUILD_TYPE = "svn" ]
then
	# If global myrevision is set and is a digit, use that to build the rpm.
	if [ -z "`echo $MYREVISION | tr -d "[:digit:]"`" -a -n "$MYREVISION" ]
	then
		# Get latest revision.
      		REV=`svn -r $MYREVISION info $SVNPATH | grep "^Last Changed Rev:"|sed -e "s/^Last Changed Rev://g"|awk '{ print $1 }'`
	else 	
		# If revision is not set by hand, ask subversion for the revision number.
		if [ -z "$REV"  ]
  		then
			
			# Check if SVN_REVISION is set.
			if [ -z "`echo $SVN_REVISION | tr -d "[:digit:]"`" -a -n "$SVN_REVISION" ]
			then

				# Get latest revision.
      				REV=`svn -r $SVN_REVISION info $SVNPATH | grep "^Last Changed Rev:"|sed -e "s/^Last Changed Rev://g"|awk '{ print $1 }'`
  			else 

				# Get latest revision.
      				REV=`svn info $SVNPATH | grep "^Last Changed Rev:"|sed -e "s/^Last Changed Rev://g"|awk '{ print $1 }'`
			fi
		fi
	fi

	if [ "$REV" -lt 1  -o -n "`echo $REV | tr -d "[:digit:]"`" ]
  	then
    		echo "Can't determine revision number for $SVNPATH"
    		exit 1
  	fi
fi

#
# Determine revision number from src.rpm.
#
if [ $BUILD_TYPE = "srpm" ]
then
	REV=`/usr/local/nbe/bin/nbe_srpm_version -s $SVNPATH`
fi

# Create directories for cache and tmp build.
TMPF=`mktemp`
CACHE=$BASEDIR/svnrpm/$RPMBASE
mkdir -p $BASEDIR/RPMS
mkdir -p $BASEDIR/SRPMS
mkdir -p $CACHE

#
# See if the rpms are already generated.
#
if [ -f $CACHE/$REV -a ! "$NBE_FORCE" = "yes" ]
then
	allfound=1
  	for i in `cat $CACHE/$REV`
  	do

    		# Try to find cached version of the rpm.
    		if [ ! -f $BASEDIR/RPMS/$i ]
    		then
      			allfound=0
      			break
    		fi
  	done

  	if [ "$allfound" = "1" ] 
  	then

  		# the sed is a hack, because make does not like ':' in filenames
    		echo " "	
    		cat $CACHE/$REV | sed 's/:/\\:/g'
    		echo $RPMBASE ":UPTODATE" >> $MYBUILDLOG
    		exit 0
  	fi  
fi

# Create the contents of the RPMTOPDIR directory.
/usr/local/nbe/bin/nbe_prep_rpmbuild -d

# Rebuild src.rpm.
if [ $BUILD_TYPE = "srpm" ]
then

	if [ "$VERBOSE" = "1" ]
  	then
    		rpmbuild --rebuild $BASEDIR/SRPMS/$SVNPATH 1>&2
    		RETVAL=$?
  	else
    		rpmbuild --rebuild $BASEDIR/SRPMS/$SVNPATH > $TMPF 2>&1
    		RETVAL=$?
  	fi
fi

#
# checkout the source, and 'make rpm'
#
if [ $BUILD_TYPE = "svn" ]
then

	# Create builddir if it does not exists yet.
	if [ ! -d $BUILDDIR ]
	then
		mkdir -p $BUILDDIR
	fi

	# Check out the sources.
  	cd $BUILDDIR
  	rm -rf $BUILDDIR/$RPMBASE
  	svn -q co -r $REV ${SVNPATH} $RPMBASE
  	cd $BUILDDIR/$RPMBASE

  	# Run the rpm build script.	
  	if [ -x BuildRpm ]
  	then

    		# Create name of lock file.
    		ETH0IP=`/sbin/ifconfig eth0|grep addr:|sed -e s/.*addr:// -e 's/ .*//'`
    		LOCKFILE="/home/$MYRELMAN/locking/nbe_autorebuild.running.$ETH0IP"

    		# Check if we are running within a autorebuild.
    		if [ -f $LOCKFILE ]
    		then

      			# Get autorebuild directory.
      			autorebuild_pid=`cat $LOCKFILE`
      			autobuild_dir=/tmp/nbe_autorebuild.$autorebuild_pid

      			if [ -d $autobuild_dir ]
      			then

        			# Get old revision.
        			OLDREV=`ls $CACHE|sort -n|tail -1`
        			OLDREV=`expr $OLDREV + 1`	

				# If OLDREV is less then 1, reset it to 1.
        			if [ "$OLDREV" -lt 1  ]
        			then
          				OLDREV=1
        			fi

        			# Only get the log for the changes.	
        			svn log ${SVNPATH} --revision $OLDREV:$REV > $autobuild_dir/$RPMBASE
      			fi	
    		fi
  	fi

  	if [ "$VERBOSE" = "1" ]
  	then
    		./BuildRpm 1>&2
    		RETVAL=$?
  	else
    		./BuildRpm > $TMPF 2>&1
    		RETVAL=$?
  	fi
fi

# Build is succesful.
if [ $RETVAL -eq 0 ] 
then
	# Save exit status to build log.
	echo $RPMBASE ":OK" >> $MYBUILDLOG
else
	# Save exit status to build log.
	echo $RPMBASE ":FAILED" >> $MYBUILDLOG
fi

#
# When build failed, give debug info.
#
if [ $RETVAL -gt 0 ]
then
  	if [ "$VERBOSE" = "0" ]
  	then
    		tail -200 $TMPF
    		echo More info may be found in $TMPF
  	fi
  	exit 1
else
	rm -f $TMPF
fi

# Only sign packages if MYSIGNAME is not empty.
if [ -n "$MYSIGNAME" ]
then
	if [ `ls $RPMTOPDIR/RPMS/*/* 2>/dev/null | wc -w` -gt 0 ]
	then
		# Sign the rpms we generated.
		for rpm in $RPMTOPDIR/RPMS/*/* 
  		do 
			echo "$MYSIGSEC" |setsid /bin/rpm --addsign $rpm > /dev/null 2>&1
       		done
	fi
fi

# save the rpms in the rpm allrpms repository
if [ `ls $RPMTOPDIR/RPMS/*/* 2>/dev/null | wc -w` -gt 0 ]
then
	cp -f $RPMTOPDIR/RPMS/*/* $BASEDIR/RPMS
	cd ..
	rm -rf $BUILDDIR/$RPMBASE
fi

# save the names of the rpms in the revision cache
mkdir -p $CACHE
if [ `ls $RPMTOPDIR/RPMS/*/* 2>/dev/null | wc -w` -gt 0 ]
then
	# Add all generated files names to the cache file.	
	for i in $RPMTOPDIR/RPMS/*/* 
  	do 
    		basename $i
  	done > $CACHE/$REV

  	# print the rpms we generated
  	# the sed is a hack, because make does not like ':' in filenames
  	cat $CACHE/$REV | sed 's/:/\\:/g'
else
  	echo nothing generated
fi

#EOF
