#!/bin/sh
#-----------------------------------------------------------------------------#
# Copyright (C) 2004 The University of Melbourne.
# This file may only be copied under the terms of the GNU General
# Public License - see the file COPYING in the Mercury distribution.
#-----------------------------------------------------------------------------#
#
# This script builds RPMs for the stable and unstable release-of-the-day
# distributions.
#
# This script should be run by root, from a root crontab.
# It uses the already-built .tar.gz source distributions
# from the Mercury web site, and uses the "latest-stable-version"
# and "latest-unstable-version" files to tell it which versions
# to build RPMs for.
#
# XXX building "unstable" RPMS not yet tested...
#
#-----------------------------------------------------------------------------#

set -x

#-----------------------------------------------------------------------------#

usage() {
	echo "Usage: $0 <branch-dir> <stability>" 1>&2
	echo '<branch-dir> can be "rotd", "0.11.1-beta", "0.11", etc.'
	echo '(It must match the subdirectory name on the web site.)'
	echo '<stability> must be either "stable" or "unstable".' 1>&2
	exit 1
}

# Parse the command-line arguments

case "$#" in
	2)	branch_dir=$1; stability=$2 ;;
	*)	usage ;;
esac
case "$stability" in
	stable|unstable)
		;;
	*)	usage ;;
esac

#-----------------------------------------------------------------------------#

#
# Set some hard-coded path names.
#

# The place to install files for the web site.
# This is also used to find the .tar.gz source distribution and the
# "latest-stable-version" and "latest-unstable-version" files.
WEBDIR=/home/mercury/public/installed_w3/download/files
BETA_WEBDIR=$WEBDIR/beta-releases/$branch_dir

# Place to install files for the FTP site
FTP_HOST=ftp.mercury.cs.mu.oz.au
FTPDIR=$FTP_HOST:/home/ftp/pub/mercury
BETA_FTPDIR=$FTPDIR/beta-releases/$branch_dir

# Place to record log file (rpmbuild.out)
LOGDIR=/home/mercury/public/test_mercury/logs

ROOT_CRON_DIR=/home/aral/root_cron

# Directory to use as a temporary installation root
# (Beware, anything in this directory will get deleted by "rm -rf"!)
BUILD_ROOT=/tmp/mercury_rpm_root

# Directory in which rpm will put the RPMs and build directory
RPMDIR=/usr/src/redhat

# The RPM "Release" is hard-coded to be 1
# (XXX This should be determined from the "Release:" field in the .spec file.)
rel=1
# XXX The RPM architecture is hard-coded to be i386
arch=i386

#-----------------------------------------------------------------------------#
#
# Subroutines for error-reporting.
#

failed() {
	echo "*** $0: $@ failed" 1>&2
}

fatal_error() {
	failed "$@"
	exit 1
}

#-----------------------------------------------------------------------------#
#
# The main program -- build the RPMs and install them.
#

#
# Figure out which version we are going to build
#

case $stability in
	stable)
		base_version=`cat $BETA_WEBDIR/latest-stable-version`
		version=$base_version
		;;
	unstable)
		base_version=`cat $BETA_WEBDIR/latest-unstable-version`
		version=$base_version-unstable
		;;
esac

#
# Get the .tar.gz distribution, and use that to build the RPMs
#

cp $BETA_WEBDIR/mercury-compiler-$version.tar.gz \
	$RPMDIR/SOURCES/mercury-compiler-$base_version \
	|| fatal_error "copying source dist"
rm -rf $BUILD_ROOT
mkdir -p $BUILD_ROOT
rpmbuild --buildroot=$BUILD_ROOT --verbose -ta --clean --rmsource --rmspec \
	$RPMDIR/SOURCES/mercury-compiler-$base_version.tar.gz \
	>& $ROOT_CRON_DIR/rpmbuild.out || fatal_error "rpmbuild"
rm -rf $BUILD_ROOT
fgrep '**' $ROOT_CRON_DIR/rpmbuild.out > $ROOT_CRON_DIR/rpmbuild.out.errs
# Because $LOGDIR is NFS-mounted with root-squash, root doesn't have
# permission to copy to it.  So we need to su to "mercury".
su mercury -c "cp $ROOT_CRON_DIR/rpmbuild.out $LOGDIR" || failed "copying log"
su mercury -c "cp $ROOT_CRON_DIR/rpmbuild.out.errs $LOGDIR" \
	|| failed "copying log errs"

#
# Copy the RPMs that we just built to the web site and the FTP site.
#

version_with_underscores=`echo $version | sed 's/-/_/g'`
base_vsn_with_us=`echo $base_version | sed 's/-/_/g'`
su mercury -c "\
cp \
   $RPMDIR/RPMS/$arch/mercury-compiler-$base_vsn_with_us-$rel.$arch.rpm \
   $BETA_WEBDIR/mercury-compiler-$version_with_underscores-$rel.$arch.rpm" \
	|| failed 'copying RPM to web site'
su mercury -c "\
cp $RPMDIR/SRPMS/mercury-compiler-$base_vsn_with_us-$rel.src.rpm \
   $BETA_WEBDIR/mercury-compiler-$version_with_underscores-$rel.src.rpm" \
	|| failed "copying SRPM to web site"
su mercury -c "\
	cd $BETA_WEBDIR; \
	PATH=/home/mercury/public/cron/scripts:$PATH; \
	generate_index_html" \
	|| failed "reindexing web site"
su mercury -c "\
scp $RPMDIR/RPMS/$arch/mercury-compiler-$base_vsn_with_us-$rel.$arch.rpm \
    $BETA_FTPDIR/mercury-compiler-$version_with_underscores-$rel.$arch.rpm" \
	|| failed 'copying RPMs to ftp site'
su mercury -c "\
scp $RPMDIR/SRPMS/mercury-compiler-$base_vsn_with_us-$rel.src.rpm \
    $BETA_FTPDIR/mercury-compiler-$version_with_underscores-$rel.src.rpm" \
	|| failed 'copying RPMs to ftp site'

/bin/rm $RPMDIR/RPMS/$arch/mercury-compiler-$base_vsn_with_us-$rel.$arch.rpm
/bin/rm $RPMDIR/SRPMS/mercury-compiler-$base_vsn_with_underscores-$rel.src.rpm

#-----------------------------------------------------------------------------#
