# NGINX module configuration file
# This file is used by NGINX's ./configure script to build the module

ngx_addon_name=ngx_http_markdown_filter_module

# Resolve Rust converter static library path. Prefer an explicit or inferred
# target triple so we do not accidentally link a cached artifact for the wrong
# libc/architecture (for example musl builds picking a GNU archive).
ngx_markdown_rust_lib=

if [ -n "$RUST_TARGET" ]; then
    ngx_markdown_rust_lib="$ngx_addon_dir/../rust-converter/target/$RUST_TARGET/release/libnginx_markdown_converter.a"
fi

if [ -z "$ngx_markdown_rust_lib" ] || [ ! -f "$ngx_markdown_rust_lib" ]; then
    ngx_markdown_uname_s=$(uname -s 2>/dev/null || echo unknown)
    ngx_markdown_uname_m=$(uname -m 2>/dev/null || echo unknown)
    ngx_markdown_libc=gnu

    if [ "$ngx_markdown_uname_s" = "Linux" ]; then
        if command -v ldd >/dev/null 2>&1; then
            if ldd --version 2>&1 | grep -qi musl || ldd /bin/sh 2>&1 | grep -qi musl; then
                ngx_markdown_libc=musl
            fi
        fi

        case "$ngx_markdown_uname_m:$ngx_markdown_libc" in
            aarch64:musl)
                ngx_markdown_target=aarch64-unknown-linux-musl
                ;;
            aarch64:gnu)
                ngx_markdown_target=aarch64-unknown-linux-gnu
                ;;
            x86_64:musl)
                ngx_markdown_target=x86_64-unknown-linux-musl
                ;;
            x86_64:gnu)
                ngx_markdown_target=x86_64-unknown-linux-gnu
                ;;
        esac
    elif [ "$ngx_markdown_uname_s" = "Darwin" ]; then
        case "$ngx_markdown_uname_m" in
            arm64)
                ngx_markdown_target=aarch64-apple-darwin
                ;;
            x86_64)
                ngx_markdown_target=x86_64-apple-darwin
                ;;
        esac
    fi

    if [ -n "$ngx_markdown_target" ]; then
        ngx_markdown_rust_lib="$ngx_addon_dir/../rust-converter/target/$ngx_markdown_target/release/libnginx_markdown_converter.a"
    else
        ngx_markdown_rust_lib="$ngx_addon_dir/../rust-converter/target/release/libnginx_markdown_converter.a"
    fi
fi

if [ ! -f "$ngx_markdown_rust_lib" ]; then
    for candidate in "$ngx_addon_dir"/../rust-converter/target/*/release/libnginx_markdown_converter.a; do
        if [ -f "$candidate" ]; then
            ngx_markdown_rust_lib="$candidate"
            break
        fi
    done
fi

# Libraries required by the Rust FFI converter.
# Use ngx_module_libs so dynamic-module builds link them into the module .so.
if [ "$(uname -s 2>/dev/null)" = "Darwin" ]; then
    ngx_module_libs="$ngx_markdown_rust_lib -lpthread -lm"
else
    ngx_module_libs="$ngx_markdown_rust_lib -lpthread -ldl -lm"
fi

# Detect arc4random_buf() for cryptographically secure random bytes.
# Available on BSD/macOS natively and Linux glibc >= 2.36.
# When available, prefer it over /dev/urandom (no fd management).
if echo "#include <stdlib.h>" | ${CC:-cc} -x c - -E >/dev/null 2>&1; then
    if echo '#include <stdlib.h>
int main(void) { unsigned char buf[1]; arc4random_buf(buf, 1); return 0; }' \
       | ${CC:-cc} -x c - -o /dev/null >/dev/null 2>&1; then
        CFLAGS="$CFLAGS -DNGX_HAVE_ARC4RANDOM=1"
    fi
fi

# Detect whether the Rust library was built with the `incremental` feature.
# If the symbol is present, define MARKDOWN_INCREMENTAL_ENABLED so the C
# header exposes the incremental API declarations.
if nm "$ngx_markdown_rust_lib" 2>/dev/null | grep -q 'markdown_incremental_new'; then
    CFLAGS="$CFLAGS -DMARKDOWN_INCREMENTAL_ENABLED"
fi

# Detect whether the Rust library was built with the `streaming` feature.
# If the symbol is present, define MARKDOWN_STREAMING_ENABLED so the C
# header exposes the streaming API declarations.
if nm "$ngx_markdown_rust_lib" 2>/dev/null | grep -q 'markdown_streaming_new'; then
    CFLAGS="$CFLAGS -DMARKDOWN_STREAMING_ENABLED"
fi

# Check if module is configured as dynamic
if test -n "$ngx_module_link"; then
    # Dynamic module
    ngx_module_type=HTTP_AUX_FILTER
    ngx_module_name=$ngx_addon_name
    ngx_module_srcs="$ngx_addon_dir/src/ngx_http_markdown_filter_module.c \
                     $ngx_addon_dir/src/ngx_http_markdown_accept.c \
                     $ngx_addon_dir/src/ngx_http_markdown_auth.c \
                     $ngx_addon_dir/src/ngx_http_markdown_buffer.c \
                     $ngx_addon_dir/src/ngx_http_markdown_eligibility.c \
                     $ngx_addon_dir/src/ngx_http_markdown_error.c \
                     $ngx_addon_dir/src/ngx_http_markdown_headers.c \
                     $ngx_addon_dir/src/ngx_http_markdown_header_plan.c \
                     $ngx_addon_dir/src/ngx_http_markdown_conditional.c \
                     $ngx_addon_dir/src/ngx_http_markdown_decompression.c \
                     $ngx_addon_dir/src/ngx_http_markdown_reason.c \
                     $ngx_addon_dir/src/ngx_http_markdown_reason_ffi.c \
                     $ngx_addon_dir/src/ngx_http_markdown_diagnostics.c \
                     $ngx_addon_dir/src/ngx_http_markdown_dynconf_snapshot.c"
    ngx_module_deps="$ngx_addon_dir/src/ngx_http_markdown_filter_module.h \
                     $ngx_addon_dir/src/ngx_http_markdown_header_plan.h \
                     $ngx_addon_dir/src/ngx_http_markdown_diagnostics.h \
                     $ngx_addon_dir/src/ngx_http_markdown_dynconf_snapshot.h"
    
    . auto/module
else
    # Static module
    HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES $ngx_addon_name"
    NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
                    $ngx_addon_dir/src/ngx_http_markdown_filter_module.c \
                    $ngx_addon_dir/src/ngx_http_markdown_accept.c \
                    $ngx_addon_dir/src/ngx_http_markdown_auth.c \
                    $ngx_addon_dir/src/ngx_http_markdown_buffer.c \
                    $ngx_addon_dir/src/ngx_http_markdown_eligibility.c \
                    $ngx_addon_dir/src/ngx_http_markdown_error.c \
                    $ngx_addon_dir/src/ngx_http_markdown_headers.c \
                    $ngx_addon_dir/src/ngx_http_markdown_header_plan.c \
                    $ngx_addon_dir/src/ngx_http_markdown_conditional.c \
                    $ngx_addon_dir/src/ngx_http_markdown_decompression.c \
                    $ngx_addon_dir/src/ngx_http_markdown_reason.c \
                    $ngx_addon_dir/src/ngx_http_markdown_reason_ffi.c \
                    $ngx_addon_dir/src/ngx_http_markdown_diagnostics.c \
                    $ngx_addon_dir/src/ngx_http_markdown_dynconf_snapshot.c"
    NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/src/ngx_http_markdown_filter_module.h \
                    $ngx_addon_dir/src/ngx_http_markdown_header_plan.h"
fi

# Note: ngx_module_libs is consumed by NGINX's auto/module logic for both
# dynamic-module and static-module builds. Avoid writing directly to CORE_LIBS
# here, or dynamic-module builds will miss the Rust FFI symbols at runtime.
