#                                               -*- 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

NGX_MRUBY_LDFLAGS=""
# We don't want use AC_CANONICAL_HOST. It requires install-sh and other helper files.
# AC_CANONICAL_HOST
host_os=`uname -s | tr [[:upper:]] [[:lower:]]`
if test "X$NGX_OPENSSL_SRC" = X ; then
  if test "${host_os}" = darwin; then
    # Homebrew, pkgsrc
    for openssl_dir in /usr/local/opt/openssl /opt/pkg
    do
      if test -d ${openssl_dir}/include/openssl; then
        NGX_MRUBY_LDFLAGS="-L${openssl_dir}/lib -lcrypto"
        NGX_MRUBY_CFLAGS="${NGX_MRUBY_CFLAGS} -I${openssl_dir}/include"
        break
      fi
    done
  fi
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)
AC_SUBST(NGX_MRUBY_LDFLAGS)

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

  # * github master branch (as of Feb 3 2020)
  #   * nginx doesn't support it yet.
  #   * VERSION exist, but no include/openssl/opensslv.h and no Makefile.
  # * 1.1.x tar ball
  #   * include/openssl/opensslv.h exists, but no Makefile.
  #   * Makefile is generated at build time.
  #   * enable-tlsext is gone.
  #   * Makefile targets are build_all_generated install_sw
  # * 1.0.x tar ball
  #   * crypto/opensslv.h and Makefile exist.
  #   * include/openssl/opensslv.h is generated at build time.
  #   * enable-tlsext is supported.
  #   * Makefile targets are build_all and install_sw.
  if test -f $NGX_OPENSSL_SRC/VERSION; then
    # MAJOR=3
    # MINOR=0
    # PATCH=0
    # PRE_RELEASE_TAG=dev
    OPENSSL_VERSION=`(. $NGX_OPENSSL_SRC/VERSION && echo "${MAJOR}.${MINOR}.${PATCH}-${PRE_RELEASE_TAG}")`
  elif test -f $NGX_OPENSSL_SRC/crypto/opensslv.h; then
      #  define OPENSSL_VERSION_TEXT    "OpenSSL 1.0.2u-fips  20 Dec 2019"
      #  define OPENSSL_VERSION_TEXT    "OpenSSL 1.0.2u  20 Dec 2019"
      OPENSSL_VERSION=`grep OPENSSL_VERSION_TEXT $NGX_OPENSSL_SRC/crypto/opensslv.h | grep -o 'OpenSSL [[0-9a-z\.]]*' | head -1 | awk '{print $2}'`
  elif test -f $NGX_OPENSSL_SRC/include/openssl/opensslv.h; then
      # define OPENSSL_VERSION_TEXT    "OpenSSL 1.1.1d  10 Sep 2019"
      OPENSSL_VERSION=`grep OPENSSL_VERSION_TEXT $NGX_OPENSSL_SRC/include/openssl/opensslv.h | grep -o 'OpenSSL [[0-9a-z\.]]*' | awk '{print $2}'`
  else
    $as_echo "$0: Can't find openssl version information." >&2
    exit 255
  fi
  eval `echo $OPENSSL_VERSION | awk -F '.' '{print "OPENSSL_MAJOR_VERSION=" $1 "\nOPENSSL_MINOR_VERSION=" $2}'`
  $as_echo "openssl version is $OPENSSL_VERSION ($OPENSSL_MAJOR_VERSION $OPENSSL_MINOR_VERSION)" >&2
  if test $OPENSSL_MAJOR_VERSION -eq 1 -a $OPENSSL_MINOR_VERSION -le 0; then 
    OPENSSL_MAKE_TARGET="build_all install_sw"
    OPENSSL_CONFIG_ENABLE_TLSEXT="enable-tlsext"
  else
    OPENSSL_MAKE_TARGET="build_all_generated install_sw"
    OPENSSL_CONFIG_ENABLE_TLSEXT=""
  fi
fi

AC_SUBST(NGX_OPENSSL_INSTALL_DIR)
AC_SUBST(OPENSSL_VERSION)
AC_SUBST(OPENSSL_MAKE_TARGET)
AC_SUBST(OPENSSL_CONFIG_ENABLE_TLSEXT)
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
