dnl Process this file with autoconf to produce a configure script.
dnl AC_REVISION($Revision: 1.70 $)dnl
dnl AM_MAINTAINER_MODE
AC_INIT(source/typen.h)

dnl extracting the version number from the source code
dnl (it has to be stored there to be accessible by the windows build too)
asc_version=`cat source/strtmesg.cpp | awk -F\" '/asc_release=/{print \$2}' | sed 's/ASC//g'`

AM_CONFIG_HEADER(config.h)
AM_INIT_AUTOMAKE(asc,$asc_version)
AM_MAINTAINER_MODE

AC_DISABLE_SHARED
AC_PROG_LIBTOOL
AM_PROG_LIBTOOL


dnl *************************************************************************
dnl Checks for programs.
dnl
AC_PROG_CC
AC_PROG_CXX



gcc296test=yes
AC_ARG_ENABLE( gcc296-detection, [  --disable-gcc296-detection  Do not perform gcc 2.96 detection test .], gcc296test=no )
if test "$GCC" = yes; then
   gcc_version=`gcc --version`
   if test "X$gcc296test" = "Xyes"; then
      gcc_version296=`echo $gcc_version | grep 2.96`
      if test "X$gcc_version296" != "X"; then
         AC_MSG_ERROR([
**** GCC $gcc_version detected. This version of gcc is not an official release
**** and does produce incorrect code. You may try to build ASC, but it will
**** most likely NOT run.
**** To build ASC, we recommend GCC 3.2 ; GCC 2.95 is still supported too.
**** You can bypass this test with --disable-gcc296-detection, so you can build
**** ASC and see for yourself. And perhaps even rewrite some parts of ASC to
**** make them Redhat compatible ;) ])
      fi

      gcc_version29=`echo $gcc_version | grep 2.9`
      if test "X$gcc_version29" != "X"; then
         AC_MSG_WARN([
**** GCC $gcc_version detected. GCC 2.95 is currently still supported, but we
**** are planning to drop support for GCC 2.x in the medium term. ) ])
      fi
   fi
fi

AC_LANG_SAVE
AC_LANG_CPLUSPLUS


AC_CHECK_HEADERS( bitset,, 
                  AC_MSG_ERROR( [[
Header <bitset> not found. This is probably caused by having an outdated 
C++ compiler / standard library. Please upgrade to a compiler that is 
ISO/IEC 14882 conformant (for example gcc 3.2) ]]))

AC_LANG_RESTORE


dnl mikem: doesn't like this stuff - ugly right away
CFLAGS="$CFLAGS -D_UNIX_ -D_SDL_"

CXXFLAGS="$CXXFLAGS -Wno-sign-compare -funsigned-char -D_UNIX_ -D_SDL_"

dnl -DHEXAGON -D_NOASM_
dnl here comes the special stuff - done on demand (developers only?)
AC_ARG_ENABLE( docbook2man, [  --enable-docbook2man    will build the man pages from the sgml source files.], SGML2MAN=manpages )
AC_SUBST([SGML2MAN])
if test "X$SGML2MAN" = "Xmanpages"; then
   AC_CHECK_PROGS(DOCBOOK2MAN, docbook-to-man docbook2man )
fi
AC_SUBST(DOCBOOK2MAN)

dnl mikem: sure this is one is only interesting for martin bickel ;)
AC_ARG_ENABLE( genparse, [  --enable-genparse       enables recreation of the command line parsers], ENABLEGENPARSE=1 )
if test "X$ENABLEGENPARSE" = "X1"; then
   AC_CHECK_PROG(GENCLL,genparse,genparse)
   if test "X$GENCLL" = "Xgenparse"; then
      GENCL=clparser
   fi
   AC_SUBST([GENCL])
fi

dnl *************************************************************************
dnl Checks for libraries.
dnl

dnl should be done by SDL
dnl AC_CHECK_LIB( pthread, pthread_create )

dnl now save current FLAGS - they are modified to allow SDL test progs to
dnl be compiled correctly
SAVE_CXXFLAGS=$CXXFLAGS
SAVE_CFLAGS=$CFLAGS
SAVE_LIBS=$LIBS


dnl check for libSDL
SDL_VERSION=1.2.2
AM_PATH_SDL($SDL_VERSION,:,
            AC_MSG_ERROR([[
*** SDL version $SDL_VERSION not found! If you have a distribution supplied 
SDL.RPM installed, perhaps the SDL-devel.RPM is missing. If your distribution 
does not include the SDL library, you can get it from http://www.libsdl.org/ ]]))

dnl set up new FLAGS
CXXFLAGS="$CXXFLAGS $SDL_CFLAGS"
CFLAGS="$CFLAGS $SDL_CFLAGS"
LIBS="$LIBS $SDL_LIBS"

AC_CHECK_LIB( SDL_image, 
              IMG_LoadJPG_RW,,
	      AC_MSG_ERROR([[ 
ASC requires the SDL_image library. If you have a distribution supplied 
SDL_image.RPM installed, perhaps the SDL_image-devel.RPM is missing. 
If your distribution does not include the SDL_image library, you can get it 
from http://www.libsdl.org/projects/SDL_image/ ]]))


AC_CHECK_LIB(jpeg, jpeg_CreateDecompress,,
	      AC_MSG_ERROR([[
ASC requires the jpeg library. This library will almost certainly be supplied
with your distribution. If you have it already installed, perhaps a jpeg-devel
package is missing.
If your distribution really does not include the jpeg library, you can get it
from http://www.ijg.org/ ]]))


AC_CHECK_LIB( SDL_mixer, 
              Mix_PlayChannelTimed,, 
	      AC_MSG_ERROR( [[
ASC requires the SDL_mixer library. If you have a distribution supplied 
SDL_mixer.RPM installed, perhaps the SDL_mixer-devel.RPM is missing. 
If your distribution does not include the SDL_image library, you can get it 
from http://www.libsdl.org/projects/SDL_mixer/ ]]))



# Check for libsigc++
#AM_PATH_SIGC(1.0.0, , AC_MSG_ERROR( [[
#ASC requires the sigc++ library. If you have a distribution supplied
#sigc++.RPM installed, perhaps the sigc++-devel.RPM is missing.
#If your distribution does not include the sigc++ library, you can get it
#from http://libsigc.sourceforge.net ]]))

dnl SigC++
PKG_CHECK_MODULES(SIGC, sigc++-1.2)

AC_SUBST(SIGC_LIBS)
AC_SUBST(SIGC_CFLAGS)



dnl How do we add $SDL_FLAGS to the CFLAGS so gcc finds these headers ?
dnl
dnl AC_CHECK_HEADERS( SDL_image.h,, AC_MSG_ERROR( ASC requires the development files for the SDL_image library. You can get the complete library at www.libsdl.org ))
dnl AC_CHECK_HEADERS( SDL_mixer.h,, AC_MSG_ERROR( ASC requires the development files for the SDL_mixer library. You can get the complete library at www.libsdl.org ))


dnl ugly work around for this yet buggy SDLmm check
AC_LANG_SAVE
AC_LANG_CPLUSPLUS

dnl AM_PATH_SDLMM(0.1.8, :, 
dnl               AC_MSG_ERROR([[
dnl *** SDLmm not found! ASC requires the SDLmm library. If you have a distribution
dnl supplied SDLmm.RPM installed, perhaps the SDLmm-devel.RPM is missing. 
dnl If your distribution does not include the SDLmm library, you can get it 
dnl from http://sdlmm.sf.net ]]))


AC_ARG_ENABLE( paragui, [  --disable-paragui       Don't use paragui .], useparagui=no )



if test "x$useparagui" != "xno"; then

   AM_PATH_PARAGUI(1.0.4,
               :,
               AC_MSG_ERROR([[
   *** PARAGUI version 1.0.4 not found! ASC uses the Paragui library.
   If you have a distribution supplied Paragui.RPM installed, perhaps the
   Paragui-devel.RPM is missing. If your distribution does not include the
   Paragui library, you can get it from http://www.paragui.org/
   Alternatively, you can add "--disable-paragui" to "configure" to build
   ASC without Paragui. Some not-so-important dialogs will be missing, but
   you will be able to play ASC ]]))

   dnl echo Paragui CFLAGS are $PARAGUI_CFLAGS
else
   PARAGUI_CFLAGS="-DNO_PARAGUI"
   PARAGUI_LIBS=""
fi


AC_LANG_RESTORE

dnl restore old flags settings
CXXFLAGS=$SAVE_CXXFLAGS
CFLAGS=$SAVE_CFLAGS
LIBS=$SAVE_LIBS


dnl libbz2 stuff (should have a versionnr about 1.0.0)
lib_bz2_static=no
AC_ARG_ENABLE( libbz2-static, [  --enable-libbz2-static      compiles a static libbz2 into the binary of asc.], [lib_bz2_static=$enableval], [lib_bz2_usecache=yes] )
AC_CHECK_LIB( bz2, BZ2_bzReadOpen, :, [checklib_bz2_static=yes] )
AC_CACHE_CHECK( [for static libbz2 flag state], [ac_cv_lib_bz2_static], [ ac_cv_lib_bz2_static=$lib_bz2_static ] )

dnl ac_cv_lib_bz2_static holds cached flag state, checklib_bz2... holds check
dnl now combine them with the current flag state if needed
if test "x$lib_bz2_usecache" = "xyes"; then
  lib_bz2_static=$ac_cv_lib_bz2_static;
else
  ac_cv_lib_bz2_static=$lib_bz2_static;
fi
if test "x$checklib_bz2_static" = "xyes"; then
  lib_bz2_static=yes;
fi

AC_MSG_RESULT( [making static libbz2... $lib_bz2_static] )

if test "x$lib_bz2_static" = "xyes"; then
  LIBBZ2=bzlib
  LDADDLIBBZ2=\${top_builddir}/source/libs/bzlib/libbz2.a;
else 
  LDADDLIBBZ2=-lbz2
  AC_DEFINE_UNQUOTED( USE_SYSTEM_BZ2, 1, [link against libbzip2] )
fi


dnl *************************************************************************
dnl Checks for header files.
dnl


dnl Checks for some STL header files (which aren't available for gcc 2.95.3 ).
AC_CHECK_HEADERS( exception limits )

dnl Checks for other header files.
AC_HEADER_DIRENT
AC_HEADER_TIME

MY_EXPAND_DIR(game_datadir, "$datadir/games/$PACKAGE" )
AC_DEFINE_UNQUOTED( GAME_DATADIR, "$game_datadir",  [The directory in which the data files are going to be installed])

dnl *************************************************************************
dnl Checks for typedefs, structures, and compiler characteristics.
dnl 
dnl We use our own AC_UNP_CHECK_TYPE macro, instead of AC_CHECK_TYPE,
dnl to #include more headers.  Our macro is defined in "aclocal.m4".
dnl
AC_C_CONST
AC_TYPE_SIZE_T
AC_HEADER_TIME

dnl *************************************************************************
dnl Checks for library functions.
AC_CHECK_FUNCS(select)
AC_CHECK_FUNCS(itoa)
AC_CHECK_FUNCS(stricmp)

dnl c++ tests
AC_LANG_CPLUSPLUS

dnl GCC 2.95.3 came with a broken standard library

AC_MSG_CHECKING("broken std::string compare")
AC_TRY_COMPILE( [#include <string>], [
      std::string s = "Hello World";
      s.compare  ( 0, 0, "Hello");
],, string_compare_broken=yes)

if test "x$string_compare_broken" = x ; then
  AC_MSG_RESULT("no")
else
  CXXFLAGS="$CXXFLAGS -D_string_compare_broken_"
  AC_MSG_RESULT("yes")
fi

AC_MSG_CHECKING("broken c_str Unicode support")
AC_TRY_COMPILE( [#include <string>], [
   std::basic_string<wchar_t> test;
   test.c_str();
],, Unicode_broken=yes)

if test "x$Unicode_broken" = x ; then
  AC_MSG_RESULT("no")
else
  CXXFLAGS="$CXXFLAGS -D_UNICODE_BROKEN_"
  AC_MSG_RESULT("yes")
fi


AC_MSG_CHECKING("broken std::vector")
AC_TRY_COMPILE( [#include <vector>], [
      std::vector<int> s;
      s.push_back(1);
      s.at(0);
],, stdvector_broken=yes)

if test "x$stdvector_broken" = x ; then
  AC_MSG_RESULT("no")
else
  CXXFLAGS="$CXXFLAGS -D_vector_at_broken_"
  AC_MSG_RESULT("yes")
fi


AC_MSG_CHECKING("distinct size_t type")
AC_COMPILE_IFELSE( [
    #include <string>
   void foo( int i ) {};
   void foo( size_t i ) {};
   void foo( unsigned int i ) {};
   int main() {
     size_t t = 7;
     foo(t);
     return 0;
   }  
], size_t_type=distinct )

if test "x$size_t_type" = x ; then
  AC_MSG_RESULT("no")
else
  CXXFLAGS="$CXXFLAGS -DSIZE_T_not_identical_to_INT"
  AC_MSG_RESULT("yes")
fi


dnl shell vars set by test&feature flag for libbz2
AC_SUBST([LIBBZ2])
AC_SUBST([LDADDLIBBZ2])

dnl docbook2man
AC_SUBST(DOCBOOK2MAN)

AC_SUBST(asc_version)

dnl *************************************************************************
dnl Checking and assembling the data files

AC_MSG_CHECKING("data files")
missingdata=`./checkfordata.pl data/buildings.con data/mk1.con data/trrobj.con data/trrobj2.con data/units-mk3.con`
if test "x$missingdata" = x ; then
  AC_MSG_RESULT("yes")
else
  AC_MSG_RESULT("no")
  AC_MSG_ERROR( [ 
The following data files could not be found: $missingdata .
If you got the ASC source from CVS it is absolutely normal that this error
occurs, since many data files are not managed with CVS.

These files must be downloaded separately. This can be done automatically by
calling the ./getdata script , or you can get them manually from
http://www.asc-hq.org/download_updates.html 
] )
fi


dnl what gehts packed into main.con (the data container file) and in which
dnl order

MAINCONDATA="palette.pal data.version title.jpg title_mapeditor.jpg \
progress.* smalaril.fnt usablack.fnt arial8.fnt \
msystem.fnt gui.fnt guicol.fnt monogui.fnt sounds.asctxt \
height2.raw windp.raw windpfei.raw farbe.raw \
allianc.raw weapicon.raw expicons.raw hexinvi2.raw \
hexinvis.raw fg8.raw windrose.raw hexfld_a.raw \
hexfld.raw in_ach.raw in_h.raw build_ah.raw \
build_h.raw hexbuild.raw allianc2.raw iconship.raw \
icontank.raw iconplan.raw pfeil2.raw pfeil3.raw \
height.raw weapons.dat message1.txt waffen.raw \
knorein.raw compi2.raw pfeil-a0.raw mausi.raw \
gebasym2.raw netcontr.raw ammoprod.raw resorinf.raw \
windpowr.raw solarpwr.raw ammotran.raw research.raw \
pwrplant.raw bldinfo.raw mining.raw tabmark.raw \
traninfo.raw attack.raw hexfeld.raw mapbkgr.raw \
hex2oct.raw weapinfo.raw amatur.raw hxborder.raw \
leergui.raw guiremov.raw  *.tec \
hexmines.raw curshex2.raw markedfield.pcx curshex.raw \
railstations2.map cancel.nic poweron.nic poweroff.nic \
extload.nic consttnk.nic reacfoff.nic reacfire.nic \
loading.nic viewmap.nic dig.nic abriss.nic \
building.nic refueld.nic refuel.nic repair.nic \
buildany.nic remomine.nic moormine.nic floatmin.nic \
persmine.nic tankmine.nic putmine.nic endturn.nic \
informat.nic ascent.nic descent.nic attack.nic \
movement.nic *.nic 640480.pcx *.raw *.scr *.fnt \
*.dat *.map helpsys?.txt message?.txt *.pcx lcac2.jpg \
bkgr4.scr *.wav *.gif *.asctxt"

dnl what gehts packed into main.con (the data container file)
AC_SUBST([MAINCONDATA])

AC_MSG_CHECKING("music files")
missingmusic=`./checkfordata.pl data/music/frontiers.mp3 data/music/time_to_strike.mp3 data/music/machine_wars.mp3`
if test "x$missingmusic" = x ; then
  AC_MSG_RESULT("yes")
  MUSICDIR=music
else
  AC_MSG_RESULT("no")
fi

AC_SUBST([MUSICDIR])



AC_OUTPUT(  Makefile \
	    asc.spec \
	    doc/Makefile \
	    doc/manpages/Makefile \
	    source/Makefile \
	    source/ai/Makefile \
	    source/clparser/Makefile \
	    source/docs/Makefile \
	    source/docs/en/Makefile \
	    source/libs/Makefile \
	    source/libs/sdlmm/Makefile \
	    source/libs/sdlmm/src/Makefile \
	    source/libs/triangul/Makefile \
	    source/sdl/Makefile \
	    source/tools/Makefile \
	    source/tools/lib/Makefile \
	    source/tools/linux/Makefile \
	    source/unix/Makefile \
	    source/unix/mapeditor/Makefile \
	    source/unix/asc/Makefile \
	    data/Makefile \
	    data/dialog/Makefile \
	    data/music/Makefile  )

AC_ARG_ENABLE( debug, [  --enable-debug          enables some debugging options and output], ENABLEDEBUG=1 )
if test "X$ENABLEDEBUG" = "X1"; then
   CXXFLAGS="$CXXFLAGS -g3 -O0"
   echo SDL_CFLAGS $SDL_CFLAGS
   echo SDL_LIBS $SDL_LIBS
dnl   echo SDLMM_CFLAGS $SDLMM_CXXFLAGS
dnl   echo SDLMM_LIBS $SDLMM_LIBS
   echo PARAGUI_CFLAGS $PARAGUI_CFLAGS
   echo PARAGUI_LIBS $PARAGUI_LIBS
   echo CFLAGS $CFLAGS
   echo LIBS $LIBS
   echo LDFLAGS $LDFLAGS
   echo SIGC_CFLAGS $SIGC_CFLAGS
   echo SIGC_LIBS $SIGC_LIBS
fi
	
if test "x$missingmusic" != x ; then
  AC_MSG_WARN( [ 
The following music files could not be found: $missingmusic .
The music files are not distributed together with the source code because 
of their size. ASC can run without the music files being present, this message
is just to inform you about the existence of the music.

You can download the music files by calling the ./getmusic script and 
run configure again, which will properly install them.
Alternatively, you can download and install them manually anytime later, 
which is described in the documentation.
] )
fi


AC_MSG_NOTICE( [ configure completed successfully. Now run "make" do build ASC.
If the build process fails for any reason, take a look at
http://asc-hq.sourceforge.net/browse.php?topic=linux
] )

