#! /bin/sh

### See http://people.gnome.org/~walters/docs/build-api.txt
# buildapi-variable-no-builddir

opt_cflags=${CFLAGS}
opt_cxxflags=${CXXFLAGS}
opt_prefix='/usr/local'
opt_libdir=''

for var in "$@" ; do
	case ${var} in
		CFLAGS=*) opt_cflags=`echo "${var}" | sed 's/^CFLAGS=//'` ;;
		CXXFLAGS=*) opt_cxxflags=`echo "${var}" | sed 's/^CXXFLAGS=//'` ;;
		--prefix=*) opt_prefix=`echo "${var}" | sed 's/^--prefix=//'` ;;
		--libdir=*) opt_libdir=`echo "${var}" | sed 's/^--libdir=//'` ;;
		--help)
			cat <<-EOF
			usage: $0 [--options]
			Available options:

			  --help          This help message.
			  --prefix=PATH   Installation path prefix [default: /usr/local]
			  --libdir=PATH   Library installation path [default: \$prefix/lib]

			Also, the following relevant environment variables can be set:

			  CFLAGS    Additional command line flags to be passed to the C compiler
			  CXXFLAGS  Additional command line flags to be passed to the C++ compiler

			NOTE: This script tries to mimic the typical usage for configure scripts
			generated by autotools, hence it will silently ignore unrecognized
			command line options.
			EOF
			exit
			;;
		*) true ;;
	esac
done

if test -z "${opt_libdir}" ; then
	opt_libdir="${opt_prefix}/lib"
fi

tee config.mk <<EOF
#---------------#
# Build options #
#---------------#
PREFIX   = ${opt_prefix}
LIBDIR   = ${opt_libdir}
CFLAGS   = ${opt_cflags}
CXXFLAGS = ${opt_cxxflags}
EOF
