#!/bin/bash
#
#	$Author$
#

TAG=$1
CURDIR="$( pwd )"

if [ "$TAG" == "" ]; then
	echo "syntax: $0 TAG"
	exit
fi


TMP="/tmp/ganglia-export-$TAG"

echo "Exporting tag $TAG"
( \
	rm -rf "${TMP}"; \
	mkdir -p "${TMP}"; \
	cd "${TMP}" ; \
	git clone git://github.com/ganglia/monitor-core.git ; \
	cd monitor-core ; \
	git checkout ${TAG}; \
	git submodule init; \
	git submodule update
)
#svn export "https://ganglia.svn.sourceforge.net/svnroot/ganglia/tags/${TAG}" "$TMP"
#svn export "https://ganglia.svn.sourceforge.net/svnroot/ganglia/branches/3.2" "$TMP"
#svn export "https://ganglia.svn.sourceforge.net/svnroot/ganglia/trunk" "$TMP"

(
	cd "$TMP"
	(
		cd monitor-core

		# Create all configure scripts, etc
		./bootstrap

		# Cleanup git repo directories after autoconf uses them
		rm -Rf .git */.git

		# Create initial ganglia.spec file with RHEL5 defaults
		cp ganglia.spec.in ganglia.spec -f
		perl -pi -e 's/\@PACKAGE\@/ganglia/g;' ganglia.spec
		perl -pi -e "s/\@VERSION\@/${TAG}/g;" ganglia.spec
		perl -pi -e 's/\@REL\@/1/g;' ganglia.spec
		perl -pi -e 's/\@varstatedir\@/\/var\/lib/g;' ganglia.spec

		# Clean extra stuff
		rm -rf autom4te.cache libmetrics/autom4te.cache
	)
	mv -v "monitor-core" "ganglia-$TAG"
	tar czvf "$CURDIR/ganglia-$TAG.tar.gz" "ganglia-$TAG"
)

echo "Built tarball in $CURDIR/ganglia-$TAG.tar.gz"

rm -Rf "$TMP"

