#!/bin/sh

set -e
set -u

srcdir="."
builddir="."
is_mingw="no"

for argument in "$@"; do
  case "$argument" in
    --srcdir=*)
      srcdir="$(echo "${argument}" | sed -e 's/--srcdir=//')"
      ;;
    --host=*)
      if echo "${argument}" | grep -q mingw; then
        is_mingw="yes"
      fi
      ;;
  esac
done

top_srcdir="${srcdir}/../.."
top_builddir="${builddir}/../.."

abs_top_srcdir="$(cd "${top_srcdir}" && pwd)"
abs_top_builddir="$(cd "${top_builddir}" && pwd)"

nginx_version=$(cat "${top_srcdir}/nginx_version")
relative_nginx_directory="vendor/nginx-${nginx_version}"
nginx_directory="${top_srcdir}/${relative_nginx_directory}"

export GROONGA_HTTPD_IN_TREE=yes
. "${top_builddir}/groonga-httpd-conf.sh"

eval "$(${top_builddir}/libtool --config)"

found_libdir_in_search_path="no"
for search_path in ${sys_lib_search_path_spec} ${sys_lib_dlsearch_path_spec}; do
  if [ "${search_path}" = "${libdir}" ]; then
    found_libdir_in_search_path="yes"
    break
  fi
done
if [ "${found_libdir_in_search_path}" = "no" ]; then
  export GROONGA_HTTPD_RPATH="${libdir}"
fi

nginx_directory_in_build_directory="${abs_top_builddir}/${relative_nginx_directory}"
if [ "${abs_top_srcdir}/src/httpd" != "$(pwd)" ]; then
  mkdir -p "${nginx_directory_in_build_directory}"
  rm -rf "${nginx_directory_in_build_directory}"
  cp -rp \
      "${abs_top_srcdir}/${relative_nginx_directory}" \
      "${nginx_directory_in_build_directory}"
fi

configure_args=""
if [ "${is_mingw}" = "yes" ]; then
  configure_args="${configure_args} --crossbuild=win32"
  configure_args="${configure_args} --with-cc=${host}-gcc"
  configure_args="${configure_args} --with-cpp=${host}-c++"
  GROONGA_HTTPD_WITH_PCRE="no"
  GROONGA_HTTPD_WITH_ZLIB="no"
fi
if [ "${GROONGA_HTTPD_DEBUG}" = "yes" ]; then
  configure_args="${configure_args} --with-debug"
fi

cc_opt="${CFLAGS-} ${CPPFLAGS-}"
ld_opt="${LDFLAGS-}"
if [ "${GROONGA_HTTPD_WITH_PCRE}" = "yes" ]; then
  cc_opt="${cc_opt} ${GROONGA_HTTPD_PCRE_CFLAGS}"
  ld_opt="${ld_opt} ${GROONGA_HTTPD_PCRE_LIBS_ONLY_L}"
else
  configure_args="${configure_args} --without-http_rewrite_module"
fi
if [ "${GROONGA_HTTPD_WITH_ZLIB}" != "yes" ]; then
  configure_args="${configure_args} --without-http_gzip_module"
fi


cd "${nginx_directory_in_build_directory}"

set +e
auto/configure \
  --prefix="${GROONGA_HTTPD_PREFIX}" \
  --sbin-path="${GROONGA_HTTPD_BIN_PATH}" \
  --conf-path="${GROONGA_HTTPD_CONF_PATH}" \
  --error-log-path="${GROONGA_HTTPD_ERROR_LOG_PATH}" \
  --http-log-path="${GROONGA_HTTPD_HTTP_LOG_PATH}" \
  --add-module="${GROONGA_HTTPD_MODULE_PATH}" \
  --pid-path="${GROONGA_HTTPD_PID_PATH}" \
  --with-cc-opt="${cc_opt}" \
  --with-ld-opt="${ld_opt}" \
  --with-http_stub_status_module \
  --with-http_v2_module \
  ${configure_args}
exit_code=$?
set -e

if [ ${exit_code} -ne 0 ]; then
  echo "Failed to configure groonga-httpd. Details:"
  cat objs/autoconf.err
  exit ${exit_code}
fi

if [ "${GROONGA_HTTPD_DEBUG}" = "yes" ]; then
  objs_makefile_backup="objs/Makefile.with-debug-flags"
  mv objs/Makefile "${objs_makefile_backup}"
  sed \
    -e '/^CFLAGS =/ s/ -O / -O0 /' \
    -e '/^CFLAGS =/ s/ -g / -g3 /' \
    "${objs_makefile_backup}" > objs/Makefile
fi

# Consider security by using position independent code (PIE).
# See http://fedoraproject.org/wiki/Packaging:Guidelines#PIE
# This is workaround for passing PIE related linker flags.
if [ -f /etc/redhat-release ]; then
  sed -i'' -e 's/lz/lz $(RPM_LD_FLAGS)/' objs/Makefile
fi
