#! /bin/sh
# scripts/mercury_config.  Generated from mercury_config.in by configure.
#---------------------------------------------------------------------------#
# Copyright (C) 2003, 2005, 2008, 2010 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.
#---------------------------------------------------------------------------#
#
# IMPORTANT: the manpage is produced automatically from this help
# message, so if you change the help message, don't forget to check
# that the manpage still looks OK.
                
Help="\
Name:   mercury_config - generate new configurations for a Mercury installation
Usage:  mercury_config [<options>] [-- <configure options>]
Options:
	--rebase
		Use this option after moving a Mercury installation to
		regenerate the scripts and configuration files so that
		the Mercury installation will work from the new location.

	--output-prefix <dir>
		Generate the new copies of the Mercury scripts and
		configuration files into the given directory.
		By default mercury_config overwrites the configuration in
		the installation hierarchy containing the mercury_config
		script.

Description:
	Generates an alternative configuration for a Mercury
	installation, for example to use a different C compiler.

	The <configure options> must result in a configuration
	which is compatible with the configuration used to build
	the installed libraries, or else linking using the new
	configuration will fail.  For example, both configurations
	must agree on the use of boxed or unboxed floats.

	To use the new configuration, put \`<dir>/bin' at the
	beginning of your PATH.

Environment variables:
        MERCURY_DEFAULT_GRADE.

Configure options:
\`configure' configures this package to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit
      --help=short        display options specific to this package
      --help=recursive    display the short help of all the included packages
  -V, --version           display version information and exit
  -q, --quiet, --silent   do not print \`checking ...' messages
      --cache-file=FILE   cache test results in FILE [disabled]
  -C, --config-cache      alias for \`--cache-file=config.cache'
  -n, --no-create         do not create output files
      --srcdir=DIR        find the sources in DIR [configure dir or \`..']

Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local/mercury-14.01.1]
  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                          [PREFIX]

By default, \`make install' will install all the files in
\`/usr/local/mercury-14.01.1/bin', \`/usr/local/mercury-14.01.1/lib' etc.  You can specify
an installation prefix other than \`/usr/local/mercury-14.01.1' using \`--prefix',
for instance \`--prefix=\$HOME'.

For better control, use the options below.

Fine tuning of the installation directories:
  --bindir=DIR            user executables [EPREFIX/bin]
  --sbindir=DIR           system admin executables [EPREFIX/sbin]
  --libexecdir=DIR        program executables [EPREFIX/libexec]
  --sysconfdir=DIR        read-only single-machine data [PREFIX/etc]
  --sharedstatedir=DIR    modifiable architecture-independent data [PREFIX/com]
  --localstatedir=DIR     modifiable single-machine data [PREFIX/var]
  --libdir=DIR            object code libraries [EPREFIX/lib]
  --includedir=DIR        C header files [PREFIX/include]
  --oldincludedir=DIR     C header files for non-gcc [/usr/include]
  --datarootdir=DIR       read-only arch.-independent data root [PREFIX/share]
  --datadir=DIR           read-only architecture-independent data [DATAROOTDIR]
  --infodir=DIR           info documentation [DATAROOTDIR/info]
  --localedir=DIR         locale-dependent data [DATAROOTDIR/locale]
  --mandir=DIR            man documentation [DATAROOTDIR/man]
  --docdir=DIR            documentation root [DATAROOTDIR/doc/PACKAGE]
  --htmldir=DIR           html documentation [DOCDIR]
  --dvidir=DIR            dvi documentation [DOCDIR]
  --pdfdir=DIR            pdf documentation [DOCDIR]
  --psdir=DIR             ps documentation [DOCDIR]

System types:
  --build=BUILD     configure for building on BUILD [guessed]
  --host=HOST       cross-compile to build programs to run on HOST [BUILD]

Optional Features:
  --disable-option-checking  ignore unrecognized --enable/--with options
  --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]

  --disable-gc-large-config
                          disable large heaps when using conservative GC
  --enable-gc-mmap        use mmap instead of sbrk when using Boehm GC
  --enable-gc-munmap      enable unmapping of unused pages when using Boehm GC
  --enable-symlinks       enable symbolic links. This is the default on all
                          systems except Windows. On Windows you can force
                          symbolic links to be used by giving this option.
                          Note that symbolic links will only work on Windows
                          if you are using the Cygwin version of GCC.
  --enable-javac-flags-for-heap-size
                          enable maximum heap size option for Java compiler
  --enable-deep-profiler=<directory>
                  install deep profiler CGI script in <directory>

  --enable-inefficient-grades
                          install inefficient versions of the library
  --disable-most-grades   install only the essential versions of the library
  --enable-nogc-grades    install no-gc versions of the library
  --enable-agc-grades     install accurate GC versions of the library
  --disable-debug-grades  do not install the debugging versions of the library
  --disable-decl-debug-grades
                          do not install the declarative debugging versions of
                          the library
  --enable-ssdebug-grades install the ssdebug versions of the library
  --disable-prof-grades   do not install profiling versions of the library
  --disable-old-prof-grades
                          do not install non-deep profiling versions of the
                          library
  --disable-trail-grades  do not install trailing versions of the library
  --disable-par-grades    do not install thread-safe versions of the library
  --enable-mm-grades      install minimal model versions of the library
  --enable-mmos-grades    install experimental minimal model own stack
                          versions of the library
  --enable-dmm-grades     install minimal model debug versions of the library
  --enable-hlc-prof-grades
                          install profiling versions of the high level C grade
  --enable-hlc-low-level-debug-grades
                          install a low-level debugging version of the high
                          level C grade
  --enable-rbmm-grades    install experimental region-based memory management
                          versions of the library
  --enable-stseg-grades   install stack segments versions of the library
  --enable-minimal-install
                          install the minimal system required to bootstrap
                          Mercury
  --enable-minimal-hlc-trail-install
                          install the minimal system that supports trailing in
                          a high level C grade
  --enable-dotnet-grades  install the .NET grades
  --enable-csharp-grade   install the C# grade
  --enable-java-grade     install the Java grade
  --enable-erlang-grade   install the Erlang grade
  --enable-libgrades=...  install exactly the given versions of the library.
                          The versions are specified using a comma-separated
                          list.
  --enable-additional-libgrades=...
                          install the given versions of the library in
                          addition to those requested by other options. The
                          versions are specified using a comma-separated list.
  --disable-extern-debug  disable the external (separate process) debugger
  --disable-dynamic-link  disable the use of dynamic linking
  --enable-new-mercuryfile-struct
                          enable new MercuryFile struct.
  --disable-tagged-trail  do not use tagged trail entries, even if they are
                          supported.

Optional Packages:
  --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]
  --without-PACKAGE       do not use PACKAGE (same as --with-PACKAGE=no)
  --with-cc=<program>     Specify which C compiler to use.
  --with-csharp-compiler=<compiler>
                          Specify which C Sharp compiler to use (default:
                          autodetect)
  --with-gc-win32-pthreads
            Force the use of pthreads-win32 or winpthreads with Boehm GC.
             This is the default for MinGW if detected.

  --with-llds-base-grade=<base_grade>
                          Force the use of <base_grade> as the base LLDS
                          grade. This is mainly useful for forcing the source
                          distribution to compile in a grade other than the
                          one which is chosen automatically. <base_grade> must
                          be one of asm_fast, asm_jump, fast, jump, reg or
                          none
  --with-default-grade=GRADE
                          Have the built compiler default to using GRADE
  --with-macosx-deployment-target=<target>
                          Specify the deployment target on Mac OS X.
  --with-msvcrt           use the MS Visual C runtime if using MS C compiler.
  --without-readline      Don't use the GPL'd GNU readline library
  --without-hwloc         Do not use libhwloc to detect the processors
                          available in low-level C parallel grades.

Some influential environment variables:
  CC          C compiler command
  CFLAGS      C compiler flags
  LDFLAGS     linker flags, e.g. -L<lib dir> if you have libraries in a
              nonstandard directory <lib dir>
  LIBS        libraries to pass to the linker, e.g. -l<library>
  CPPFLAGS    (Objective) C/C++ preprocessor flags, e.g. -I<include dir> if
              you have headers in a nonstandard directory <include dir>
  CPP         C preprocessor
  ERLC        Erlang/OTP compiler command [autodetected]
  ERLCFLAGS   Erlang/OTP compiler flags [none]
  ERL         Erlang/OTP interpreter command [autodetected]
  PKG_CONFIG  path to pkg-config utility
  PKG_CONFIG_PATH
              directories to add to pkg-config's search path
  PKG_CONFIG_LIBDIR
              path overriding pkg-config's built-in search path
  libhwloc_CFLAGS
              C compiler flags for libhwloc, overriding pkg-config
  libhwloc_LIBS
              linker flags for libhwloc, overriding pkg-config
  YACC        The \`Yet Another Compiler Compiler' implementation to use.
              Defaults to the first program found out of: \`bison -y', \`byacc',
              \`yacc'.
  YFLAGS      The list of arguments that will be passed by default to $YACC.
              This script will default YFLAGS to the empty string to avoid a
              default value of \`-d' given by some make applications.

Use these variables to override the choices made by \`configure' or to help
it to find libraries and programs with nonstandard names/locations.

Report bugs to the package provider.
"
#---------------------------------------------------------------------------#

default_grade=${MERCURY_DEFAULT_GRADE-asm_fast.gc}
libgrades="asm_fast.gc asm_fast.gc.debug.stseg asm_fast.gc.decldebug.stseg asm_fast.gc.memprof asm_fast.gc.prof asm_fast.gc.profdeep asm_fast.gc.trseg asm_fast.gc.trseg.debug.stseg hlc.gc hlc.gc.trseg"
input_prefix=/usr/pkg
output_prefix=/usr/pkg
exe_ext=
unset MERCURY_STDLIB_DIR
unset MERCURY_CONFIG_DIR
recursive=no

orig_arity=$#

while : ; do
	case $1 in 
		--rebase)
			input_prefix=$(readlink -f $0)
			input_prefix=${input_prefix%/bin/mercury_config}
			output_prefix=${input_prefix}
			;;

		# This option is only for use by
		# bindist/bindist.INSTALL.in
		--input-prefix=*)
			input_prefix=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'`
			;;
		--input-prefix)
			input_prefix="$2"
			shift
			;;

		--output-prefix=*)
			output_prefix=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'`
			;;
		--output-prefix)
			output_prefix="$2"
			shift
			;;

		--recursive)
			recursive=yes
			;;

		-h|--help|"-?")
			echo "$Help"
			exit 0
			;;

		--)
			shift
			break
			;;

		*)
			break
			;;
	esac
	shift
done

input_libdir="$input_prefix/lib/mercury"
output_libdir="$output_prefix/lib/mercury"

if test "$orig_arity" -eq 0 && ! test -d "$input_prefix"
then
	echo "Input directory \`$input_prefix' not found."
	echo "Did you mean \`$0 --rebase'?"
	exit 1
fi

# mercury_config regenerates itself, so we can't just execute it in place.
case $recursive in
    no)
	if [ -d "$TMPDIR" ]; then
		:
	elif [ -d /tmp ]; then
		TMPDIR="/tmp"
	else
		TMPDIR="."
	fi
	TMPDIR=$TMPDIR/mercury_config.$$
	trap 'rm -rf $TMPDIR; exit 1' 1 2 3 13 15 
	[ -d $TMPDIR ] || mkdir -p $TMPDIR || \
		{ echo mercury_config: invalid TMPDIR: $TMPDIR; exit 1; }
	export TMPDIR

	cp $input_prefix/bin/mercury_config $TMPDIR
	case $# in
	0)
		exec $TMPDIR/mercury_config --recursive \
			--input-prefix "$input_prefix" \
			--output-prefix "$output_prefix" ;;
	*)
		exec $TMPDIR/mercury_config --recursive \
			--input-prefix "$input_prefix" \
			--output-prefix "$output_prefix" "$@" ;;
	esac
esac

cp -r $input_libdir/reconf/* $TMPDIR || exit 1
mkdir $TMPDIR/bindist || exit 1

# The configure script tries to process these files, but they won't be used.
touch $TMPDIR/scripts/Mercury.config.bootstrap.in || exit 1
touch $TMPDIR/Mmake.common.in $TMPDIR/bindist/bindist.INSTALL.in || exit 1
touch $TMPDIR/bindist/bindist.Makefile.in || exit 1
mkdir $TMPDIR/tools || exit 1
touch $TMPDIR/tools/lmc.in || exit 1
touch $TMPDIR/tools/dotime.in || exit 1
touch $TMPDIR/runtime/mercury_dotnet.cs.in || exit 1
mkdir $TMPDIR/java || exit 1
mkdir $TMPDIR/java/runtime || exit 1
touch $TMPDIR/java/runtime/Constants.java.in || exit 1
touch $TMPDIR/java/runtime/Native.java.in || exit 1
mkdir $TMPDIR/compiler || exit 1
touch $TMPDIR/compiler/COMP_FLAGS.in || exit 1
mkdir $TMPDIR/library || exit 1
touch $TMPDIR/library/LIB_FLAGS.in || exit 1
touch $TMPDIR/library/erlang_conf.hrl.in || exit 1
mkdir $TMPDIR/mdbcomp || exit 1
touch $TMPDIR/mdbcomp/MDBCOMP_FLAGS.in || exit 1
mkdir $TMPDIR/browser || exit 1
touch $TMPDIR/browser/MDB_FLAGS.in || exit 1
mkdir $TMPDIR/slice || exit 1
touch $TMPDIR/slice/SLICE_FLAGS.in || exit 1
mkdir $TMPDIR/ssdb || exit 1
touch $TMPDIR/ssdb/SSDB_FLAGS.in || exit 1
mkdir $TMPDIR/profiler || exit 1
touch $TMPDIR/profiler/PROF_FLAGS.in || exit 1
mkdir $TMPDIR/deep_profiler || exit 1
touch $TMPDIR/deep_profiler/DEEP_FLAGS.in || exit 1

enable_libgrades=`echo $libgrades | sed 's/ /,/g'`

cd $TMPDIR
case $# in
	0)
		./configure  --build=armv6--netbsdelf-eabihf --host=armv6--netbsdelf-eabihf --infodir=/usr/pkg/info --mandir=/usr/pkg/man --enable-option-checking=yes \
			--with-default-grade="$default_grade" \
			--enable-libgrades="$enable_libgrades" \
			--cache-file=/dev/null \
			--prefix="$input_prefix" \
			--enable-reconfigure="$output_prefix" || exit 1
		;;
	*)
		./configure  --build=armv6--netbsdelf-eabihf --host=armv6--netbsdelf-eabihf --infodir=/usr/pkg/info --mandir=/usr/pkg/man --enable-option-checking=yes "$@" \
			--with-default-grade="$default_grade" \
			--enable-libgrades="$enable_libgrades" \
			--cache-file=/dev/null \
			--prefix="$input_prefix" \
			--enable-reconfigure="$output_prefix" || exit 1
		;;
esac

[ -d $output_libdir/conf ] || mkdir -p $output_libdir/conf || exit 1
[ -d $output_libdir/mmake ] || mkdir -p $output_libdir/mmake || exit 1
[ -d $output_libdir/mdb ] || mkdir -p $output_libdir/mdb || exit 1
[ -d $output_prefix/bin ] || mkdir -p $output_prefix/bin || exit 1

#
# Copy the new configuration into place.
#
cp runtime/mercury_conf.h $output_libdir/conf || exit 1
cp scripts/Mercury.config $output_libdir/conf || exit 1
cp scripts/Mmake.vars $output_libdir/mmake || exit 1
if [ "$input_prefix" != "$output_prefix" ]; then
	cp $input_prefix/bin/mdemangle${exe_ext} \
		$input_prefix/bin/mfiltercc${exe_ext} \
		$input_prefix/bin/mkinit${exe_ext} \
		$input_prefix/bin/info_to_mdb${exe_ext} $output_prefix/bin
	cp $input_libdir/mmake/Mmake.rules $output_libdir/mmake
fi
cd scripts
echo *
for file in *; do
	case "$file" in
		*.in|Mmake.*|Mercury.config*|*.sh-subr) ;;
		mdbrc)
			cp $file $output_libdir/mdb || exit 1
			;;
		*)
			cp $file $output_prefix/bin || exit 1
			chmod u+wx $output_prefix/bin/$file || exit 1
			;;
	esac
done
rm -rf $TMPDIR
