# 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

if [ -z "$ngx_module_link" ]; then
cat << END

$0: error: Brotli module requires recent version of NGINX (1.9.11+).

END
    exit 1
fi

#
# HTTP static module
#

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=
ngx_module_order=

. auto/module

have=NGX_HTTP_GZIP . auto/have
have=NGX_HTTP_BROTLI_STATIC . auto/have
have=NGX_HTTP_BROTLI_STATIC_MODULE . auto/have  # deprecated

#
# HTTP filter module with Brotli library
#


ngx_module_type=HTTP_FILTER
ngx_module_name=ngx_http_brotli_filter_module

brotli="/usr/local"

if [ ! -f "$brotli/include/brotli/encode.h" ]; then

brotli="$ngx_addon_dir/deps/brotli/c"

if [ ! -f "$brotli/include/brotli/encode.h" ]; then
cat << END

$0: error: \
Brotli library is missing from the $brotli directory.

Please make sure that the git submodule has been checked out:

    cd $ngx_addon_dir && git submodule update --init && cd $PWD

END
    exit 1
fi

ngx_module_incs="$brotli/include"
ngx_module_deps="$brotli/common/constants.h \
                 $brotli/common/context.h \
                 $brotli/common/dictionary.h \
                 $brotli/common/platform.h \
                 $brotli/common/transform.h \
                 $brotli/common/version.h \
                 $brotli/enc/backward_references.h \
                 $brotli/enc/backward_references_hq.h \
                 $brotli/enc/backward_references_inc.h \
                 $brotli/enc/bit_cost.h \
                 $brotli/enc/bit_cost_inc.h \
                 $brotli/enc/block_encoder_inc.h \
                 $brotli/enc/block_splitter.h \
                 $brotli/enc/block_splitter_inc.h \
                 $brotli/enc/brotli_bit_stream.h \
                 $brotli/enc/cluster.h \
                 $brotli/enc/cluster_inc.h \
                 $brotli/enc/command.h \
                 $brotli/enc/compress_fragment.h \
                 $brotli/enc/compress_fragment_two_pass.h \
                 $brotli/enc/dictionary_hash.h \
                 $brotli/enc/encoder_dict.h \
                 $brotli/enc/entropy_encode.h \
                 $brotli/enc/entropy_encode_static.h \
                 $brotli/enc/fast_log.h \
                 $brotli/enc/find_match_length.h \
                 $brotli/enc/hash.h \
                 $brotli/enc/hash_forgetful_chain_inc.h \
                 $brotli/enc/hash_longest_match64_inc.h \
                 $brotli/enc/hash_longest_match_inc.h \
                 $brotli/enc/hash_longest_match_quickly_inc.h \
                 $brotli/enc/hash_to_binary_tree_inc.h \
                 $brotli/enc/histogram.h \
                 $brotli/enc/histogram_inc.h \
                 $brotli/enc/literal_cost.h \
                 $brotli/enc/memory.h \
                 $brotli/enc/metablock.h \
                 $brotli/enc/metablock_inc.h \
                 $brotli/enc/params.h \
                 $brotli/enc/prefix.h \
                 $brotli/enc/quality.h \
                 $brotli/enc/ringbuffer.h \
                 $brotli/enc/static_dict.h \
                 $brotli/enc/static_dict_lut.h \
                 $brotli/enc/utf8_util.h \
                 $brotli/enc/write_bits.h"
ngx_module_srcs="$brotli/common/dictionary.c \
                 $brotli/common/transform.c \
                 $brotli/enc/backward_references.c \
                 $brotli/enc/backward_references_hq.c \
                 $brotli/enc/bit_cost.c \
                 $brotli/enc/block_splitter.c \
                 $brotli/enc/brotli_bit_stream.c \
                 $brotli/enc/cluster.c \
                 $brotli/enc/compress_fragment.c \
                 $brotli/enc/compress_fragment_two_pass.c \
                 $brotli/enc/dictionary_hash.c \
                 $brotli/enc/encode.c \
                 $brotli/enc/encoder_dict.c \
                 $brotli/enc/entropy_encode.c \
                 $brotli/enc/histogram.c \
                 $brotli/enc/literal_cost.c \
                 $brotli/enc/memory.c \
                 $brotli/enc/metablock.c \
                 $brotli/enc/static_dict.c \
                 $brotli/enc/utf8_util.c \
                 $ngx_addon_dir/src/ngx_http_brotli_filter_module.c"
ngx_module_libs="-lm"

else # encode.h in /usr/local

ngx_module_incs="$brotli/include"
ngx_module_deps="$brotli/include/brotli/encode.h \
                 $brotli/include/brotli/port.h \
                 $brotli/include/brotli/types.h"
ngx_module_srcs="$ngx_addon_dir/src/ngx_http_brotli_filter_module.c"
ngx_module_libs="-lbrotlienc -lm"

fi # encode.h in /usr/local

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 \
                  ngx_http_copy_filter_module \
                  ngx_http_range_body_filter_module \
                  ngx_http_not_modified_filter_module \
                  ngx_http_slice_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.

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

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

CFLAGS="$CFLAGS -Wno-deprecated-declarations"

have=NGX_HTTP_BROTLI_FILTER . auto/have
have=NGX_HTTP_BROTLI_FILTER_MODULE . auto/have  # deprecated
