#----------------------------------------------------------------------------
# Autoconf input script. Start the ./autogen.sh script for producing a
# the configure script.
#----------------------------------------------------------------------------
AC_PREREQ([2.54])

#----------------------------------------------------------------------------
# Initialize autoconf.
#----------------------------------------------------------------------------
AC_INIT([cel], [0.99], [cel-main@lists.sourceforge.net])
CS_PACKAGEINFO([Crystal Entity Layer],
    [Copyright (C)2002-2005 Jorrit Tyberghein and others.],
    [http://cel.crystalspace3d.org/])
AC_CONFIG_SRCDIR([mk/jam/build.jam])
AC_CONFIG_AUX_DIR([mk/autoconf])
AC_CANONICAL_HOST

#----------------------------------------------------------------------------
# Check for tools.
#----------------------------------------------------------------------------
CS_CHECK_MKDIR
CS_EMIT_BUILD_PROPERTY([CMD.MKDIR], [$MKDIR])
CS_EMIT_BUILD_PROPERTY([CMD.MKDIRS], [$MKDIRS])

CS_PROG_CC
AS_IF([test -z "$CC"],
    [AC_MSG_ERROR([Could not find a usable C compiler.])])
CS_PROG_CXX
AS_IF([test -z "$CXX"],
    [AC_MSG_ERROR([Could not find a usable C++ compiler.])])
CS_PROG_LINK

CS_CHECK_PROGS([INSTALL], [install])
CS_EMIT_BUILD_PROPERTY([INSTALL], [$INSTALL])

# The default RANLIB in Jambase is wrong on some platforms, and is also
# unsuitable during cross-compilation, so we set the value unconditionally
# (sixth argument of CS_EMIT_BUILD_PROPERTY).
AC_PROG_RANLIB
CS_EMIT_BUILD_PROPERTY([RANLIB], [$RANLIB], [], [], [], [Y])

CS_CHECK_PROGS([DOXYGEN], [doxygen])
CS_EMIT_BUILD_PROPERTY([CMD.DOXYGEN], [$DOXYGEN])

CS_CHECK_TOOLS([DOT], [dot])
CS_EMIT_BUILD_PROPERTY([CMD.DOT], [$DOT])

CS_CHECK_PROGS([TEXI2DVI], [texi2dvi])
CS_EMIT_BUILD_PROPERTY([CMD.TEXI2DVI], [$TEXI2DVI])

CS_CHECK_PROGS([TEXI2PDF], [texi2pdf])
CS_EMIT_BUILD_PROPERTY([CMD.TEXI2PDF], [$TEXI2PDF])

CS_CHECK_PROGS([DVIPS], [dvips])
CS_EMIT_BUILD_PROPERTY([CMD.DVIPS], [$DVIPS])

CS_CHECK_PROGS([DVIPDF], [dvipdf])
CS_EMIT_BUILD_PROPERTY([CMD.DVIPDF], [$DVIPDF])

CS_CHECK_PROGS([MAKEINFO], [makeinfo])
CS_EMIT_BUILD_PROPERTY([CMD.MAKEINFO], [$MAKEINFO])

CS_CHECK_PROGS([PERL], [perl5 perl])
CS_EMIT_BUILD_PROPERTY([PERL], [$PERL])

CS_CHECK_TEMPLATE_TOOLKIT2([emit])

CS_CHECK_PROGS([SWIG], [swig])
AS_IF([test -n "$SWIG"],
    [CS_CHECK_PROG_VERSION([swig], [$SWIG -version 2>&1], [1.3.22],
	[9.9|.9], [CS_EMIT_BUILD_PROPERTY([CMD.SWIG], [$SWIG])])])

#----------------------------------------------------------------------------
# Determine system type.
#----------------------------------------------------------------------------
CS_CHECK_HOST

#------------------------------------------------------------------------------
# Check how to enable and disable compilation warnings, and how to promote
# diagnostics from warning to error status.
#------------------------------------------------------------------------------
CS_COMPILER_WARNINGS([C++], [cs_cv_prog_cxx_enable_warnings],
    [CS_EMIT_BUILD_PROPERTY([COMPILER.CFLAGS],
	[$cs_cv_prog_cxx_enable_warnings], [+])])

CS_COMPILER_IGNORE_UNUSED([C++], [cs_cv_prog_cxx_ignore_unused],
    [CS_EMIT_BUILD_PROPERTY([COMPILER.C++FLAGS.WARNING.NO_UNUSED],
	[$cs_cv_prog_cxx_ignore_unused])])

CS_COMPILER_IGNORE_UNINITIALIZED([C++], [cs_cv_prog_cxx_ignore_uninitialized],
    [CS_EMIT_BUILD_PROPERTY([COMPILER.C++FLAGS.WARNING.NO_UNINITIALIZED],
	[$cs_cv_prog_cxx_ignore_uninitialized])])

#----------------------------------------------------------------------------
# Check for external libraries.
#----------------------------------------------------------------------------
CS_CHECK_CSWIN32LIBS
CS_CHECK_PYTHON()
CS_CHECK_CPPUNIT([emit])
CS_CHECK_PTHREAD([cygwin*])

CS_CHECK_LIB_WITH([NL],
    [AC_LANG_PROGRAM([[#include <nl.h>]], [nlOpen(0,0)])],
    [], [C++],
    [CS_EMIT_BUILD_RESULT([cs_cv_libNL], [HAWKNL])], [],
    [$cs_cv_sys_pthread_cflags],
    [$cs_cv_sys_pthread_lflags],
    [$cs_cv_sys_pthread_libs])

#------------------------------------------------------------------------------
# Use the CPPFLAGS, CXXFLAGS, CFLAGS, and LDFLAGS passed to configure, as well
# as any additional flags provided by cs-win32libs.
#------------------------------------------------------------------------------
CS_EMIT_BUILD_PROPERTY([COMPILER.CFLAGS], [$CPPFLAGS $CFLAGS], [+])
CS_EMIT_BUILD_PROPERTY([COMPILER.C++FLAGS], [$CPPFLAGS $CXXFLAGS], [+])
CS_EMIT_BUILD_PROPERTY([COMPILER.LFLAGS], [$LDFLAGS], [+])

#----------------------------------------------------------------------------
# Check for Crystal Space (CS)
#----------------------------------------------------------------------------
AC_ARG_WITH([cs], [AC_HELP_STRING([--without-cs],
  [do not look for an installed Crystal Space SDK; this option is useful only
  if you need to perform routine maintenance tasks, such as generating
  project files or converting documentation, without having the Crystal Space
  SDK installed])])
AS_IF([test -z "$with_cs"], [with_cs=yes])

AS_IF([test $with_cs != no],
    [AS_IF([test $cs_cv_python != no],
	[cs_opt_libs="crystalspace_python"], [cs_opt_libs=''])
    CS_PATH_CRYSTAL_EMIT([0.99], [],
	[AC_MSG_ERROR([
*** Crystal Space could not be found or was unusable. The latest version is
*** always available from http://www.crystalspace3d.org/
*** Also, be sure that you have either installed Crystal Space or set the
*** CRYSTAL environment variable properly.
])], [crystalspace], [$cs_opt_libs])])

#----------------------------------------------------------------------------
# We checked for Python earlier, and used that result to adjust the CS test
# above, but whether we actually build CEL's Python module depends upon whether
# CS was built with Python support, which we can only know at this point (after
# running the CS test).
#----------------------------------------------------------------------------
AS_IF([test $with_cs != no && test $cs_cv_python != no],
    [AC_MSG_CHECKING([for Crystal Space Python support])
    cel_do_python=no
    for m in $CRYSTAL_AVAILABLE_LIBS; do
	AS_IF([test crystalspace_python = "$m"],
	    [cel_do_python=yes
	    break])
    done
    AC_MSG_RESULT([$cel_do_python])
    AS_IF([test $cel_do_python = yes],
	[CS_EMIT_BUILD_RESULT([cs_cv_python], [PYTHON])])])

#----------------------------------------------------------------------------
# Check if the Crystal Space static plugins lib was built.
#----------------------------------------------------------------------------
AS_IF([test $with_cs != no],
    [AC_MSG_CHECKING([for Crystal Space static plugins])
    cel_have_crystal_static=no
    for m in $CRYSTAL_AVAILABLE_LIBS; do
	AS_IF([test crystalspace_staticplugins = "$m"],
	    [cel_have_crystal_static=yes
	    break])
    done
    AC_MSG_RESULT([$cel_have_crystal_static])
    CS_EMIT_BUILD_PROPERTY([HAVE_STATIC_PLUGINS], [$cel_have_crystal_static])])

#------------------------------------------------------------------------------
# Determine if plugin meta-information should be embedded directly into plugin
# modules or if it should exist in stand-alone .csplugin files.
#------------------------------------------------------------------------------
CS_META_INFO_EMBED([emit], [no])
AS_IF([test $enable_meta_info_embedding = yes],
    [CS_EMIT_BUILD_PROPERTY([COMPILER.CFLAGS],
	[AS_ESCAPE([$(EMBED_META.CFLAGS)])], [+])
    CS_EMIT_BUILD_PROPERTY([COMPILER.LFLAGS],
	[AS_ESCAPE([$(EMBED_META.LFLAGS)])], [+])])

#----------------------------------------------------------------------------
# Package configuration switches
#----------------------------------------------------------------------------
AC_ARG_ENABLE([debug],
    [AC_HELP_STRING([--enable-debug], [build in debugging mode (default NO)])],
    [test "$enableval" = "yes" && cs_build_mode="debug"])
AC_MSG_CHECKING([build mode])
AS_VAR_SET_IF([cs_build_mode], [], [cs_build_mode=optimize])
AC_MSG_RESULT([$cs_build_mode])
CS_EMIT_BUILD_PROPERTY([MODE], [$cs_build_mode])

#-----------------------------------------------------------------------------
# Output Installpaths/package info
#-----------------------------------------------------------------------------
CS_OUTPUT_INSTALLDIRS
CS_EMIT_PACKAGEINFO

#----------------------------------------------------------------------------
# Output generated files.  Also create a Jamfile when compiling outside
# source directory.
#----------------------------------------------------------------------------
CS_JAMCONFIG_OUTPUT([Jamconfig])
AC_CONFIG_FILES([Jamfile])
AC_OUTPUT

AC_MSG_NOTICE([

Please note that this project uses Jam as its build tool,
therefore you must type 'jam' to build the project.

Jam home: http://www.perforce.com/jam/jam.html
Jam source: ftp://ftp.perforce.com/jam/
])
