 # Copyright 2004 Apache Software Foundation
 #
 #  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.
 #
 # Originally developed by Gregory Trubetskoy.
 #

dnl Process this file with autoconf to produce a configure script.
AC_INIT(src/mod_python.c)

# includes
INCLUDES="-I`pwd`/src/include"

dnl Checks for programs.
AC_PROG_CC
AC_SUBST(AR)
AC_CHECK_PROGS(AR, ar aal, ar)
AC_PROG_INSTALL
AC_PROG_MAKE_SET

dnl Replace `main' with a function in -lm:
AC_CHECK_LIB(m, main)

dnl Checks for header files.

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

### humor lowers blood pressure
AC_MSG_CHECKING(your blood pressure)
AC_MSG_RESULT([a bit high, but we can proceed])

## The goal is to find apxs
AC_CHECKING(whether apxs is available)
AC_SUBST(APXS)
AC_SUBST(DSO)
AC_SUBST(ALL)

# check for --with-apxs
AC_MSG_CHECKING(for --with-apxs)
AC_ARG_WITH(apxs, [--with-apxs=PATH 	Path to apxs],
[
  if test -x "$withval"
  then
    AC_MSG_RESULT([$withval executable, good])
    APXS=$withval
  else
    echo
    AC_MSG_ERROR([$withval not found or not executable])
  fi
],
AC_MSG_RESULT(no))

# if no apxs found yet, check /usr/local/apache/sbin
# since it's the default Apache location
if test -z "$APXS"; then
  AC_MSG_CHECKING(for apxs in /usr/local/apache/sbin)
  if test -x /usr/local/apache/sbin/apxs; then
    APXS=/usr/local/apache/sbin/apxs
    AC_MSG_RESULT([found, we'll use this. Use --with-apxs to specify another.])
  else
    AC_MSG_RESULT(no)
  fi
fi

# second last resort
if test -z "$APXS"; then
  AC_MSG_CHECKING(for apxs in your PATH)
  AC_PATH_PROG(APXS, apxs)
  if test -n "$APXS"; then
    AC_MSG_RESULT([found $APXS, we'll use this. Use --with-apxs to specify another.])
  fi
fi  

# last resort
# some linux distributions use apxs2 for apache2 installations,
# so check for apxs2 before we give up.
if test -z "$APXS"; then
  AC_MSG_CHECKING(for apxs2 in your PATH)
  AC_PATH_PROG(APXS, apxs2)
  if test -n "$APXS"; then
    AC_MSG_RESULT([found $APXS, we'll use this. Use --with-apxs to specify another.])
  fi
fi  

# if apxs was still not found, then no DSO
AC_SUBST(LIBEXECDIR)
AC_SUBST(SOLARIS_HACKS)
AC_SUBST(HTTPD)
if test -z "$APXS"; then
  AC_MSG_WARN([**** apxs was not found, DSO compilation will not be available.])
  AC_MSG_WARN([**** You can use --with-apxs to specify where your apxs is.])
  DSO="no_dso"
  ALL="static"
else
  DSO="do_dso"
  ALL="dso"

  # check Apache version
  AC_MSG_CHECKING(Apache version)
  HTTPD="`${APXS} -q SBINDIR`/`${APXS} -q TARGET`"
  ver=`$HTTPD -v | awk '/version/ {print $3}' | awk -F/ '{print $2}'`
  AC_MSG_RESULT($ver)

  # make sure version begins with 2
  if test -z "`echo $ver | egrep \^2`"; then
    AC_MSG_ERROR([This version of mod_python only works with Apache 2. The one you have seems to be $ver.])
  fi

  # determine LIBEXEC
  AC_MSG_CHECKING(for Apache libexec directory)
  LIBEXECDIR=`${APXS} -q LIBEXECDIR`
  AC_MSG_RESULT($LIBEXECDIR)

  # determine INCLUDES
  AC_MSG_CHECKING([for Apache include directory])
  AP_INCLUDES="-I`${APXS} -q INCLUDEDIR`"
  AC_MSG_RESULT($AP_INCLUDES)

  dnl Small hack to work around _eprintf.o problem on Solaris
  if test "`uname`" = "SunOS"; then
    AC_MSG_CHECKING([for gcc on Solaris possible missing _eprintf problem])
    if test "$CC" = "gcc"; then
      SOLARIS_HACKS="_eprintf.o _floatdidf.o _muldi3.o"
    fi
    AC_MSG_RESULT("done")
  fi

fi

# check for --with-apache
AC_SUBST(AP_SRC)
AC_SUBST(AP_SRC_OWN)
AC_SUBST(AP_SRC_GRP)
## static is disabled, thus no --with-apache
##AC_MSG_CHECKING(for --with-apache)
AC_ARG_WITH(apache, [--with-apache=DIR 	Path to Apache sources],
[

  # temporarily disable static on 2.0 until I figure out how to
  # do it right
  AC_MSG_ERROR([Sorry, --with-apache (static compilation) is not supported at this time!])

  AP_SRC=`cd $withval; pwd`

  dnl  Make sure this looks like Apache source
  if test ! -f "$AP_SRC/include/httpd.h"; then
    AC_MSG_ERROR([$withval does not look like an Apache 2.0 source directory.])
  fi

  AC_MSG_RESULT($AP_SRC)
  AP_INCLUDES="-I${AP_SRC}/src/include -I${AP_SRC}/src/os/unix"

  # note who owns the apache source directory
  AP_SRC_OWN="`ls -ld $AP_SRC | awk '{print $3}'`"
  AP_SRC_GRP="`ls -ld $AP_SRC | awk '{print $4}'`"
],)
##AC_MSG_RESULT(no))

AC_SUBST(STATIC)
if test -z "$AP_SRC"; then
##  AC_MSG_WARN([**** No apache sources specified, static compilation will not be available.])
##  AC_MSG_WARN([**** You can use --with-apache to specify where your Apache sources are.])
  STATIC="no_static"
else
  STATIC="do_static"
fi

if test "$STATIC" = "no_static" -a "$DSO" = "no_dso"; then
  AC_MSG_ERROR([Neither static nor DSO option available, there is no point in continuing.])
fi

AC_SUBST(PYTHON_BIN)
AC_MSG_CHECKING(for --with-python)
AC_ARG_WITH(python, [--with-python=PATH	Path to specific Python binary],
[
  PYTHON_BIN="$withval"
  AC_MSG_RESULT($PYTHON_BIN)
],
AC_MSG_RESULT(no))

# check for Python executable
if test -z "$PYTHON_BIN"; then
  AC_PATH_PROG(PYTHON_BIN, python)
  if test -z "$PYTHON_BIN"; then
    AC_MSG_ERROR(python binary not found in path)
  fi
fi

# find out python version
AC_MSG_CHECKING(Python version)
PyVERSION=`$PYTHON_BIN -c ['import sys; print sys.version[:3]'`]
PyMAJVERSION=`$PYTHON_BIN -c ['import sys; print sys.version[:1]'`]
AC_MSG_RESULT($PyVERSION)

# make sure Python is version 2
if test "$PyMAJVERSION" != "2"; then
  AC_MSG_ERROR([This version of mod_python only works with Python major version 2. The one you have seems to be $PyVERSION.])
fi

# find out compiled in install prefix
AC_MSG_CHECKING(Python install prefix)
PyEXEC_INSTALLDIR=`$PYTHON_BIN -c "import sys; print sys.exec_prefix"`
AC_MSG_RESULT($PyEXEC_INSTALLDIR)

# this is where the Python libraries will get installed
AC_SUBST(PY_STD_LIB)
AC_MSG_CHECKING(checking where python libraries are installed)
PY_STD_LIB=`$PYTHON_BIN -c 'import distutils.sysconfig; print distutils.sysconfig.get_python_lib(plat_specific=1, standard_lib=1)'`
AC_MSG_RESULT($PY_STD_LIB)
# set python std library variable
AC_SUBST(LIBS)

PyLIBP=${PY_STD_LIB}
PyLIBPL=${PyLIBP}/config
PyPYTHONLIBS=${PyLIBPL}/libpython${PyVERSION}.a
PyLIBS=`grep "^LIB[[SMC]]=" ${PyLIBPL}/Makefile | cut -f2 -d= | tr '\011\012\015' '   '`
PyMODLIBS=`grep "^LOCALMODLIBS=" ${PyLIBPL}/Makefile | cut -f2 -d= | tr '\011\012\015' '   '`
PyFRAMEWORK=`grep "^PYTHONFRAMEWORK=" ${PyLIBPL}/Makefile | cut -f2 -d= | tr '\011\012\015' '   '`
PyFRAMEWORKDIR=`grep "^PYTHONFRAMEWORKDIR=" ${PyLIBPL}/Makefile | cut -f2 -d= | tr '\011\012\015' '   ' | awk '{print $1}'`

save_LDFLAGS="$LDFLAGS"
save_LIBS="$LIBS"
if test "$PyFRAMEWORKDIR" != "no-framework"; then
  if test -n "$PyFRAMEWORK"; then
    PyPYTHONLIBS="-framework $PyFRAMEWORK"
    LDFLAGS="${LDFLAGS} -Wl,-framework,Python"
  fi
else
  LDFLAGS="${LDFLAGS} -L${PyLIBPL}"
  AC_CHECK_LIB(python${PyVERSION}, Py_NewInterpreter,
             [ PyPYTHONLIBS="-lpython${PyVERSION}" ],
             [ LDFLAGS="$save_LDFLAGS"
               if test -f ${PyLIBPL}/libpython${PyVERSION}.a; then
                  PyPYTHONLIBS=${PyLIBPL}/libpython${PyVERSION}.a
               else
                  AC_ERROR(Can not link to python)
               fi
              ], 
              [ ${PyLIBS} ${PyMODLIBS} ] )
fi
LIBS="$save_LIBS"

# (actually this check already just happened above)
AC_MSG_CHECKING(what libraries Python was linked with) 
PY_LIBS="${PyPYTHONLIBS} ${PyLIBS} ${PyMODLIBS}"
 
## XXX this is a small work around for a weird RedHat problem
## erase -lieee from library list
if test -f /etc/redhat-release; then
  PY_LIBS="`echo $PY_LIBS | sed s/-lieee//`"
fi

LIBS="${LIBS} ${PY_LIBS}"
AC_MSG_RESULT($PY_LIBS)

AC_MSG_CHECKING(linker flags used to link Python)
AC_SUBST(LDFLAGS)
PyLFS=`grep "^LINKFORSHARED=" ${PyLIBPL}/Makefile | cut -f2 -d= | tr '\011\012\015' '   '`
PyLDFLAGS=`grep "^LDFLAGS=" ${PyLIBPL}/Makefile | cut -f2 -d= | tr '\011\012\015' '   '`
LDFLAGS="${LDFLAGS} ${PyLFS} ${PyLDFLAGS}"
LDFLAGS="${LDFLAGS} ${PY_LDFLAGS}"
AC_MSG_RESULT($PY_LDFLAGS)

AC_MSG_CHECKING(where Python include files are)
AC_SUBST(INCLUDES)
PY_INCLUDES="-I${PyEXEC_INSTALLDIR}/include/python${PyVERSION}"
INCLUDES="${INCLUDES} ${AP_INCLUDES} ${PY_INCLUDES}"
AC_MSG_RESULT($PY_INCLUDES)

# this for the test.py script
AC_SUBST(TEST_SERVER_ROOT)
TEST_SERVER_ROOT="`pwd`/test"
AC_SUBST(MOD_PYTHON_SO)
MOD_PYTHON_SO="`pwd`/src/mod_python.so"

# get the mod_python version
AC_SUBST(MP_VERSION)
MP_VERSION=`awk '/MPV_STRING/ {print $3}' src/include/mpversion.h`
MP_VERSION=`echo $MP_VERSION | sed 's/["]//g'`

# get --with-python-src. The python src is required to generate the documentation
# It is not required to compile or install mod_python itself
AC_SUBST(PYTHON_SRC)
AC_MSG_CHECKING(for --with-python-src)
AC_ARG_WITH(python-src, [--with-python-src=DIR	Path to python sources - required if you want to generate the documenation],
[
  PYTHON_SRC="$withval"
  AC_MSG_RESULT($PYTHON_SRC)
],
AC_MSG_RESULT(no))
if test -z "$PYTHON_SRC"; then
  PYTHON_SRC=""
fi

# configure the MUTEX_DIR for location of mutex locks
AC_SUBST(MUTEX_DIR)
AC_MSG_CHECKING(for --with-mutex-dir)
AC_ARG_WITH(mutex-dir, [--with-mutex-dir=DIR	Mutex directory],
[
  MUTEX_DIR="$withval"
  AC_MSG_RESULT($MUTEX_DIR)
],
AC_MSG_RESULT(no))

if test -z "$MUTEX_DIR"; then
  MUTEX_DIR="/tmp"
fi
# TODO - check if MUTEX_DIR is an absolute path 
AC_MSG_RESULT([Using MUTEX_DIR $MUTEX_DIR])

# configure the MAX_LOCKS for number of mutex locks
AC_SUBST(MAX_LOCKS)
AC_MSG_CHECKING(for --with-max-locks)
AC_ARG_WITH(max-locks, [--with-max-locks=INTEGER	Maximum number of locks],
[
  MAX_LOCKS="$withval"
  AC_MSG_RESULT($MAX_LOCKS)
],
AC_MSG_RESULT(no))

if test -z "$MAX_LOCKS"; then
  MAX_LOCKS="8"
fi
AC_MSG_RESULT([Using $MAX_LOCKS MAX_LOCKS.])

# Check for correct flex version
# Requires flex 2.5.31 for reentrant support
# See README for more details
AC_SUBST(LEX)

AC_MSG_CHECKING(for --with-flex)
AC_ARG_WITH(flex, [--with-flex=PATH	Path to specific flex binary.
	Flex Version 2.5.31 or greater is required to regenerate psp_parser.c
	from psp_parse.l. A prepared psp_parser.c file is included with the
	source, so you will only need flex if you make changes to psp_parser.l
    	See the README for more information.],
[
  LEX="$withval"
  AC_MSG_RESULT($LEX)
],
AC_MSG_RESULT(no))

# check for flex executable
if test -z "$LEX"; then
  AC_PATH_PROG(LEX, flex)
fi

if test "$LEX" && test -x "$LEX"; then
  AC_MSG_RESULT([found $LEX, we'll use this. Use --with-flex to specify another.])

  AC_MSG_CHECKING(flex version)
  FlexVERSION=`$LEX --version | sed 's/version//g' | awk '/flex/ {print $2}'`
  Flex_MAJOR=`echo $FlexVERSION| awk -F .  '{print $1}'`
  Flex_MINOR=`echo $FlexVERSION| awk -F .  '{print $2}'`
  Flex_PATCH=`echo $FlexVERSION| awk -F .  '{print $3}'`
  
  if test "$Flex_MAJOR" -eq "2" && test "$Flex_MINOR" -eq "5" && test "$Flex_PATCH" -ge "31"; then
    AC_MSG_RESULT([$FlexVERSION. Good])
  else
    AC_MSG_WARN([Flex version $FlexVERSION found.
    Version 2.5.31 or greater is required.  You can generally ignore this
    warning unless you need to regenerate psp_parser.c from psp_parse.l.
    If you do need regenerate psp_parser.c, use --with-flex to specify the
    location of the correct flex version. See the README for more information.])
  fi

else
  AC_MSG_WARN([flex $LEX not found
  You can generally ignore this warning unless you need to regenerate
  psp_parser.c from psp_parse.l.  If you do need regenerate psp_parser.c,
  use --with-flex to specify the location of flex.
  See the README for more information.])
fi

AC_OUTPUT(Makefile src/Makefile Doc/Makefile src/include/mod_python.h test/Makefile test/testconf.py dist/setup.py dist/Makefile)



