#!/usr/bin/env sh
#
#
# NOTE: Before you start yelling, yes this is NOT a GNU auto* build
#       system. However, I have written this in a way that it
#       has as few changes as needed.
#
#       You still have all the features:
#
#       - Cross Compilation Support
#       - Support for various compilers and operating systems
#       - Out-of-tree builds
#       - DESTDIR/PREFIX install
#       - Test suite
#
#       PLEASE READ THE README / HACKING GUIDE FOR MORE INFORMATION.

CONFIGURE_CMD_ARGS="${*}"

PACKAGE_VERSION="2.14.0"
PACKAGE_DATE="01-Nov-2026"
PACKAGE_STRING="gcli $PACKAGE_VERSION"
PACKAGE_BUGREPORT="https://lists.sr.ht/~herrhotzenplotz/gcli-discuss"
PACKAGE_URL="https://sr.ht/~herrhotzenplotz/gcli"

# What follows is an ugly hack:
exec 2>config.log # stderr goes to config log
exec 3>&1         # terminal output in fd #3
exec 1>&2         # stdout also goes to the config log
set -x            # enable xtrace to stderr

# Helpers for terminal logging
l_echo() {
	echo >&3 "$@"
}

l_printf() {
	printf >&3 "$@"
}

# Notes:
#
#  Comments that should end up in the config.log can be made
#  using the : builtin.
#
#  Use above helpers for printing to the terminal.

find_program() {
	varname=$1
	shift

	l_printf "Checking for $varname ..."
	for x in $*; do
		if command -v $x >/dev/null 2>&1 && [ -x $(command -v $x) ]; then
			binary="$(command -v $x)"
			l_printf " $binary\n"
			echo "${binary}"
			exit 0
		fi
	done
	l_printf " not found\n"
	exit 1
}

die() {
	l_printf "%s\n" "${*}"
	exit 1
}

tolower() {
	tr '[:upper:]' '[:lower:]'
}

check_compiler() {
	command -v "${1}" > /dev/null 2>&1 || die " '${1}' not found"
}

compiler_type() {
	if ${1} -v 2>&1 | grep clang > /dev/null; then
		echo "clang"
	elif ${1} -v 2>&1 | grep '^gcc' > /dev/null; then
		echo "gcc"
	elif ${1} -qversion 2>&1 | grep 'IBM XL C' >/dev/null; then
		echo "xlc"
	elif ${1} -V 2>&1 | grep 'Studio' > /dev/null; then
		echo "sunstudio"
	elif ${1} --compiler-info 2>&1 | grep -i kefir > /dev/null; then
		echo "kefir"
	else
		echo "unknown"
	fi
}

normalise_compiler_target() {
	tolower | sed -e 's|x86_64|amd64|g' -e 's| ||g'
}

compiler_target() {
	ccom=$1
	cc=$2

	: Trying to find out target for $cc which appears to be $ccom

	case $ccom in
		gcc|clang)
			$cc -v 2>&1 \
				| grep '^Target' \
				| cut -d: -f2
			;;
		xlc)
			# for xlc we can't easily get its target. instead we ask its
			# configured assembler for the target. Right now I assume it
			# is a GNU assembler. If this doesn't work for you please fix!
			# I don't have access to any equipment running anything else
			# and I will not beg IBM for giving me access.
			xlc_config=$($cc -v 2>&1 | grep XL_CONFIG | sed 's|.*XL_CONFIG=\([^:]*\):.*|\1|')
			[ -z "${xlc_config}" ] && die "xlc config file not detected"
			xlc_as=$(cat $xlc_config | grep -E 'as[\t ]+.*=[ ]+' | sed 1q | cut -d= -f2 | sed 's| ||g')
			[ -z "${xlc_as}" ] && die "failed to figure out assembler used by XL C"
			$xlc_as --version 2>&1 | grep 'GNU assembler' >/dev/null || die "Can only derive compiler target from GNU assembler, please submit fix for your assembler"
			$xlc_as --version 2>&1 | grep target | sed "s|.*\`\([^']*\)'.*|\1|"
			;;
		sunstudio)
			# Only tested on Solaris 10 ... if this messes up on Linux or SunOS
			# i86pc feel free to send a patch...
			if $cc -V 2>&1 | grep SunOS_sparc > /dev/null; then
				echo "sparc64-unknown-solaris"
			else
				echo "unknown-unknown-unknown"
			fi
			;;
		kefir)
			(
				eval $($cc --environment-info 2>&1)
				[ -n "${KEFIR_TARGET}" ] && echo ${KEFIR_TARGET} || echo "unknown-unknown-unknown"
			)
			;;
        	*)
			echo "unknown-unknown-unknown"
			;;
	esac | normalise_compiler_target
}

# Check whether we should check for something ($1 = 1 or auto)
#
# args: $enable
check_if_needed() {
	case "${1}" in
	1|auto) return 0 ;;
	*)      return 1 ;;
	esac
}

# args= pkgconfig-name variable-name is-mandatory is-enabled
find_package() {
	eval local force_enabled="\$ENABLE_${2}"

	l_printf "Checking for $1 ..."
	if ! $PKG_CONFIG --exists $1; then
		: '<< ERROR >>' $PKG_CONFIG failed
		if [ "${3}" = optional ] && ! [ "$force_enabled" = 1 ]; then
			l_printf " not found\n"
			export ${2}_FOUND=0
			return
		else # $3 = required or force_enabled
			l_printf "not found\n"
			l_printf "configure: required or forced dependency not found: %s\n" "${1}"
			die "configure: exiting due to dependency problems"
		fi
	fi

	# readline and possibly other dependencies in Debian (yikes)
	# return -D_XOPEN_SOURCE which breaks builds. Rip it out -
	# we know what we're doing... ha!
	export ${2}_CFLAGS="$($PKG_CONFIG --cflags $1 | sed 's|-D_XOPEN_SOURCE=[[:digit:]]*||g')"
	export ${2}_LIBS="$($PKG_CONFIG --libs $1)"
	export ${2}_FOUND=1
	export ${2}_VERSION="$($PKG_CONFIG --modversion $1)"

	l_printf " found\n"
}

usage() {
	cat >&3 <<EOF
usage: ./configure [options]

Configure build directory of gcli.

OPTIONS:
    --prefix=PREFIX         Set installation prefix to PREFIX [DEFAULT: /usr/local]
    --enable-libedit        Force use of libedit for interactive editing [DEFAULT: autodetect]
                            This flag is mutually exclusive with --enable-libreadline.
    --disable-libedit       Disable use of libedit for interactive editing
    --enable-libreadline    Force use of libreadline for interactive editing
                            This flag is mutually exclusive with --enable-libedit.
    --disable-libreadline   Disable use of libreadline for interactive editing [DEFAULT: autodetect]
    --enable-liblowdown     Force use of liblowdown for markdown rendering [DEFAULT: autodetect]
    --disable-liblowdown    Disable use of liblowdown for markdown rendering
    --disable-librt         Forcefully disable linking librt [DEFAULT: no, except on Darwin]
    --enable-maintainer     Enable Maintainer Mode [DEFAULT: no]
                            You only want this if you're working on the gcli source code.
                            In this case certain build targets get enabled that only work
                            when the source directory is writable.
                            Do NOT use this flag together with --release or when packaging
                            gcli.
    --enable-sanitisers     Enable compiler-specific sanitisers
    --debug                 Compile without optimisations, generate code with
                            debug information and enable additional compiler-specific
                            warnings and portability checks.
    --release               Optimise for release
    --help                  Print this help

ENVIRONMENT:
    CC                      Host system compiler to use [DEFAULT: cc]
    CC_FOR_BUILD            Build system compiler [DEFAULT: \$CC]
    CFLAGS                  Host system C compiler flags
    CFLAGS_FOR_BUILD        Build system compiler flags
    CPPFLAGS                C Preprocessor flags for host system compiler
    CPPFLAGS_FOR_BUILD      C Preprocessor flags for build system compiler
    LDFLAGS                 Host system linker flags
    LDFLAGS_FOR_BUILD       Build system linker flags
    PKG_CONFIG              pkg-config to use [DEFAULT: autodetect]
    AR                      Archiver to use [DEFAULT: autodetect]
    RANLIB                  Ranlib to use [DEFAULT: autodetect]
    RM                      rm to use [DEFAULT: autodetect]
    CCACHE                  Path to ccache [DEFAULT: autodetect]
    PERL                    Path to perl for test suite [DEFAULT: autodetect]
    GZIP                    Path to gzip for manual page compression [DEFAULT: autodetect]
    INSTALL                 Path to install [DEFAULT: autodetect]
    EXEEXT                  File extension of executable files for the host system [DEFAULT: '', '.exe' on Windows]
    OBJEXT                  File extension of object files for the host system [DEFAULT: '.o', '.obj' on Windows]
    LIBEXT                  File extension of static libraries for the host system [DEFAULT: '.a', '.lib' on Windows]
    EXEEXT_FOR_BUILD        File extension of executable files for the build system [DEFAULT: '', '.exe' on Windows]
    OBJEXT_FOR_BUILD        File extension of object files for the build system [DEFAULT: '.o', '.obj' on Windows]
    LIBEXT_FOR_BUILD        File extension of static libraries for the build system [DEFAULT: '.a', '.lib' on Windows]
EOF
	exit 1
}

# Install options
#
PREFIX=/usr/local
OPTIMISE=
ENABLE_LIBEDIT=auto
ENABLE_LIBREADLINE=auto
ENABLE_LIBLOWDOWN=auto
ENABLE_LIBRT=1
ENABLE_MAINTAINER=${ENABLE_MAINTAINER:-0}
ENABLE_TESTS=auto
ENABLE_SANITISERS=${ENABLE_SANITISERS:-0}
DEPRECATED_OPTIONS_USED=0
UNKNOWN_OPTIONS_USED=0

# Parse flags
while [ $# -gt 0 ]; do
	case "$1" in
		--prefix=*)
			PREFIX=${1##--prefix=}
			shift
			;;
		--prefix)
			PREFIX=${2}
			shift; shift
			;;
		--enable-libedit)      ENABLE_LIBEDIT=1;     shift ;;
		--disable-libedit)     ENABLE_LIBEDIT=0;     shift ;;
		--enable-libreadline)  ENABLE_LIBREADLINE=1; shift ;;
		--disable-libreadline) ENABLE_LIBREADLINE=0; shift ;;
		--enable-liblowdown)   ENABLE_LIBLOWDOWN=1;  shift ;;
		--disable-liblowdown)  ENABLE_LIBLOWDOWN=0;  shift ;;
		--disable-librt)       ENABLE_LIBRT=0;       shift ;;
		--enable-maintainer)   ENABLE_MAINTAINER=1;  shift ;;
		--disable-tests)       ENABLE_TESTS=0;       shift ;;
		--enable-sanitisers)   ENABLE_SANITISERS=1;  shift ;;
		--debug)
			OPTIMISE=debug
			shift
			;;
		--release)
			OPTIMISE=release
			shift
			;;
		--help)
			usage
			;;
		# Unknown. Do not error out, but inform the user later.
		*)
			l_echo "configure: warning: unrecognised option '$1'"
			UNKNOWN_OPTIONS_USED=1
			shift
			;;
	esac
done

REALPATH=${REALPATH:-$(find_program realpath realpath grealpath)}
[ $? -eq 0 ] || die "error: need realpath" # exit code of find_program()

rel_srcdir="$(dirname ${0})"
srcdir="$(${REALPATH} ${rel_srcdir})"

###############################################################
# COMPILERS
###############################################################

:
: Begin compiler checks
:

:
: Host compiler
:
l_printf "Checking host compiler ..."
CC=${CC:-cc}
check_compiler "${CC}"
l_printf " $CC\n"

# Detect the compiler type and enable dependency tracking
l_printf "Checking host compiler type ..."
CCOM=$(compiler_type "${CC}")
l_printf " $CCOM\n"

l_printf "Checking host compiler target ..."
HOST=$(compiler_target $CCOM "$CC $CFLAGS $CPPFLAGS")
l_printf " $HOST\n"

:
: Build compiler
:
CC_FOR_BUILD=${CC_FOR_BUILD:-${CC}}

l_printf "Checking for build compiler..."
check_compiler "${CC_FOR_BUILD}"
l_printf " ${CC_FOR_BUILD}\n"

l_printf "Checking build compiler type..."
CCOM_FOR_BUILD=$(compiler_type "${CC_FOR_BUILD}")
l_printf " ${CCOM_FOR_BUILD}\n"

l_printf "Checking build compiler target ..."
BUILD=$(compiler_target $CCOM_FOR_BUILD $CC_FOR_BUILD)
l_printf " $BUILD\n"

####################################################################
# The following check is to determine whether we can use 700
# for _XOPEN_SOURCE. We need this for PATH_MAX because Solaris is
# silly. Don't do this check if we already have the X/Open Flag in
# CPPFLAGS otherwise it piles up on reconfig. Also don't override
# user-provided flags.
# args: cc
solaris_check_xopensource() {
	tmpfile="$(mktemp "gcli_conftest.XXX.c")"
	cc="${1}"

	cat > "${tmpfile}" <<EOF
#include <stdio.h>
int main(void) { puts("1"); return 0; }
EOF

	$cc -D_XOPEN_SOURCE=700 -o /tmp/gcliconftest $tmpfile >/dev/null 2>&1 &&
		echo 1 ||
		echo 0

	rm -f /tmp/gcliconftest ${tmpfile}
}

misses_xopen_source() {
	case "${1}" in
	*XOPEN_SOURCE=*) return 1 ;;
	*) return 0 ;;
	esac
}

:
: Checking if we should define _XOPEN_SOURCE and if so, to what value
:
if misses_xopen_source "${CPPFLAGS}"; then
	XOPEN_SOURCE="-D_XOPEN_SOURCE=700"
	if [ "${CCOM}" = sunstudio ] && [ "${HOST}" = sparc64-unknown-solaris ]; then
		l_printf "Checking whether we can define _XOPEN_SOURCE=700 for the host ... "
		if [ "$(solaris_check_xopensource "${CC} ${CPPFLAGS} ${CFLAGS}")" = 1 ]; then
			l_printf " yes\n"
		else
			l_printf "no, falling back to 600\n"
			XOPEN_SOURCE="-D_XOPEN_SOURCE=600"
		fi
	fi

	[ -z "${CPPFLAGS}" ] &&
		CPPFLAGS="${XOPEN_SOURCE}" ||
		CPPFLAGS="${CPPFLAGS} ${XOPEN_SOURCE}"
fi

if misses_xopen_source "${CPPFLAGS_FOR_BUILD}"; then
	XOPEN_SOURCE_FOR_BUILD="-D_XOPEN_SOURCE=700"
	if [ "${CCOM_FOR_BUILD}" = sunstudio ] && [ "${BUILD}" = sparc64-unknown-solaris ]; then
		l_printf "Checking whether we can define _XOPEN_SOURCE=700 for the build system ... "
		if [ "$(solaris_check_xopensource "${CC_FOR_BUILD} ${CPPFLAGS_FOR_BUILD} ${CFLAGS_FOR_BUILD}")" = 1 ]; then
			l_printf " yes\n"
		else
			l_printf "no, falling back to 600\n"
			XOPEN_SOURCE_FOR_BUILD="-D_XOPEN_SOURCE=600"
		fi
	fi

	[ -z "${CPPFLAGS_FOR_BUILD}" ] &&
		CPPFLAGS_FOR_BUILD="${XOPEN_SOURCE_FOR_BUILD}" ||
		CPPFLAGS_FOR_BUILD="${CPPFLAGS_FOR_BUILD} ${XOPEN_SOURCE_FOR_BUILD}"
fi

############################################################################
# LIBRARIES
############################################################################
:
: Detecting dependencies via pkgconf / pkg-config
:
PKG_CONFIG=${PKG_CONFIG:-$(find_program pkg-config pkg-config pkgconf)}
[ $? -eq 0 ] || die "error: need pkg-config or pkgconf"

# We only support one interactive editing helper
if [ "${ENABLE_LIBEDIT}" = 1 ] && [ "${ENABLE_LIBREADLINE}" = 1 ]; then
	die "configure: error: --enable-libedit and --enable-libreadline don't make sense together. You must choose either one or none!"
fi

find_package libcurl LIBCURL required
find_package libcrypto LIBCRYPTO required

: Look for libedit if not disabled
if check_if_needed "$ENABLE_LIBEDIT"; then
	find_package libedit LIBEDIT optional
else
	LIBEDIT_FOUND=0
fi

# Now only look for readline if libedit hasn't been found/enabled
# and readline support hasn't been disabled.
if [ $LIBEDIT_FOUND -eq 1 ]; then
	LIBREADLINE_FOUND=0
elif check_if_needed "$ENABLE_LIBREADLINE"; then
	find_package readline LIBREADLINE optional
else
	LIBREADLINE_FOUND=0
fi

# Lowdown
if check_if_needed "$ENABLE_LIBLOWDOWN"; then
	find_package lowdown LIBLOWDOWN optional

	# Workaround for breaking API changes and header without versions:
	#
	# See https://github.com/kristapsdz/lowdown/issues/148
	LIBLOWDOWN_MAJOR=$(echo $LIBLOWDOWN_VERSION | cut -d. -f1)
	LIBLOWDOWN_MINOR=$(echo $LIBLOWDOWN_VERSION | cut -d. -f2)
else
	LIBLOWDOWN_FOUND=0
fi

# If there is no system-provided pdjson, use the vendored one
find_package pdjson PDJSON optional

# On Darwin the POSIX Real-Time extensions are in libc. We must not
# link against librt because it doesn't exist.
if check_if_needed "${ENABLE_LIBRT}"; then
	l_printf "Checking whether host is Darwin for -lrt..."
	case "${HOST}" in
		*-apple-darwin*)
			l_printf " yes, won't link -lrt\n"
			ENABLE_LIBRT=0
			;;
		*)
			l_printf " no\n"
			;;
	esac
fi

###################################################################
# File extensions for operating systems
#
# LIBEXT refers to static libraries.
guess_extension() {
	# Executables
	if [ "$1" = EXE ]; then
		case "${2}" in
		*mingw*|*cygwin*|*winnt*) echo ".exe" ;;
		*)                        echo "" ;;
		esac

	# Objects
	elif [ "$1" = OBJ ]; then
		case "${2}" in
		*mingw*|*cygwin*|*winnt*) echo ".obj" ;;
		*)                        echo ".o" ;;
		esac

	# Static Libraries
	elif [ "$1" = LIB ]; then
		case "${2}" in
		*mingw*|*cygwin*|*winnt*) echo ".lib" ;;
		*)                        echo ".a" ;;
		esac

	else
		die "internal configure error"
	fi
}

:
: Guessing file extensions
:
EXEEXT=${EXEEXT:-$(guess_extension EXE $HOST)} || die "error: failed to guess executable extension for $HOST"
OBJEXT=${OBJEXT:-$(guess_extension OBJ $HOST)} || die "error: failed to guess object extension for $HOST"
LIBEXT=${LIBEXT:-$(guess_extension LIB $HOST)} || die "error: failed to guess static library extension for $HOST"

EXEEXT_FOR_BUILD=${EXEEXT_FOR_BUILD:-$(guess_extension EXE $BUILD)} || die "error: failed to guess executable extension for $BUILD"
OBJEXT_FOR_BUILD=${OBJEXT_FOR_BUILD:-$(guess_extension OBJ $BUILD)} || die "error: failed to guess object extension for $BUILD"
LIBEXT_FOR_BUILD=${LIBEXT_FOR_BUILD:-$(guess_extension LIB $BUILD)} || die "error: failed to guess static library extension for $BUILD"

###################################################################
# TESTS and other programs
###################################################################
AR=${AR:-ar}
RANLIB=${RANLIB:-ranlib}
RM=${RM:-rm}
CCACHE=${CCACHE:-$(find_program ccache ccache)}
INSTALL=${INSTALL:-$(find_program install install ginstall)}
[ $? -eq 0 ] || die "error: need install program"
SHELL=${SHELL:-$(find_program sh sh zsh bash)}

# If maintainer mode is enabled, we need lowdown(1)
if [ ${ENABLE_MAINTAINER} -eq 1 ]; then
	LOWDOWN=${LOWDOWN:-$(find_program lowdown lowdown)}
	[ $? -eq 0 ] || die "error: couldn't find lowdown which is required in maintainer mode"
fi

:
: Dependencies and checks for the test suite
:
if check_if_needed "${ENABLE_TESTS}"; then
	PERL=${PERL:-$(find_program perl perl perl5)}
	if ! [ $? -eq 0 ]; then
		ENABLE_TESTS=0
		l_echo "configure: warning: tests are disabled because perl couldn't be found"
	fi

	# Determine Version of TAP::Harness Module
	l_printf "Checking version of TAP::Harness ... "
	TAPHARNESS_VER="$($PERL -MTAP::Harness -e 'print $TAP::Harness::VERSION ."\n";' 2>&1)"
	if [ ! $? -eq 0 ]; then
		l_printf 'failed to determine: %s. Disabling tests.\n' "${TAPHARNESS_VER}"
		ENABLE_TESTS=0
	else
		l_printf '%s, ' "${TAPHARNESS_VER}"
		TAPHARNESS_MAJOR="${TAPHARNESS_VER%.*}"
		TAPHARNESS_MINOR="${TAPHARNESS_VER#*.}"

		if [ ${TAPHARNESS_MAJOR} -gt 3 ] ||
		   [ ${TAPHARNESS_MAJOR} -eq 3 -a ${TAPHARNESS_MINOR} -ge 48 ]
		then
			l_printf 'tests are enabled.\n'
			TAPVER=14
			ENABLE_TESTS=1
		else
			l_printf 'too old, disabling tests.\n'
			ENABLE_TESTS=0
		fi
	fi
fi
###################################################################
# Manual Pages require gzip
###################################################################
GZIP=${GZIP:-$(find_program gzip gzip)}
[ $? -eq 0 ] || die "Need gzip for manual page compression"

###################################################################
# Configure Makefile
###################################################################
#
# FIXME: this escapes the source directory in case it contains
#        spaces. however, both GNU and BSD make are incapable of
#        handling hese situations in combination with VPATH. On
#        NetBSD we get a bug where it hard-splits the path on spaces
#        in a prerequisite no matter whether it is escaped or not.
ESC_SRCDIR="$(echo ${srcdir} | sed -e 's| |\\\\ |g')"

sed \
	-e "s|@SRCDIR@|${ESC_SRCDIR}|g" \
	-e "s|@OPTIMISE@|$OPTIMISE|g" \
	-e "s|@LIBCURL_CFLAGS@|$LIBCURL_CFLAGS|g" \
	-e "s|@LIBCURL_LIBS@|$LIBCURL_LIBS|g" \
	-e "s|@LIBCRYPTO_CFLAGS@|$LIBCRYPTO_CFLAGS|g" \
	-e "s|@LIBCRYPTO_LIBS@|$LIBCRYPTO_LIBS|g" \
	-e "s|@LIBEDIT_FOUND@|$LIBEDIT_FOUND|g" \
	-e "s|@LIBEDIT_CFLAGS@|$LIBEDIT_CFLAGS|g" \
	-e "s|@LIBEDIT_LIBS@|$LIBEDIT_LIBS|g" \
	-e "s|@LIBREADLINE_FOUND@|$LIBREADLINE_FOUND|g" \
	-e "s|@LIBREADLINE_CFLAGS@|$LIBREADLINE_CFLAGS|g" \
	-e "s|@LIBREADLINE_LIBS@|$LIBREADLINE_LIBS|g" \
	-e "s|@LIBLOWDOWN_FOUND@|$LIBLOWDOWN_FOUND|g" \
	-e "s|@LIBLOWDOWN_CFLAGS@|$LIBLOWDOWN_CFLAGS|g" \
	-e "s|@LIBLOWDOWN_LIBS@|$LIBLOWDOWN_LIBS|g" \
	-e "s|@LIBLOWDOWN_MAJOR@|$LIBLOWDOWN_MAJOR|g" \
	-e "s|@LIBLOWDOWN_MINOR@|$LIBLOWDOWN_MINOR|g" \
	-e "s|@PDJSON_FOUND@|$PDJSON_FOUND|g" \
	-e "s|@PDJSON_CFLAGS@|$PDJSON_CFLAGS|g" \
	-e "s|@PDJSON_LIBS@|$PDJSON_LIBS|g" \
	-e "s|@ENABLE_LIBRT@|$ENABLE_LIBRT|g" \
	-e "s|@CONFIGURE_CMD_ARGS@|$CONFIGURE_CMD_ARGS|g" \
	-e "s|@CC@|$CC|g" \
	-e "s|@CCOM@|$CCOM|g" \
	-e "s|@ENV_CFLAGS@|$CFLAGS|g" \
	-e "s|@ENV_LDFLAGS@|$LDFLAGS|g" \
	-e "s|@ENV_LDFLAGS_FOR_BUILD@|$LDFLAGS_FOR_BUILD|g" \
	-e "s|@ENV_CPPFLAGS@|$CPPFLAGS|g" \
	-e "s|@CC_FOR_BUILD@|$CC_FOR_BUILD|g" \
	-e "s|@CCOM_FOR_BUILD@|$CCOM_FOR_BUILD|g" \
	-e "s|@ENV_CFLAGS_FOR_BUILD@|$CFLAGS_FOR_BUILD|g" \
	-e "s|@ENV_CPPFLAGS_FOR_BUILD@|$CPPFLAGS_FOR_BUILD|g" \
	-e "s|@ENV_PKG_CONFIG@|$PKG_CONFIG|g" \
	-e "s|@ENV_PKG_CONFIG_PATH@|$PKG_CONFIG_PATH|g" \
	-e "s|@ENV_PKG_CONFIG_LIBDIR@|$PKG_CONFIG_LIBDIR|g" \
	-e "s|@ENV_PKG_CONFIG_SYSROOT_DIR@|$PKG_CONFIG_SYSROOT_DIR|g" \
	-e "s|@AR@|$AR|g" \
	-e "s|@CCACHE@|$CCACHE|g" \
	-e "s|@RANLIB@|$RANLIB|g" \
	-e "s|@RM@|$RM|g" \
	-e "s|@PERL@|$PERL|g" \
	-e "s|@GZIP@|$GZIP|g" \
	-e "s|@INSTALL@|$INSTALL|g" \
	-e "s|@SHELL@|$SHELL|g" \
	-e "s|@PREFIX@|$PREFIX|g" \
	-e "s|@PACKAGE_STRING@|$PACKAGE_STRING|g" \
	-e "s|@PACKAGE_DATE@|$PACKAGE_DATE|g" \
	-e "s|@PACKAGE_BUGREPORT@|$PACKAGE_BUGREPORT|g" \
	-e "s|@PACKAGE_URL@|$PACKAGE_URL|g" \
	-e "s|@PACKAGE_VERSION@|$PACKAGE_VERSION|g" \
	-e "s|@EXEEXT@|$EXEEXT|g" \
	-e "s|@OBJEXT@|$OBJEXT|g" \
	-e "s|@LIBEXT@|$LIBEXT|g" \
	-e "s|@EXEEXT_FOR_BUILD@|$EXEEXT_FOR_BUILD|g" \
	-e "s|@OBJEXT_FOR_BUILD@|$OBJEXT_FOR_BUILD|g" \
	-e "s|@LIBEXT_FOR_BUILD@|$LIBEXT_FOR_BUILD|g" \
	-e "s|@ENABLE_MAINTAINER@|$ENABLE_MAINTAINER|g" \
	-e "s|@ENABLE_TESTS@|$ENABLE_TESTS|g" \
	-e "s|@ENABLE_SANITISERS@|$ENABLE_SANITISERS|g" \
	-e "s|@LOWDOWN@|$LOWDOWN|g" \
	< "${srcdir}/Makefile.in" > Makefile

echo "Writing config.h"
tee config.h <<EOF
#define PACKAGE_STRING "$PACKAGE_STRING"
#define PACKAGE_URL "$PACKAGE_URL"
#define PACKAGE_VERSION "$PACKAGE_VERSION"
#define PACKAGE_BUGREPORT "$PACKAGE_BUGREPORT"
#define PACKAGE_DATE "$PACKAGE_DATE"
#define HOSTOS "$HOST"
EOF

l_echo "Configuration summary for ${PACKAGE_STRING}:"
l_echo ""
l_echo "    Build system type: ${BUILD}"
l_echo "     Host system type: ${HOST}"
l_echo "         optimise for: ${OPTIMISE:-none}"
l_echo " Executable extension: Host: '${EXEEXT}', Build: '${EXEEXT_FOR_BUILD}'"
l_echo "     Object extension: Host: '${OBJEXT}', Build: '${OBJEXT_FOR_BUILD}'"
l_echo "    Library extension: Host: '${LIBEXT}', Build: '${LIBEXT_FOR_BUILD}'"
l_echo "                   CC: ${CC}"
l_echo "         CC_FOR_BUILD: ${CC_FOR_BUILD}"
l_echo "               CFLAGS: ${CFLAGS}"
l_echo "     CFLAGS_FOR_BUILD: ${CFLAGS_FOR_BUILD}"
l_echo "             CPPFLAGS: ${CPPFLAGS}"
l_echo "   CPPFLAGS_FOR_BUILD: ${CPPFLAGS_FOR_BUILD}"
l_echo "              LDFLAGS: ${LDFLAGS}"
l_echo "    LDFLAGS_FOR_BUILD: ${LDFLAGS_FOR_BUILD}"
l_echo "       LIBCURL_CFLAGS: ${LIBCURL_CFLAGS}"
l_echo "         LIBCURL_LIBS: ${LIBCURL_LIBS}"
if [ $LIBEDIT_FOUND -eq 1 ]; then
l_echo " Using libedit:"
l_echo "       LIBEDIT_CFLAGS: ${LIBEDIT_CFLAGS}"
l_echo "         LIBEDIT_LIBS: ${LIBEDIT_LIBS}"
elif [ $LIBREADLINE_FOUND -eq 1 ]; then
l_echo " Using libreadline:"
l_echo "   LIBREADLINE_CFLAGS: ${LIBREADLINE_CFLAGS}"
l_echo "     LIBREADLINE_LIBS: ${LIBREADLINE_LIBS}"
fi
if [ $LIBLOWDOWN_FOUND -eq 1 ]; then
l_echo " Using lowdown ${LIBLOWDOWN_VERSION}:"
l_echo "    LIBLOWDOWN_CFLAGS: ${LIBLOWDOWN_CFLAGS}"
l_echo "      LIBLOWDOWN_LIBS: ${LIBLOWDOWN_LIBS}"
fi
if [ $PDJSON_FOUND -eq 1 ]; then
l_echo " Using pdjson:"
l_echo "        PDJSON_CFLAGS: ${PDJSON_CFLAGS}"
l_echo "          PDJSON_LIBS: ${PDJSON_LIBS}"
else
l_echo ""
l_echo "     WARNING: pdjson not found, using vendored version."
l_echo "              You may wish to install a system version instead."
fi
if [ $ENABLE_LIBRT -eq 0 ]; then
l_echo " Not linking -lrt because it was disabled"
fi
if [ $ENABLE_MAINTAINER -eq 1 ]; then
l_echo ""
l_echo "     WARNING: Maintainer mode is enabled!"
fi
if [ $ENABLE_MAINTAINER -eq 1 ] && [ "${OPTIMISE}" = "release" ]; then
l_echo ""
l_echo "     WARNING: --enable-maintainer and --release specified together."
l_echo "              You probably don't want this for packaging."
l_echo "              Drop --enable-maintainer!"
fi
if [ $DEPRECATED_OPTIONS_USED -eq 1 ]; then
l_echo ""
l_echo "     WARNING: Deprecated options have been supplied to the configure script."
l_echo "              Please review the messages above and remove deprecated options."
fi
if [ $UNKNOWN_OPTIONS_USED -eq 1 ]; then
l_echo ""
l_echo "     WARNING: Unknown options have been supplied to the configure script."
l_echo "              Please review the messages above and fix the supplied options."
fi
l_echo ""
l_echo "Configuration done. You may now run make."
l_echo "For more information about available build targets, run 'make help'."
l_echo ""

# vim: ft=sh
