# Copyright (C) 2015-2016 Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

ngx_addon_name=ngx_brotli

#
# static module (no dependencies)
#

if [ -n "$ngx_module_link" ]; then
    # nginx-1.9.11+
    ngx_module_type=HTTP
    ngx_module_name=ngx_http_brotli_static_module
    ngx_module_incs=
    ngx_module_deps=
    ngx_module_srcs="$ngx_addon_dir/src/ngx_http_brotli_static_module.c"
    ngx_module_libs=

    . auto/module
else
    HTTP_MODULES="$HTTP_MODULES \
                  ngx_http_brotli_static_module"

    NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
                    $ngx_addon_dir/src/ngx_http_brotli_static_module.c"
fi

have=NGX_HTTP_GZIP . auto/have
have=NGX_HTTP_BROTLI_STATIC_MODULE . auto/have

if [ -n "$NGX_BROTLI_STATIC_MODULE_ONLY" ]; then
    return
fi

#
# filter module (depends on Brotli library)
#

# <brotli/encode.h>

ngx_feature_name=
ngx_feature_run=no
ngx_feature_incs="#include <brotli/encode.h>"
ngx_feature_test="BrotliEncoderCreateInstance(NULL, NULL, NULL)"

# auto-discovery
ngx_feature="Brotli library"
ngx_feature_path=
ngx_feature_libs="-lbrotlienc -lm"
. auto/feature

if [ $ngx_found = no ]; then
    # FreeBSD, OpenBSD
    ngx_feature="Brotli library in /usr/local/"
    ngx_feature_path="/usr/local/include"
    if [ $NGX_RPATH = YES ]; then
        ngx_feature_libs="-R/usr/local/lib -L/usr/local/lib -lbrotlienc -lm"
    else
        ngx_feature_libs="-L/usr/local/lib -lbrotlienc -lm"
    fi
    . auto/feature
fi

if [ $ngx_found = no ]; then
    # NetBSD
    ngx_feature="Brotli library in /usr/pkg/"
    ngx_feature_path="/usr/pkg/include"
    if [ $NGX_RPATH = YES ]; then
        ngx_feature_libs="-R/usr/pkg/lib -L/usr/pkg/lib -lbrotlienc -lm"
    else
        ngx_feature_libs="-L/usr/pkg/lib -lbrotlienc -lm"
    fi
    . auto/feature
fi

if [ $ngx_found = no ]; then
    # MacPorts
    ngx_feature="Brotli library in /opt/local/"
    ngx_feature_path="/opt/local/include"
    if [ $NGX_RPATH = YES ]; then
        ngx_feature_libs="-R/opt/local/lib -L/opt/local/lib -lbrotlienc -lm"
    else
        ngx_feature_libs="-L/opt/local/lib -lbrotlienc -lm"
    fi
    . auto/feature
fi

# <brotli/enc/encode.h>

if [ $ngx_found = no ]; then
    ngx_feature_name=NGX_HAVE_BROTLI_ENC_ENCODE_H
    ngx_feature_run=no
    ngx_feature_incs="#include <brotli/enc/encode.h>"
    ngx_feature_test="BrotliEncoderCreateInstance(NULL, NULL, NULL)"

    # auto-discovery
    ngx_feature="Brotli library"
    ngx_feature_path=
    ngx_feature_libs="-lbrotlienc -lm"
    . auto/feature
fi

if [ $ngx_found = no ]; then
    # FreeBSD, OpenBSD
    ngx_feature="Brotli library in /usr/local/"
    ngx_feature_path="/usr/local/include"
    if [ $NGX_RPATH = YES ]; then
        ngx_feature_libs="-R/usr/local/lib -L/usr/local/lib -lbrotlienc -lm"
    else
        ngx_feature_libs="-L/usr/local/lib -lbrotlienc -lm"
    fi
    . auto/feature
fi

if [ $ngx_found = no ]; then
    # NetBSD
    ngx_feature="Brotli library in /usr/pkg/"
    ngx_feature_path="/usr/pkg/include"
    if [ $NGX_RPATH = YES ]; then
        ngx_feature_libs="-R/usr/pkg/lib -L/usr/pkg/lib -lbrotlienc -lm"
    else
        ngx_feature_libs="-L/usr/pkg/lib -lbrotlienc -lm"
    fi
    . auto/feature
fi

if [ $ngx_found = no ]; then
    # MacPorts
    ngx_feature="Brotli library in /opt/local/"
    ngx_feature_path="/opt/local/include"
    if [ $NGX_RPATH = YES ]; then
        ngx_feature_libs="-R/opt/local/lib -L/opt/local/lib -lbrotlienc -lm"
    else
        ngx_feature_libs="-L/opt/local/lib -lbrotlienc -lm"
    fi
    . auto/feature
fi

if [ $ngx_found = no ]; then
    echo "$0: error: ngx_brotli filter module requires Brotli library."
    exit 1
fi

if [ $HTTP_GZIP = YES ]; then
    next=ngx_http_gzip_filter_module
elif echo $HTTP_FILTER_MODULES | grep ngx_pagespeed_etag_filter >/dev/null; then
    next=ngx_pagespeed_etag_filter
else
    next=ngx_http_range_header_filter_module
fi

if [ -n "$ngx_module_link" ]; then
    # nginx-1.9.11+
    ngx_module_type=HTTP_FILTER
    ngx_module_name=ngx_http_brotli_filter_module
    ngx_module_incs="$ngx_feature_path"
    ngx_module_deps=
    ngx_module_srcs="$ngx_addon_dir/src/ngx_http_brotli_filter_module.c"
    ngx_module_libs="$ngx_feature_libs"
    ngx_module_order="$ngx_module_name \
                      ngx_pagespeed \
                      ngx_http_postpone_filter_module \
                      ngx_http_ssi_filter_module \
                      ngx_http_charset_filter_module \
                      ngx_http_xslt_filter_module \
                      ngx_http_image_filter_module \
                      ngx_http_sub_filter_module \
                      ngx_http_addition_filter_module \
                      ngx_http_gunzip_filter_module \
                      ngx_http_userid_filter_module \
                      ngx_http_headers_filter_module"

    . auto/module

    if [ $ngx_module_link != DYNAMIC ]; then
        # ngx_module_order doesn't work with static modules,
        # so we must re-order filters here.
        HTTP_FILTER_MODULES=`echo $HTTP_FILTER_MODULES \
                             | sed "s/$ngx_module_name//" \
                             | sed "s/$next/$next $ngx_module_name/"`
    fi
else
    CORE_INCS="$CORE_INCS $ngx_feature_path"
    CORE_LIBS="$CORE_LIBS $ngx_feature_libs"

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

    NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
                    $ngx_addon_dir/src/ngx_http_brotli_filter_module.c"
fi

have=NGX_HTTP_BROTLI_FILTER_MODULE . auto/have
