#! /bin/sh
#
#	build -- KRoC/Linux build script
#	Copyright (C) 2001-2006 Fred Barnes <frmb@kent.ac.uk>
#	Copyright (C) 2007-2009 University of Kent
#
#	This program is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation; either version 2 of the License, or
#	(at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program; if not, write to the Free Software
#	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

# Remove ACLOCAL from the environment, because KRoC includes all the .m4
# files it might need anyway, and having ACLOCAL pointing at an older
# version of KRoC can cause build problems.
unset ACLOCAL

supportaddress="kroc-bugs@kent.ac.uk"
if [ "$1" = "" -o "$1" = "--help" ]; then
	cat <<EOF
Usage: $0 OPTIONS ...

This script will configure, compile and install KRoC. It is intended for
use by end-users who have downloaded a KRoC source tree using
Subversion.  Options given to this script will be passed to KRoC's
configure script.

Common options:

  --prefix=DIRECTORY         Select directory to install into
                             (you must specify this; e.g. /usr/local)
  --with-toolchain=tvm       Build the Transterpreter toolchain
                             (default: build the KRoC toolchain)
  --enable-pony              Enable pony networking support

If you are working on KRoC itself, or building packages of it, you
should probably not use this script but instead use the regular
autotools commands.

Please report problems with KRoC to <$supportaddress>.
EOF
	exit 0
fi

script="typescript"
if [ "$1" != "--kroc-build-typescript" ]; then
	cat >$script <<EOF
KRoC typescript file, generated at `date`

Build arguments: $*
uname -a: `uname -a 2>&1`

EOF

	$0 --kroc-build-typescript "$@" 2>&1 | tee -a $script
	exit $?
fi
shift

status () {
	printf '\n*** %s ***\n\n' "$1"
}

fail () {
	status "Installation failed"
	cat <<EOF
The KRoC installation failed. If the messages above don't help you work
out why, please mail:

    $supportaddress

attaching the "$script" file and a description of your system (e.g.
architecture, operating system version, etc.).

EOF
	exit 1
}

status "Generating build system"
autoreconf -v -f -i || fail

if [ "$1" = "--autoreconf-only" ]; then
	exit 0
fi

status "Configuring with arguments: $*"
./configure "$@" || fail

status "Cleaning source tree"
make -k clean

status "Compiling"
make || fail

status "Installing"
make install || fail

status "Installation successful"

exit 0
