#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.67)
AC_INIT()

# Checks for programs.
AC_PROG_CC
AC_PROG_MAKE_SET

# Checks for libraries.
# FIXME: Replace `main' with a function in `-lm':
AC_CHECK_LIB([m], [main])
AC_CHECK_LIB([crypto], [main])

# Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([string.h unistd.h],,[AC_MSG_ERROR(not found the header file for mod_mruby.)])

# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST

# Checks for library functions.
AC_FUNC_STAT
AC_CHECK_FUNCS([memset putenv strtol])

# nginx src root
NGX_SRC_ROOT=no
AC_ARG_WITH(ngx_src_root, AC_HELP_STRING([--with-ngx-src-root=DIR],
    [pathname to ngx_src_root [[ngx_src_root]]]),
    [NGX_SRC_ROOT="$with_ngx_src_root"])
if test "$NGX_SRC_ROOT" = no; then
    AC_MSG_ERROR([nginx src dir not found.])
fi
AC_SUBST(NGX_SRC_ROOT)

NGX_OPENSSL_SRC=
AC_ARG_WITH(openssl_src, AC_HELP_STRING([--with-openssl-src=DIR],
    [set path to OpenSSL library sources]),
    [NGX_OPENSSL_SRC="$with_openssl_src"])
if test "X$NGX_OPENSSL_SRC" != X ; then
    MRUBY_BUILD_DEPEND_TARGETS=openssl  
fi
AC_SUBST(NGX_OPENSSL_SRC)
AC_SUBST(MRUBY_BUILD_DEPEND_TARGETS)

BUILD_DIR=build
AC_ARG_WITH(build_dir, AC_HELP_STRING([--with-build-dir=DIR],
    [set build directory path]),
    [BUILD_DIR="$with_build_dir"])
AC_SUBST(BUILD_DIR)

AC_ARG_ENABLE(dynamic_module, AC_HELP_STRING([--enable-dynamic-module],
    [Use nginx dynamic modules]))
if test "x$enable_dynamic_module" = xyes ; then
  NGX_MAKEFILE_DEPENDS_TARGET=mrbgems_config_dynamic
  NGX_CONFIG_ADD_MODULE_OPT_NAME=--add-dynamic-module
  BUILD_DYNAMIC_MODULE=TRUE
  NGX_MRUBY_CFLAGS=-fPIC
else
  NGX_MAKEFILE_DEPENDS_TARGET=mrbgems_config
  NGX_CONFIG_ADD_MODULE_OPT_NAME=--add-module
  BUILD_DYNAMIC_MODULE=""
  NGX_MRUBY_CFLAGS=""
fi
AC_SUBST(NGX_MAKEFILE_DEPENDS_TARGET)
AC_SUBST(NGX_CONFIG_ADD_MODULE_OPT_NAME)
AC_SUBST(BUILD_DYNAMIC_MODULE)
AC_SUBST(NGX_MRUBY_CFLAGS)

if test "X$NGX_OPENSSL_SRC" != X ; then
  NGX_OPENSSL_INSTALL_DIR=$NGX_OPENSSL_SRC/.openssl
  NGX_CFLAGS=-I$NGX_OPENSSL_INSTALL_DIR/include
  if test "x$enable_dynamic_module" = xyes ; then
      NGX_LDFLAGS="-L$NGX_OPENSSL_INSTALL_DIR/lib\ -Wl,-rpath=$NGX_OPENSSL_INSTALL_DIR/lib"
  else
      NGX_LIBS="$NGX_OPENSSL_INSTALL_DIR/lib/libssl.a $NGX_OPENSSL_INSTALL_DIR/lib/libcrypto.a"
  fi
fi
AC_SUBST(NGX_OPENSSL_INSTALL_DIR)
AC_SUBST(NGX_CFLAGS)
AC_SUBST(NGX_LDFLAGS)
AC_SUBST(NGX_LIBS)

NGX_OPENSSL_OPT=
AC_ARG_WITH(openssl_opt, AC_HELP_STRING([--with-openssl-opt=OPTIONS],
    [set additional build options for OpenSSL]),
    [NGX_OPENSSL_OPT="$with_openssl_opt"])
AC_SUBST(NGX_OPENSSL_OPT)

NGX_CONFIG_OPT=
AC_ARG_WITH(ngx_config_opt, AC_HELP_STRING([--with-ngx-config-opt=OPT],
    [nginx configure option [[ngx_config_opt]]]),
    [NGX_CONFIG_OPT="$with_ngx_config_opt"])
if test "X$NGX_OPENSSL_SRC" != X ; then
    NGX_CONFIG_OPT="$NGX_CONFIG_OPT --with-openssl=$NGX_OPENSSL_SRC"
fi
if test "X$NGX_OPENSSL_OPT" != X ; then
    NGX_CONFIG_OPT="$NGX_CONFIG_OPT --with-openssl-opt=$NGX_OPENSSL_OPT"
fi
AC_SUBST(NGX_CONFIG_OPT)

# mruby root
MRUBY_ROOT=`pwd`/mruby
AC_ARG_WITH(mruby_root, AC_HELP_STRING([--with-mruby-root=DIR],
    [pathname to mruby_root [[mruby_root]]]),
    [MRUBY_ROOT="$with_mruby_root"])
AC_SUBST(MRUBY_ROOT)

# mruby include directory
MRUBY_INCDIR=""
AC_ARG_WITH(mruby_incdir, AC_HELP_STRING([--with-mruby-incdir=DIR],
    [include directory for mruby [[mruby_incdir]]]),
    [MRUBY_INCDIR="$with_mruby_incdir"])
AC_SUBST(MRUBY_INCDIR)

# mruby library directory which contains libmruby
MRUBY_LIBDIR=""
AC_ARG_WITH(mruby_libdir, AC_HELP_STRING([--with-mruby-libdir=DIR],
    [library directory to libmruby [[mruby_libdir]]]),
    [MRUBY_LIBDIR="$with_mruby_libdir"])
AC_SUBST(MRUBY_LIBDIR)

# ndk root
NDK_ROOT=`pwd`/dependence/ngx_devel_kit
AC_ARG_WITH(ndk_root, AC_HELP_STRING([--with-ndk-root=DIR],
    [pathname to ndk_root [[ndk_root]]]),
    [NDK_ROOT="$with_ndk_root"])
AC_SUBST(NDK_ROOT)

AC_CONFIG_FILES([Makefile config])
AC_OUTPUT
