#!/bin/sh
set -eu

prefix=/usr/local
cc=${CC:-cc}
make_cmd=${MAKE:-make}
install_cmd=${INSTALL:-install}
ar=${AR:-ar}
version=$(sed -n 's/^#define ENSIFER_VERSION "\([^"]*\)"/\1/p' inc/enfilade.h)
if [ -z "$version" ]; then
    echo "configure: ENSIFER_VERSION missing from inc/enfilade.h" >&2
    exit 1
fi

usage() {
    cat <<'EOF'
Usage: ./configure [OPTIONS]

Options:
  --prefix=PATH   installation prefix (default: /usr/local)
  --prefix PATH
  -V, --version   print configure version
  -h, --help      show this help

Environment overrides: CC, CFLAGS, CPPFLAGS, LDFLAGS, AR, INSTALL, MAKE
EOF
}

while [ "$#" -gt 0 ]; do
    case $1 in
        --prefix=*) prefix=${1#*=} ;;
        --prefix)
            shift
            [ "$#" -gt 0 ] || { echo "configure: --prefix requires a path" >&2; exit 2; }
            prefix=$1
            ;;
        -V|--version) printf 'configure version %s\n' "$version"; exit 0 ;;
        -h|--help) usage; exit 0 ;;
        *) echo "configure: unknown option: $1" >&2; usage >&2; exit 2 ;;
    esac
    shift
done

case $prefix in
    "") echo "configure: installation prefix cannot be empty" >&2; exit 2 ;;
    *' '*) echo "configure: paths containing spaces are not supported by make" >&2; exit 2 ;;
esac

command -v "$cc" >/dev/null 2>&1 || { echo "configure: C compiler not found: $cc" >&2; exit 1; }
command -v "$make_cmd" >/dev/null 2>&1 || { echo "configure: make not found: $make_cmd" >&2; exit 1; }
command -v "$install_cmd" >/dev/null 2>&1 || { echo "configure: install not found: $install_cmd" >&2; exit 1; }
command -v "$ar" >/dev/null 2>&1 || { echo "configure: archiver not found: $ar" >&2; exit 1; }

tmp=${TMPDIR:-/tmp}/ensifer-configure-$$
mkdir "$tmp" || exit 1
trap 'rm -rf "$tmp"' EXIT HUP INT TERM
cat >"$tmp/conftest.c" <<'EOF'
int ensifer_configure_probe(void) { return 0; }
EOF
if ! "$cc" ${CPPFLAGS:-} ${CFLAGS:-} -fPIC -shared -o "$tmp/conftest.so" "$tmp/conftest.c" ${LDFLAGS:-} >"$tmp/output" 2>&1; then
    cat "$tmp/output" >&2
    echo "configure: compiler does not support building shared libraries with -fPIC -shared" >&2
    exit 1
fi

dl_flags=
cat >"$tmp/dltest.c" <<'EOF'
#include <dlfcn.h>
int main(void) { return dlopen(0, RTLD_NOW) == 0; }
EOF
if "$cc" ${CPPFLAGS:-} ${CFLAGS:-} -o "$tmp/dltest" "$tmp/dltest.c" -ldl ${LDFLAGS:-} >/dev/null 2>&1; then
    dl_flags='-DWITH_DL'
    dl_libs=-ldl
else
    dl_libs=
fi

{
    echo "# Generated by ./configure; edit configure options instead."
    printf 'VERSION := %s\n' "$version"
    printf 'CC := %s\n' "$cc"
    printf 'AR := %s\n' "$ar"
    printf 'INSTALL := %s\n' "$install_cmd"
    printf 'PREFIX := %s\n' "$prefix"
    printf 'CFLAGS ?= %s\n' "${CFLAGS:--O2 -Wall -Wextra -std=c11}"
    printf 'CPPFLAGS ?= %s\n' "${CPPFLAGS:-}"
    printf 'LDFLAGS ?= %s\n' "${LDFLAGS:-}"
    printf 'S7_CPPFLAGS := %s -DWITH_SYSTEM_EXTRAS=0\n' "$dl_flags"
    printf 'S7_LIBS := %s\n' "$dl_libs"
} > config.mk

cat > config.h <<EOF
#ifndef ENSIFER_CONFIG_H
#define ENSIFER_CONFIG_H
#define ENSIFER_VERSION "$version"
#endif
EOF

{
    echo "Ensifer configure"
    echo "version: $version"
    echo "prefix: $prefix"
    echo "compiler: $cc"
    echo "make: $make_cmd"
    echo "install: $install_cmd"
    echo "S7 dynamic loading: ${dl_flags:-disabled}"
    echo "shared library support: yes (-fPIC -shared)"
} > config.log

trap - EXIT HUP INT TERM
rm -rf "$tmp"
echo "configure: ready to build Ensifer $version"
echo "  prefix: $prefix"
echo "  run: make"
