#!/bin/sh
# Copyright (C) 2006-2007,2009,2011 G.P. Halkes
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# 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, see <http://www.gnu.org/licenses/>.

# Set zsh to emulate sh (otherwise all kinds of eval's wont work)
[ -n "${ZSH_VERSION}" ] && emulate sh

#==============================
# Functions
#==============================
error() {
	echo "$*"
	[ -n "$DUMP_LOG" ] && cat config.log
	exit 1
}

not() {
	if "$@" ; then
		false
	else
		true
	fi
}

check() {
	if not "$@" ; then
		error "Error executing $*. Aborting."
	fi
}

check_message() {
	printf "$*"
	echo "----------------------------------" >> config.log
	echo "$*" >> config.log
}

check_message_result() {
	echo "$*"
	echo "$*" >> config.log
}

clean() {
	rm -rf "$@" >/dev/null 2>&1
}

test_make() {
	printf "%s" "Running: ${MAKE} -f .Makefile" >> config.log
	for ARG in "$@"
	do
		printf " '%s'" "${ARG}" >> config.log
	done
	echo >> config.log
	"${MAKE}" -f .Makefile "$@"
}

test_install() {
	clean .foo
	check_message "Checking for working install ($*)... "

	if  test_make "INSTALL=$*" "install" >> config.log 2>&1 ; then
		check_message_result "yes"
		true
	else
		check_message_result "no"
		false
	fi
}

test_and_set() {
	SETTING="$4${5+ }$5"
	if $1 "$2${SETTING+ in }${SETTING}" "TESTFLAGS=$4" "TESTLIBS=$5" ; then
		eval "${3}_FLAGS=\"$4\""
		eval "${3}_LIBS=\"$5\""
		true
	else
		false
	fi
}

add_replace_settings() {
	for SETTING
	do
		echo "/^${SETTING%%=*}/s/^/# Disabled by configure: /"
		echo "/\.POSIX/a\\
${SETTING}"
	done
}

replace_default() {
	if [ -n "$2" ] ; then
		echo "/^$1=/c\\
$1=$2"
	fi
}

insert() {
	echo "/^$1/d"
	[ -n "$2" ] && echo "/^\\.POSIX:/a\\
$1=$2"
}

create_makefile() {
	for _INSTALL in ${TEST_INSTALL}
	do
		# Don't test the sun install program, because it is dangerous.
		if [ "`uname`" = "SunOS" ] && [ "$_INSTALL" = "install" ] ; then
			continue
		fi
		test_install "${_INSTALL}" && INSTALL="${_INSTALL}" && break
	done
	if [ -z "${INSTALL}" ] ; then
		check_message_result "!! No working install program found. The install target will not work."
	fi

	{
		echo '/\.POSIX/a\
# Settings from configure script'
		replace_default prefix "${PREFIX}"
		for INSTALLDIR in ${INSTALLDIRS}
		do
			[ -n "`eval echo \\${option_${INSTALLDIR}}`" ] && replace_default "${INSTALLDIR}" "`eval echo \\${option_${INSTALLDIR}}`"
		done
		replace_default INSTALL "${INSTALL}"

		insert LDFLAGS "${LDFLAGS}"
		insert LDLIBS "${LDLIBS}"

		for EXT in ${EXTENSIONS}
		do
			sed_rules_${EXT}
		done

		add_replace_settings "$@"
		[ -n "${SEDEXTRA}" ] && ${SEDEXTRA}
	} > .sedscript
	for MAKEFILE in ${MAKEFILES:-Makefile}
	do
		echo "----------------------------------">> config.log
		check_message_result "Creating ${MAKEFILE}"
		cat ${MAKEFILE}.in | sed -f .sedscript > ${MAKEFILE}
	done
	clean .Makefile .Makefile.in .sedscript .foo .config_base
	for EXT in ${EXTENSIONS}
	do
		clean_${EXT}
	done
	exit 0
}

#==============================
# Setup
#==============================
unset LINKRULE COMPILERULE USERRULES USERHELP EXTENSIONS SWITCHES PRECHECKFUNC INSTALLDIRS DUMP_LOG HOST_PREFIX

. ./config.pkg

for MAKEFILE in ${MAKEFILES:-Makefile}
do
	[ -f ${MAKEFILE}.in ] || error "${MAKEFILE}.in does not exist"
	grep "^\\.POSIX:" ${MAKEFILE}.in > /dev/null || error "${MAKEFILE}.in does not contain .POSIX target"
done

#@INCLUDE_START
[ "${EXTENSIONS}" = "c libtool pkgconfig verbose_compile pkgconfig_dep gettext x11 lfs" ] || error "EXTENSIONS changed after running merge_config. Run merge_config again."
SWITCHES="${SWITCHES} +c99"
SUFFIXES="${SUFFIXES} .c .o"
[ -z "${LINKRULE}" ] && LINKRULE='$(CC) $(CFLAGS) $(LDFLAGS) -o $@ .config.o $(LDLIBS) $(TESTLIBS)'
[ -z "${COMPILERULE}" ] && COMPILERULE='$(CC) $(CFLAGS) $(TESTFLAGS) -c -o $@ $<'

clean_c() {
	clean .config .config.o .config.c
}

show_help_c() {
	if [ "$1" = VARIABLES ] ; then
		cat <<EOF
  CC          C compiler to use (defaults to search gcc, clang and make default)
  CFLAGS      C-compiler flags to use [-O2]
EOF
	fi
}

print_rules_c() {
	if [ -n "${CC}" ] ; then
		echo "CC=${CC}"
	fi
	[ -z "${CFLAGS}" ] && CFLAGS=-O2
	cat <<EOF
CFLAGS=${CFLAGS}

.config:.config.o
	${LINKRULE}

.c.o:
	${COMPILERULE}

EOF
}

basic_test_c() {
	cat > .config.c <<EOF
int main(int argc, char *argv[]) {
	return 0;
}
EOF
	if [ -z "$CC" ] ; then
		for COMPILER in gcc clang
		do
			if test_link "working C compiler (${COMPILER})" CC="${COMPILER}" ; then
				CC="${COMPILER}"
				return
			fi
		done
	fi
	test_link "working C compiler (${CC-${MAKE} default})" || error "No working C compiler found. See config.log for errors."
}

has_support_c99() {
	if [ "no" = "${with_c99}" ] ; then
		return
	fi
	clean_c
	cat > .config.c <<EOF
#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
#error Not a C99 compiler
#endif
int main(int argc, char *argv[]) {
	return 0;
}
EOF
	if test_link "C99 support in ${CC} as default" ; then
		C99=1
	elif test_link "C99 support in ${CC} with -std=c99" CFLAGS="$CFLAGS -std=c99" ; then
		CFLAGS="${CFLAGS} -std=c99"
		C99=1
	fi
}

sed_rules_c() {
	insert CFLAGS "${CFLAGS}"
	[ -n "${CC}" ] && insert CC "${CC}"
}

test_compile() {
	clean .config.o
	check_message "Checking for $1... "
	shift

	if test_make "CFLAGS=$CFLAGS" "$@" .config.o >> config.log 2>&1 ; then
		check_message_result "yes"
		true
	else
		check_message_result "no"
		echo "Source of the failed compile:" >> config.log
		nl -ba .config.c >> config.log
		false
	fi
}

test_link() {
	clean .config
	check_message "Checking for $1... "
	shift

	if test_make "CFLAGS=$CFLAGS" "$@" .config >> config.log 2>&1 ; then
		check_message_result "yes"
		true
	else
		check_message_result "no"
		echo "Source of the failed compile:" >> config.log
		nl -ba .config.c >> config.log
		false
	fi
}

touch_c() {
	[ -f .config.c ] && touch .config.c
}
SUFFIXES="${SUFFIXES} .lo .la"
[ -z "${LTLINKRULE}" ] && LTLINKRULE='$(LIBTOOL) --mode=link --tag=CC $(CC) $(CFLAGS) $(LDFLAGS) -o $@ .configlt.lo $(LDLIBS) $(TESTLIBS) -rpath /usr/lib'
[ -z "${LTCOMPILERULE}" ] && LTCOMPILERULE='$(LIBTOOL) --mode=compile --tag=CC $(CC) $(CFLAGS) $(TESTFLAGS) -c -o $@ $<'

clean_libtool() {
	clean .configlt.la libconfiglt.la .configlt.c .configlt.lo .configlt.o .libs
}

show_help_libtool() {
	if [ "$1" = VARIABLES ] ; then
		cat <<EOF
  LIBTOOL     libtool executable [libtool]
EOF
	fi
}

print_rules_libtool() {
	[ -z "${LIBTOOL}" ] && LIBTOOL="libtool"
	cat <<EOF
LIBTOOL=${LIBTOOL}

.configlt.la:libconfiglt.la
	mv libconfiglt.la .configlt.la

libconfiglt.la:.configlt.lo
	${LTLINKRULE} ${LTSHARED+-shared}

.c.lo:
	${LTCOMPILERULE} ${LTSHARED+-shared}
EOF
}

basic_test_libtool() {
	cat > .configlt.c <<EOF
int main(int argc, char *argv[]) {
	return 0;
}
EOF
	test_link_libtool "working libtool C compile" || {
		if ${LIBTOOL} -V 2>&1 | grep Apple >/dev/null ; then
			error "libtool command appears to be Apple libtool, not GNU libtool. Use LIBTOOL=<path to GNU libtool> if you do have GNU libtool installed."
		else
			error "libtool not working for C. See config.log for errors."
		fi
	}
}

sed_rules_libtool() {
	replace_default LIBTOOL "${LIBTOOL}"
}

test_compile_libtool() {
	clean .configlt.lo
	check_message "Checking for $1... "
	shift

	if test_make "$@" .configlt.lo >> config.log 2>&1 ; then
		check_message_result "yes"
		true
	else
		check_message_result "no"
		echo "Source of the failed compile:" >> config.log
		nl -ba .configlt.c >> config.log
		false
	fi
}

test_link_libtool() {
	clean .configlt.la
	check_message "Checking for $1... "
	shift

	if test_make "$@" .configlt.la >> config.log 2>&1 ; then
		check_message_result "yes"
		true
	else
		check_message_result "no"
		echo "Source of the failed compile:" >> config.log
		nl -ba .configlt.c >> config.log
		false
	fi
}

unset pkgconfigdir HASPKGCONFIG

OPTIONS="${OPTIONS} pkgconfigdir"

PKGCONFIGLIBDIR=no
for INSTALLDIR in ${INSTALLDIRS}
do
	if [ "${INSTALLDIR}" = libdir ] ; then
		PKGCONFIGLIBDIR=yes
		break
	fi
done
if [ "${PKGCONFIGLIBDIR}" = no ] ; then
	INSTALLDIRS="${INSTALLDIRS} libdir"
fi

show_help_pkgconfig() {
	if [ "$1" = OPTIONS ] ; then
		cat <<EOF
  --pkgconfigdir=<dir>   Set pkg-config data dir [<libdir>/pkgconfig]
EOF
	fi
}

print_rules_pkgconfig() {
	:
}

clean_pkgconfig() {
	:
}

basic_test_pkgconfig() {
	[ -z "${PKG_CONFIG}" ] && PKG_CONFIG=pkg-config
	check_message "Checking for ${PKG_CONFIG}... "
	echo "Running: ${PKG_CONFIG} --variable pc_path pkg-config" >> config.log
	PKGCONFIGPATH="`${PKG_CONFIG} --variable pc_path pkg-config 2>> config.log`"
	if [ $? -eq 0 ] ; then
		check_message_result "yes"
		HASPKGCONFIG=yes

		if [ -n "${option_pkgconfigdir}" ] ; then
			pkgconfigdir="${option_pkgconfigdir}"
			EXPANDED_PKGCONFIGDIR="${option_pkgconfigdir}"
		else
			pkgconfigdir='$(libdir)/pkgconfig'
			EXPANDED_PKGCONFIGDIR="${option_libdir:-${PREFIX:-/usr/local}/lib}/pkgconfig"
		fi

		IFS=':'
		PKGCONFIGDIR_FOUND=no
		for DIR in ${PKGCONFIGPATH}
		do
			if [ "${DIR}" = "${EXPANDED_PKGCONFIGDIR}" ] ; then
				PKGCONFIGDIR_FOUND=yes
				break
			fi
		done
		unset IFS
		[ "${PKGCONFIGDIR_FOUND}" = no ] && check_message_result "WARNING: ${EXPANDED_PKGCONFIGDIR} is not in the pkg-config path. This expected for cross compilation."

	else
		HASPKGCONFIG=no
		check_message_result "no"
	fi
}

sed_rules_pkgconfig() {
	replace_default pkgconfigdir "${pkgconfigdir}"
}

gen_pkgconfig() {
	if [ -z "$PKGCONFIG_DESC" ] || [ -z "$PKGCONFIG_VERSION" ] ; then
		error "gen_pkgconfig requires PKGCONFIG_DESC and PKGCONFIG_VERSION"
	fi

	[ -z "$PKGCONFIG_NAME" ] && PKGCONFIG_NAME="$1"

	echo "----------------------------------">> config.log
	check_message_result "Creating $1.pc"
	{
		LOCAL_PREFIX="${PREFIX:-/usr/local}"
		echo "prefix=${LOCAL_PREFIX}"

		if [ -z "${option_libdir}" ] ; then
			echo "libdir=\${prefix}/lib"
		else
			WITHOUT_PREFIX="${option_libdir#${LOCAL_PREFIX}/}"
			if [ "${option_libdir}" = "${WITHOUT_PREFIX}" ] ; then
				echo "libdir=${option_libdir}"
			else
				echo "libdir=\${prefix}/${WITHOUT_PREFIX}"
			fi
		fi

		if [ -z "${option_includedir}" ] ; then
			echo "includedir=\${prefix}/include"
		else
			WITHOUT_PREFIX="${option_includedir#${LOCAL_PREFIX}/}"
			if [ "${option_includedir}" = "${WITHOUT_PREFIX}" ] ; then
				echo "includedir=${option_includedir}"
			else
				echo "includedir=\${prefix}/${WITHOUT_PREFIX}"
			fi
		fi

		echo
		echo "Name: $PKGCONFIG_NAME"
		echo "Description: $PKGCONFIG_DESC"
		echo "Version: $PKGCONFIG_VERSION"
		[ -n "$PKGCONFIG_URL" ] && echo "URL: $PKGCONFIG_URL"
		[ -n "$PKGCONFIG_REQUIRES" ] && echo "Requires: $PKGCONFIG_REQUIRES"
		[ -n "$PKGCONFIG_REQUIRES_PRIVATE" ] && echo "Requires.private: $PKGCONFIG_REQUIRES_PRIVATE"
		echo "Libs: -L\${libdir} $PKGCONFIG_LIBS"
		[ -n "$GETTEXTLIBS" ] && PKGCONFIG_LIBS_PRIVATE="$PKGCONFIG_LIBS_PRIVATE $GETTEXTLIBS"
		[ -n "$PKGCONFIG_LIBS_PRIVATE" ] && echo "Libs.private: $PKGCONFIG_LIBS_PRIVATE"
		[ -n "$PKGCONFIG_CFLAGS" ] && echo "Cflags: $PKGCONFIG_CFLAGS"
	} > "$1.pc"
}

append_pkgconfig() {
	eval $1="\"\${$1:+\${$1}, }$2\""
}
SWITCHES="${SWITCHES} -verbose-compile"

clean_verbose_compile() {
	:
}

show_help_verbose_compile() {
	if [ "$1" = OPTIONS ] ; then
		cat <<EOF
  --with-verbose-compile   Echo all commands during compile
EOF
	fi
}

print_rules_verbose_compile() {
	:
}

basic_test_verbose_compile() {
	:
}

sed_rules_verbose_compile() {
	[ yes = "${with_verbose_compile}" ] && echo '/^SILEN\(CE\|T\)/d'
}
unset HASPKGCONFIG

show_help_pkgconfig_dep() {
	:
}

print_rules_pkgconfig_dep() {
	:
}

clean_pkgconfig_dep() {
	:
}

basic_test_pkgconfig_dep() {
	:
}

sed_rules_pkgconfig_dep() {
	:
}

pkgconfig() {
	[ -z "${PKG_CONFIG}" ] && PKG_CONFIG=pkg-config
	if [ -z "${HASPKGCONFIG}" ] ; then
		check_message "Checking for pkg-config... "
		echo "Running: ${PKG_CONFIG} --variable pc_path pkg-config" >>config.log
		if ${PKG_CONFIG} --variable pc_path pkg-config > /dev/null 2>>config.log ; then
			check_message_result "yes"
			HASPKGCONFIG=yes
		else
			check_message_result "no"
			HASPKGCONFIG=no
		fi
	fi

	PKGNAME="${1%/*}"
	PKGVERSION="${1##*/}"

	if [ "${PKGVERSION}" = "$1" ] ; then
		unset PKGVERSION
	else
		PKGVERSION=" >= $PKGVERSION"
	fi

	if [ "${HASPKGCONFIG}" = no ] ; then
		check_message_result "Skipping check for $PKGNAME pkg-config because pkg-config is unavailable"
		return 2
	fi

	unset "${2}_FLAGS" "${2}_LIBS"
	check_message "Checking for $PKGNAME$PKGVERSION pkg-config... "
	echo "Running: ${PKG_CONFIG} --print-errors --exists $PKGNAME$PKGVERSION" >>config.log
	if not ${PKG_CONFIG} --print-errors --exists $PKGNAME $PKGVERSION 2>>config.log >/dev/null ; then
		check_message_result "no"
		return 1
	fi
	check_message_result "yes"

	if [ $# -ge 3 ] ; then
		if $3 "$PKGNAME pkg-config compile" "TESTFLAGS=\`${PKG_CONFIG} --cflags $PKGNAME\`" "TESTLIBS=\`${PKG_CONFIG} --libs $PKGNAME\`" 2>>config.log ; then
			eval "${2}_FLAGS"="\"\\\`${PKG_CONFIG} --cflags $PKGNAME\\\`\""
			eval "${2}_LIBS"="\"\\\`${PKG_CONFIG} --libs $PKGNAME\\\`\""
		elif $3 "$PKGNAME pkg-config --define-prefix compile" "TESTFLAGS=\`${PKG_CONFIG} --define-prefix --cflags $PKGNAME\`" "TESTLIBS=\`${PKG_CONFIG} --define-prefix --libs $PKGNAME\`" 2>>config.log ; then
			eval "${2}_FLAGS"="\"\\\`${PKG_CONFIG} --define-prefix --cflags $PKGNAME\\\`\""
			eval "${2}_LIBS"="\"\\\`${PKG_CONFIG} --define-prefix --libs $PKGNAME\\\`\""
		else
			return 1
		fi
		[ $# -ge 4 ] &&	eval $4="\"\${$4:+\${$4}, }$PKGNAME$PKGVERSION\""
	fi
	return 0
}
SWITCHES="${SWITCHES} +gettext"
OPTIONS="${OPTIONS} localedir"
SUFFIXES="${SUFFIXES} .mo .po"

[ -z "${DEFAULT_LINGUAS}" ] && error "!! DEFAULT_LINGUAS not set"

show_help_gettext() {
	if [ "$1" = OPTIONS ] ; then
		echo "  --without-gettext      Disable gettext translations"
		echo "  --localedir=<dir>      Install directory for locales [prefix/share/locale]"
	fi
}

print_rules_gettext() {
	cat <<EOF
.po.mo:
	msgfmt -o \$@ \$<
EOF
}

clean_gettext() {
	clean .config.po .config.mo
}

sed_rules_gettext() {
	replace_default GETTEXTFLAGS "${GETTEXTFLAGS}"
	replace_default GETTEXTLIBS "${GETTEXTLIBS}"
	[ -n "${option_localedir}" ] &&	replace_default LOCALEDIR "${option_localedir}"
	replace_default LINGUAS "${linguas}"
}

basic_test_gettext() {
	unset GETTEXTFLAGS GETTEXTLIBS linguas LOCALEDIR
	# Check for gettext functionality
	if [ "yes" = "${with_gettext}" ] ; then
		cat > .config.c <<EOF
#include <locale.h>
#include <libintl.h>

int main(int argc, char *argv[]) {
	setlocale(LC_ALL, "");
	bindtextdomain("package", "/usr/share/locale");
	dgettext("package", "string");
	textdomain("package");
	gettext("package");
	return 0;
}
EOF
		{
			test_compile "gettext and related functions" && {
				test_link "gettext in standard library" ||
				{ test_link "gettext in -lintl" TESTLIBS=-lintl && GETTEXTLIBS=-lintl ; } ||
				{ test_link "gettext in -lintl -liconv" "TESTLIBS=-lintl -liconv" && GETTEXTLIBS="-lintl -liconv" ; }
			} && {
				GETTEXTFLAGS="-DUSE_GETTEXT"

				clean .config.po .config.mo
				cat > .config.po <<EOF
msgid  "msg"
msgstr "translation"
EOF
				check_message "Checking for msgfmt... "
				if test_make .config.mo >> config.log 2>&1 ; then
					check_message_result "yes"
					true
				else
					check_message_result "no"
					false
				fi
			}
		} || {
			error "!! Could not compile with gettext. Try configuring with --without-gettext."
		}
		GETTEXTFLAGS="-DUSE_GETTEXT"

		if [ -n "${LINGUAS+set}" ] ; then
			check_message "Checking for available selected translations... "
			for lingua in ${LINGUAS}
			do
				found=0
				for test_lingua in ${DEFAULT_LINGUAS}
				do
					if [ "${test_lingua}" = "${lingua}" ] ; then
						found=1
						break
					fi
				done
				[ "${found}" = 1 ] && linguas="${linguas}${linguas+ }${lingua}"
			done
			check_message_result "done [${linguas}]"
		else
			linguas="${DEFAULT_LINGUAS}"
		fi
	fi
}
unset X11_FLAGS X11_LIBS

show_help_x11() {
	:
}

print_rules_x11() {
	:
}

clean_x11() {
	:
}

basic_test_x11() {
	:
}

sed_rules_x11() {
	replace_default X11_FLAGS "${X11_FLAGS}"
	replace_default X11_LIBS "${X11_LIBS}"
}


basic_x11() {
	clean_c
	cat > .config.c <<EOF
#include <X11/Xlib.h>
#include <X11/keysym.h>

int main(int argc, char *argv[]) {
	Display *display = XOpenDisplay(NULL);
	return 0;
}
EOF
	if pkgconfig x11 "X11" test_link ; then
		:
	else
		clean .config.o .config

		local X11_INCLUDEDIR
		if [ -n "${option_includedir}" ] ; then
			# Take into account that option_includedir may have ${DESTDIR} in it
			X11_INCLUDEDIR="`eval echo \"${option_includedir}\"`"
		else
			# Take into account that PREFIX may have ${DESTDIR} in it
			X11_INCLUDEDIR="`eval echo \"${PREFIX-/usr/local}/include\"`"
		fi

		if test_compile "X11 headers in standard path" ; then
			:
		elif test_compile "X11 headers in ${X11_INCLUDEDIR%/*}/X11R6/include" "TESTFLAGS=-I${X11_INCLUDEDIR%/*}/X11R6/include" ; then
			X11_FLAGS="-I${X11_INCLUDEDIR%/*}/X11R6/include"
		elif [ "${X11_INCLUDEDIR}" != "/usr/include" ] && test_compile "X11 headers in /usr/X11R6/include" "TESTFLAGS=-I/usr/X11R6/include" ; then
			X11_FLAGS="-I/usr/X11R6/include"
		else
			false
			return
		fi

		local X11_LIBDIR
		if [ -n "${option_libdir}" ] ; then
			# Take into account that option_libdir may have ${DESTDIR} in it
			X11_LIBDIR="`eval echo \"${option_libdir}\"`"
		else
			# Take into account that PREFIX may have ${DESTDIR} in it
			X11_LIBDIR="`eval echo \"${PREFIX-/usr/local}/lib\"`"
		fi

		if test_link "X11 using -lX11" "TESTLIBS=-lX11" ; then
			X11_LIBS="-lX11"
		elif test_link "X11 using -L${X11_LIBDIR}/X11 -lX11" "TESTLIBS=-L${X11_LIBDIR} -lX11" ; then
			X11_LIBS="-L${X11_LIBDIR} -lX11"
		elif test_link "X11 using -L${X11_LIBDIR%/*}/X11R6/${X11_LIBDIR##*/} -lX11" "TESTLIBS=-L${X11_LIBDIR%/*}/X11R6/${X11_LIBDIR##*/} -lX11" ; then
			X11_LIBS="-L${X11_LIBDIR%/*}/X11R6/${X11_LIBDIR##*/} -lX11"
		elif [ "${X11_LIBDIR}" != "/usr/lib" ] && test_link "X11 using -L/usr/lib/X11 -lX11" "TESTLIBS=-L/usr/lib/X11 -lX11" ; then
			X11_LIBS="-L/usr/lib/X11 -lX11"
		elif [ "${X11_LIBDIR}" != "/usr/lib" ] && test_link "X11 using -L/usr/X11R6/lib -lX11" "TESTLIBS=-L/usr/X11R6/lib -lX11" ; then
			X11_LIBS="-L/usr/X11R6/lib/X11 -lX11"
		else
			unset X11_LIBS
			unset X11_FLAGS
			false
			return
		fi
	fi
	true
}
show_help_lfs() {
	if [ "$1" = VARIABLES ] ; then
		cat <<EOF
  LFSFLAGS   C(++) compiler flags to use to enable Large File Support
  LFSLIBS    C(++) link flags to use to enable Large File Support
EOF
	fi
}

print_rules_lfs() {
	:
}

clean_lfs() {
	:
}

basic_test_lfs() {
	clean_c
	cat > .config.c <<EOF
#include <sys/types.h>
int off_t_assert[sizeof(off_t) > 4 ? 1 : -1];
int main(int argc, char *argv[]) {
	(void) argc; (void) argv;
	return 0;
}
EOF
	if not test_link "Large File Support as default" TESTFLAGS="$LFSFLAGS" TESTLIBS="$LFSLIBS" ; then
		if [ -z "$HOST_PREFIX" ]
		then
			LFSFLAGS="$(getconf LFS_CFLAGS 2>/dev/null)"
			LFSLIBS="$(getconf LFS_LDFLAGS 2>/dev/null)"
			if [ -n "$LFSLIBS" ]
			then
				LFSLIBS="${LFSLIBS} "
			fi
			LFSLIBS="${LFSLIBS}$(getconf LFS_LIBS 2>/dev/null)"
			if [ -n "$LFSFLAGS" ] || [ -n "$LFSLIBS" ] ; then
				touch_c
				if not test_link "Large File Support with getconf LFS_XXX flags" \
							TESTFLAGS="$LFSFLAGS" TESTLIBS="$LFSLIBS" ; then
					unset LFSFLAGS
					unset LFSLIBS
				fi
			fi
		else
			LFSFLAGS=-D_FILE_OFFSET_BITS=64
			LFSLIBS=
			if [ -n "$LFSFLAGS" ] || [ -n "$LFSLIBS" ] ; then
				touch_c
				if not test_link "Large File Support with -D_FILE_OFFSET_BITS=64" \
							TESTFLAGS="$LFSFLAGS" TESTLIBS="$LFSLIBS" ; then
					unset LFSFLAGS
					unset LFSLIBS
				fi
			fi
		fi

		if [ -z "$LFSFLAGS" ] && [ -z "$LFSLIBS" ] ; then
			check_message_result "WARNING: no Large File Support (LFS) found. If your \
system does have LFS, compiling without LFS may cause problems opening files on certain file systems."
		fi
	fi

	# Set both CFLAGS and CXXFLAGS, as either may be in use
	[ -n "$LFSFLAGS" ] && CFLAGS="$CFLAGS $LFSFLAGS" CXXFLAGS="$CXXFLAGS $LFSFLAGS"
	[ -n "$LFSLIBS" ] && LDLIBS="$LDLIBS $LFSLIBS"
	true
}

sed_rules_lfs() {
	:
}
#@INCLUDE_END

for _switch in ${SWITCHES}
do
	_name=`echo "${_switch}" | sed -e 's/^[+-]//' -e 's/-/_/g'`
	_pm="${_switch%%[!-+]*}"
	if [ "${_pm}" = "+" ] ; then
		eval with_${_name}="yes"
	else
		eval with_${_name}="no"
	fi
done
_SWITCHES=`echo " ${SWITCHES}" | sed 's/ [+-]/ /g'`

for PARAM
do
	case "${PARAM}" in
		-h|--help)
			cat <<EOF
Usage: configure [--prefix=<dir>] [<var>=<value>]

  --dump-log             Dump config.log to stdout on error
  --prefix=<dir>         Prefix for installation [/usr/local]
EOF
			for INSTALLDIR in ${INSTALLDIRS}
			do
				case "${INSTALLDIR}" in
					bindir)        echo '  --bindir=<dir>         Binaries directory [<prefix>/bin]' ;;
					sbindir)       echo '  --sbindir=<dir>        System binaries directory [<prefix>/sbin]' ;;
					libdir)        echo '  --libdir=<dir>         Library directory [<prefix>/lib]' ;;
					includedir)    echo '  --includedir=<dir>     Include file directory [<prefix>/include]' ;;
					datadir)       echo '  --datadir=<dir>        Directory for data [<prefix>/share]' ;;
					mandir)        echo '  --mandir=<dir>         Manual page directory [<prefix>/share/man]' ;;
					infodir)       echo '  --infodir=<dir>        Info page directory [<prefix>/share/man]' ;;
					docdir)        echo '  --docdir=<dir>         Document dir [<prefix>/share/doc/<name>-<version>]' ;;
				esac
			done
			[ -n "${USERINSTALLDIRSHELP}" ] && echo "${USERINSTALLDIRSHELP}"

			for EXT in ${EXTENSIONS}
			do
				show_help_$EXT OPTIONS
			done
cat <<EOF

Environment variables that tune the build:
  MAKE        Make program to use [make]
  PREFIX      See --prefix=<dir>
  INSTALL     The install program to use
  LDFLAGS     Linker flags to use (default determined by make)
  LDLIBS      Extra libraries to link
EOF
			for EXT in ${EXTENSIONS}
			do
				show_help_$EXT VARIABLES
			done

			[ -n "${USERHELP}" ] && { echo ; echo "Package specific settings" ; ${USERHELP} ; }

			cat <<EOF

Note: Environment variables may also be specified as parameters.
EOF
		exit 0
		;;
		--prefix=*)
			PREFIX="${PARAM#--prefix=}"
		;;
		--host=*)
			HOST_PREFIX="${PARAM#--host=}"
			[ -z "${LIBTOOL}" ] && LIBTOOL="${HOST_PREFIX}-libtool"
			if which "${LIBTOOL}" > /dev/null && echo "${EXTENSIONS}" | grep "libtool" > /dev/null
			then
				[ -z "${CC}" ] && CC="`"${LIBTOOL}" --config | grep '^CC' | tail -n1 | sed 's/.*"\([^"]\+\)".*/\1/'`"
				[ -z "${CXX}" ] && CXX="`"${LIBTOOL}" --tag=CXX --config | grep '^CC' | tail -n1 | sed 's/.*"\([^"]\+\)".*/\1/'`"
			else
				[ -z "${CC}" ] && CC="${HOST_PREFIX}-gcc"
				[ -z "${CXX}" ] && CXX="${HOST_PREFIX}-g++"
			fi
			[ -z "${PKG_CONFIG}" ] && PKG_CONFIG="${HOST_PREFIX}-pkg-config"

			echo "INFO: cross compiling using the following settings:"
			echo "  CC=$CC"
			echo "  CXX=$CXX"
			echo "  LIBTOOL=$LIBTOOL"
			echo "  PKG_CONFIG=$PKG_CONFIG"
		;;
		--dump-log)
			DUMP_LOG=1
		;;
		--*=*)
			unset _match
			for _option in ${OPTIONS} ${INSTALLDIRS}
			do
				case "${PARAM}" in
					--${_option}=*)
						_name=`echo "${_option}" | sed 's/-/_/g'`
						_value="${PARAM#*=}"
						eval option_${_name}="'${_value}'"
						_match="1"
						break
					;;
				esac
			done
			[ -z "${_match}" ] && echo "WARNING: ignoring unknown parameter: ${PARAM}" >&2
		;;
		--*)
			unset _match
			for _switch in ${_SWITCHES}
			do
				case "${PARAM}" in
					--with-${_switch})
						_name=`echo "${_switch}" | sed 's/-/_/g'`
						eval with_${_name}="yes"
						_match="1"
						break
					;;
					--without-${_switch})
						_name=`echo "${_switch}" | sed 's/-/_/g'`
						eval with_${_name}="no"
						_match="1"
						break
					;;
				esac
			done
			[ -z "${_match}" ] && echo "WARNING: ignoring unknown parameter: ${PARAM}" >&2
		;;
		*=*)
			name="${PARAM%%=*}"
			value="${PARAM#*=}"
			eval "${name}"="\"${value}\""
		;;
		*)
			error "Error on commandline: ${PARAM}"
		;;
	esac
done

# Remove trailing slashes from the prefix. These are superfluous and get in
# the way of testing.
STRIPPED_PREFIX="${PREFIX%/}"
while [ "${PREFIX}" != "${STRIPPED_PREFIX}" ]
do
	PREFIX="${STRIPPED_PREFIX}"
	STRIPPED_PREFIX="${PREFIX%/}"
done

if [ -n "${INSTALL}" ] ; then
	TEST_INSTALL="${INSTALL}"
	unset INSTALL
else
	TEST_INSTALL="install ./install.sh"
fi

echo "Configuration test log created at `date`" > config.log
echo "-- configure called with $0 $@" >> config.log

clean config.log .Makefile .Makefile.in .sedscript .foo .config_base
for EXT in ${EXTENSIONS}
do
	clean_${EXT}
done

{
	echo ".POSIX:"
	[ -z "${LDFLAGS}" ] || echo "LDFLAGS=${LDFLAGS}"
	[ -z "${LDLIBS}" ] || echo "LDLIBS=${LDLIBS}"

	[ -z "${SUFFIXES}" ] || echo ".SUFFIXES: ${SUFFIXES}"

	[ -z "${USERRULES}" ] || ${USERRULES}

	cat <<EOF
install:
	\$(INSTALL) -d .foo/bar
	\$(INSTALL) -m 755 configure .foo/bar
	test -x .foo/bar/configure

.config_base:
	touch \$@

EOF

	for EXT in ${EXTENSIONS}
	do
		print_rules_${EXT}
	done
} > .Makefile.in

cp .Makefile.in .Makefile

echo "Using settings:" >> config.log
grep '^[[:upper:]_]\+=' .Makefile >> config.log

[ -n "${MAKE}" ] ||	MAKE=make

[ -z "${PRECHECKFUNC}" ] || ${PRECHECKFUNC}

check_message "Checking for working make (${MAKE})... "
if test_make .config_base >> config.log 2>&1 ; then
	check_message_result "yes"
else
	check_message_result "no"
	error "${MAKE} failed. See config.log for errors."
fi

for EXT in ${EXTENSIONS}
do
	basic_test_${EXT}
	sed_rules_${EXT} >> .sedscript
	sed -f .sedscript .Makefile.in > .Makefile
done

config
error "Error in config.pkg. Cannot continue."
