#!/bin/bash
# vim: ft=sh ts=4 sw=4 et
# Copyright (C) 2013-2015 The Mercury team.
#
# This script builds the Mercury source distribution.
#
# After any options, this script expects exactly three arguments
#
# The first argument should be the path name of the Mercury workspace
# that the distribution should be based on.
#
# The second argument should be name associated with the version.
# The two usual forms of these names are rotd-yyyy-mm-dd for ROTDs
# and yy.mm for major releases.
#
# The third is the absolute path name where the tar file containing
# the Mercury source distribution should be put. To prevent surprises
# when the tar file is unpacked, the name should end in
# mercury-srcdist-VERSION.tar
#

set -e

CC=gcc
TMPDIR=/tmp
PARALLEL=1
# We should set the default LEAVE_TMP_DIR to "no"
# once this script has been in use for a while.
LEAVE_TMP_DIR=yes
UPDATE_VERSION_FILE=yes
TAR=tar

USAGE="\
$0 [options] orig_ws version tar_file_name
Options:
    -c CC
        Use CC as the C compiler.
    -d
        The directory in which to do work. It should be a directory intended
        for short-lived files, such as /tmp or /var/tmp.
    -h
        Output this help message and exit.
    -j N
        Run N jobs in parallel.
    -l
        Leave the temporary directory as it was when done.
    -n
        Do not update the VERSION file.
    -t TAR
        Use TAR as the archiving utility (default: tar).
"

while getopts "c:d:hj:lnt:" OPT
do
    case "${OPT}" in
        c)
            CC=${OPTARG}
            ;;
        d)
            TMPDIR=${OPTARG}
            ;;
        h)
            echo "${USAGE}"
            exit 0
            ;;
        j)
            PARALLEL=${OPTARG}
            ;;
        l)
            LEAVE_TMP_DIR=yes
            ;;
        n)
            UPDATE_VERSION_FILE=no
            ;;
        t)
            TAR=${OPTARG}
            ;;
        ?)
            echo "${USAGE}"
            exit 1
            ;;
        *)
            echo "Getopt error \"${OPT}\""
            echo "${USAGE}"
            exit 1
            ;;
    esac
done

shift `expr ${OPTIND} - 1`

if test "$#" != 3
then
    echo "${USAGE}"
    exit 1
fi

orig_ws="$1"
version="$2"
tar_file_name="$3"

if test ! -d "${orig_ws}"
then
    echo "Error: ${orig_ws} is not a directory"
    exit 2
fi

if test ! -e "${orig_ws}/.git"
then
    echo "Error: ${orig_ws} is not a git workspace"
    exit 2
fi

if test ! -f "${orig_ws}/library/private_builtin.m"
then
    echo "Error: ${orig_ws} is not a Mercury git workspace"
    exit 2
fi

case "${tar_file_name}" in
    /*)
        ;;
    *)
        echo "Error: ${tar_file_name} is not an absolute path name"
        exit 2
        ;;
esac

srcdist_name="mercury-srcdist-${version}"
srcdist_dir="${TMPDIR}/${srcdist_name}"

if test -e "${srcdist_dir}"
then
    echo "Error: the name of the temporary directory ${srcdist_dir} is in use"
    exit 3
fi

cp -rp "${orig_ws}" "${srcdist_dir}"
cd "${srcdist_dir}"

/bin/rm -rf stage2 stage3 Mmake.params Mmake.stage.params
# git checkout -- VERSION
git submodule deinit -f .
git clean -d -f -x
git rev-parse HEAD > COMMIT_ID
if test "${UPDATE_VERSION_FILE}" = "yes"
then
    sed "s/VERSION=.*/VERSION=${version}/" VERSION > VERSION.new
    mv VERSION.new VERSION
fi
./prepare.sh
# NOTE: the hlc.gc.pregen grade that we use *requires* these settings.
NUM_TAG_BITS=2
BITS_PER_WORD=32
BYTES_PER_WORD=4
UNBOXED_FLOATS=no
mercury_cv_low_tag_bits=${NUM_TAG_BITS} \
mercury_cv_bits_per_word=${BITS_PER_WORD} \
mercury_cv_bytes_per_word=${BYTES_PER_WORD} \
mercury_cv_unboxed_floats=${UNBOXED_FLOATS} \
sh configure --with-cc="${CC}" &&
mmake \
    TAR="${TAR}" \
    GRADE=hlc.gc.pregen \
    MMAKEFLAGS="EXTRA_MCFLAGS='-O5 --opt-space' -j${PARALLEL}" \
    SRCDIST_NAME="${srcdist_name}" \
    TAR_FILE_NAME="${tar_file_name}" \
    tar

if test "${LEAVE_TMP_DIR}" = "yes"
then
    echo "leaving build directory in ${TMPDIR}/build_srcdist.$$"
    mv "${srcdist_dir}" "${TMPDIR}/build_srcdist.$$"
else
    /bin/rm -fr "${srcdist_dir}"
fi

exit 0
