#!/bin/sh
# Creates release archives in the directory its run from with the specified version
# number using the code from the head of the branch for the specified major
# version.  A working directory named 'openbiblio' is created and removed in the
# process and must not exist beforehand.
#
# Make sure $CVSROOT is set properly.

if [ "$1" = "-t" ]; then
	testrun=true
	shift
else
	testrun=false
fi

if [ -z "$1" -o -z "$2" ]; then
	echo Usage: ./mainline/tools/release '<major version>' '<release version>' >&2
	exit 1
fi
if ! (echo "$1" | egrep -q '^[0-9]+\.[0-9]+(\.[0-9]+)?$'); then
	echo $0: Bad major version number: $1 >&2
	exit 1
fi
if ! (echo "$2" | egrep -q '^[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?([a-z][a-z0-9]*)?$'); then
	echo $0: Bad release version number: $1 >&2
	exit 1
fi
if [ -d openbiblio ]; then
	echo $0: a directory named openbiblio already exists >&2
	exit 1
fi

module='OpenBiblio Source'
branch=RB_`echo $1 | sed 's/\./_/g'`
tag=REL_`echo $2 | sed 's/\./_/g'`
basefile=openbiblio-$2
cvs -z3 co -r $branch -d openbiblio -P "$module" || exit 1
cd openbiblio || exit 1
$testrun && echo TEST RUN: not tagging
$testrun || cvs tag $tag || exit 1

# Clean up the tree
find . -depth -name 'CVS' -exec rm -rf '{}' ';'
rm -rf tools tests
mv database_constants_deploy.php database_constants.php
sed "/OBIB_CODE_VERSION/s/CVS/$2/" shared/global_constants.php >shared/global_constants.tmp
mv shared/global_constants.tmp shared/global_constants.php

# Make archives
cd ..
tar cjf $basefile.tar.bz2 openbiblio
tar czf $basefile.tar.gz openbiblio
zip -f $basefile.zip openbiblio

rm -rf openbiblio
