# -*- sh -*-

groonga_strip_switch()
{
  # skip "-I" from "-I/usr/..."
  tail -c +3
}

if [ "$GROONGA_HTTPD_IN_TREE" = yes ]; then
  groonga_cflags="-I${GROONGA_HTTPD_IN_TREE_INCLUDE_PATH}"
  groonga_cflags="${groonga_cflags} -DNGX_HTTP_GROONGA_LOG_PATH=\\\"\"${GROONGA_HTTPD_GROONGA_LOG_PATH}\"\\\""
  groonga_cflags="${groonga_cflags} -DNGX_HTTP_GROONGA_QUERY_LOG_PATH=\\\"\"${GROONGA_HTTPD_GROONGA_QUERY_LOG_PATH}\"\\\""
  groonga_libs="-L${GROONGA_HTTPD_IN_TREE_LINK_PATH} -lgroonga"
  if [ -n "${GROONGA_HTTPD_RPATH}" ]; then
    groonga_libs="$groonga_libs -Wl,-rpath -Wl,${GROONGA_HTTPD_RPATH}"
  fi

  ngx_addon_name=ngx_http_groonga_module
  HTTP_MODULES="$HTTP_MODULES ngx_http_groonga_module"
  NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_groonga_module.c"
  CFLAGS="$CFLAGS $groonga_cflags"
  CORE_LIBS="$CORE_LIBS $groonga_libs"

  return 0
fi

groonga_cflags="$(pkg-config --cflags groonga)"
groonga_feature_path="$(pkg-config --cflags-only-I groonga |
                        groonga_strip_switch)"
groonga_libs="$(pkg-config --libs groonga)"

ngx_feature="groonga"
ngx_feature_name=
ngx_feature_run=no
ngx_feature_incs="#include <groonga.h>"
ngx_feature_path="$groonga_feature_path"
ngx_feature_libs="$groonga_libs"
ngx_feature_test="grn_get_version()"
. auto/feature

if [ $ngx_found = yes ]; then
  ngx_addon_name=ngx_http_groonga_module
  HTTP_MODULES="$HTTP_MODULES ngx_http_groonga_module"
  NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_groonga_module.c"
  CFLAGS="$CFLAGS $groonga_cflags"
  CORE_LIBS="$CORE_LIBS $groonga_libs"
else
  cat << END
$0: error: the groonga module requires the groonga library.
END
  exit 1
fi
