#!/bin/bash
PWD=`pwd`
arch=`uname -i`
who=`whoami`
if [ $# -eq 1 ] ; then
	if [ -d $1 ] ; then
		RPMDIR=$1
	else
        	echo "Directory $1 does not exit"
		exit 1	
	fi
else
        RPMDIR="ERRATA.$arch"	
        echo "Using Directory $RPMDIR"
fi
cd $RPMDIR
RPMLIST=`echo *src.rpm`
if [ "$RPMLIST" = "*src.rpm" ] ; then
  echo "No rpms to rebuild in $RPMDIR, exiting"
  exit 1
fi
cd ..

if [ ! -e status/$arch ]; then mkdir status/$arch ; fi
if [ ! -e status/$arch/complete ]; then mkdir status/$arch/complete; fi
if [ ! -e done.$RPMDIR ]; then mkdir done.$RPMDIR; fi

rm -f status/$arch/finished
echo "-----------------------------" >>status/$arch/buildlog
echo `date` >>status/$arch/buildlog

for RPM in $RPMLIST
do
	if [ -f done.$RPMDIR/$RPM ] ; then
	   echo "Skipping $RPM as it was already done"
           rm $RPMDIR/$RPM
	else
	   echo "Rebuilding $RPM" >> status/$arch/buildlog
	   NAME=`rpm -qp --qf "%{NAME}" $RPMDIR/$RPM`
	   # Now need to check each arch and put in special targets
	   case "$arch" in
  	     i386)
	       case "$NAME" in
	         kernel)
                   if [ ! -f /etc/beehive-root ] ; then
		     touch /etc/beehive-root
		   fi
		   RPMOPTS="--rcfile $PWD/rpmrc.$arch --target=noarch,i686 "
		   ;;
	         glibc6)
		   RPMOPTS="--rcfile $PWD/rpmrc.$arch --target=i386,i686"
		   ;;
	         openssl)
		   RPMOPTS="--rcfile $PWD/rpmrc.$arch --target=i386,i686"
		   ;;
	         ccs | cman | dlm | fence | gulm | gnbd | iddev | magma )
		   RPMOPTS="--rcfile $PWD/rpmrc.$arch --target=i686"
		   ;;
	         cman-kernel | GFS-kernel | dlm-kernel | gnbd-kernel )
		   RPMOPTS="--rcfile $PWD/rpmrc.$arch --target=i686 --nodeps"
		   ;;
	         sudo | emacs | openoffice.org)
		   if [ ! $who = "root" ] ; then
		      RPMOPTS="--rcfile $PWD/rpmrc.$arch --target=i386"
                   else
		      echo "skipping building of $NAME as must be build as non root"
                      continue
		   fi
		   ;;
		 *)
		   RPMOPTS="--rcfile $PWD/rpmrc.$arch "
		   ;;
	       esac # $NAME for i386
	       ;;
             x86_64)
	       case "$NAME" in
	         kernel)
                   if [ ! -f /etc/beehive-root ] ; then
		     touch /etc/beehive-root
		   fi
		   RPMOPTS="--rcfile $PWD/rpmrc.$arch --target=x86_64"
		   ;;
	         cman-kernel | GFS-kernel | dlm-kernel | gnbd-kernel)
		   RPMOPTS="--rcfile $PWD/rpmrc.$arch --target=x86_64 --nodeps"
		   ;;
	         glibc)
		   RPMOPTS="--rcfile $PWD/rpmrc.$arch --target=x86_64"
		   ;;
	         grub)
                   echo "Note that grub needs to be statically linked"
                   echo "The ncurses-static patch turns on static then"
                   echo "then turns on dynamic resulting in glib dynamic"
                   echo "solution is to not include the ncurses-static patch"
		   RPMOPTS="--rcfile $PWD/rpmrc.$arch "
		   ;;
		 *)
		   RPMOPTS="--rcfile $PWD/rpmrc.$arch "
		   ;;
	       esac # $NAME for x86_64
	       ;;
	     ia64)
	       RPMOPTS=""
	       ;;
           esac # for $arch
	   # This is for rpm's that don't matter about arch,
	   # but we need a reminder about something or another.
	   case "$NAME" in
	     devhelp)
	     	echo "  Before building devhelp, make sure you have the latest mozilla installed"
	     ;;
	   esac # for archless
	   echo "Rebuilding $RPM with $RPMOPTS" 
	   rpmbuild --rebuild $RPMOPTS $RPMDIR/$RPM >status/$arch/$RPM.out 2>&1
	   grep -q "^Wrote" status/$arch/$RPM.out
	   if [ $? -eq 0 ] ; then
		mv -f status/$arch/$RPM.out status/$arch/complete
		mv $RPMDIR/$RPM done.$RPMDIR
		echo "$RPM : success" >> status/$arch/buildlog
	   else
		echo "$RPM : failed" >> status/$arch/buildlog
	   fi
	fi  
done

echo -n "Successfull builds: " >> status/$arch/buildlog
grep 'success$' status/$arch/buildlog | wc -l >> status/$arch/buildlog
echo -n "Failed builds: " >> status/$arch/buildlog
grep 'failed$' status/$arch/buildlog | wc -l >> status/$arch/buildlog

touch status/$arch/finished
