#!/bin/sh
# Original small dependency probe; no Autoconf or generated client code.
set -eu
prefix=/usr/local
with_curses=yes
for arg do
    case "$arg" in
        --prefix=*) prefix=${arg#--prefix=} ;;
        --without-curses) with_curses=no ;;
        --with-curses) with_curses=yes ;;
        --help|-h)
            printf '%s\n' 'Usage: ./configure [--prefix=DIR] [--without-curses]' \
                '--without-curses builds and installs standalone 1keyirc only.' \
                'Environment: CC CPPFLAGS CFLAGS LDFLAGS CURSES_LIBS TLS_LIBS' \
                'Creates a Makefile for POSIX make. No programs are installed.'
            exit 0 ;;
        *) printf 'configure: unknown option: %s\n' "$arg" >&2; exit 2 ;;
    esac
done
CC=${CC:-cc}
CPPFLAGS=${CPPFLAGS:-}
CFLAGS=${CFLAGS:--O2}
LDFLAGS=${LDFLAGS:-}
TLS_LIBS=${TLS_LIBS:--lssl -lcrypto}
CPPFLAGS="$CPPFLAGS -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -D_XOPEN_SOURCE_EXTENDED=1"
CFLAGS="$CFLAGS -std=c11 -Wall -Wextra -Wpedantic"
for value in "$prefix" "$CC" "$CPPFLAGS" "$CFLAGS" "$LDFLAGS" "${CURSES_LIBS:-}" "$TLS_LIBS"; do
    case "$value" in
        *'
'*) printf '%s\n' 'configure: newlines are not supported in build variables' >&2; exit 2 ;;
    esac
done
work=$(mktemp -d "${TMPDIR:-/tmp}/vipirc-config.XXXXXXXX")
trap 'rm -rf "$work"' EXIT HUP INT TERM
: >config.log
cat >"$work/check.c" <<'EOF'
#include <getopt.h>
#include <poll.h>
#include <termios.h>
#include <wchar.h>
int main(int argc, char **argv) {
    struct termios t;
    static const struct option options[] = {{0,0,0,0}};
    (void)getopt_long(argc, argv, "", options, 0);
    (void)tcgetattr(0, &t); (void)wcwidth(L'a');
    return poll(0, 0, 0);
}
EOF
if ! $CC $CPPFLAGS $CFLAGS $LDFLAGS "$work/check.c" -o "$work/check" >>config.log 2>&1; then
    printf '%s\n' 'configure: a C11 compiler, getopt_long, POSIX terminals and poll are required.' >&2
    exit 1
fi
curses_target=
check_target=check-idle
install_target=install-onekey
uninstall_target=uninstall-onekey
if test "$with_curses" = yes; then
cat >"$work/check.c" <<'EOF'
#include <curses.h>
#include <getopt.h>
#include <wchar.h>
int main(int argc, char **argv) {
    wint_t key = 0;
    static const struct option options[] = {{0,0,0,0}};
    (void)getopt_long(argc, argv, "", options, 0);
    initscr(); (void)get_wch(&key); (void)addnwstr(L"ok", 2); endwin();
    return 0;
}
EOF
found=no
for candidate in "${CURSES_LIBS:-}" '-lncursesw' '-lcursesw' '-lncurses' '-lcurses'; do
    test -n "$candidate" || continue
    if $CC $CPPFLAGS $CFLAGS $LDFLAGS "$work/check.c" -o "$work/check" $candidate >>config.log 2>&1; then
        CURSES_LIBS=$candidate; found=yes; break
    fi
    test -z "${CURSES_LIBS:-}" || break
done
if test "$found" != yes; then
    printf '%s\n' 'configure: a C11 compiler, getopt_long, and wide-character curses headers/library are required.' \
        'Install your curses development package or set CPPFLAGS/CURSES_LIBS; see config.log.' >&2
    exit 1
fi
    curses_target=vipirc
    check_target=check-full
    install_target=install-full
    uninstall_target=uninstall-full
else
    CURSES_LIBS=
fi

cat >"$work/check.c" <<'EOF'
#include <openssl/ssl.h>
#include <openssl/x509v3.h>
int main(void) {
    SSL_CTX *ctx = SSL_CTX_new(TLS_client_method());
    SSL *ssl = SSL_new(ctx);
    SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
    (void)SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
    (void)X509_VERIFY_PARAM_set1_host(SSL_get0_param(ssl), "example.invalid", 0);
    SSL_free(ssl); SSL_CTX_free(ctx);
    return 0;
}
EOF
if ! $CC $CPPFLAGS $CFLAGS $LDFLAGS "$work/check.c" -o "$work/check" $TLS_LIBS >>config.log 2>&1; then
    printf '%s\n' 'configure: OpenSSL-compatible TLS headers/library with hostname verification are required.' \
        'Install OpenSSL/LibreSSL development files or set CPPFLAGS/TLS_LIBS; see config.log.' >&2
    exit 1
fi
escape() { printf '%s' "$1" | sed 's/[\\&|]/\\&/g'; }
sed -e "s|@PREFIX@|$(escape "$prefix")|g" \
    -e "s|@CC@|$(escape "$CC")|g" \
    -e "s|@CPPFLAGS@|$(escape "$CPPFLAGS")|g" \
    -e "s|@CFLAGS@|$(escape "$CFLAGS")|g" \
    -e "s|@LDFLAGS@|$(escape "$LDFLAGS")|g" \
    -e "s|@CURSES_LIBS@|$(escape "$CURSES_LIBS")|g" \
    -e "s|@TLS_LIBS@|$(escape "$TLS_LIBS")|g" \
    -e "s|@CURSES_TARGET@|$curses_target|g" \
    -e "s|@CHECK_TARGET@|$check_target|g" \
    -e "s|@INSTALL_TARGET@|$install_target|g" \
    -e "s|@UNINSTALL_TARGET@|$uninstall_target|g" Makefile.in >"$work/Makefile"
cp "$work/Makefile" Makefile
printf 'Configured 1keyirc/vipirc: CC=%s, curses=%s, TLS=%s\n' "$CC" "$CURSES_LIBS" "$TLS_LIBS"
printf '%s\n' 'Run make, then make check. Use make install PREFIX=... DESTDIR=... for installation.'
