#
# CAUTION!
#
# The file configure.ac is auto-generated from configure.ac.in
# Changes to configure.ac will be overwritten by bootstrap.sh
#

AC_PREREQ(2.52)
AC_INIT([nest], [2.2.2], [nest_user@nest-initiative.org])

# These variables are exported to libnestutil/config.h
# The patchlevel is updated automatically by the script
# releasetools/buildnest
SLI_MAJOR=2
SLI_MINOR=2
SLI_PATCHLEVEL=2

SLI_PRGNAME="nest-$SLI_MAJOR.$SLI_MINOR.$SLI_PATCHLEVEL"
SLI_VERSION=$SLI_MAJOR.$SLI_MINOR.$SLI_PATCHLEVEL

# Exporting source and build directories to SLI requires full path names.
# Thus we have to expand. Here, we are in top build dir, since source dir
# must exist, we can just move there and call pwd
if test "x$srcdir" = x ; then
  PKGSRCDIR=`pwd`
else
  PKGSRCDIR=`cd $srcdir && pwd`
fi
PKGBUILDDIR=`pwd`

# If the user did not supply --prefix, we fail loudly
if test $prefix = NONE; then
  echo "Please supply a valid installation prefix, for example ./configure --prefix=\$HOME/opt/nest !"
  exit 1
fi

# If this is not called, install-sh will be put into .. by bootstrap.sh
# moritz, 06-26-06
AC_CONFIG_AUX_DIR(.)

# We might consider switching to tar-pax (2001) in the future, for now
# tar-ustar (1988) is probably good enough. tar-v7 is not an option, since
# we already hit the 99 char filename limit.
AM_INIT_AUTOMAKE([tar-ustar])

# Allow to build NEST using silent rules.
m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES])

# Obtain host system type; HEP 2004-12-20
AC_CANONICAL_HOST

# ------------------------------------------------------------------------
# Handle options
#
# NOTE: No programs/compilations must be run in this section;
#       otherwise CFLAGS and CXXFLAGS may take on funny default
#       values.
#       HEP 2004-12-20
# ------------------------------------------------------------------------

# Use PS_ARRAYS
AC_DEFINE(PS_ARRAYS, 1, [Use PS array construction semantics])

# Debug level
# Note, $SLI_debug is used in acinclude.m4
SLI_debug="unset"
# Flags depend on platform and compiler and will be set later
SLI_debugflags=""

# Optimization level
# Note, $SLI_optimize is used in acinclude.m4
SLI_optimize="set"
# Flags depend on platform and compiler and will be set later
SLI_optimizeflags=""

# Warning level
# Note, $SLI_warning is used in acinclude.m4
SLI_warning="set"
# Flags depend on platform and compiler and will be set later
SLI_warningflags=""

# Select elementary unit of time
# example:
#    ./configure --with-tics-per-ms=1000    // 1 microsecond, default
#    ./configure --with-tics-per-ms=1e3     // the same
#    ./configure --with-tics-per-ms='std::ldexp(1.0,13)'  // 2^13 tics per ms
#
AC_ARG_WITH(tics_per_ms,[  --with-tics-per-ms=nnn	Specify elementary unit of time.],
[if test "$withval" != no; then
 AC_DEFINE(HAVE_TICS_PER_MS,1, [Tics per millisecond specified? ])
 AC_DEFINE_UNQUOTED(CONFIG_TICS_PER_MS, ${withval}, [tics per ms in simulation])
fi])


# Select resolution of simulation
# example:
#    ./configure --with-tics-per-step=100   // 100 tics per step = 0.1ms (default)
#
AC_ARG_WITH(tics_per_step,[  --with-tics-per-step=nnn	Specify resolution.],
[if test "$withval" != no; then
 AC_DEFINE(HAVE_TICS_PER_STEP,1, [Tics per step specified? ])
 AC_DEFINE_UNQUOTED(CONFIG_TICS_PER_STEP, ${withval}, [tics per step in simulation])
fi])


# Select usage of the vampir tool
#
# in order to analyze the result of a program run
# the required environment variables are:
#    PAL_ROOT        e.g. /usr/nld/vampir
#    PAL_LICENSEFILE e.g. /usr/nld/vampir/etc/license.dat
# and path to the binary vampir which needs to be called with the
# trace file .bpv as a parameter.
#
# 28.3.03 Diesmann, Morrison
#
AC_ARG_WITH(vampir,[  --with-vampir			Include support for the Vampir tool.],
[if test "$withval" != no; then
 AC_DEFINE(HAVE_VAMPIR,1, [Vampir support enabled? ])
 VAMPIR_PATH="${withval}"
 VAMPIR_LIBS="-L${VAMPIR_PATH}/lib -lVT -lpmpi"
 VAMPIR_CFLAGS="-I${VAMPIR_PATH}/include"
 else
  unset HAVE_VAMPIR
  unset VAMPIR_PATH
  unset VAMPIR_LIBS
  unset VAMPIR_CFLAGS
fi])


configure_bluegene="no"
bluegene_architecture="none"
AC_MSG_CHECKING(whether we are configuring for Blue Gene)
AC_ARG_ENABLE([bluegene],
	[AS_HELP_STRING([--enable-bluegene],
	[Configure for Blue Gene; the specific BG model must be given as argument (=L/P/Q).])],
	[if test "x$enableval" != xno ; then
	   configure_bluegene="yes"
	   enableval_uc=`echo ${enableval} | awk {'print toupper($_)'}`
	   if test "x$enableval_uc" = xL || test "x$enableval_uc" = xP || test "x$enableval_uc" = xQ; then
	     bluegene_architecture="$enableval_uc"
           else
             echo
	     AC_MSG_ERROR([Only L, P, or Q are valid arguments for --enable-bluegene.])
           fi
         fi],[])

if test "x$configure_bluegene" = xno ; then
  AC_MSG_RESULT(no)
else
  AC_MSG_RESULT([yes ($bluegene_architecture)])
fi

# register variable IS_BLUEGENE to be visible in Makefile.am
# required to suppress slidoc installation, which would fail due to cross-compile
AM_CONDITIONAL(IS_BLUEGENE, test "x$configure_bluegene" != xno)

# alloc configuration of K in a manner similar to bluegene above. We only have
# one K, but we can either cross-compile or build interactively, and need to
# handle some aspects differently, depending.

configure_k="no"
k_interactive="no"
AC_MSG_CHECKING(whether we are configuring for K)
AC_ARG_ENABLE([k],
	[AS_HELP_STRING([--enable-k],
	[Configure for K. Set "yes" for cross compilation and "interactive" for interactive build.])],
	[if test "x$enableval" != xno ; then
	   configure_k="yes"
	   if test "x$enableval" = "xinteractive"; then
	       k_interactive="yes"
           fi
         fi],[])

if test "x$configure_k" = xno ; then
  AC_MSG_RESULT(no)
else
    if test "x$k_interactive" = xno ; then
      AC_MSG_RESULT([yes (cross-compilation)])
    else
      AC_MSG_RESULT([yes (on interactive nodes)])
    fi
fi

# Manually activate MPI, allow to specify directory containing MPI
#
# The Scali mpic++ wrapper leads to link problems, so we cannot use it.
# Instead, we use the plain C++ compiler and look for MPI in the scali
# installation directory, by default /opt/scali.
#
# To make this easy for the user, there are two mutually exclusive options:
#   --with-mpi    for non-Scali MPI
#   --with-scali  for Scali MPI
#
# H. E. Plesser, 2007-01-05
#
SLI_distributed="unset"
SLI_mpi_prefix="unset"
SLI_mpi_option="no"
SLI_scali_option="no"

if test "x$configure_bluegene" = xyes ; then
  SLI_distributed=set
  SLI_mpi_option=yes
  if test "x$bluegene_architecture" = xL; then
    AC_DEFINE(IS_BLUEGENE_L, 1, [Configuring for Blue Gene/L])
    bluegene_dynamic_libs=no # no dynamic libs on BG/L
  elif test "x$bluegene_architecture" = xP; then
    AC_DEFINE(IS_BLUEGENE_P, 1, [Configuring for Blue Gene/P])
    bluegene_dynamic_libs=yes
  elif test "x$bluegene_architecture" = xQ ; then
    AC_DEFINE(IS_BLUEGENE_Q, 1, [Configuring for Blue Gene/Q])
    bluegene_dynamic_libs=yes
  fi
else
  AC_ARG_WITH(mpi,[  --with-mpi[[=directory]]	Request compilation with MPI; optionally give directory with MPI installation.],
  [
    if test "$SLI_scali_option" = "yes" ; then
      AC_MSG_ERROR([The --with-mpi and --with-scali options are mutually exclusive.])
    fi

    if test "$withval" = "yes" ; then
      SLI_distributed="set"
      SLI_mpi_option="yes"
    elif test "$withval" != "no" ; then
      SLI_distributed="set"
      SLI_mpi_prefix=`echo ${withval} | sed 's/\/*$//'` # remove trailing slashes
      SLI_mpi_option="yes"
    fi
  ])

  AC_ARG_WITH(scali, [  --with-scali[[=directory]]	Request compilation with Scali MPI; optionally give directory with Scali [[/opt/scali]]],
  [
    if test "$SLI_mpi_option" = "yes" ; then
      AC_MSG_ERROR([The --with-mpi and --with-scali options are mutually exclusive.])
    fi

    if test "$withval" = "yes" ; then
      SLI_distributed="set"
      SLI_mpi_prefix="/opt/scali"
      SLI_scali_option="yes"
    elif test "$withval" != "no" ; then
      SLI_distributed="set"
      SLI_mpi_prefix=`echo ${withval} | sed 's#\(.*\)/$#\1# ; q'`
      SLI_scali_option="yes"
    fi
  ])
fi

if test "x$configure_k" = xyes ; then
  AC_DEFINE(IS_K, 1, [Configuring for K])
fi
if test "x$k_interactive" = xyes ; then
  AC_DEFINE(IS_K_INTERACTIVE, 1, [Configuring for K interactive build])
fi

# Use the MUSIC library to allow for simulator interoperation
#
AC_ARG_WITH(music,[  --with-music[[=directory]]       Request the use of MUSIC. Optionally give the directory, where MUSIC is installed],
  [
    if test "$withval" != "no"; then
      if test "$SLI_distributed" != "set"; then
         AC_MSG_ERROR([MUSIC requires --with-mpi.])
      else
        AC_DEFINE(HAVE_MUSIC,1, [MUSIC support enabled?])
        if test "$withval" = "yes"; then
      	  MUSIC_LIBS="-lmusic"
	  MUSIC_INCLUDE=""
        else
          MUSIC_PATH="${withval}"
      	  MUSIC_LIBS="-L${MUSIC_PATH}/lib -lmusic"
	  MUSIC_INCLUDE="-I${MUSIC_PATH}/include"
        fi
      fi
    else
      unset HAVE_MUSIC
    fi
  ])


# Select optimization mode
# note: see debugging mode selection
#
AC_ARG_WITH(optimize,[  --with-optimize[[=flags]]	Select optimization level.],
[ SLI_debug="unset"
  SLI_optimize="unset"
  if test "$withval" != no; then
    SLI_optimize="set"
   if test "$withval" != "yes"; then
    SLI_optimizeflags="$withval"
   fi
fi])

# select debugging mode
# note: --with-debug     -> withval = yes
#       --without-debug  -> withval = no
#       --with-debug=foo -> withval = foo
#
AC_ARG_WITH(debug,[  --with-debug[[=flags]]		Enable debugging information.],
[SLI_debug="unset"
 if test "$withval" != "no"; then
   SLI_debug="set"
   ## Debugging should be done without optimizations
   SLI_optimize="unset"
   if test "$withval" != "yes"; then
     SLI_debugflags="$withval"
   fi
fi])

# select warning level
#
AC_ARG_WITH(warnings,[  --with-warnings[[=flags]]	Enable warnings.],
[SLI_warning="unset"
 if test "$withval" != "no"; then
   SLI_warning="set"
   SLI_warningflags="$withval"
fi])

# Set the platform-dependent compiler flags based on the canonical
# host string.  These flags are placed in AM_{C,CXX}FLAGS.  If
# {C,CXX}FLAGS are given as environment variables, then they are
# appended to the set of automatically chosen flags.  After
# {C,CXX}FLAGS have been read out, they must be cleared, since
# system-dependent defaults will otherwise be placed into the
# Makefiles.  HEP 2004-12-20.

# Before we can determine the proper compiler flags, we must know
# which compiler we are using.  Since the pertaining AC macros run the
# compiler and set CFLAGS, CXXFLAGS to system-dependent values, we
# need to save command line/enviroment settings of these variables
# first. AC_AIX must run before the compiler is run, so we must run it
# here.
# HEP 2004-12-21

# Compiler selection:
# - C compiler is chosen using AC_PROG_CC. C code occurs only in a
#   few files in librandom. None of that code is MPI related.
#
# - C++ compiler is chosen as follows if distributed simulation is
#   chosen:
#
#   1. If simulation is not distributed, use AC_PROC_CXX.
#   2. Otherwise, if no prefix is given, search for mpiCC or equivalent
#      using AC_PROG_CXX(mpiCC).
#      Search order can be influenced by setting PATH before calling
#      configure.
#   3. If distributed simulation is requested and a prefix given, use
#      standard compiler from AC_PROG_CXX and check for libraries in
#      prefix directory.
#   4. Setting CXX overrides compiler selection brute force. AC_PROG_CXX
#      handles this.
#   5. The final configuration is tested for if it works.
#
# HEP 2007-01-03

SLI_SAVE_CFLAGS=$CFLAGS
SLI_SAVE_CXXFLAGS=$CXXFLAGS

AC_AIX

AC_PROG_CC

if test "$SLI_distributed" = unset ; then

  # no-distributed simulation
  AC_PROG_CXX

else

  if test "$SLI_scali_option" = "yes" ; then

    # We cannot use the mpic++ wrapper for Scali, since it
    # leads to a linker error due to "hidden symbol __dso_handle"
    # on several systems. So we just choose the default C++ compiler
    # and handle MPI via compiler flags later.
    AC_PROG_CXX

  else

    # Non-Scali MPI. Here we assume that the wrappers work. If an
    # explicit mpi-path is given, we add its bin to PATH first, then
    # test, and then redefine the compiler variables to full path
    # names. This is necessary since AC_PROG_CXX internally searches
    # through the entire PATH; since AC_PROG_CXX contains many checks
    # in addition to the search, we cannot simply write our own
    # absolute-path version.
    #
    # Search for MPI C++ compiler wrapper:
    # 1. Search for mpicxx, since mpicc and mpiCC are the same file
    #    on non-case-sensitive file systems (most OSX filesystems).
    # 2. Search for mpiCC, which is MPICH standard
    # 3. Do not look for Scali's mpic++, since the Scali compiler wrapper
    #    does not work.

    if test "$SLI_mpi_prefix" = unset ; then
      AC_PROG_CXX(mpicxx mpiCC)
    else
      SLI_SAVE_PATH=$PATH
      # add /bin, avoiding duplicate //
      mpi_bin=${SLI_mpi_prefix}/bin

      PATH=${mpi_bin}:$PATH
      AC_PROG_CXX(mpicxx mpiCC)

      # If mpicxx or mpiCC are chosen, we check if they exists in the prefix
      # path. If so, we add the prefix path. Otherwise, we leave CXX untouched.
      if test $CXX = mpicxx -o $CXX = mpiCC ; then
        if test -x ${mpi_bin}/$CXX ; then
          CXX=$mpi_bin/$CXX
          ac_ct_CXX=$CXX
          ac_cv_prog_ac_ct_CXX=$CXX
        fi
      fi
      PATH=$SLI_SAVE_PATH
    fi

  fi

fi
# further processing of distributed case below (see SLI_NEW_PATH_MPI)


# disable PyNEST for Blue Gene or if explicitly stated
AC_MSG_CHECKING([whether to compile PyNEST])
if test "x$configure_bluegene" = xyes ; then
  HAVE_PYTHON=no
else
  HAVE_PYTHON=yes
  PYTHON_EXEC=`which python`

  AC_ARG_WITH(python,[  --with-python=python		specify the Python interpreter to use],
  [
    if test "$withval" == no; then
      HAVE_PYTHON=no
    else
      if test "$withval" != yes; then
        PYTHON_EXEC=$withval
      fi
    fi
  ])
fi
AC_MSG_RESULT([$HAVE_PYTHON])

# Check, whether we are able to compile PyNEST. This will break, if no Python
# interpreter is found.
# JME 2008/04
if test $HAVE_PYTHON = yes; then
  AC_CHECK_FILE($PYTHON_EXEC, HAVE_PYTHON=yes, AC_MSG_ERROR([Python interpreter '$PYTHON_EXEC' not found.]))
  PYTHON_VERSION=`$PYTHON_EXEC -c "from distutils import sysconfig;\
                                   print sysconfig.get_python_version()"`

  PYNEST_SUBDIR="pynest"

  AC_MSG_CHECKING([for Python headers])
  PYTHON_INC_PATH="`$PYTHON_EXEC $PKGSRCDIR/$PYNEST_SUBDIR/checkpython.py`"
  if test $PYTHON_INC_PATH != "yes"; then
    AC_MSG_RESULT([no])
    AC_MSG_ERROR([Python headers not found. Make sure you have the development package installed.
                  If you do not need PyNEST, use --without-python to disable it.])
  fi
  AC_MSG_RESULT([yes])

  if test $exec_prefix = NONE; then
    PYNEST_PREFIX=${prefix}
  else
    PYNEST_PREFIX=${exec_prefix}
  fi

  # Check for a non-standard installation path for PyNEST
  AC_ARG_WITH(pynest-prefix,[  --with-pynest-prefix=path	install PyNEST to path/lib/python<version>/site-packages.],
  [
    PYNEST_PREFIX=$withval
  ])
fi

# register variable HAVE_PYTHON to be visible in Makefile.am
# required to run or not run the PyNEST tests in the testsuite
AM_CONDITIONAL(HAVE_PYTHON, test "x$HAVE_PYTHON" != xno)


##-- use ltdl library for dynamic loading of modules
##   if not Cygwin or blue gene
AC_MSG_CHECKING([whether to build shared libraries])
if test "$host_os" != "cygwin"; then
  if test "x$configure_bluegene" = xno ; then
    BUILD_SHARED=yes
  else
    BUILD_SHARED=$bluegene_dynamic_libs
  fi
else
  BUILD_SHARED=no
  SLI_debug="unset"
  SLI_debugflags="-g"
  SLI_optimize="set"
  SLI_optimizeflags="-O"
fi
if test "x$BUILD_SHARED" != xno ; then
   AC_ENABLE_SHARED    ## do not build static libraries by default, use
   AC_DISABLE_STATIC   ## --enable-static to build them too
else
   AC_DISABLE_SHARED
   AC_ENABLE_STATIC
fi

AC_MSG_RESULT([$BUILD_SHARED])

SLI_SET_CFLAGS
CFLAGS=
SLI_SET_CXXFLAGS
CXXFLAGS=

# -------------------------------------------
# END Handle options
# -------------------------------------------

## search for the Doxygen executable
AC_PATH_PROG([DOXYGEN], [doxygen])
AM_CONDITIONAL(DOXYGEN_AVAILABLE, test x$DOXYGEN != x)

## see if dot from graphviz is available
## this is only needed to choose between
## Doxygen files, see doc/Makefile.am; HEP
AC_CHECK_PROG(HAVE_DOT, dot, "yes")
AM_CONDITIONAL(DOT_AVAILABLE, test x$HAVE_DOT = xyes)

## Configure C environment

AC_PROG_INSTALL

LT_PATH_LD
LT_CONFIG_LTDL_DIR([libltdl])
_LTDL_CONVENIENCE                 ## put libltdl into a convenience library
LT_INIT([dlopen])                 ## use libtool
m4_pattern_allow([LT_LIBEXT])     ## supress false positive message by autoconf

AC_CONFIG_SUBDIRS(libltdl)        ## also configure subdir containing libltdl

# This is a fix for a bug in Debian's libtool.
# See http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=526750
if echo $LIBLTDL | grep -s -q top_build_prefix; then
  top_build_prefix=${PKGBUILDDIR}/
  LIBLTDL=`eval echo $LIBLTDL`
fi

if test "x$BUILD_SHARED" != xno ; then
  if test "x$LIBLTDL" != x ; then
    AC_DEFINE(HAVE_LIBLTDL, 1, [Havel libltdl, can load dynamic modules])
  fi
fi

#-- Stuff for GSL -----------------------------------------
# NOTE: MUST come BEFORE switching to AC_LANG_CPLUSPLUS !!!
# HEP 2001-08-07
# NOTE: Adding @GSL_CFLAGS@ to the AM_CPPFLAGS variable
#       in Makefile.am will result in warnings about re-ordering
#       the search path.
# HEP 2002-07-10
# GSL Related HAVE_* variables:
#
#   HAVE_GSL     indicates that some GSL release version is available
#                (i.e., version 1.0 or higher)
#   HAVE_GSL_X_Y indicates that the GSL is version >= X.Y
#
# Currently implemented:
#   HAVE_GSL
#   HAVE_GSL_1_2   # minimum requirement for GSL Random Generators
#   HAVE_GSL_1_11  # fixes bug in ODE solvers, see #313 and #88
#
# Add further HAVE_GSL_MAJOR_MINOR if necessary according to scheme for
# HAVE_GSL_1_2 below.
#
# For each HAVE_GSL_X_Y, define also an automake conditional
# GSL_1_2_AVAILABLE to allow for conditional compilation.  See example
# below, and librandom for an example of how to use it in Makefile.am.
#
# HEP 2002-08-21
AM_PATH_GSL(1.0, ac_have_gsl="yes", ac_have_gsl="no")

if test "$ac_have_gsl" != no ; then
  AC_DEFINE(HAVE_GSL,1, [Is the GNU Science Library available (ver. >= 1.0)?])
  AC_SUBST(HAVE_GSL)
  AC_SUBST(GSL_LIBS)
  AC_SUBST(GSL_CFLAGS)
  AM_PATH_GSL(1.2, ac_have_gsl_1_2="yes", ac_have_gsl_1_2="no")
  if test "x$ac_have_gsl_1_2" = xyes ; then
    AC_DEFINE(HAVE_GSL_1_2, 1, [Is GSL Version >= 1.2?])

    AM_PATH_GSL(1.11, ac_have_gsl_1_11="yes", ac_have_gsl_1_11="no")
    if test "x$ac_have_gsl_1_11" = xyes ; then
      AC_DEFINE(HAVE_GSL_1_11, 1, [Is GSL Version >= 1.11?])
    else
      ac_have_gsl_1_11="no"
    fi
  fi
else
  unset GSL_LIBS
  unset GSL_CFLAGS
  unset GSL_VERSION
  ac_have_gsl_1_2="no"
fi
# AM_CONDITIONAL must not be invoked conditionally
AM_CONDITIONAL(GSL_1_2_AVAILABLE, test x$ac_have_gsl_1_2 = xyes)
AM_CONDITIONAL(GSL_1_11_AVAILABLE, test x$ac_have_gsl_1_11 = xyes)

# C Inline stuff revised, HEP 2003-03-11
SLI_C_INLINE

if test "$ac_cv_c_inline" != no ; then
  AC_DEFINE(HAVE_INLINE,1, [Do we have the inline macro in C?])
  AC_SUBST(HAVE_INLINE)
fi

## --- end GSL STUFF -----------------------------------

# fails to find available functions if run with language
# set to C++; HEP 2003-03-13
AC_CHECK_FUNCS(popen pclose execv fork setenv sigaction)

#-- Set the language to C++
AC_LANG_CPLUSPLUS

# check if expm1 is available after we have switched to C++
AC_MSG_CHECKING([for expm1 ])
AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <math.h>]],
[[double x = ::expm1(0.1);]])],
ac_have_expm1="yes", ac_have_expm1="no")
if test "$ac_have_expm1" = yes ; then
  AC_DEFINE(HAVE_EXPM1,1, ["Define if expm1() is available"])
fi
AC_MSG_RESULT($ac_have_expm1)

#-- Look for programs needed in the Makefile
AC_PROG_CXXCPP

AC_PATH_PROGS([MAKE],[gmake make],[make])

SLI_LIBS="$SLI_LIBS $LDFLAGS"

# Check for readline if not configuring for Blue Gene
if test "x$configure_bluegene" = xno ; then
  AC_ARG_WITH([readline], [AS_HELP_STRING([--without-readline], [disable support for GNU readline])],
  [],
  [with_readline=yes])

  if test "x$with_readline" != xno ; then
    AC_CHECK_LIB(curses,tgetstr, SLI_LIBS="${SLI_LIBS} -lcurses",\
   [AC_CHECK_LIB(termcap,tgetstr, SLI_LIBS="${SLI_LIBS} -ltermcap",\
   [AC_CHECK_LIB(ncursesw,tgetstr,  SLI_LIBS="${SLI_LIBS} -lncursesw",\
   [AC_CHECK_LIB(ncurses,tgetstr,  SLI_LIBS="${SLI_LIBS} -lncurses",\
   [AC_MSG_ERROR([You need either libncurses, libcurses, or libtermcap to compile NEST.
   ])])])])])
   AC_CHECK_LIB(readline, readline,\
   [SLI_LIBS="${SLI_LIBS} -lreadline"\
   AC_DEFINE(HAVE_READLINE, 1,[Use GNU libreadline])],\
   [AC_MSG_ERROR([No readline found, --without-readline to disable])],\
   [${SLI_LIBS}])
  fi
fi

# Test if to use multithreading with either OpenMP or POSIX threads
# JME 2011/09/29
AC_MSG_CHECKING(which threading library to use)

# Testing for OpenMP
AC_ARG_WITH([openmp], [AS_HELP_STRING([--without-openmp], [disable OpenMP multithreading])],
            [with_openmp=$withval],     # withval=yes, if configured with --with-openmp, no with --without-openmp
            [with_openmp=default])      # withval would be yes, if configured without args

# Testing for POSIX threads
AC_ARG_WITH([pthread], [AS_HELP_STRING([--with-pthread], [enable POSIX threads])],
            [with_pthread=$withval],
            [with_pthread=no])

# This allows to override the default (OpenMP) by only using
# --with-pthread without also having to use --without-openmp
if test "$with_openmp" = default && test "$with_pthread" = yes; then
  with_openmp=no
fi

# Force a sensible combination of --with-openmp and --with-pthread
if test "$with_openmp" = yes && test "$with_pthread" = yes; then
  echo # we need a newline here because there won't be a result to print
  AC_MSG_ERROR([Only one of OpenMP and POSIX threads can be enabled manually.])
fi

# On Blue Gene/L we can't use POSIX threads
if test "$with_pthread" = yes && test "x$bluegene_architecture" = xL; then
  echo # we need a newline here because there won't be a result to print
  AC_MSG_ERROR([POSIX threads are not available on Blue Gene/L, either use OpenMP, or no threading.])
fi

KOMP_LIBS=""
# Check if the OpenMP setup is usable
if test "$with_openmp" = yes || test "$with_openmp" = default; then
  AC_MSG_RESULT(openmp)
  if test "x$configure_bluegene" = xyes; then
    # On Blue Gene, the macro AC_OPENMP is not defined, so we set the flags manually
    OPENMP_CXXFLAGS="-qsmp=omp"
  elif test "x$configure_k" = xyes; then
    # On K, the macro AC_OPENMP is not defined, so we set the flags manually
    OPENMP_CXXFLAGS="-Kopenmp"
    # With interactive build on K and with Python we need to explicitly pass a
    # few extra libraries for the python interface library.
    if test "x$k_interactive" = xyes && test "x$HAVE_PYTHON" = xyes ; then
      KOMP_LIBS="-lfj90i -lfj90f"
    fi
  else
    m4_ifdef([AC_OPENMP], [AC_OPENMP])
    if test "$OPENMP_CXXFLAGS" = ""; then
      AC_MSG_ERROR([OpenMP not found, please use --without-openmp to disable.])
    fi
  fi


  AM_CFLAGS="$AM_CFLAGS $OPENMP_CXXFLAGS"
  AM_CXXFLAGS="$AM_CXXFLAGS $OPENMP_CXXFLAGS"
  with_openmp=yes
else
  # Check if the POSIX threads are usable
  if test "$with_pthread" = yes; then
    AC_MSG_RESULT(pthreads)
    AC_CHECK_LIB(pthread, pthread_testcancel,
                [SLI_LIBS="${SLI_LIBS} -lpthread"
                 SLI_THREAD_DEFS="-D_POSIX_C_SOURCE=199506L"
                 SLI_HAVE_PTHREADS="true"
                 AC_DEFINE(HAVE_PTHREADS,1,[pthreads are available])],\
                [echo; AC_MSG_ERROR([No pthread library found, please use --without-pthread to disable.])])
  else
    AC_MSG_RESULT(none)
  fi
fi
# End of thread library processing


# is long long available for type representation and fully usable?
SLI_HAVE_LONG_LONG

# Check whether Alpha cxx includes *.h standard headers
# and C-style macros correctly
SLI_ALPHA_CXX_STD_BUG

# For a description of MPI detection, see comment on compiler selection.
if test "$SLI_distributed" = "set"; then
  if test "x$configure_bluegene" = xyes ; then
    BLUEGENE_MPI
  else
    SLI_NEW_PATH_MPI
  fi
fi

# Makros M_E and M_PI
#
# !HAVE_M_E && HAVE_CMATH_MAKROS_IGNORED
#    --> cmath needs to be included with #undef __PURE_CNAME
#
# HAVE_M_E  || HAVE_CMATH_MAKROS_IGNORED  --> M_E  is present
# HAVE_M_PI || HAVE_CMATH_MAKROS_IGNORED  --> M_PI is present
#
# Diesmann, 28.8.02
#
SLI_CXX_HAVE_M_E
SLI_CXX_HAVE_M_PI

SLI_CHECK_STATIC_TEMPLATE_DECLARATION

SLI_ALPHA_CMATH_MAKROS_IGNORED
SLI_ALPHA_CXX_SIGUSR_IGNORED
if test "$SLI_HAVE_PTHREADS" = "true"; then
   SLI_ALPHA_CXX_PTHREAD_IGNORED

   # must run aftr SLI_ALPHA_CXX_PTHREAD_IGNORED
   SLI_HAVE_PTHREAD_SETCONCURRENCY
fi

SLI_CHECK_ISTREAM_HEADER
SLI_CHECK_OSTREAM_HEADER
SLI_CHECK_SSTREAM_HEADER

SLI_CHECK_STL_VECTOR_CAPACITY_BASE_UNITY
SLI_CHECK_STL_VECTOR_CAPACITY_DOUBLING

SLI_CHECK_EXITCODES

# ----------------------------------------------------
# Configure external modules to be linked into nest
# ----------------------------------------------------
# SLI_USER_MODULES collects module names for informative messages
SLI_USER_MODULES=""
# LINKED_USER_MODULES collects what needs to be added to LDADD
LINKED_USER_MODULES=""

AC_ARG_WITH(modules,[  --with-modules = external modules to be linked in],
[
if test "$withval" != no; then
  LINKED_USER_MODULES="-L$prefix/lib/nest"
  for mod in $withval; do
    LINKED_USER_MODULES="$LINKED_USER_MODULES -l$mod"
    SLI_USER_MODULES="$SLI_USER_MODULES $mod"
  done
fi
])


# ---------------------------------------------------------------
# Configure directories to be built
# ---------------------------------------------------------------

# Classification of modules:
#
# - SLI_PUBLIC_MODULES
#      statically linked, included in release
# - SLI_NONPUBLIC_MODULES
#      statically linked, not included in release
#
# - SLI_PUBLIC_DYNMODULES
#      dynamically linked, automatically loaded, included in release
# - SLI_NONPUBLIC_DYNMODULES
#      dynamically linked, automatically loaded, not included in release

SLI_PUBLIC_MODULES="models precise topology conngen"
SLI_NONPUBLIC_MODULES=""

SLI_PUBLIC_DYNMODULES=""
SLI_NONPUBLIC_DYNMODULES=""

# Now go through all candidates and set up necessary variables and directories
# NOTE: We only include those modules that shall be loaded automatically.
#       The _NOLOAD modules will only be included with the SUBDIRS below.
SLI_EXTRA_MODULE_CANDIDATES="$SLI_PUBLIC_MODULES $SLI_NONPUBLIC_MODULES"

# add dynmodules only if building shared libs
if test "x$BUILD_SHARED" != xno ; then
  SLI_EXTRA_MODULE_CANDIDATES="$SLI_EXTRA_MODULE_CANDIDATES $SLI_PUBLIC_DYNMODULES $SLI_NONPUBLIC_DYNMODULES"
fi

# collect modules that shall be linked in
# these are modules from the NEST code base, ie, in the source directory
# and dependency tracking shall happen for these modules
LINKED_MODULES=""
SLI_EXTRA_MODULES=""

# collect directories for which AC_CONFIG_FILES need to be run
NEED_MAKEFILE_DIRS=""

# check all candidates: must contain Makefile.am
for mod in $SLI_EXTRA_MODULE_CANDIDATES; do
  SLI_CHECK_SOURCE_FILE([$srcdir/$mod/Makefile.am], mod_exists="yes", mod_exists="no")
  if test "x$mod_exists" = xyes; then
    SLI_EXTRA_MODULES="$SLI_EXTRA_MODULES $mod"
    NEED_MAKEFILE_DIRS="$NEED_MAKEFILE_DIRS $mod"
    newmod="${PKGBUILDDIR}/${mod}/lib${mod}module.la"
    LINKED_MODULES="$LINKED_MODULES $newmod"

    # check if we need to create Makefiles in doc, examples, tests, pynest
    for sdir in doc examples tests pynest; do
      SLI_CHECK_SOURCE_FILE([$srcdir/$mod/$sdir/Makefile.am],
                            [NEED_MAKEFILE_DIRS="$NEED_MAKEFILE_DIRS $mod/$sdir"], [printf ""])
    done
  fi
done

# now build include and addmodule lines for use in (py)neststartup
# this is a bit redundant with the above, but here we look only at the
# static modules.
# The variables built here go to $PKGBUILDDIR/nest/static_modules.h via
# AC_CONFIG_FILES(). They should not be put into the Makefile using
# AC_SUBST().

INCLUDE_STATIC_MODULES=''
ADD_STATIC_MODULES=''
for mod in $SLI_PUBLIC_MODULES $SLI_NONPUBLIC_MODULES; do
    modheader="$mod/${mod}module.h"
    # get module name from the module's main header
    Mod=`grep SLIModule ${srcdir}/${modheader} | sed -n 's/class *\(.*\) *:.*/\1/p' | tr -d ' '`
    # the line breaks must remain in the strings below
    SLI_CHECK_SOURCE_FILE([$srcdir/$mod/Makefile.am],
                  [INCLUDE_STATIC_MODULES="${INCLUDE_STATIC_MODULES}#include \"../${modheader}\"
"
                   ADD_STATIC_MODULES="${ADD_STATIC_MODULES}  engine.addmodule(new nest::${Mod}(net));
"],
		  [printf ""])
done

# Now instruct configure to generate $PKGBUILDDIR/nest/static_modules.h,
# including the required headers and providing the add_static_modules()
# function.
# This way of doing it is quite a hack. We cannot use AC_CONFIG_FILES(), since
# we cannot AC_SUBST() the multiline variables we create here.
AC_CONFIG_COMMANDS([nest/static_modules.h],
	           [printf "#ifndef STATIC_MODULES_H\n#define STATIC_MODULES_H\n\n$acc_include_static_modules\n#include \"interpret.h\"\n#include \"network.h\"\n\nvoid add_static_modules(SLIInterpreter& engine, nest::Network& net)\n{\n$acc_add_static_modules\n}\n#endif\n" > nest/static_modules.h],
		   [acc_include_static_modules='$INCLUDE_STATIC_MODULES'
		    acc_add_static_modules='$ADD_STATIC_MODULES'])

# directories to include in distribution tarballs
SLI_CORE_LIBS="libnestutil sli librandom lib nestkernel $SLI_PUBLIC_MODULES $SLI_PUBLIC_DYNMODULES"
SLI_DIST_SUBDIRS="$SLI_CORE_LIBS nest $PYNEST_SUBDIR"
SLI_SUBDIRS="$SLI_CORE_LIBS $SLI_EXTRA_MODULES nest"

# generate a list of absolute paths where to search for help files
for dir in $SLI_SUBDIRS; do
  HELPDIRS="$HELPDIRS $PKGSRCDIR/$dir"
  # Automatically include sli directory if it exists
  if test -d "$PKGSRCDIR/$dir/sli" ; then
    HELPDIRS="$HELPDIRS $PKGSRCDIR/$dir/sli"
  fi
done

HELPDIRS=`echo $HELPDIRS | tr " " ":"`
INSTALL_HELP_LOG="$PKGBUILDDIR/install-help.log"

PKGDATADIR=$datadir/$PACKAGE
PKGDOCDIR=$datadir/doc/$PACKAGE
TESTSUITE_BASEDIR=`eval eval echo $PKGDOCDIR`

SLI_PREFIX=$prefix

# This hack resolves '${prefix}' in the variables
eval eval eval PKGDOCDIR_AS_CONFIGURED=$PKGDOCDIR
eval eval eval PKGDATADIR_AS_CONFIGURED=$PKGDATADIR

#-- Replace these variables in *.in
AC_SUBST(HAVE_ISTREAM)
AC_SUBST(HAVE_OSTREAM)
AC_SUBST(HAVE_SSTREAM)
AC_SUBST(HAVE_STL_VECTOR_CAPACITY_BASE_UNITY)
AC_SUBST(HAVE_STL_VECTOR_CAPACITY_DOUBLING)
AC_SUBST(HAVE_ERRNO_IGNORED)
AC_SUBST(HAVE_M_E)
AC_SUBST(HAVE_M_PI)
AC_SUBST(HAVE_CMATH_MAKROS_IGNORED)
AC_SUBST(HAVE_SIGUSR_IGNORED)
AC_SUBST(HAVE_PTHREAD)
AC_SUBST(HAVE_PTHREAD_IGNORED)
AC_SUBST(HAVE_READLINE)
AC_SUBST(HAVE_VAMPIR)
AC_SUBST(VAMPIR_LIBS)
AC_SUBST(VAMPIR_CFLAGS)
AC_SUBST(IS_BLUEGENE_L)
AC_SUBST(IS_BLUEGENE_P)
AC_SUBST(IS_BLUEGENE_Q)
AC_SUBST(IS_K)
AC_SUBST(SLI_MAJOR)
AC_SUBST(SLI_MINOR)
AC_SUBST(SLI_PATCHLEVEL)
AC_SUBST(SLI_VERSION)
AC_SUBST(SLI_PRGNAME)
AC_SUBST(SLI_PREFIX)
AC_SUBST(PKGSRCDIR)
AC_SUBST(PKGBUILDDIR)
AC_SUBST(PKGDATADIR)
AC_SUBST(PKGDATADIR_AS_CONFIGURED)
AC_SUBST(PKGDOCDIR)
AC_SUBST(PKGDOCDIR_AS_CONFIGURED)
AC_SUBST(TESTSUITE_BASEDIR)
AC_SUBST(SLI_EXITCODE_ABORT)
AC_SUBST(SLI_EXITCODE_SEGFAULT)
AC_SUBST(KERNEL)
AC_SUBST(HOST)
AC_SUBST(SED)
AC_SUBST(LD)
AC_SUBST(host_os)
AC_SUBST(host_cpu)
AC_SUBST(host_vendor)
AC_SUBST(AS)
AC_SUBST(CC)
AC_SUBST(CXX)
AC_SUBST(AR)
AC_SUBST(ARFLAGS)
AC_SUBST(CXX_AR)
AC_SUBST(AM_CXXFLAGS)
AC_SUBST(AM_CFLAGS)
AC_SUBST(OPENMP_CXXFLAGS)
AC_SUBST(MAKE)
AC_SUBST(MAKE_FLAGS)
AC_SUBST(SLI_SUBDIRS)
AC_SUBST(HELPDIRS)
AC_SUBST(INSTALL_HELP_LOG)
AC_SUBST(SLI_DIST_SUBDIRS)
AC_SUBST(SLI_THREAD_DEFS)
AC_SUBST(SLI_LIBS)
AC_SUBST(MPI_LIBS)
AC_SUBST(MPI_INCLUDE)
AC_SUBST(HAVE_MUSIC)
AC_SUBST(MUSIC_LIBS)
AC_SUBST(KOMP_LIBS)
AC_SUBST(MUSIC_INCLUDE)
AC_SUBST(INCLTDL)
AC_SUBST(LIBLTDL)
AC_SUBST(HAVE_LIBLTDL)
AC_SUBST(LIBADD_DL)
AC_SUBST(LINKED_MODULES)
AC_SUBST(LINKED_USER_MODULES)
AC_SUBST(PYTHON_EXEC)
AC_SUBST(PYTHON_VERSION)
AC_SUBST(PYNEST_PREFIX)
AC_SUBST(PYNEST_SUBDIR)
AC_SUBST(HAVE_PYTHON)

AC_CONFIG_HEADER(libnestutil/config.h)
AC_CONFIG_FILES(libnestutil/sliconfig.h)
AC_CONFIG_FILES(Makefile)
AC_CONFIG_FILES(doc/Makefile)
AC_CONFIG_FILES(examples/Makefile)
AC_CONFIG_FILES(examples/nest/Makefile)
AC_CONFIG_FILES(examples/sli/Makefile)
AC_CONFIG_FILES(testsuite/Makefile)
AC_CONFIG_FILES(libnestutil/Makefile)
AC_CONFIG_FILES(librandom/Makefile)
AC_CONFIG_FILES(sli/Makefile)
AC_CONFIG_FILES(lib/Makefile)
AC_CONFIG_FILES(nestkernel/Makefile)
AC_CONFIG_FILES(nest/Makefile)
AC_CONFIG_FILES(extras/nest-config)
AC_CONFIG_FILES(testsuite/do_tests.sh)

# In the following loop, we need to have
# a case statement and list all directories
# with Makefile.am explicitly. Otherwise,
# automake does not generate Makefile.in.
for dir in $NEED_MAKEFILE_DIRS; do
  case $dir in
    models)
      AC_CONFIG_FILES(models/Makefile)
      ;;
    precise)
      AC_CONFIG_FILES(precise/Makefile)
      ;;
    conngen)
      AC_CONFIG_FILES(conngen/Makefile)
      ;;
    topology)
      AC_CONFIG_FILES(topology/Makefile)
      if test "$HAVE_PYTHON" = yes; then
        AC_CONFIG_FILES(topology/setup.py)
      fi
      ;;
    topology/doc)
      AC_CONFIG_FILES(topology/doc/Makefile)
      ;;
## INSERT CODE FOR CANDIDATES HERE ----- DO NOT EDIT THIS LINE
  esac
done

EXTRAS_SUBDIRS=

# if Python is found, configure and compile PyNEST
if test "$HAVE_PYTHON" = yes; then
 AC_CONFIG_FILES(pynest/Makefile)
 AC_CONFIG_FILES(pynest/setup.py)
 AC_CONFIG_FILES(pynest/do_tests.py)
 AC_CONFIG_FILES(extras/ConnPlotter/Makefile)
 EXTRAS_SUBDIRS="$EXTRAS_SUBDIRS ConnPlotter"
fi

AC_SUBST(EXTRAS_SUBDIRS)
AC_CONFIG_FILES(extras/Makefile)

# -------------------------------------------------------------------
# Create output
# -------------------------------------------------------------------
AC_OUTPUT


# -------------------------------------------------------------------
# Patch libtool.
# See https://bugs.launchpad.net/ubuntu/+source/kdevelop/+bug/380367
# -------------------------------------------------------------------
if grep -s -q \$echo libtool; then
  grep -s -q echo\= libtool || patch -f -p0 < $PKGSRCDIR/extras/libtool-echo-fix.patch
fi


# -------------------------------------------------------------------
# Report, after output at end of configure run Must come after
# AC_OUTPUT, so that it is displayed after libltdl has been configured
# -------------------------------------------------------------------

echo
echo "--------------------------------------------------------------------------------"
echo "NEST Configuration Summary"
echo "--------------------------------------------------------------------------------"
echo
echo "C compiler          : $CC"
echo "C compiler flags    : "`echo $AM_CFLAGS | sed 's/^[ ]*//g'`
echo "C++ compiler        : $CXX"
echo "C++ compiler flags  : "`echo $AM_CXXFLAGS | sed 's/^[ ]*//g'`
echo

if test "$HAVE_PYTHON" = yes; then
  echo "Enable PyNEST       : Yes (Python: $PYTHON_EXEC)"
else
  echo "Enable PyNEST       : No"
fi

if test x`echo $SLI_USER_MODULES | tr -d " "` != x; then
  echo "User modules        : "`echo $SLI_USER_MODULES | sed 's/^[ ]*//g'`
else
  echo "User modules        : None"
fi

if test x`echo $SLI_EXTRA_MODULES | tr -d " "` != x; then
  echo "Extra modules       : "`echo $SLI_EXTRA_MODULES | sed 's/^[ ]*//g'`
else
  echo "Extra modules       : None"
fi

SLI_UNLOADED_MODULES="$SLI_PUBLIC_DYNMODULES_NOLOAD $SLI_NONPUBLIC_DYNMODULES_NOLOAD"
if test x`echo $SLI_UNLOADED_MODULES | tr -d " "` != x; then
  echo "Dynamic modules     : "`echo $SLI_UNLOADED_MODULES | sed 's/^[ ]*//g'`
else
  echo "Dynamic modules     : None"
fi

echo
if test "$with_openmp" = yes; then
  echo "Use threading       : Yes (OpenMP)"
else
  if test "$with_pthread" = yes; then
    echo "Use threading       : Yes (POSIX threads)"
  else
    echo "Use threading       : No"
  fi
fi

echo
echo "Use GSL             :" `echo $ac_have_gsl | tr "[ny]" "[NY]"`

if test $SLI_distributed = set; then
  if test $SLI_mpi_prefix = "unset"; then
    echo "Use MPI             : Yes"
  else
    echo "Use MPI             : Yes ($SLI_mpi_prefix)"
  fi
else
  echo "Use MPI             : No"
fi

if test "x$MUSIC_LIBS" != x; then
  echo "Use MUSIC           : Yes"
  echo "  MUSIC inlude      : $MUSIC_INCLUDE"
  echo "  MUSIC libs        : $MUSIC_LIBS"
else
  echo "Use MUSIC           : No"
fi

echo
echo "--------------------------------------------------------------------------------"
echo
echo "The NEST executable will be installed to:"
printf "  "; eval eval echo "$exec_prefix/bin/"
echo
echo "Documentation and examples will be installed to:"
echo  "  ${PKGDOCDIR_AS_CONFIGURED}/"
echo
if test "$HAVE_PYTHON" != no; then
  if test "$PYNEST_PREFIX" != "NONE"; then
    echo "PyNEST will be installed to:"
    echo "  $PYNEST_PREFIX/lib/python$PYTHON_VERSION/site-packages/nest"
    echo
  fi
fi
echo "--------------------------------------------------------------------------------"
echo

if test "$SLI_scali_option" = yes; then
  echo
  echo "WARNING: We advise against using the SCALI MPI Library for the time being,"
  echo "WARNING: because of instabilities observed when simulating with 28 or more"
  echo "WARNING: processes. We recommend using OpenMPI (www.open-mpi.org) instead!"
  echo
  echo "--------------------------------------------------------------------------------"
  echo
fi

if test "$ac_have_gsl" != no; then
  if test "$ac_have_gsl_1_2" != yes ; then
    echo
    echo "Your GNU Scientific Library is earlier than GSL v 1.2."
    echo "GSL Random Number Generators will not be available."
    echo "See http://www.gnu.org/software/gsl for newer versions."
    echo
    echo "--------------------------------------------------------------------------------"
    echo
  fi
  if test "$ac_have_gsl_1_11" != yes ; then
    echo
    echo "Your GNU Scientific Library is earlier than GSL v 1.11."
    echo "The aeif_cond_alpha neuron will not be available."
    echo "See http://www.gnu.org/software/gsl for newer versions."
    echo
    echo "--------------------------------------------------------------------------------"
    echo
  fi
else
  echo
  echo "You're about to compile NEST without the GNU Scientific"
  echo "Library. Some models and random number generators will"
  echo "not be available."
  echo
  echo "--------------------------------------------------------------------------------"
  echo
fi

echo "You can now build and install NEST with"
echo "  make"
echo "  make install"
echo "  make installcheck"
echo

if test $SLI_distributed = set; then
  echo "You have configured NEST with support for distributed computing."
  echo "After running make install, please tell NEST how to start a"
  echo "distributed job on your system before running make installcheck."
  echo "You can do this by modifying the template for command /mpirun in"
  echo "~/.nestrc, which is created by the first start of NEST."
  echo
fi

echo "If you experience problems with the installation or the use of"
echo "NEST, please see http://nest-initiative.org/index.php/FAQ, or"
echo "go to http://nest-initiative.org/index.php/Community to find out"
echo "how to join the user mailing list"
echo