# ===========================================================================
#            http://www.nongnu.org/autoconf-archive/ax_python.html
# ===========================================================================
#
# SYNOPSIS
#
#   AX_PYTHON
#
# DESCRIPTION
#
#   This macro does a complete Python development environment check.
#
#   It recurses through several python versions (from 2.1 to 2.6 in this
#   version), looking for an executable. When it finds an executable, it
#   looks to find the header files and library.
#
#   It sets PYTHON_BIN to the name of the python executable,
#   PYTHON_INCLUDE_DIR to the directory holding the header files, and
#   PYTHON_LIB to the name of the Python library.
#
#   This macro calls AC_SUBST on PYTHON_BIN (via AC_CHECK_PROG),
#   PYTHON_INCLUDE_DIR and PYTHON_LIB.
#
# LICENSE
#
#   Copyright (c) 2008 Michael Tindal
#
#   This program is free software; you can redistribute it and/or modify it
#   under the terms of the GNU General Public License as published by the
#   Free Software Foundation; either version 2 of the License, or (at your
#   option) any later version.
#
#   This program is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
#   Public License for more details.
#
#   You should have received a copy of the GNU General Public License along
#   with this program. If not, see <http://www.gnu.org/licenses/>.
#
#   As a special exception, the respective Autoconf Macro's copyright owner
#   gives unlimited permission to copy, distribute and modify the configure
#   scripts that are the output of Autoconf when processing the Macro. You
#   need not follow the terms of the GNU General Public License when using
#   or distributing such scripts, even though portions of the text of the
#   Macro appear in them. The GNU General Public License (GPL) does govern
#   all other use of the material that constitutes the Autoconf Macro.
#
#   This special exception to the GPL applies to versions of the Autoconf
#   Macro released by the Autoconf Archive. When you make and distribute a
#   modified version of the Autoconf Macro, you may extend this special
#   exception to the GPL to apply to your modified version as well.

AC_DEFUN([AX_PYTHON],

[

AM_PATH_PYTHON

case "${host}" in
   *-mingw32*)
      ax_search_path="/usr/i686-pc-mingw32/sys-root/mingw/"
      ;;
   *)
      ax_search_path="$INSROOT /usr/local /usr/local/python /usr"
        ;;
   esac
]

[AC_MSG_CHECKING(for python build information)
AC_MSG_RESULT([])
for python in python2.8 python2.7 python2.6 python2.5 python2.4 python2.3 python2.2 python2.1 python; do
  AC_CHECK_PROGS(PYTHON_BIN, [$python])
  ax_python_bin=$PYTHON_BIN
  if test x$ax_python_bin != x; then
    AC_CHECK_LIB($ax_python_bin, main, ax_python_lib=$ax_python_bin, ax_python_lib=no)
    for ax_python_path_test in $ax_search_path ; do
      if test "${build_cpu}" = "x86_64"; then
        if test -r "$ax_python_path_test/lib64/lib$python.so"; then
          ax_python_ldpath="$ax_python_path_test/lib64/"
          ax_python_lib="$python"
          break
        fi
      else
        if test -r "$ax_python_path_test/lib/lib$python.so -o $ax_python_path_test/lib/lib$python.dll.a"; then
          ax_python_ldpath="$ax_python_path_test/lib/"
          ax_python_lib="$python"
          break
        fi
      fi
    done

    for ax_python_path_test in $ax_search_path ; do
      if test -r "$ax_python_path_test/include/$ax_python_bin/Python.h"; then
        ax_python_include="$ax_python_path_test/include/$ax_python_bin"
        break;
      fi
    done
    if test x$ax_python_lib != xno; then
      if test x$ax_python_include != xno; then
        break;
      fi
    fi
  fi
done

if test x$ax_python_bin = x; then
   ax_python_bin="no"
fi
if test x$ax_python_include = x; then
   ax_python_include="no"
fi
if test x$ax_python_lib = x; then
   ax_python_lib="no"
fi

AC_MSG_RESULT([results of the Python check:])
AC_MSG_RESULT([  Binary:      $ax_python_bin])
AC_MSG_RESULT([  Ldpath:      $ax_python_ldpath])
AC_MSG_RESULT([  Library:     $ax_python_lib])
AC_MSG_RESULT([  Include Dir: $ax_python_include])

if test x$ax_python_include != xno; then
  PYTHON_INCLUDE_DIR=-I$ax_python_include
  AC_SUBST(PYTHON_INCLUDE_DIR)
fi
if test x$ax_python_ldpath != xno; then
  PYTHON_LDPATH=-L$ax_python_ldpath
  AC_SUBST(PYTHON_LDPATH)
fi
if test x$ax_python_lib != xno; then
  PYTHON_LIB=-l$ax_python_lib
  AC_SUBST(PYTHON_LIB)
fi
])dnl

AC_DEFUN([AC_SEARCH_PYTHON],
[
    ac_with_Python=no
    ac_search_libs="python2.8 python2.7 python2.6 python2.5 python2.4 python2.3 python2.2 python2.1 python"
    case "${host}" in
      *-mingw32*)
        ac_search_lib_path="/usr/i686-pc-mingw32/sys-root/mingw/lib"
        ac_search_include_path="/usr/i686-pc-mingw32/sys-root/mingw/include"
        ;;
      *)
        if test "${build_cpu}" = "x86_64"; then
           ac_search_lib_path="$INSROOT/lib64 /usr/lib64 /usr/local/lib64"
        else
           ac_search_lib_path="$INSROOT/lib /usr/lib /usr/local/lib"
        fi
        ac_search_include_path="$INSROOT/include /usr/include /usr/local/include"
        ;;
   esac

     AC_ARG_WITH(Python,
          AC_HELP_STRING([--with-Python=DIR], [root directory of Python installation]),
          ac_with_Python=$withval
          if test "x${ac_with_Python}" != xyes && test "x${ac_with_Python}" != xno; then
               ac_Python_include="-I$withval/include/python"
               if test "${build_cpu}" = "x86_64"; then
                  ac_Python_libdir="-L$withval/lib64"
               else
                  ac_Python_libdir="-L$withval/lib"
               fi
               ac_Python_libs="-lpython"
          fi
     )

     AC_ARG_WITH([Python-include-dir],
          AC_HELP_STRING([--with-Python-include-dir=DIR], [Python header files are in DIR]),
          ac_with_Python=$withval
          if test "x${ac_with_Python}" != xyes && test "x${ac_with_Python}" != xno; then
               ac_Python_include="$withval"
          fi
     )

     AC_ARG_WITH([Python-lib-dir],
          AC_HELP_STRING([--with-Python-lib-dir=DIR], [The Python library is in DIR]),
          ac_with_Python=$withval
          if test "x${ac_with_Python}" != xyes && test "x${ac_with_Python}" != xno; then
               ac_Python_libdir="$withval"
               ac_Python_libs="-lpython"
               ac_Python_incprefix="$withval"
          fi
     )

     AC_ARG_WITH([Python-lib-name],
          AC_HELP_STRING([--with-Python-lib-name=DIR], [The Python library name, e.g. python, Python]),
          ac_with_Python=$withval
          if test "x${ac_with_Python}" != xyes && test "x${ac_with_Python}" != xno; then
               ac_Python_libs="-l$withval"
               ac_Python_incprefix="$withval"
          fi
     )


if test "x${ac_Python_libdir}" == "x"; then
    for ac_python_path_test in $ac_search_lib_path ; do
#    AC_MSG_NOTICE(Check for lib path $ac_python_path_test/)
      if test -r "$ac_python_path_test/"; then
         for ac_python_lib_test in $ac_search_libs ; do
#            AC_MSG_NOTICE( Check for lib $ac_python_path_test/lib$ac_python_lib_test.*)
            ls $ac_python_path_test/*$ac_python_lib_test.*  >/dev/null 2>&1
            if test $? -eq 0; then
              ac_Python_libdir="-L$ac_python_path_test/"
              ac_Python_lib="-l$ac_python_lib_test"
              ac_Python_incprefix="$ac_python_lib_test"
              break;
            fi
         done
         if test "$ac_Python_lib"; then
            break;
         fi
      fi
    done
  fi
  

   if test "x${ac_Python_include}" == "x"; then
    for ac_python_path_test in $ac_search_include_path ; do
#      AC_MSG_NOTICE(Check for include path $ac_python_path_test/$ac_Python_incprefix/Python.h)
      if test -r "$ac_python_path_test/$ac_Python_incprefix/Python.h"; then
         ac_Python_include="-I$ac_python_path_test/$ac_Python_incprefix"
         break;
      fi
    done
   fi


    AC_SEARCH_PYTHON_LOCAL($ac_Python_libdir, $ac_Python_include, $ac_Python_lib)
])


# subfunction for easier reuse
AC_DEFUN([AC_SEARCH_PYTHON_LOCAL],
[
     OLD_LDFLAGS=$LDFLAGS
     OLD_CXXFLAGS=$CXXFLAGS
     OLD_LIBS=$LIBS

     AC_MSG_NOTICE($1 : $2 : $3)

     if test "$1"; then
          LDFLAGS="$LDFLAGS $1"
     fi
     if test "$2"; then
          CXXFLAGS="$CXXFLAGS $2"
     fi
     if test "$3"; then
          LIBS="$3"
     fi

     AC_LANG_PUSH(C++)
     AC_MSG_CHECKING(for Python in $1 in $2 in $3)
     AC_TRY_LINK([
          #include <Python.h>
     ],[
          ;
     ],[
     success=yes
     ],[
     success=no
     ])
     AC_MSG_RESULT($success)
     AC_LANG_POP()


     LDFLAGS=$OLD_LDFLAGS
     CXXFLAGS=$OLD_CXXFLAGS
     LIBS=$OLD_LIBS
     AM_CONDITIONAL(HAVE_PYTHON, true)

     if test "x$success" != xyes; then
          AC_MSG_ERROR(Python not (properly) installed. Get a (free) recent version from: http://python.sourceforge.net/)
     else
          if test "$1"; then
               AC_SUBST(PYTHON_LDFLAGS, "$1")
          fi
          if test "$2"; then
               AC_SUBST(PYTHON_CXXFLAGS, "$2")
          fi
      if test "$3"; then
           AC_SUBST(PYTHON_LIB, "$3")
      fi
     fi

])

AC_DEFUN([AX_PYTHON_MODULE],[
    if test -z $PYTHON;
    then
        PYTHON="python"
    fi
    PYTHON_NAME=`basename $PYTHON`
    AC_MSG_CHECKING($PYTHON_NAME module: $1)
    $PYTHON -c "import $1" 2>/dev/null
    if test $? -eq 0;
    then
        AC_MSG_RESULT(yes)
        eval AS_TR_CPP(HAVE_PYMOD_$1)=yes
    else
        AC_MSG_RESULT(no)
        eval AS_TR_CPP(HAVE_PYMOD_$1)=no
        #
        if test -n "$2"
        then
            AC_MSG_ERROR(failed to find required module $1)
            exit 1
        fi
    fi
])
