#!/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

#set -x

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

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

if [ $# -lt 2 ]
then
  echo usage: $0 base-rpm-name svn-path svn-revision-number
  exit 1
fi

echo Building $* 1>&2

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

# Get the needed info. 
RPMBASE=$1
SVNPATH=$2
BASEDIR=~/current/reltree/allrpms/$MYPROD/$MYPRODREL/$MYDIST/$MYARCH/$MYCUST

#
# 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=http://ftp.neonova.nl/svn/private/$SVNPATH
  fi
  REV=$3
fi

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

  GLOBAL_SRPMS="$HOME/current/reltree/allrpms/External-SRPMS/$MYARCH/SRPMS"

  #echo $SVNPATH  	

  # 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 ~/current/reltree/allrpms/External-SRPMS/$MYARCH/SRPMS/$SVNPATH ]
	then
		echo "cp -l $GLOBAL_SRPMS/$SVNPATH $BASEDIR/SRPMS"
		cp -l "$GLOBAL_SRPMS/$SVNPATH" "$BASEDIR/SRPMS" 	
	else 
		echo "Can't find srpm : $SVNPATH"
    		exit 1
	fi
  fi 	
fi

#
# Determine revision number from subversion.
#
if [ $BUILD_TYPE = "svn" ]
then
  if [ -z "$REV"  ]
  then
    if [ $MYDIST = "fc2" -o $MYDIST = "fc5" ]
    then		
      REV=`svn log $SVNPATH | grep -v -- ------- | head -1 | awk '{ print $1 }' | sed s/r//`
    else 	
      REV=`svn info $SVNPATH | grep "^Last Changed Rev:"|sed -e "s/^Last Changed Rev://g"|awk '{ print $1 }'`
    fi	
  fi
  if [ "$REV" -lt 1  ]
  then
    echo "Can't determine revision number for $2"
    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 ]
then
  allfound=1
  for i in `cat $CACHE/$REV`
  do
    #echo Looking for $BASEDIR/RPMS/$i 1>&2
    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

# recreate 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
  mkdir -p $BUILDDIR
  cd $BUILDDIR
  rm -rf $RPMBASE
  svn -q co -r $REV ${SVNPATH} $RPMBASE
  cd $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/nnrelman/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" -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

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

##else
##  if [ "$VERBOSE" = "1" ]
##  then
##    make rpm 1>&2
##  else
##    make rpm > $TMPF 2>&1
##  fi
##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

# save the rpms in the rpm allrpms repository
cp $RPMTOPDIR/RPMS/*/* $BASEDIR/RPMS
cd ..
rm -rf $RPMBASE

# 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
  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
