ngx_addon_name=ngx_http_unzstd_filter_module
ngx_module_type=HTTP_FILTER
ngx_module_name=ngx_http_unzstd_filter_module
ngx_module_srcs="$ngx_addon_dir/ngx_http_unzstd_filter_module.c"
ngx_module_order="$ngx_module_name \
                  ngx_http_unbrotli_filter_module \
                  ngx_http_gunzip_filter_module"

ngx_found=no

if [ -n "$ZSTD_INC" ] && [ -n "$ZSTD_LIB" ]; then

    # Use user-specified paths; prefer shared library over static

    ngx_feature="zstd library in \$ZSTD_LIB"
    ngx_feature_name=
    ngx_feature_run=no
    ngx_feature_incs="#define ZSTD_STATIC_LINKING_ONLY
#include <zstd.h>"
    ngx_feature_path="$ZSTD_INC"
    ngx_feature_test="ZSTD_customMem cmem = { NULL, NULL, NULL };
#if ZSTD_VERSION_NUMBER < 10400
#error libzstd 1.4.0 or newer is required
#endif
ZSTD_DStream *dstream = ZSTD_createDStream_advanced(cmem);
ZSTD_DDict *ddict = ZSTD_createDDict_byReference(NULL, 0);
(void) dstream;
(void) ddict;"

    if [ -f "$ZSTD_LIB/libzstd.so" ] || [ -f "$ZSTD_LIB/libzstd.dylib" ]; then
        ngx_feature_libs="-L$ZSTD_LIB -lzstd"
    else
        ngx_feature_libs="$ZSTD_LIB/libzstd.a"
    fi

    . auto/feature

    if [ $ngx_found = no ]; then
        echo
        echo "$0: error: ngx_http_unzstd_filter_module: zstd not found in ZSTD_INC=$ZSTD_INC and ZSTD_LIB=$ZSTD_LIB"
        echo
        exit 1
    fi

else

    # Try system default paths

    ngx_feature="zstd library"
    ngx_feature_name=
    ngx_feature_run=no
    ngx_feature_incs="#define ZSTD_STATIC_LINKING_ONLY
#include <zstd.h>"
    ngx_feature_path=
    ngx_feature_libs="-lzstd"
    ngx_feature_test="ZSTD_customMem cmem = { NULL, NULL, NULL };
#if ZSTD_VERSION_NUMBER < 10400
#error libzstd 1.4.0 or newer is required
#endif
ZSTD_DStream *dstream = ZSTD_createDStream_advanced(cmem);
ZSTD_DDict *ddict = ZSTD_createDDict_byReference(NULL, 0);
(void) dstream;
(void) ddict;"
    . auto/feature

    if [ $ngx_found = no ]; then

        # Try common non-default installation paths

        for ngx_zstd_prefix in /usr/local /opt/local /opt/homebrew; do
            ngx_feature="zstd library in ${ngx_zstd_prefix}"
            ngx_feature_path="${ngx_zstd_prefix}/include"

            if [ -f "${ngx_zstd_prefix}/lib/libzstd.so" ] || [ -f "${ngx_zstd_prefix}/lib/libzstd.dylib" ]; then
                ngx_feature_libs="-L${ngx_zstd_prefix}/lib -lzstd"
            else
                ngx_feature_libs="${ngx_zstd_prefix}/lib/libzstd.a"
            fi

            . auto/feature

            if [ $ngx_found = yes ]; then
                break
            fi
        done

    fi

    if [ $ngx_found = no ]; then
        echo
        echo $0: error: ngx_http_unzstd_filter_module requires the zstd library.
        echo
        exit 1
    fi

fi

ngx_module_incs="$ngx_feature_path"
ngx_module_libs="$ngx_feature_libs"

. auto/module

if [ "$ngx_module_link" != DYNAMIC ]; then
    # ngx_module_order doesn't work with static modules,
    # so we must re-order here.
    if echo $HTTP_FILTER_MODULES | grep ngx_http_unbrotli_filter_module >/dev/null; then
        next=ngx_http_unbrotli_filter_module
    elif echo $HTTP_FILTER_MODULES | grep ngx_http_gunzip_filter_module >/dev/null; then
        next=ngx_http_gunzip_filter_module
    elif echo $HTTP_FILTER_MODULES | grep ngx_http_sub_filter_module >/dev/null; then
        next=ngx_http_sub_filter_module
    else
        next=ngx_http_charset_filter_module
    fi

    HTTP_FILTER_MODULES=`echo $HTTP_FILTER_MODULES \
                         | sed "s/$ngx_module_name//" \
                         | sed "s/$next/$next $ngx_module_name/"`
fi
