#!/bin/sh
# Build a zzz rpm set. 
# Uses temp space here, does not touch the source.
# Runnable as myself, does not use root permission at all.

PROG="`basename $0`"

usage () {
	cat <<EOF
Usage: $PROG SPECFILE SOURCE [SOURCE ...]

Builds the RPM package(s) driven by SPECFILE from
the given SOURCEs, using the temporary build area specified 
by the ``%_topdir`` declaration in ``~/.rpmmacros``.

If SOURCE is a file, copy it to the RPM temporary build area.
If SOURCE is a directory, then make a .tar.gz file out of it,
and copy the .tar.gz to the build area.
EOF
}

die () {
	rc="$1"
	shift

	echo -n "$PROG: "
	if [ -z "$1" ]; then cat 1>&2; else echo 1>&2 "$@"; fi
	
	exit $rc
}


## process cmd line args

spec="$1";
echo SPEC_FILE=$spec

# rest  of args is SOURCE files...'
source="$2";
echo SOURCES=$source

topdir="$3";
echo BUILDING_DIR=$topdir

utiljar="$4";
echo UTIL_JAR=$utiljar

swig="$5";
echo SWIG=$swig

jdk="$6"
echo JDK=$jdk

lcmaps="$7"
echo LCMAPS=$lcmaps

lcmaps_without_gsi="$8"
echo LCMAPS_WITHOUT_GSI=$lcmaps_without_gsi 

lcmaps_plugins_basic="$9"
echo LCMAPS_PLUGINS_BASIC=$lcmaps_plugins_basic

lcmaps_plugins_voms=${10}
echo LCMAPS_PLUGINS_VOMS=$lcmaps_plugins_voms

libacl_devel=${11}
echo LIBACL_DEVEL=$libacl_devel

libattr_devel=${12}
echo LIBATTR_DEVEL=$libattr_devel

xfsprogs_devel=${13}
echo XFSPROGS_DEVEL=$xfsprogs_devel

version=${14}
echo VERSION=$version

age=${15}
echo AGE=$age

PACKAGE=${16}
echo PACKAGE=$PACKAGE

# some Java parameters
if [ -z "$JAVA_HOME" ]; then
  echo "Error: JAVA_HOME is not set."
  echo " .. try to continue .. " 
else
  echo "run java in $JAVA_HOME"
  JAVA_HOME=$JAVA_HOME
fi

JAVA=/usr/java/latest/bin/java
echo JAVA=$JAVA

PLATFORM=`${JAVA} -cp ${utiljar} -Xmx32m it.grid.storm.Platform platform | sed -e "s/ /_/g"`
echo NATIVE_PLATFORM=$PLATFORM

OS_DIST=`${JAVA} -cp ${utiljar} -Xmx32m it.grid.storm.Platform os_dist | sed -e "s/ /_/g"`
echo OS_DIST=$OS_DIST

#PACKAGE="storm-backend-server"

# make rpm directory structure
echo "make rpm directory structure ", $topdir
if [ ! -d "$topdir" ]; then
        echo 'oo ', $topdir
	mkdir -v $topdir
fi

echo "create rpm directory structure ", $topdir
mkdir -p -v $topdir/SOURCES
mkdir -p -v $topdir/SRPMS
mkdir -p -v $topdir/SPECS
mkdir -p -v $topdir/BUILD
mkdir -p -v $topdir/RPMS

echo "rm the source(s)"
rm -rf $topdir/SOURCES/$source

echo "copy the source(s) ", $source
cp -v $source $topdir/SOURCES/

echo "now build the binaries and the rpms. ", $spec
cp -v $spec $topdir/SPECS/

cd $topdir

distributionrpm="sl5"
lpvage="5.1"
lwgage="4"
lwgversion="1.4.3"

echo "create rpm $topdir"
# Generate RPMs
rpmbuild --define "_topdir ${topdir}" --define "platform ${PLATFORM}" --define "swig ${swig}" --define "jdk ${jdk}" --define "lcmaps ${lcmaps}" --define "lcmaps_without_gsi ${lcmaps_without_gsi}" --define "lcmaps_plugins_basic ${lcmaps_plugins_basic}" --define "lcmaps_plugins_voms ${lcmaps_plugins_voms}" --define "libacl_devel ${libacl_devel}" --define "libattr_devel ${libattr_devel}" --define "xfsprogs_devel ${xfsprogs_devel}" --define "version ${version}" --define "age ${age}" --define "distribution ${OS_DIST}" --define "distributionrpm ${distributionrpm}" --define "lpvage ${lpvage}" --define "lwgage ${lwgage}" --define "lwgversion ${lwgversion}" -ba SPECS/storm-backend-server.spec

