
AC_REVISION(norev)
AC_PREREQ(2.53)
AC_INIT(Makefile.config.in)
AC_CONFIG_HEADER(config.h)

CONFIGURE_RUN=yes
CONFIGURE_ARGUMENTS=$*
echo "Arguments to configure: $CONFIGURE_ARGUMENTS"

MAJOR_VERSION=2
MINOR_VERSION=7
SUB_VERSION=1   # range 0-7 due to eMule limitations

AC_CANONICAL_HOST

OS_FILES=unix
OS_FILES2=unix
SYSTEM=unknown
case $host in
  *mingw*)
    SYSTEM=mingw
    OS_FILES=mingw
    OS_FILES2=mingw
    PTHREAD_LIBS="-lpthreadGC2 -lwsock32"
    PTHREAD_CFLAGS="-DPTW32_STATIC_LIB"
    ;;
  *cygwin*)
    SYSTEM=cygwin
    OS_FILES2=cygwin
    ;;
  *freebsd*)
    SYSTEM=freebsd
    CPPFLAGS="${CPPFLAGS} -I/usr/local/include"
    LDFLAGS="${LDFLAGS} -L/usr/local/lib"
    ;;
  *openbsd*)
    SYSTEM=openbsd
    CPPFLAGS="${CPPFLAGS} -I/usr/local/include"
    LDFLAGS="${LDFLAGS} -L/usr/local/lib"
    ;;
  *netbsd*)
    SYSTEM=netbsd
    ;;
  *linux*)
    SYSTEM=linux
    ;;
  *solaris*)
    SYSTEM=solaris
    ;;
  *morphos*|*amigaos*)
    SYSTEM=morphos
    ;;
  *aix*)
    SYSTEM=aix
    ;;
  *beos*)
    SYSTEM=beos
    ;;
  *osf*)
    SYSTEM=osf
    ;;
  *irix*)
    SYSTEM=irix
    ;;
  *hppa*|*hpux*)
    SYSTEM=hpux
    ;;
  *darwin*|*rhapsody*|*macosx*)
    SYSTEM=macosx
    ;;
esac
if test "x$SYSTEM" = "xunknown"; then
  AC_MSG_FAILURE([Unknown build system, please notify MLdonkey developer team])
fi

if test -d .svn; then
  AC_MSG_NOTICE(checking SVN revision)
  AC_CHECK_PROG(SVNVERSION,svnversion,svnversion)
  if test "$ac_cv_prog_SVNVERSION" = "svnversion"; then
    SUB_VERSION3="SVN"
    SCM_VERSION=`svnversion -n .`
  else AC_MSG_NOTICE(cannot checking SVN revision... no SVNVERSION)
       SCM_VERSION="cannot find out SVN revision (no SVNVERSION)"  
  fi     
fi

if test -f ./CVS/Entries ; then
  AC_MSG_NOTICE(checking CVS checkout date)
  AC_CHECK_PROG(STAT, stat, stat)
  if test "$ac_cv_prog_STAT" = "stat"; then
    AC_CHECK_PROG(SED, sed, sed)
    if test "$ac_cv_prog_SED" = "sed"; then
      AC_CHECK_PROG(CUT, cut, cut)
      if test "$ac_cv_prog_CUT" = "cut"; then
        SUB_VERSION3="CVS"
	if [ test "$SYSTEM" = "freebsd"] || [ test "$SYSTEM" = "darwin"]; then
	  SCM_VERSION=`$STAT -f "%Sm" ./CVS/Entries | $SED -e 's/\(.*\) \(.*\) \(.*\) \(.*\)/\4-\1-\2 \3/'`
	else
	  if [ test "$SYSTEM" = "openbsd"]; then
	    AC_CHECK_PROG(GREP, grep, grep)
	    if test "$ac_cv_prog_GREP" = "grep"; then
	      SCM_VERSION=`$STAT -f %a ./CVS/Entries | $GREP -v Entries | $SED -e 's/\(.*\) \(.*\) \(.*\) \(.*\)/\1-\2-\4 \3/'`
	    fi
	  else
	    if [ test "$SYSTEM" = "mingw"] || [ test "$SYSTEM" = "cygwin"]; then
	      AC_CHECK_PROG(GREP, grep, grep)
	      if test "$ac_cv_prog_GREP" = "grep"; then
	        SCM_VERSION=`$STAT ./CVS/Entries  | $GREP Modify | $SED -e 's/\(.*\) \(.*\) \(.*\) \(.*\) \(.*\)/\5-\2-\3 \4/'`
	      fi
	    else
	      SCM_VERSION=`$STAT -Lc %y ./CVS/Entries | $SED -e 's#\.[0-9]*##' | $CUT -c0-19`
	    fi
	  fi
	fi
      else
        AC_MSG_NOTICE(cannot checking CVS checkout date... no CUT)
        SCM_VERSION="cannot find out SCM Version (no CUT)"
      fi
    else
      AC_MSG_NOTICE(cannot checking CVS checkout date... no SED)
      SCM_VERSION="cannot find out SCM Version (no SED)"
    fi
  else
    AC_MSG_NOTICE(cannot checking CVS checkout date... no STAT)
    SCM_VERSION="cannot find out SCM Version (no STAT)"
  fi
fi
MLDONKEY_VERSION=$MAJOR_VERSION.$MINOR_VERSION.$SUB_VERSION
if test -f ./subrelease; then
  MLDONKEY_VERSION=$MLDONKEY_VERSION`cat ./subrelease`
fi
if test ! -z "$SUB_VERSION3"; then
  MLDONKEY_VERSION=$MLDONKEY_VERSION.$SUB_VERSION3
fi
REQUIRED_OCAML=3.08.4
DOWNLOAD_OCAML_MAJOR=3.08
DOWNLOAD_OCAML=3.08.4

REQUIRED_LABLGTK=1.2.7

touch install-sh

LABLGTK_NAME=lablgtk

OPEN_NAPSTER=no
GNUTELLA=yes
GNUTELLA2=yes
DIRECT_CONNECT=no
SOULSEEK=no
OPENFT=no
DONKEY=yes
BITTORRENT=yes
FASTTRACK=yes
FILETP=yes
MULTINET=yes
AC_ARG_ENABLE(multinet,      [  --disable-multinet      allows you to only compile support for edonkey + Overnet], [MULTINET="$enableval"])
if test ! "$MULTINET" = "yes"; then
  OPEN_NAPSTER=no
  GNUTELLA=no
  GNUTELLA2=no
  DIRECT_CONNECT=no
  SOULSEEK=no
  OPENFT=no
  FASTTRACK=no
  FILETP=no
  BITTORRENT=no
  RELEASENAME="$RELEASENAME-nomultinet"
fi
AC_ARG_ENABLE(donkey,        [  --disable-donkey        allows you to remove support for eDonkey], [DONKEY="$enableval"])
AC_ARG_ENABLE(bittorrent,    [  --disable-bittorrent    allows you to remove support for Bittorent], [BITTORRENT="$enableval"])
AC_ARG_ENABLE(filetp,        [  --disable-filetp        allows you to remove support for fileTP], [FILETP="$enableval"])
AC_ARG_ENABLE(gnutella, [  --disable-gnutella      allows you to remove support for Gnutella], [GNUTELLA="$enableval"])
AC_ARG_ENABLE(gnutella2, [  --disable-gnutella2     allows you to remove support for Gnutella2], [GNUTELLA2="$enableval"])
#AC_ARG_ENABLE(soulseek, [  --disable-soulseek      allows you to remove support for SoulSeek], [SOULSEEK="$enableval"])
AC_ARG_ENABLE(fasttrack,     [  --disable-fasttrack     allows you to remove support for FastTrack], [FASTTRACK="$enableval"])
#AC_ARG_ENABLE(opennap,       [  --disable-opennap       allows you to remove support for Open Napster], [OPEN_NAPSTER="$enableval"])
#AC_ARG_ENABLE(directconnect, [  --disable-directconnect  allows you to remove support for Direct Connect], [DIRECT_CONNECT="$enableval"])
#AC_ARG_ENABLE(openft,        [  --disable-openft        allows you to remove support for OpenFT], [OPENFT="$enableval"])

BATCH=no
AC_ARG_ENABLE(batch, [  --enable-batch          reply YES to all queries in this script], [BATCH="$enableval"])

AC_ARG_ENABLE(ocamlver, [  --enable-ocamlver=VER   allows you to change the required version of ocaml to VER], [REQUIRED_OCAML="$enableval"])

FORCE_MINGW=no
AC_ARG_ENABLE(mingw, [  --enable-mingw          forces compilation with MINGW on Cygwin], [FORCE_MINGW="$enableval"])

AC_ARG_ENABLE(checks, [  --enable-checks         force mldonkey to perform bound checks on array/string access], [CHECKBOUNDS="$enableval"])
if test "$CHECKBOUNDS" = "yes"; then
  CHECKBOUNDS=true
else
  CHECKBOUNDS=false
fi

PROFILE=no
AC_ARG_ENABLE(profile, [  --enable-profile        allows you to profile mlnet using gprof], [PROFILE="$enableval"])

DEBUG=no
AC_ARG_ENABLE(debug, [  --enable-debug          allows you to compile mlnet with debug symbols], [DEBUG="$enableval"])

GUI=no
AC_ARG_ENABLE(gui,           [  --disable-gui           allows you to disable GUI build (default)], [GUI="$enableval"])
AC_ARG_ENABLE(gui,           [  --enable-gui=oldgui|newgui1|newgui2
                          allows you to choose a GUI (default: newgui2 - is a GTK2 GUI, other GUIs use GTK1)], [GUI="$enableval"])
if test "$GUI" = "yes"; then
  GUI=newgui2
fi
if test ! "$GUI" = "newgui2"; then
  if test ! "$GUI" = "newgui1"; then
    if test ! "$GUI" = "oldgui"; then
      GUI=no
    fi
  fi
fi

USE_PTHREAD=yes
AC_ARG_ENABLE(pthread, [  --disable-pthread       allows you to disable pthread support in mldonkey], [USE_PTHREAD="$enableval"])
AC_ARG_ENABLE(pthread-lib, [  --enable-pthread-lib    legacy option], [USE_PTHREAD="$enableval"])

ICONV=yes
AC_ARG_ENABLE(iconv,       [  --disable-iconv         disable the use of iconv for compiling on broken windows environments], [ICONV="$enableval"])

GD=yes
AC_ARG_ENABLE(gd,       [  --disable-gd            disable the use of gd], [GD="$enableval"])

USE_BZIP2=yes
AC_ARG_ENABLE(bzip2,       [  --disable-bzip2         disable the use of bzip2], [USE_BZIP2="$enableval"])

ifelse(AC_ACVERSION, [2.13], [],
[dnl Large file enabled
   AC_SYS_LARGEFILE

])

IM=yes

AC_CHECK_PROG(CC,gcc3,"gcc3",cc)
AC_PROG_CC
AC_EXEEXT
AC_PROG_CPP
AC_PROG_CXX

AC_CHECK_PROG(NEWCXX, [$CXX], [$CXX], [no])
if test x"$NEWCXX" = x"no"
then
  AC_MSG_ERROR("No valid c++ compiler found")
else
  CXX=$NEWCXX
fi

if test "$FORCE_MINGW" = "yes"; then
    CC="$CC -mno-cygwin"
    CPP="$CPP -mno-cygwin"
    OS_FILES=mingw
    OS_FILES2=mingw
    SYSTEM=mingw
fi
AC_PROG_RANLIB

OCAML_PATH=

AC_CHECK_PROG(GNU_MAKE, gmake, gmake)
AC_CHECK_PROG(GNU_MAKE, make, make)

if test "x$GNU_MAKE" != "x"; then
    AC_MSG_CHECKING( [if $GNU_MAKE is GNU make] )
    $GNU_MAKE --version > /dev/null 2>&1
    if test "$?" != "0"; then
        AC_MSG_RESULT([no])
	AC_MSG_ERROR(GNU make not found)
    else
        AC_MSG_RESULT([yes])
    fi
else
    AC_MSG_ERROR(GNU make not found)
fi

cd ..
SOURCE_DIR=`pwd`
cd config

CONFIG_DIR=$SOURCE_DIR/config
PATCH_DIR=$SOURCE_DIR/patches
LOCAL_DIR=$PATCH_DIR/local
BUILD_DIR=$PATCH_DIR/build

AC_ARG_ENABLE(local-prefix, [  --enable-local-prefix=DIR  allows you to specify where you want temporary tools to be installed (DIR must be absolute)], [LOCAL_DIR="$enableval"])

LOCAL=$LOCAL_DIR
LOCAL_OCAML=$LOCAL_DIR/bin

AC_PATH_PROG(OCAMLC,ocamlc.opt,"",[$LOCAL_DIR/bin:$PATH])
AC_CHECK_PROG(OCAMLC,ocamlc,ocamlrun ocamlc)

case "$host" in
  alpha*-*-osf*)                arch=alpha; system=digital;;
  alpha*-*-linux*)              arch=alpha; system=linux;;
  alpha*-*-freebsd*)            arch=alpha; system=freebsd;;
  alpha*-*-netbsd*)             arch=alpha; system=netbsd;;
  alpha*-*-openbsd*)            arch=alpha; system=openbsd;;
  sparc-*-sunos4.*)             arch=sparc; system=sunos;;
  sparc-*-solaris2.*)           arch=sparc; system=solaris;;
  sparc-*-*bsd*)                arch=sparc; system=bsd;;
  sparc-*-linux*)               arch=sparc; system=linux;;
  i?86-*-linux*)           arch=i386; system=linux;;
  i386-*-*bsd*)            arch=i386; system=bsd;;
  i486-*-*bsd*)            arch=i486; system=bsd;;
  i586-*-*bsd*)            arch=i586; system=bsd;;
  i686-*-*bsd*)            arch=i686; system=bsd;;
  i?86-*-nextstep*)        arch=i386; system=nextstep;;
  i?86-*-solaris*)         arch=i386; system=solaris;;
  i?86-*-beos*)            arch=i386; system=beos;;
  i?86-*-cygwin*)          arch=i386; system=cygwin;;
  mips-*-irix6*)                arch=mips; system=irix;;
  hppa1.1-*-hpux*)              arch=hppa; system=hpux;;
  hppa1.1-*-nextstep*)          arch=hppa; system=nextstep;;
  rs6000-*-aix*)                arch=power; model=rs6000; system=aix;;
  powerpc-*-aix*)               arch=power; model=ppc; system=aix;;
  powerpc-*-linux*)             arch=power; model=ppc; system=elf;;
  powerpc-*-rhapsody*)          arch=power; model=ppc; system=rhapsody;;
  powerpc-*-darwin*)            arch=power; model=ppc; system=rhapsody;;
  arm*-*-linux*)                arch=arm; system=linux;;
  ia64-*-linux*)                arch=ia64; system=linux;;
esac


BUILD_OCAML=no
if test -z "$ac_cv_prog_OCAMLC"; then
   BUILD_OCAML=yes
else
  OCAMLVERSION=`$OCAMLC -v | sed -n -e 's|.*version* *\(.*\)$|\1|p' `
  case "$OCAMLVERSION" in
    "$REQUIRED_OCAML"*) ;;
    3.08.3*) ;;
    3.08.2*)
        if test "$system" = "rhapsody"; then
            case "$OCAMLVERSION" in
                3.08.2+4* | 3.08.2+5* | 3.08.2+6* | 3.08.2+7* | 3.08.2+8*) ;;
                *)
                    echo "Need build"
                    BUILD_OCAML=yes
                    ;;
            esac
        fi
        ;;
    *)
        echo "Need build"
        BUILD_OCAML=yes
        ;;
  esac
fi

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

  AC_CHECK_FILE("$LOCAL_OCAML"/ocamlc.opt, [
    OCAMLVERSION=`$LOCAL_OCAML/ocamlc.opt -v | sed -n -e 's|.*version* *\(.*\)$|\1|p' `
    REBUILD_OCAML=no
  case "$OCAMLVERSION" in
    "$REQUIRED_OCAML"*) ;;
    3.08.2*)
        if test "$system" = "rhapsody"; then
            case "$OCAMLVERSION" in
                3.08.2+4* | 3.08.2+5* | 3.08.2+6* | 3.08.2+7* | 3.08.2+8*) ;;
                *)
                    REBUILD_OCAML=yes
                    ;;
            esac
        fi
        ;;
    *)
        REBUILD_OCAML=yes
        ;;
  esac
    if test "$REBUILD_OCAML" = "no"; then
        echo "Need rebuild"
        BUILD_OCAML=no
        OCAMLC=$LOCAL_OCAML/ocamlc.opt
        ac_cv_prog_OCAMLC=$LOCAL_OCAML/ocamlc.opt
    fi
  ])
fi

AC_CHECK_PROG(RPMBUILD,rpmbuild,rpmbuild)
AC_CHECK_PROG(RPMBUILD,rpm,rpm)
AC_CHECK_PROG(WGET, wget, wget)
if test -z "$ac_cv_prog_WGET"; then
  $CC -o wget wget.c || echo "Cannot compile wget.c"
  AC_PATH_PROG(WGET, wget,,$SOURCE_DIR/config)
  ac_cv_prog_WGET="$ac_cv_path_WGET"
fi
WGET="$ac_cv_prog_WGET"

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

  echo "********  Objective-Caml $REQUIRED_OCAML is required  *********" 1>&2;
  if test "$WGET" = ""; then
    echo "********          wget is missing          *********" 1>&2;
    echo "********       cannot download Ocaml       *********" 1>&2;
    exit 1
  fi
  echo "*******  Check http://caml.inria.fr/  ********" 1>&2;
  echo "Do you want this script to try to download and install ocaml"
  echo "LOCALLY in mldonkey directory ?"
  if test "$BATCH" = "no"; then read i; else i=yes; fi
  case "$i" in
   y* | Y*)
      cd $PATCH_DIR
      if test ! -f ocaml-"$DOWNLOAD_OCAML".tar.gz; then
        echo Downloading ...
        $WGET http://caml.inria.fr/pub/distrib/ocaml-"$DOWNLOAD_OCAML_MAJOR"/ocaml-"$DOWNLOAD_OCAML".tar.gz
        # $WGET http://savannah.nongnu.org/download/mldonkey/tools/ocaml-"$REQUIRED_OCAML".tar.gz
        # $WGET http://pauillac.inria.fr/~lefessan/mldonkey/ocaml-"$REQUIRED_OCAML".tar.gz
      fi
      if test ! -f ocaml-"$DOWNLOAD_OCAML".tar.gz; then exit 1; fi

      echo Uncompressing ...
      mkdir -p $BUILD_DIR
      cd $BUILD_DIR
      rm -rf ocaml-"$REQUIRED_OCAML"
      gzip -cd $PATCH_DIR/ocaml-"$DOWNLOAD_OCAML".tar.gz | tar vxf -
      cd ocaml-"$DOWNLOAD_OCAML"
      if test -f $PATCH_DIR/ocaml-"$REQUIRED_OCAML".patch; then
        echo Patching ...
        patch -p1 < $PATCH_DIR/ocaml-"$REQUIRED_OCAML".patch
      fi
      echo Configuring ...
      ./configure -prefix $LOCAL_DIR
      cd config
      cp -f Makefile Makefile.old
      sed "s/OTHERLIBRARIES=.*/OTHERLIBRARIES=unix dynlink num str bigarray threads/" Makefile.old > Makefile
      cd ..
      cp -f $PATCH_DIR/Makefile.ocamldoc ocamldoc/Makefile
      echo Compiling ...
      $GNU_MAKE world opt opt.opt
      echo Installing ...
      $GNU_MAKE install
      if ! test -f $LOCAL_DIR/lib/ocaml/threads; then
        if ! test -f $LOCAL_DIR/lib/ocaml/vmthreads; then
          ln -s vmthreads  $LOCAL_DIR/lib/ocaml/threads
        fi
      fi
      cd $BUILD_DIR
      rm -rf ocaml-"$REQUIRED_OCAML"
      cd $CONFIG_DIR
      echo Ocaml locally installed for mldonkey

      if test -f "$LOCAL_OCAML"/ocamlc.opt; then
         OCAMLC=$LOCAL_OCAML/ocamlc.opt
         ac_cv_prog_OCAMLC=$LOCAL_OCAML/ocamlc.opt
      else
      if test -f "$LOCAL_OCAML"/ocamlc; then
         OCAMLC=$LOCAL_OCAML/ocamlc
         ac_cv_prog_OCAMLC=$LOCAL_OCAML/ocamlc
      else
        echo "Ocaml installation failed"; exit 1
      fi
      fi
      ;;
   *)  exit 1;;
  esac
fi

if test "$OCAMLC" = "$LOCAL_OCAML"/ocamlc.opt ; then
  OCAML_PATH="$LOCAL_OCAML"/
  OCAMLOPT=$LOCAL_OCAML/ocamlopt.opt
  OCAMLMKTOP=$LOCAL_OCAML/ocamlmktop
  OCAMLDEP=$LOCAL_OCAML/ocamldep
  OCAMLLEX=$LOCAL_OCAML/ocamllex.opt
  OCAMLYACC=$LOCAL_OCAML/ocamlyacc
  CAMLP4=$LOCAL_OCAML/camlp4
else
  if test "$OCAMLC" = "$LOCAL_OCAML"/ocamlc ; then
  OCAML_PATH="$LOCAL_OCAML"/
  if test -f "$LOCAL_OCAML/ocamlopt"; then
    OCAMLOPT=$LOCAL_OCAML/ocamlopt
  else
    OCAMLOPT=no
  fi
  OCAMLMKTOP=$LOCAL_OCAML/ocamlmktop
  OCAMLDEP=$LOCAL_OCAML/ocamldep
  OCAMLLEX=$LOCAL_OCAML/ocamllex
  OCAMLYACC=$LOCAL_OCAML/ocamlyacc
  CAMLP4=$LOCAL_OCAML/camlp4
  else
  AC_CHECK_PROG(OCAMLOPT, ocamlopt.opt, ocamlopt.opt)
  AC_CHECK_PROG(OCAMLOPT, ocamlopt, ocamlopt, no)
  AC_CHECK_PROG(OCAMLDEP, ocamldep, ocamldep)
  AC_CHECK_PROG(OCAMLLEX, ocamllex.opt, ocamllex.opt)
  AC_CHECK_PROG(OCAMLLEX, ocamllex, ocamllex)
  AC_CHECK_PROG(OCAMLYACC, ocamlyacc, ocamlyacc)
  AC_CHECK_PROG(OCAMLMKTOP, ocamlmktop, ocamlmktop)
  AC_CHECK_PROG(CAMLP4, camlp4, camlp4)
fi
fi



AC_PATH_PROG(PERL, perl, perl)

OCAMLVERSION=`$OCAMLC -v | sed -n -e 's|.*version* *\(.*\)$|\1|p' `
OCAMLLIB=`$OCAMLC -where`
REBUILD_OCAML=no
case "$OCAMLVERSION" in
    "$REQUIRED_OCAML"*) ;;
    3.08.3*) ;;
    3.08.2*)
        if test "$system" = "rhapsody"; then
            case "$OCAMLVERSION" in
                3.08.2+4* | 3.08.2+5* | 3.08.2+6* | 3.08.2+7* | 3.08.2+8*) ;;
                *)
                    REBUILD_OCAML=yes
                    ;;
            esac
        fi
        ;;
    *)
        REBUILD_OCAML=yes
        ;;
  esac
    if test "$REBUILD_OCAML" = "yes"; then
      echo "********  Version $REQUIRED_OCAML of Objective-Caml is required  *********" 1>&2;
      echo "*******  Check http://caml.inria.fr/  ********" 1>&2;
      exit 1;
    fi


if test "$OCAMLOPT" = "no"; then
  TARGET_TYPE=byte
  OCAMLLIB_EXT=cma
else
  TARGET_TYPE=opt
  OCAMLLIB_EXT=cmxa

  OCAMLOPTVERSION=`$OCAMLOPT -v | sed -n -e 's|.*version* *\(.*\)$|\1|p' `

  test "$OCAMLOPTVERSION" = "$OCAMLVERSION" || {
    echo "********  Version $REQUIRED_OCAML of ocamlopt is required  *********" 1>&2;
    echo "*******  Check http://caml.inria.fr/  ********" 1>&2;
    exit 1; }

fi

if test "$PROFILE" = "yes"; then
   OCAMLOPT="$OCAMLOPT -p"
   CFLAGS="$CFLAGS -pg"
fi
if test "$FORCE_MINGW" = "yes"; then
    OCAMLC="$OCAMLC -ccopt -mno-cygwin"
    OCAMLOPT="$OCAMLOPT -ccopt -mno-cygwin"
    OCAMLMKTOP="$OCAMLMKTOP -ccopt -mno-cygwin"
fi
if test "$DEBUG" = "yes"; then
    OCAMLC="$OCAMLC -g"
fi

# Sizes of various common basic types
AC_CHECK_SIZEOF(int)
AC_CHECK_SIZEOF(long)
AC_CHECK_SIZEOF(void *)
AC_CHECK_SIZEOF(char)
AC_CHECK_SIZEOF(short)
AC_CHECK_SIZEOF(float)
AC_CHECK_SIZEOF(double)

# various header files
AC_CHECK_HEADERS([sys/utsname.h])
AC_CHECK_HEADERS(arpa/inet.h,,)
AC_CHECK_HEADERS([sys/types.h netinet/in_systm.h netinet/in.h netinet/ip.h],,,
[#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_NETINET_IN_SYSTM_H
#include <netinet/in_systm.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
])

# poll if possible
AC_CHECK_HEADERS(sys/poll.h,,)
AC_CHECK_FUNCS(poll,,)

AC_CHECK_HEADERS(sys/vfs.h,,)
AC_CHECK_HEADERS([sys/param.h sys/mount.h],,,
[#if HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
])

AC_CHECK_LIB(z,inflate,,[AC_MSG_FAILURE([Zlib missing, did you install zlib and zlib-developer packages?])])
AC_CHECK_HEADERS(zlib.h,,[AC_MSG_FAILURE([Zlib missing, did you install zlib and zlib-developer packages?])])
AC_CHECK_LIB(z,zlibVersion,[AC_DEFINE(HAVE_ZLIBVERSION, 1,)])

BZIP2=no
HAS_BZIP2=false
if test "$USE_BZIP2" != "no"; then
  AC_CHECK_HEADERS(bzlib.h,[AC_CHECK_LIB(bz2,BZ2_bzReadOpen,[BZIP2=yes],)])
fi

if test "$BZIP2" = "yes"; then
  HAS_BZIP2=true
  cp -f ../src/utils/lib/misc2_bz2.ml ../src/utils/lib/misc2.ml
  cp -f ../src/utils/cdk/tar_bz2.ml ../src/utils/cdk/tar.ml
  cp -f ../src/utils/cdk/tar_bz2.mli ../src/utils/cdk/tar.mli
  AC_CHECK_LIB(bz2,BZ2_bzlibVersion,[AC_DEFINE(HAVE_BZLIBVERSION, 1,)])
  AC_DEFINE(USE_BZIP2, 1, [Define to 1 if you have bzip2 support.])
else
  AC_MSG_WARN(Your system lacks bzip2 support)
  cp -f ../src/utils/lib/misc2_no_bz2.ml ../src/utils/lib/misc2.ml
  cp -f ../src/utils/cdk/tar_no_bz2.ml ../src/utils/cdk/tar.ml
  cp -f ../src/utils/cdk/tar_no_bz2.mli ../src/utils/cdk/tar.mli
fi

AC_CHECK_HEADERS(byteswap.h,,)

if test "$ICONV" = "yes"; then
  AM_ICONV
  if test "$am_cv_func_iconv" != "yes"; then
    AC_MSG_FAILURE([iconv() is missing, did you install the libraries and the developer packages for libiconv?])
  fi
  LIBS="$LIBS $LIBICONV"
  AC_CHECK_FUNCS([locale_charset],,
  AC_TRY_LINK([#include <langinfo.h>],
  [char* cs = nl_langinfo(CODESET);],
  AC_MSG_NOTICE(found nl_langinfo(CODESET)),
  [AC_MSG_FAILURE(Your iconv implementation is incomplete)]))
  AC_CHECK_LIB(charset,locale_charset)
  AC_CHECK_HEADERS([libcharset.h])
  AC_CHECK_HEADERS([locale.h])
else
  AC_DEFINE(DISABLE_ICONV, 1, [Define to 1 if you want to replace iconv and related with stubs.])
fi

# On linux plaforms, we will have to check that includes from kernel are
# available. glibc version test was shamelessly taken from
# http://public.activestate.com/gsar/APC/perl-5.8.x/Configure
GLIBC_VERSION=""
case $host in
        *linux*)
           AC_CHECK_HEADERS(linux/limits.h,, [
             OLD_CPPFLAGS=$CPPFLAGS
             CPPFLAGS="-I /usr/src/linux/include"
             AC_CHECK_HEADER(linux/types.h,[CONFIG_INCLUDES="-I /usr/src/linux/include"])
             CPPFLAGS=$OLD_CPPFLAGS]
           )
	   AC_CHECK_HEADERS(gnu/libc-version.h,,)
           AC_MSG_CHECKING(for GNU C Library version)

cat >try.c <<'EOCP'
/* Find out version of GNU C library.  __GLIBC__ and __GLIBC_MINOR__
   alone are insufficient to distinguish different versions, such as
   2.0.6 and 2.0.7.  The function gnu_get_libc_version() appeared in
   libc version 2.1.0.      A. Dougherty,  June 3, 2002.
*/
#include <stdio.h>
int main(void)
{
#ifdef __GLIBC__
#   ifdef __GLIBC_MINOR__
#       if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1
#           include <gnu/libc-version.h>
    printf("%s\n",  gnu_get_libc_version());
#       else
    printf("%d.%d\n",  __GLIBC__, __GLIBC_MINOR__);
#       endif
#   else
printf("%d\n",  __GLIBC__);
#   endif
    return 0;
#else
    return 1;
#endif
}
EOCP

           $CC -o try ./try.c
           GLIBC_VERSION="`./try`"
           AC_MSG_RESULT($GLIBC_VERSION)
           rm -f try try.*
        ;;
        *)         ;;
esac

AC_MSG_CHECKING(Additionnal includes)
AC_MSG_RESULT($CONFIG_INCLUDES)

OCAMLLIB=`$OCAMLC -where`

if test -f $OCAMLLIB/lablgl.cma; then
  LABLGL_CMA=lablgl.cma
fi

if test -f $OCAMLLIB/lablgl.$OCAMLLIB_EXT; then
  LABLGL_CMXA=lablgl.$OCAMLLIB_EXT
fi
AC_MSG_NOTICE(checking for activated networks)
  if test "$DONKEY" = "yes"; then
    AC_CHECK_FILE(../src/networks/donkey,DONKEY=yes, [
      DONKEY=no
    ])
  fi

  if test "$OPEN_NAPSTER" = "yes" ; then
    AC_CHECK_FILE(../src/networks/opennap,OPEN_NAPSTER=yes, [
      OPEN_NAPSTER=no
    ])
  fi

#  if test "$DIRECT_CONNECT" = "yes" ; then
#    AC_CHECK_FILE(../src/networks/direct_connect,DIRECT_CONNECT=yes, [
      DIRECT_CONNECT=no
#    ])
#  fi

  if test "$GNUTELLA" = "yes"; then
       AC_CHECK_FILE(../src/networks/gnutella,GNUTELLA=yes, [
         GNUTELLA=no
       ])
  fi

  if test "$GNUTELLA2" = "yes"; then
       AC_CHECK_FILE(../src/networks/gnutella2,GNUTELLA2=yes, [
         GNUTELLA2=no
       ])
  fi

  if test "$BITTORRENT" = "yes"; then
    AC_CHECK_FILE(../src/networks/bittorrent,BITTORRENT=yes, [
      BITTORRENT=no
    ])
  fi

  if test "$FILETP" = "yes"; then
    AC_CHECK_FILE(../src/networks/fileTP,FILETP=yes, [
      FILETP=no
    ])
  fi

  if test "$SOULSEEK" = "yes"; then
       AC_CHECK_FILE(../src/networks/soulseek,SOULSEEK=yes, [
         SOULSEEK=no
       ])
  fi

  if test "$OPENFT" = "yes" ; then
    AC_CHECK_FILE(../src/networks/openFT,OPENFT=yes, [
      OPENFT=no
    ])
  fi

  if test "$FASTTRACK" = "yes" ; then
    AC_CHECK_FILE(../src/networks/fasttrack,FASTTRACK=yes, [
      FASTTRACK=no
    ])
  fi

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

  AC_CHECK_FILE(../src/networks/donkey,[
    DONKEY=yes
    AC_CHECK_FILE(../src/networks/server,DONKEY_SERVER=yes, [
      DONKEY_SERVER=no
    ])
  ], [
      DONKEY=no
      DONKEY_SERVER=no
  ])
fi

if test "$USE_PTHREAD" = "no"; then
 PTHREAD_LIBS=""
 echo "Dont use threads"
else
  ACX_PTHREAD
fi

# Check for GTK only if we want to build the GUI
if test ! "$GUI" = "no"; then
  echo "---------------------------------------------------------"
  echo "Checking the libraries required to build the GTK GUI."
  echo "---------------------------------------------------------"
  if test "$GUI" = "newgui2"; then
      REQUIRED_GTK=2.4.0
      PKG_GTK=gtk+-2.0
      AC_MSG_CHECKING(for GTK+ - version >= $REQUIRED_GTK)
      if pkg-config --atleast-version $REQUIRED_GTK $PKG_GTK; then
        AC_MSG_RESULT(yes)
        REQUIRED_RSVG=2.4.0
        PKG_RSVG=librsvg-2.0
        AC_MSG_CHECKING(for librsvg - version >= $REQUIRED_RSVG)
        if pkg-config --atleast-version $REQUIRED_RSVG $PKG_RSVG; then
          GTK_CONFIG="pkg-config $PKG_GTK $PKG_RSVG"
          REQUIRED_LABLGTK=2.6.0
          LABLGTK_NAME=lablgtk2
          AC_MSG_RESULT(yes)
        else
          GTK_CONFIG=no
          AC_MSG_RESULT(no)
        fi
      else
        GTK_CONFIG=no
        AC_MSG_RESULT(no)
      fi
  else
    if test "$OS_FILES2" = "mingw"; then
      REQUIRED_GTK=1.3.0
      PKG_GTK=gtk+-1.3-win32-production
      AC_MSG_CHECKING(for GTK+ - version >= $REQUIRED_GTK)
      if pkg-config --atleast-version $REQUIRED_GTK $PKG_GTK; then
        GTK_CONFIG="pkg-config $PKG_GTK"
        AC_MSG_RESULT(yes)
      else
        GTK_CONFIG=no
        AC_MSG_RESULT(no)
      fi
    else
      AC_CHECK_PROG(GTK_CONFIG, gtk-config, gtk-config, no)
      if test "$GTK_CONFIG" = "no"; then
        AC_MSG_RESULT(no)
      else
        AC_MSG_RESULT(yes)
      fi
    fi
  fi
else
  GTK_CONFIG=no
fi
if test "$GTK_CONFIG" = "no"; then
     LABLGTK_CONFIG=no
else
  AC_MSG_CHECKING(for native code $LABLGTK_NAME)
  if test -f `$OCAMLC -where`/$LABLGTK_NAME/lablgtk.$OCAMLLIB_EXT; then
     if test "$GUI" = "newgui2"; then
       if test -f `$OCAMLC -where`/$LABLGTK_NAME/lablrsvg.$OCAMLLIB_EXT; then
         AC_MSG_RESULT(yes)
         LABLGTK_CONFIG=yes
       else
         AC_MSG_RESULT(no)
         LABLGTK_CONFIG=no
         if test -f `$OCAMLC -where`/$LABLGTK_NAME/lablrsvg.cma; then
           echo "---------------------------------------------------------"
           echo "lablrsvg is not installed properly. See the INSTALL.txt"
           echo "file of mldonkey to see how to compile $LABLGTK_NAME. You"
           echo "probably forgot to call 'make opt' before 'make install'."
           echo "---------------------------------------------------------"
         else
           echo "---------------------------------------------------------"
           echo "lablrsvg is not installed. See the INSTALL.txt file of"
           echo "mldonkey to see how to compile $LABLGTK_NAME. Librsvg"
           echo "may not be installed when you compiled $LABLGTK_NAME."
           echo "---------------------------------------------------------"
         fi
       fi
     else
       AC_MSG_RESULT(yes)
       LABLGTK_CONFIG=yes
     fi
  else
     AC_MSG_RESULT(no)
     LABLGTK_CONFIG=no
     if test -f `$OCAMLC -where`/$LABLGTK_NAME/lablgtk.cma; then
       echo "-------------------------------------------------------------"
       echo "$LABLGTK_NAME is not installed properly. See the INSTALL.txt"
       echo "file of mldonkey to see how to compile $LABLGTK_NAME. You"
       echo "probably forgot to call 'make opt' before 'make install'."
       echo "-------------------------------------------------------------"
     else
       echo "Do you want this script to try to download and install $LABLGTK_NAME"
       echo "LOCALLY in mldonkey directory ?"
       if test "$BATCH" = "no"; then read i; else i=yes; fi
       case "$i" in
        y* | Y*)

         cd $PATCH_DIR
         rm -rf lablgtk-"$REQUIRED_LABLGTK"
         if test ! -f lablgtk-"$REQUIRED_LABLGTK".tar.gz; then
           echo Downloading ...
           $WGET http://wwwfun.kurims.kyoto-u.ac.jp/soft/olabl/dist/lablgtk-"$REQUIRED_LABLGTK".tar.gz
         fi
         if test ! -f lablgtk-"$REQUIRED_LABLGTK".tar.gz; then exit 1; fi

         mkdir -p $BUILD_DIR
         cd $BUILD_DIR
        echo Uncompressing ...
         gzip -cd $PATCH_DIR/lablgtk-"$REQUIRED_LABLGTK".tar.gz | tar xf -
         (cd lablgtk-"$REQUIRED_LABLGTK"
          PATH=$OCAML_PATH:$PATH
          export PATH
          echo $PATH

          if test "$GUI" = "newgui2"; then
            ./configure --prefix=$LOCAL_DIR
            $GNU_MAKE world
            $GNU_MAKE install
          else
            $GNU_MAKE configure
            $GNU_MAKE
            $GNU_MAKE opt
            $GNU_MAKE install
          fi

          cd ..
          rm -rf lablgtk-"$REQUIRED_LABLGTK"
         )
         cd $CONFIG_DIR
         if test -f `$OCAMLC -where`/$LABLGTK_NAME/lablgtk.$OCAMLLIB_EXT; then
            LABLGTK_CONFIG=yes
         else
            echo "Installation of $LABLGTK_NAME failed"
            LABLGTK_CONFIG=no
         fi
         ;;
        *);;
      esac
     fi
  fi
fi

if test "$GUI" = "newgui2" ; then
  GUIS="mldonkey_gui\$(EXE)"
  GTKCFLAGS="`pkg-config --cflags-only-I gtk+-2.0`"
  GTKLLIBS="`pkg-config --libs-only-L gtk+-2.0`"
  GTKLFLAGS="`pkg-config --libs-only-l gtk+-2.0`"
else
  GUIS="mldonkey_gui\$(EXE) mldonkey_gui2\$(EXE) mlchat\$(EXE)"
fi

if test "$IM" = "yes" ; then
  AC_CHECK_FILE(../src/im,IM=yes, [
  IM=no
  ])
else
   IM=no
fi

if test "$LABLGTK_CONFIG" = "no"; then
  GUI="no"
else
  MORE_TARGETS="$MORE_TARGETS $GUIS"
  AC_MSG_CHECKING(GToolbox.popup_menu args)
  OCAML_LIB_DIR="`$OCAMLC -where`"
  GTOOLBOX="$OCAML_LIB_DIR/$LABLGTK_NAME/gToolbox.mli"
  grep popup $GTOOLBOX | grep -i button 2> /dev/null && GTOOLBOX_ARGS=new
  if test "$GTOOLBOX_ARGS" = "new"; then
    GTOOLBOX_POPUPMENU=' ~button: button ~time: time '
    AC_MSG_RESULT(new)
  else
    GTOOLBOX_POPUPMENU=' ~x: button ~y: time '
    AC_MSG_RESULT(old)
  fi
  echo "---------------------------------------------------------"
  echo "End of GTK GUI configuration."
  echo "---------------------------------------------------------"
fi

AC_CHECK_PROG(COMPRESS, bzip2, bzip2)
AC_CHECK_PROG(COMPRESS, gzip, gzip)

if test "$ac_cv_prog_COMPRESS" = "bzip2"; then
   COMPRESS_EXT=bz2
else
   COMPRESS_EXT=gz
fi

# testing for gd support

HAS_GD=false
HAS_GD_JPG=false
HAS_GD_PNG=false
GD_JPG=no
GD_PNG=no
if test "$GD" != "no"; then
  GD=no
  LIBGD=no
  SAVE_LIBS=$LIBS

  AC_CHECK_PROG(GDLIBCONFIG, gdlib-config, gdlib-config)
  if test "$ac_cv_prog_GDLIBCONFIG" = "gdlib-config"; then
    GDLIBCONFIG=`$GDLIBCONFIG --libs`
    GDLIBS="$LIBS -lgd $GDLIBCONFIG"
    LIBS=$GDLIBS
  fi

  AC_MSG_CHECKING(for gdImageCreate in -lgd)
  AC_TRY_LINK([#include <gd.h>],
  [gdImageCreate (0,0);],
  [LIBGD=yes], [LIBGD=no])
  AC_MSG_RESULT($LIBGD)
  LIBS=$SAVE_LIBS

  if test "$LIBGD" = "no"; then
    AC_MSG_NOTICE([not found, trying another method])
    AC_CHECK_LIB(gd, gdImageCreate, [LIBGD=yes], [LIBGD=no])
  fi

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

    LIBPNG=no
    AC_CHECK_LIB(png, png_create_read_struct , [LIBPNG=yes])

    if test "$LIBPNG" = "no"; then
      AC_MSG_NOTICE(png support not available)
    else
      LIBGD_PNG=no
      LIBS=$GDLIBS
      AC_MSG_CHECKING(for gdImagePng in -lgd)
      AC_TRY_LINK([#include <gd.h>],
      [gdImagePng (0,0);],
      [LIBGD_PNG=yes], [LIBGD_PNG=no])
      AC_MSG_RESULT($LIBGD_PNG)
      LIBS=$SAVE_LIBS

      if test "$LIBGD_PNG" = "no"; then
        AC_MSG_NOTICE([not found, trying another method])
        AC_CHECK_LIB(gd, gdImagePng, [LIBGD_PNG=yes], [LIBGD_PNG=no])
      fi

      AC_MSG_CHECKING(for png support in libgd)
      if test "$LIBGD_PNG" = "yes"; then
        GD=yes
        HAS_GD_PNG=true
	GD_PNG=yes
        AC_MSG_RESULT(yes)
	AC_CHECK_LIB(png, png_access_version_number,[AC_DEFINE(HAVE_PNGVERSION, 1,)])
        AC_DEFINE(HAVE_GD_PNG, 1, [Define to 1 if you have png support in libgd.])
      else
        HAS_GD_PNG=false
        AC_MSG_RESULT(no)
      fi
    fi

    LIBJPEG=no
    AC_CHECK_LIB(jpeg, jpeg_CreateCompress, [LIBJPEG=yes])

    if test "$LIBJPEG" = "no"; then
      AC_MSG_NOTICE(jpeg support not available)
    else
      LIBGD_JPG=no
      LIBS=$GDLIBS
      AC_MSG_CHECKING(for gdImageJpeg in -lgd)
      AC_TRY_LINK([#include <gd.h>],
      [gdImageJpeg (0,0,0);],
      [LIBGD_JPG=yes], [LIBGD_JPG=no])
      AC_MSG_RESULT($LIBGD_JPG)
      LIBS=$SAVE_LIBS

      if test "$LIBGD_JPG" = "no"; then
        AC_MSG_NOTICE([not found, trying another method])
        AC_CHECK_LIB(gd, gdImageJpeg, [LIBGD_JPG=yes], [LIBGD_JPG=no])
      fi

      AC_MSG_CHECKING(for jpeg support in libgd)
      if test "$LIBGD_JPG" = "yes"; then
        GD=yes
  	HAS_GD_JPG=true
	GD_JPG=yes
        AC_DEFINE(HAVE_GD_JPG, 1, [Define to 1 if you have jpg support in libgd.])
        AC_MSG_RESULT(yes)
      else
  	HAS_GD_JPG=false
        AC_MSG_RESULT(no)
      fi
    fi
  else
    AC_MSG_NOTICE(libgd not available)
  fi
  LIBS=$SAVE_LIBS
fi
if test "$GD" != "no"; then
  HAS_GD=true
  cp -f ../src/daemon/common/commonGraphics.gd.ml ../src/daemon/common/commonGraphics.ml
else
  HAS_GD=false
  cp -f ../src/daemon/common/commonGraphics.nogd.ml ../src/daemon/common/commonGraphics.ml
fi

MD4ARCH=$arch
case $host in
  i386-pc-linux*|i386-*-*bsd*) MD4COMP=as; MD4ARCH=i386;;
  i486-pc-linux*|i486-*-*bsd*) MD4COMP=as; MD4ARCH=i486;;
  i586-pc-linux*|i586-*-*bsd*) MD4COMP=as; MD4ARCH=i586;;
  i686-pc-linux*|i686-*-*bsd*) MD4COMP=as; MD4ARCH=i686;;
  i386-pc-mingw32*) MD4COMP=cc; MD4ARCH=i386;;
  i486-pc-mingw32*) MD4COMP=cc; MD4ARCH=i486;;
  i586-pc-mingw32*) MD4COMP=cc; MD4ARCH=i586;;
  i686-pc-mingw32*) MD4COMP=cc; MD4ARCH=i686;;
  *) MD4COMP=cc;;
esac

ARCH=$arch

case "$OCAMLVERSION" in
  3.06* | 3.07* | 3.08* ) TYPE_FORMAT="";;
  *) TYPE_FORMAT="type ('a,'b,'c) ml_format = ('a,'b, 'c,'c) format4  type ('a,'b,'c) format = ('a,'b, 'c) ml_format";;
esac

if test "$OS_FILES" = "mingw"; then
   OCAMLDEP_OPTIONS="-slash"
fi

TMK=no

AC_SUBST(LIBS)
AC_SUBST(CFLAGS)
AC_SUBST(CPPFLAGS)
AC_SUBST(LDFLAGS)
AC_SUBST(CC)
AC_SUBST(CPP)
AC_SUBST(CXX)
AC_SUBST(CONFIG_INCLUDES)

AC_SUBST(EXTRA_DIRS)

AC_SUBST(TYPE_FORMAT)
AC_SUBST(OCAMLC)
AC_SUBST(OCAMLLIB)
AC_SUBST(OCAMLOPT)
AC_SUBST(OCAMLLEX)
AC_SUBST(OCAMLYACC)
AC_SUBST(OCAMLDEP)
AC_SUBST(OCAMLDEP_OPTIONS)
AC_SUBST(CAMLP4)
AC_SUBST(PERL)
AC_SUBST(OCAMLMKTOP)
AC_SUBST(SYSTEM)

AC_SUBST(MORE_TARGETS)
AC_SUBST(MORE_SUBDIRS)
AC_SUBST(LABLGL_CMA)
AC_SUBST(LABLGL_CMXA)
AC_SUBST(MD4ARCH)
AC_SUBST(MD4COMP)
AC_SUBST(BZIP2)
AC_SUBST(GD)
AC_SUBST(GD_JPG)
AC_SUBST(GD_PNG)
AC_SUBST(ICONV)

AC_SUBST(OPEN_NAPSTER)
AC_SUBST(DIRECT_CONNECT)
AC_SUBST(GNUTELLA)
AC_SUBST(GNUTELLA2)
AC_SUBST(BITTORRENT)
AC_SUBST(FILETP)
AC_SUBST(SOULSEEK)
AC_SUBST(OPENFT)
AC_SUBST(FASTTRACK)
AC_SUBST(DONKEY)
AC_SUBST(OPEN_DONKEY)
AC_SUBST(DONKEY_SERVER)

AC_SUBST(GUI)
AC_SUBST(REQUIRED_OCAML)
AC_SUBST(REQUIRED_LABLGTK)
AC_SUBST(ARCH)
AC_SUBST(COMPRESS)
AC_SUBST(COMPRESS_EXT)
AC_SUBST(RELEASENAME)
AC_SUBST(CHECKBOUNDS)
AC_SUBST(MLDONKEY_VERSION)
AC_SUBST(SCM_VERSION)
AC_SUBST(MAJOR_VERSION)
AC_SUBST(MINOR_VERSION)
AC_SUBST(SUB_VERSION)
AC_SUBST(GTOOLBOX_POPUPMENU)
AC_SUBST(OS_FILES)
AC_SUBST(OS_FILES2)
AC_SUBST(TARGET_TYPE)
AC_SUBST(RPMBUILD)

AC_SUBST(TMK)
AC_SUBST(GTKCFLAGS)
AC_SUBST(GTKLLIBS)
AC_SUBST(GTKLFLAGS)
AC_SUBST(HAS_GD)
AC_SUBST(HAS_GD_PNG)
AC_SUBST(HAS_GD_JPG)
AC_SUBST(HAS_BZIP2)

BUILD_SYSTEM="`uname -s` `uname -m` `uname -r`"
AC_SUBST(BUILD_SYSTEM)
AC_SUBST(GLIBC_VERSION)
AC_SUBST(CONFIGURE_ARGUMENTS)
AC_SUBST(CONFIGURE_RUN)

AUTOCONF=../src/utils/lib/autoconf.ml
GTK_AUTOCONF=../src/utils/lib/gAutoconf.ml
AC_OUTPUT(\
  Makefile.config \
  mldonkey.rc \
  $AUTOCONF.new $GTK_AUTOCONF.new \
   ../packages/rpm/mldonkey.spec \
   ../packages/rpm/Makefile \
   ../packages/slackware/mldonkey.options \
   ../packages/windows/mlnet.nsi)
diff $AUTOCONF.new $AUTOCONF 2> /dev/null > /dev/null || cp -f $AUTOCONF.new $AUTOCONF
diff $GTK_AUTOCONF.new $GTK_AUTOCONF 2> /dev/null > /dev/null || cp -f $GTK_AUTOCONF.new $GTK_AUTOCONF

cd ..

echo -e "\nBuilding dependencies (if it blocks, try '$GNU_MAKE depend' to see the problem)"
$GNU_MAKE depend 2> /dev/null > /dev/null || echo "Building dependencies fails: try: '$GNU_MAKE depend'"

echo -e -n "\nConfiguring MLDonkey" $MLDONKEY_VERSION
if test ! -z "$SCM_VERSION"; then
  echo -n " - SCM: $SCM_VERSION"
fi
echo " completed."

echo -e "\nNetwork modules:"
echo -n " - eDonkey           "
if test "$DONKEY" = "yes"; then
  echo "enabled"
else
  echo "        disabled"
fi

echo -n " - BitTorrent        "
if test "$BITTORRENT" = "yes"; then
  echo "enabled"
else
  echo "        disabled"
fi

echo -n " - FileTP (aka wget) "
if test "$FILETP" = "yes"; then
  echo "enabled"
else
  echo "        disabled"
fi

echo -n " - Fasttrack         "
if test "$FASTTRACK" = "yes"; then
  echo "enabled"
else
  echo "        disabled"
fi

echo -n " - Gnutella          "
if test "$GNUTELLA" = "yes"; then
  echo "enabled"
else
  echo "        disabled"
fi

echo -n " - Gnutella2         "
if test "$GNUTELLA2" = "yes"; then
  echo "enabled"
else
  echo "        disabled"
fi

echo -n " - Open Napster      "
if test "$OPEN_NAPSTER" = "yes"; then
  echo "enabled          - currently not usable"
else
  echo "        disabled - currently not usable"
fi

echo -n " - Direct Connect    "
if test "$DIRECT_CONNECT" = "yes"; then
  echo "enabled          - currently not usable"
else
  echo "        disabled - currently not usable"
fi

echo -n " - Soulseek          "
if test "$SOULSEEK" = "yes"; then
  echo "enabled          - currently not usable"
else
  echo "        disabled - currently not usable"
fi

echo -n " - OpenFT            "
if test "$OPENFT" = "yes"; then
  echo "enabled          - currently not usable"
else
  echo "        disabled - currently not usable"
fi

echo -e "\nCore features:"

echo -n " - threads           "
if test "$USE_PTHREAD" = "yes"; then
  echo "enabled"
else
  echo "        disabled"
fi

echo " - zlib              enabled"
echo -n " - bzip2             "
if test "$BZIP2" = "yes"; then
  echo "enabled"
else
  echo "        disabled"
fi

echo -n " - iconv             "
if test "$ICONV" = "yes"; then
  echo "enabled"
else
  echo "        disabled"
fi

echo -n " - graphical stats   "
if test "$GD" = "yes"; then
  echo "enabled"
  echo -n "   - png support     "
  if test "$GD_PNG" = "yes"; then
    echo "enabled"
  else
    echo "        disabled"
  fi

  echo -n "   - jpg support     "
  if test "$GD_JPG" = "yes"; then
    echo "enabled"
  else
    echo "        disabled"
  fi
else
  echo "        disabled"
fi

if test "$LABLGTK_CONFIG" = "yes"; then
  echo -n -e "\n - GUI support       "
  if test "$GUI" = "newgui1"; then
    echo "GTK1 newgui"
  else
    if test "$GUI" = "newgui2"; then
      echo "GTK2 GUI"
    else
      echo "GTK1 oldgui"
    fi
  fi
fi

echo -e "\nNow execute '$GNU_MAKE' to start compiling with Ocaml $OCAMLVERSION. Good luck!"
echo -e "\nTo compile a static code execute:     $GNU_MAKE mlnet.static"
echo      "To produce a release tarball execute: $GNU_MAKE release.mlnet.static"
echo      "To clean the build directory execute: $GNU_MAKE maintainerclean"
echo -e "\nCompiling CryptoPP.cc can take several minutes, on slow machines up to half an hour."
