#! /bin/sh
set -x
#
# This is the beginnings of a process to create Linux RPMs
#
mpiversion=0.96
#
# Create a spec file; set it up to be relocatable and to use a build root.
sourcefile="ftp://ftp.mcs.anl.gov/pub/mpi/mpich2-${mpiversion}.tar.gz"
#
# The source tar file must have name mpich-${mpiversion}.tar.gz (or .bz2)
# and must expand into mpich-${mpiversion} .  Use
#  maketar -local -name=mpich-${mpiversion} -tarfilename=mpich-${mpiversion}
# to build such a tar file.
#
#Patch: ftp://ftp.mcs.anl.gov/pub/mpi/patch/${mpiversion}/patch.all
#
# Local source (we'll extract the rpm from there)
localsrcdir=.
#
# Add 
#  %patch 
# after setup if there is a patch.
if [ ! -s $localsrcdir/maint/mpich2.spec ] ; then
    echo "Could not access spec file in directory $localsrcdir/maint"
    exit 1
fi
# 
mytmpdir="/sandbox/$LOGNAME"
# Take advantage of new rpm features to build in a relative way.
if [ ! -d $mytmpdir ] ; then
    if mkdir $mytmpdir ; then 
        :
    else 
        echo "Could not create temporary directory $mytmpdir"
	exit 1
    fi
fi
#
# Create the RPM using a build root
if [ ! -d $mytmpdir/buildroot ] ; then
    mkdir -p $mytmpdir/buildroot
fi
# Create an RPM topdir
if [ ! -d $mytmpdir/rpm ] ; then
    mkdir -p $mytmpdir/rpm
    for dir in BUILD RPMS RPMS/i586 SOURCES SPECS SRPMS ; do
        mkdir -p $mytmpdir/rpm/$dir
    done
fi
if [ ! -h $mytmpdir/rpm/SOURCES/mpich-${mpiversion}-1.tar.gz ] ; then
    ln -sf /home/MPI/maint/mpich-${mpiversion}.tar.gz \
	$mytmpdir/rpm/SOURCES/mpich-${mpiversion}-1.tar.gz
fi
cp $localsrcdir/maint/mpich2.spec $mytmpdir/rpm/SPECS
cd $mytmpdir/rpm
#
# At this point, we could select devices and compilers by sed'ing the
# appropriate lines from the spec file.
# prefer rpmbuild if it exists
# (note that rpmbuild gives an error if there is no spec file, even when
# only --version is specified!)
if rpmbuild --version SPECS/mpich2.spec >/dev/null 2>&1 ; then
    rbmbuild -ba SPECS/mpich2.spec
    rc=$?
elif rpm --version >/dev/null 2>&1 ; then
    rpm -ba SPECS/mpich2.spec
    rc=$?
else
    echo "Could not find an rpm build command or encountered an error "
    exit 1
fi
#
#
exit $rc
