have=NGX_HTTP_GZIP . auto/have

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_userid_filter_module \
                  ngx_http_headers_filter_module \
                  ngx_http_copy_filter_module \
                  ngx_http_range_body_filter_module \
                  ngx_http_not_modified_filter_module \
                  ngx_http_slice_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="#include <zstd.h>"
    ngx_feature_path="$ZSTD_INC"
    ngx_feature_test="
#if ZSTD_VERSION_NUMBER < 10400
#error libzstd 1.4.0 or newer is required
#endif
ZSTD_DStream *dstream = ZSTD_createDCtx();
ZSTD_DDict *ddict = ZSTD_createDDict(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="#include <zstd.h>"
    ngx_feature_path=
    ngx_feature_libs="-lzstd"
    ngx_feature_test="
#if ZSTD_VERSION_NUMBER < 10400
#error libzstd 1.4.0 or newer is required
#endif
ZSTD_DStream *dstream = ZSTD_createDCtx();
ZSTD_DDict *ddict = ZSTD_createDDict(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.
    for next in ngx_http_gunzip_filter_module \
                ngx_http_addition_filter_module ngx_http_sub_filter_module \
                ngx_http_image_filter_module ngx_http_xslt_filter_module \
                ngx_http_charset_filter_module ngx_http_ssi_filter_module \
                ngx_http_postpone_filter_module
    do
        case " $HTTP_FILTER_MODULES " in
            *" $next "*) break ;;
        esac
    done

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