# 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\"
"
pagespeed_include="$mod_pagespeed_dir
                   $mod_pagespeed_dir/third_party/chromium/src/
                   $mod_pagespeed_dir/third_party/protobuf/src/
                   $mod_pagespeed_dir/out/Release/obj/gen/"
ngx_feature_path="$pagespeed_include"
pagespeed_automatic_dir="$mod_pagespeed_dir/net/instaweb/automatic"
pagespeed_libs="-lstdc++ $pagespeed_automatic_dir/pagespeed_automatic.a -lrt"
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_SRCS="$NGX_ADDON_SRCS $ps_src/ngx_pagespeed.cc"
  NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ps_src/ngx_rewrite_driver_factory.cc"
  HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES $ngx_addon_name"
  CORE_LIBS="$CORE_LIBS $pagespeed_libs"
  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
