#! /usr/bin/env bash
# -*- coding: iso-8859-1 ; mode: shell-script ;-*-
# (C) Copyright  IBM Corp. 2004
#
# $Id: build-classpath-from-cvs,v 1.3 2004/05/21 21:19:31 augart-oss Exp $
#
# This little program builds and installs GNU Classpath
# from a checked-out CVS repository.
#
# This is intended for use by a nightly regression script.
# You can also use it to build Classpath yourself.  When you've run it,
# then set CLASSPATH_INSTALLATION in RVM_{HOST,TARGET}_CONFIG to
# the second argument, which should now be the head of a 
# good GNU Classpath installation. 
# 
# @author Steven Augart
# @date 3 May 2004

ME=${0##*/}
function usage () {
    cat >&2 <<EOF
Usage: $ME <classpath-checkout-dir> \$CLASSPATH_INSTALLATION <classpath-config-args>
  Configure and build a GNU Classpath CVS Head checkout which is
  already present in the directory <classpath-checkout-dir> and 
  then install it in <classpath-installation-dir>.  Use the proper
  configuration options for Jikes RVM.

  <classpath-config-args> are any extra configuration arguments you 
  want to use.
EOF
}
if (( $# < 2 )); then
    usage;
    exit 2;
fi
function croak () {
    echo "${ME}: $*" >&2
    echo "${ME}: Aborting execution"
    exit 3
}
declare -r srcdir="$1" installdir="$2"
shift; shift
set -e
trap 'echo "Unexpected error; aborting"' ERR
cd $srcdir || croak "Unable to change directories into $srcdir."
[ -f HACKING ] || croak "Can not find a file named $srcdir/HACKING; this probably isn't a GNU Classpath checkout."
grep "Classpath" HACKING > /dev/null || croak "The file $srcdir/HACKING does not contain the word 'Classpath'; this might not be a GNU Classpath checkout."
if ! [ -f configure ] || ! [ -x configure ]; then
    echo "$ME: Running autogen.sh to build a configure file for GNU Classpath"
    ./autogen.sh
    echo "$ME: done running autogen.sh"
fi

if ! [[ $JIKES ]] \
    && [[ ${RVM_HOST_CONFIG} ]] && [[ -r ${RVM_HOST_CONFIG} ]]
then
    eval $(grep JIKES ${RVM_HOST_CONFIG})
fi
if [[ $JIKES ]] && ! [[ -x $JIKES ]]; then
    unset JIKES
fi
if ! [[ $JIKES ]] \
    && [[ ${RVM_TARGET_CONFIG} ]] && [[ -r ${RVM_TARGET_CONFIG} ]]
then
    eval $(grep JIKES ${RVM_TARGET_CONFIG})
fi
if [[ ! $JIKES ]]; then
    JIKES=$(type -p jikes)
fi

./configure --disable-gtk-peer --prefix=${installdir} ${JIKES+--with-jikes=}${JIKES} "$@"
make
make install
