#!/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"
CVS_MODULE_NAME="bidiui"
SRCDIR=`pwd`
# the following are paths relative to the source dir ${SRCDIR}
DOWNLOADS="../../downloads"
BUILDTOOLS="../buildtools"
TEMPDIR="${SRCDIR}/tmp"

# determine version number
VERSION=`grep VERSION=\" dobuild | cut -d\" -f2 | head -1`
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}"
# make dobuild not build a beta
export BIDIUI_RELEASE=1

# 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}_${VERSION}_${target}.xpi ${DOWNLOADS}/${VERSION}/${SHORTNAME}_${VERSION}_${target}.xpi
        cp build/${target}/${SHORTNAME}_${VERSION}_${target}.xpi ${DOWNLOADS}/latest/${SHORTNAME}_${target}.xpi
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}

cd ${SRCDIR}

# 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
./dummy_export ${CVS_USERNAME}

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

cd ${SRCDIR}/${DOWNLOADS}

if [ ! -e ${VERSION}/CVS ]; then
	cvs -d $CVSROOT add ${VERSION}/
fi
XPI_ENTIRES_IN_VERSION_DIR=`grep xpi ${VERSION}/CVS/Entries | wc -l`
if [ ${XPI_ENTIRES_IN_VERSION_DIR} == 0 ]; then
        cvs -d $CVSROOT add `ls ${VERSION}/*.xpi`
fi
cvs -d $CVSROOT ci ${VERSION}/ latest/
