ngx_addon_name="ngx_js_module"

NJS_OPENSSL=${NJS_OPENSSL:-YES}
NJS_LIBXSLT=${NJS_LIBXSLT:-YES}
NJS_ZLIB=${NJS_ZLIB:-YES}
NJS_QUICKJS=${NJS_QUICKJS:-YES}

NJS_DEPS="$ngx_addon_dir/ngx_js.h \
    $ngx_addon_dir/ngx_js_form.h \
    $ngx_addon_dir/ngx_js_http.h \
    $ngx_addon_dir/ngx_js_fetch.h \
    $ngx_addon_dir/ngx_js_modules.h \
    $ngx_addon_dir/ngx_js_shared_dict.h"
NJS_SRCS="$ngx_addon_dir/ngx_js.c \
    $ngx_addon_dir/ngx_js_form.c \
    $ngx_addon_dir/ngx_js_http.c \
    $ngx_addon_dir/ngx_js_fetch.c \
    $ngx_addon_dir/ngx_js_regex.c \
    $ngx_addon_dir/ngx_js_shared_dict.c"

QJS_DEPS=""
QJS_SRCS=""

NJS_OPENSSL_LIB=
NJS_XSLT_LIB=
NJS_ZLIB_LIB=
NJS_QUICKJS_LIB=
NJS_QUICKJS_INC=
NJS_QUICKJS_DEFAULT_INCS="$ngx_addon_dir/../src $ngx_addon_dir/../build"
NJS_HAVE_QUICKJS=

if [ $NJS_QUICKJS != NO ]; then

    ngx_feature="QuickJS library -lquickjs.lto"
    ngx_feature_name=NJS_HAVE_QUICKJS
    ngx_feature_run=yes
    ngx_feature_incs="#include <quickjs_compat.h>"
    ngx_feature_path="$NJS_QUICKJS_DEFAULT_INCS"
    ngx_feature_libs="-lquickjs.lto -lm -ldl -lpthread"
    ngx_feature_test="JSRuntime *rt;

                      rt = JS_NewRuntime();
                      JS_FreeRuntime(rt);
                      return 0;"
    . auto/feature

    if [ $ngx_found = no ]; then
        ngx_feature="QuickJS library -lquickjs"
        ngx_feature_libs="-lquickjs -lm -ldl -lpthread"

        . auto/feature
    fi

    if [ $ngx_found = no ]; then
        ngx_feature="QuickJS library -I/usr/include/quickjs/ -L/usr/lib/quickjs/ -lquickjs.lto"
        ngx_feature_path="$NJS_QUICKJS_DEFAULT_INCS /usr/include/quickjs/"
        ngx_feature_libs="-L/usr/lib/quickjs/ -lquickjs.lto -lm -ldl -lpthread"

        . auto/feature
    fi

    if [ $ngx_found = no ]; then
        ngx_feature="QuickJS library -I/usr/include/quickjs/ -L/usr/lib/quickjs/ -lquickjs"
        ngx_feature_libs="-L/usr/lib/quickjs/ -lquickjs -lm -ldl -lpthread"

        . auto/feature
    fi

    if [ $ngx_found = no ]; then
        if command -v pkg-config >/dev/null 2>&1 && pkg-config --exists quickjs-ng; then

            # pkg-config

            ngx_feature="QuickJS-NG library via pkg-config"

            ngx_feature_path="$NJS_QUICKJS_DEFAULT_INCS"
            ngx_feature_path="$ngx_feature_path $(pkg-config quickjs-ng --cflags-only-I | sed -e 's/-I//g')"
            ngx_feature_libs=$(pkg-config quickjs-ng --libs)

            . auto/feature
        fi
    fi

    if [ $ngx_found = no ]; then
        ngx_feature="QuickJS-NG library -lqjs"
        ngx_feature_path="$NJS_QUICKJS_DEFAULT_INCS"
        ngx_feature_libs="-lqjs -lm -ldl -lpthread"

        . auto/feature
    fi

    if [ $ngx_found = yes ]; then

        ngx_feature="QuickJS JS_GetClassID()"
        ngx_feature_name=NJS_HAVE_QUICKJS_GET_CLASS_ID
        ngx_feature_run=no
        ngx_feature_test="(void) JS_GetClassID(JS_UNDEFINED);"

        . auto/feature

        if [ $ngx_found = no ]; then
            echo
            echo $0: error: QuickJS library found, but JS_GetClassID\(\) is missing.
            echo
            exit 1;
        fi

        NJS_HAVE_QUICKJS=YES
        NJS_QUICKJS_LIB="$ngx_feature_libs"
        NJS_QUICKJS_INC=`echo "$ngx_feature_path" | sed -e "s|^$NJS_QUICKJS_DEFAULT_INCS||"`

        echo " enabled QuickJS engine"
    fi
fi

if [ $NJS_OPENSSL != NO ]; then
    NJS_OPENSSL_LIB=OPENSSL
    have=NJS_HAVE_OPENSSL . auto/have
    NJS_SRCS="$NJS_SRCS $ngx_addon_dir/../external/njs_crypto_module.c"
    NJS_SRCS="$NJS_SRCS $ngx_addon_dir/../external/njs_webcrypto_module.c"

    if [ "$NJS_HAVE_QUICKJS" = "YES" ];  then
        NJS_SRCS="$NJS_SRCS $ngx_addon_dir/../external/qjs_crypto_module.c"
        NJS_SRCS="$NJS_SRCS $ngx_addon_dir/../external/qjs_webcrypto_module.c"
    fi

    echo " enabled crypto and webcrypto modules"
fi

if [ $NJS_LIBXSLT != NO ]; then
    NJS_XSLT_LIB=LIBXSLT
    have=NJS_HAVE_XML . auto/have
    NJS_SRCS="$NJS_SRCS $ngx_addon_dir/../external/njs_xml_module.c"

    if [ "$NJS_HAVE_QUICKJS" = "YES" ];  then
        NJS_SRCS="$NJS_SRCS $ngx_addon_dir/../external/qjs_xml_module.c"
    fi

    echo " enabled xml module"
fi

if [ $NJS_ZLIB != NO ]; then
    NJS_ZLIB_LIB=ZLIB
    have=NJS_HAVE_ZLIB . auto/have
    NJS_SRCS="$NJS_SRCS $ngx_addon_dir/../external/njs_zlib_module.c"

    if [ "$NJS_HAVE_QUICKJS" = "YES" ];  then
        NJS_SRCS="$NJS_SRCS $ngx_addon_dir/../external/qjs_zlib_module.c"
    fi

    echo " enabled zlib module"
fi


NJS_ENGINE_DEP="$ngx_addon_dir/../build/libnjs.a"
NJS_ENGINE_LIB="$ngx_addon_dir/../build/libnjs.a"
if [ "$NJS_HAVE_QUICKJS" = "YES" ];  then
    NJS_ENGINE_DEP="$ngx_addon_dir/../build/libqjs.a"
    NJS_ENGINE_LIB="$ngx_addon_dir/../build/libnjs.a $ngx_addon_dir/../build/libqjs.a"
    QJS_SRCS="$QJS_SRCS $ngx_addon_dir/ngx_qjs_fetch.c"
fi

if [ $HTTP != NO ]; then
    ngx_module_type=HTTP_AUX_FILTER
    ngx_module_name="ngx_http_js_module ngx_http_js_core_module"
    ngx_module_incs="$ngx_addon_dir/../src $ngx_addon_dir/../build \
                     $NJS_QUICKJS_INC"
    ngx_module_deps="$NJS_ENGINE_DEP $NJS_DEPS $QJS_DEPS"
    ngx_module_srcs="$ngx_addon_dir/ngx_http_js_module.c $NJS_SRCS $QJS_SRCS"
    ngx_module_libs="PCRE $NJS_OPENSSL_LIB $NJS_XSLT_LIB $NJS_ZLIB_LIB \
                     $NJS_QUICKJS_LIB $NJS_ENGINE_LIB -lm"

    . auto/module

    if [ "$ngx_module_link" != DYNAMIC ]; then
        NJS_SRCS=
    fi
fi

if [ $STREAM != NO ]; then
    ngx_module_type=STREAM
    ngx_module_name="ngx_stream_js_module ngx_stream_js_core_module"
    ngx_module_incs="$ngx_addon_dir/../src $ngx_addon_dir/../build \
                     $NJS_QUICKJS_INC"
    ngx_module_deps="$NJS_ENGINE_DEP $NJS_DEPS $QJS_DEPS"
    ngx_module_srcs="$ngx_addon_dir/ngx_stream_js_module.c $NJS_SRCS $QJS_SRCS"
    ngx_module_libs="PCRE $NJS_OPENSSL_LIB $NJS_XSLT_LIB $NJS_ZLIB_LIB \
                     $NJS_QUICKJS_LIB $NJS_ENGINE_LIB -lm"

    . auto/module
fi
