# Copyright 2012 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

mod_pagespeed_dir="$ngx_addon_dir/../mod_pagespeed/src"
ngx_feature="psol"
ngx_feature_name=""
ngx_feature_run=no
ngx_feature_incs="
#include \"net/instaweb/htmlparse/public/html_parse.h\"
#include \"net/instaweb/htmlparse/public/html_writer_filter.h\"
#include \"net/instaweb/util/public/string.h\"
#include \"net/instaweb/util/public/string_writer.h\"
#include \"net/instaweb/util/public/null_message_handler.h\"
"

os_name='unknown_os'
arch_name='unknown_arch'
uname_os=`uname`
uname_arch=`uname -m`

if [ $uname_os = 'Linux' ]; then
   os_name='linux'
elif [ $uname_os = 'Darwin' ]; then
   os_name='mac'
else
   echo "OS not supported: $uname_os"
   exit 1
fi

if [ $uname_arch = 'x86_64' ]; then
   arch_name='x64'
elif [ $uname_arch = 'x86_32' ]; then
   arch_name='ia32'
else
   echo "Architecture not supported: $uname_arch"
   exit 1
fi

if [ "$NGX_DEBUG" = "YES" ]; then
    buildtype=Debug
else
    buildtype=Release
fi

pagespeed_include="$mod_pagespeed_dir
                   $mod_pagespeed_dir/third_party/chromium/src/
                   $mod_pagespeed_dir/third_party/protobuf/src/
                   $mod_pagespeed_dir/out/$buildtype/obj/gen/
                   $mod_pagespeed_dir/third_party/apr/src/include/
                   $mod_pagespeed_dir/third_party/apr/gen/arch/$os_name/$arch_name/include/
                   $mod_pagespeed_dir/third_party/aprutil/src/include/
                   $mod_pagespeed_dir/third_party/aprutil/gen/arch/$os_name/$arch_name/include/"
ngx_feature_path="$pagespeed_include"
pagespeed_automatic_dir="$mod_pagespeed_dir/net/instaweb/automatic"
pagespeed_libs="-lstdc++ $pagespeed_automatic_dir/pagespeed_automatic.a -lrt -pthread"
ngx_feature_libs="$pagespeed_libs"
ngx_feature_test="
    GoogleString output_buffer;
    net_instaweb::StringWriter write_to_string(&output_buffer);

    net_instaweb::NullMessageHandler handler;
    net_instaweb::HtmlParse html_parse(&handler);
    net_instaweb::HtmlWriterFilter html_writer_filter(&html_parse);

    html_writer_filter.set_writer(&write_to_string);
    html_parse.AddFilter(&html_writer_filter);

    html_parse.StartParse(\"http:example.com\");
    html_parse.ParseText(
        \"<html ><body ><h1 >Test</h1 ><p>Test Text</p></body></html>\n\");
    html_parse.FinishParse();

    printf(\"parsed as: %s\", output_buffer.c_str())"

# Test whether we have pagespeed and can compile and link against it.
. "$ngx_addon_dir/cpp_feature"

if [ $ngx_found = yes ]; then
  ps_src="$ngx_addon_dir/src"
  ngx_addon_name=ngx_pagespeed
  NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ps_src/ngx_cache.h"
  NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ps_src/ngx_pagespeed.h"
  NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ps_src/ngx_base_fetch.h"
  NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ps_src/ngx_server_context.h"
  NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ps_src/ngx_rewrite_options.h"
  NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ps_src/ngx_rewrite_driver_factory.h"
  NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ps_src/../../mod_pagespeed/src/net/instaweb/apache/apr_thread_compatible_pool.cc"
  NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ps_src/../../mod_pagespeed/src/net/instaweb/apache/serf_url_async_fetcher.cc"
  NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ps_src/../../mod_pagespeed/src/net/instaweb/apache/apr_mem_cache.cc"
  NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ps_src/../../mod_pagespeed/src/net/instaweb/util/key_value_codec.cc"
  NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ps_src/../../mod_pagespeed/src/third_party/aprutil/apr_memcache2.c"
  NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ps_src/ngx_pagespeed.cc"
  NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ps_src/ngx_rewrite_driver_factory.cc"
  NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ps_src/ngx_rewrite_options.cc"
  NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ps_src/ngx_server_context.cc"
  NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ps_src/ngx_base_fetch.cc"
  NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ps_src/ngx_cache.cc"
  HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES $ngx_addon_name"
  CORE_LIBS="$CORE_LIBS 
             $pagespeed_libs
             $mod_pagespeed_dir/out/$buildtype/obj.target/third_party/serf/libserf.a
             $mod_pagespeed_dir/out/$buildtype/obj.target/third_party/aprutil/libaprutil.a
             $mod_pagespeed_dir/out/$buildtype/obj.target/third_party/apr/libapr.a"
  CORE_INCS="$CORE_INCS $pagespeed_include"
else
  cat << END
$0: error: module ngx_pagespeed requires the pagespeed optimization library
END
  exit 1
fi

have=NGX_PAGESPEED . auto/have
