#!/bin/sh -e
#
# run this from the source directory!

set -o errexit

if [ -z $1 ]; then
	echo "Enter CVS username"
	exit
fi

CVS_USERNAME=$1
CVSROOT=":pserver:${CVS_USERNAME}@www.mozdev.org:/cvs"
SRCDIR=`pwd`
# the following are paths relative to the source dir ${SRCDIR}
DOWNLOADS="../../downloads"
BUILDTOOLS="../buildtools"

# determine version number
VERSION=`grep VERSION=\" dobuild | cut -d\" -f2`
SHORTNAME=`grep SHORTNAME=\" dobuild | cut -d\" -f2`
USCORED_VERSION=`echo $VERSION | tr . _`
CAPS_SHORTNAME=`echo ${SHORTNAME} | tr a-z A-Z`
RELEASE_TAG="${CAPS_SHORTNAME}_RELEASE_${USCORED_VERSION}"

# create new version directory

if [ ! -d ${DOWNLOADS}/${VERSION} ]; then
        mkdir ${DOWNLOADS}/${VERSION}
fi

# build the release versions and place them in the appropriate directories

(./dobuild suiterunner && ./dobuild suitelegacy && ./dobuild tbird ) || exit

for target in tbird suiterunner suitelegacy; do
        cp build/${target}/${SHORTNAME}_${target}.xpi ${DOWNLOADS}/${VERSION}/${SHORTNAME}_${VERSION}_${target}.xpi
        cp build/${target}/${SHORTNAME}_${target}.xpi ${DOWNLOADS}/latest/
done


# create a CVS tag for the code used to build the release

cvs -d $CVSROOT tag -d ${RELEASE_TAG}
cvs -d $CVSROOT tag ${RELEASE_TAG}

cd ${SRCDIR}/${BUILDTOOLS}

cvs -d $CVSROOT tag -d ${RELEASE_TAG}
cvs -d $CVSROOT tag ${RELEASE_TAG}


# Workaround for a mozdev cvs tagging bug, see
# https://www.mozdev.org/bugs/show_bug.cgi?id=14797
# and
# http://www.mozdev.org/drupal/wiki/MozdevCVSTipsTroubleshooting
# for details

mkdir ${SRCDIR}/${TEMPDIR}
cd ${SRCDIR}/${TEMPDIR}
cvs -d $CVSROOT export -r ${RELEASE_TAG} -d ${SHORTNAME} ${SHORTNAME}/src
cd ${SRCDIR} || exit
rm -r ${SRCDIR}/${TEMPDIR}

# add the release to the CVS repository
# (it gets propagated to the mozdev download mirrors)

cd ${SRCDIR}/${DOWNLOADS}

cvs -d $CVSROOT add ${VERSION}/
cvs -d $CVSROOT add `ls ${VERSION}/*.xpi`
cvs -d $CVSROOT ci ${VERSION}/ latest/
