
# **********                                                                                  **********
# **********                            Autoconf, configure internals                         **********
# **********                                                                                  **********

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

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

cd ..
SOURCE_DIR=`pwd`
cd config

# **********                                                                                  **********
# **********                                   MLDonkey version                               **********
# **********                                                                                  **********

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

# **********                                                                                  **********
# **********                                  check for C compiler                            **********
# **********                                                                                  **********

AC_PROG_CC
CC_VERSION=`$CC -dumpversion`
AC_EXEEXT
AC_PROG_CPP

# **********                                                                                  **********
# **********                                    System checks                                 **********
# **********                                                                                  **********

AC_CANONICAL_HOST

OS_FILES=unix
OS_FILES2=unix
SYSTEM=unknown
FIX_BROKEN_CPP=
case $host in
  *mingw*)
    SYSTEM=mingw
    OS_FILES=mingw
    OS_FILES2=mingw
    PTHREAD_LIBS="-lpthreadGC2 -lwsock32"
    PTHREAD_CFLAGS="-DPTW32_STATIC_LIB"
    AC_MSG_CHECKING(for mingw-runtime version)
      
cat >mingw_rt_ver.c <<'END'
#include <stdio.h>
#include <_mingw.h>
main(int argc, char *argv[[]])  {

int required_runtime_major=3;       /**********     change required mingw-runtime   **********/
int required_runtime_minor=10;      /**********              version here           **********/

#ifdef __MINGW32_MAJOR_VERSION
#  ifdef __MINGW32_MINOR_VERSION
     switch(argv[[1]][[0]]) {
       case 'x':
         printf("%d.%d", __MINGW32_MAJOR_VERSION, __MINGW32_MINOR_VERSION);
         break;
       case 'y':
         if ((__MINGW32_MAJOR_VERSION == required_runtime_major && __MINGW32_MINOR_VERSION < required_runtime_minor) || 
             (__MINGW32_MAJOR_VERSION < required_runtime_major)) {
           printf("no");
           break; }
         else {
           printf("yes");
           break; }
       case 'z':
         printf("%d.%d", required_runtime_major, required_runtime_minor);
         break;
     }
     return 0;
#  else
   printf("__MINGW32_MINOR_VERSION not defined in _mingw.h");
   return 0;
#  endif
#else
 printf("__MINGW32_MAJOR_VERSION not defined in _mingw.h");
 return 0;
#endif
}
END

    $CC -o mingw_rt_ver ./mingw_rt_ver.c
    MINGW_RT_VER="`./mingw_rt_ver x`"
    MINGW_RT_VER_OK="`./mingw_rt_ver y`"
    MINGW_RT_VER_REQ="`./mingw_rt_ver z`"
    rm -f mingw_rt_ver mingw_rt_ver.*
    AC_MSG_RESULT($MINGW_RT_VER)
    if test "$MINGW_RT_VER_OK" = "no"; then
      echo "********  mingw-runtime version $MINGW_RT_VER_REQ is required  *********" 1>&2;
      exit 1
    fi
    ;;
  *cygwin*)
    SYSTEM=cygwin
    OS_FILES2=cygwin
    ;;
  *freebsd*|*dragonfly*)
    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
    AC_CHECK_PROG(SED, sed, sed)
    if test "$ac_cv_prog_SED" = "sed"; then
      FIX_BROKEN_CPP="| sed -n '/^\#pragma/!p'"
    else
      AC_CHECK_PROG(GREP, grep, grep)
      if test "$ac_cv_prog_GREP" = "grep"; then
        FIX_BROKEN_CPP="| grep -v '^\#pragma'"
      fi
    fi
    if test "xFIX_BROKEN_CPP" = "x"; then
      echo "Apple cpp produces broken files, your system lacks sed or grep to fix it"
      exit 1
    fi
    ;;
esac
if test "x$SYSTEM" = "xunknown"; then
  echo "Unknown build system, please report to the MLDonkey development team at http://mldonkey.sourceforge.net/forums/"
  exit 1
fi

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

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

# **********                                                                                  **********
# **********               Parse checkout date (CVS) or revision (SVN) if available           **********
# **********                                                                                  **********

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_PROGS(STAT, stat gstat, stat)
  if [ test "$ac_cv_prog_STAT" = "stat" ] || [ test "$ac_cv_prog_STAT" = "gstat" ]; 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" = "netbsd"] || ( [ test "$SYSTEM" = "macosx" ] && [ test "$ac_cv_prog_STAT" != "gstat" ] ); 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 -c 1-19`
        fi  #if [ test "$SYSTEM" = "mingw"] || ...
        fi  #if [ test "$SYSTEM" = "openbsd"]
        fi  #if [ test "$SYSTEM" = "freebsd"] || ...
      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

# **********                                                                                  **********
# **********             build MLDonkey version, Required Ocaml and LablGTK versions          **********
# **********                                                                                  **********

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.09.2
DOWNLOAD_OCAML_MAJOR=3.09
DOWNLOAD_OCAML=3.09.2

REQUIRED_LABLGTK=1.2.7

touch install-sh

LABLGTK_NAME=lablgtk

# **********                                                                                  **********
# **********                    Network and Feature configure switch variables                **********
# **********                                                                                  **********

# **********   currently not working Networks, can't be enabled

SOULSEEK=no
OPEN_NAPSTER=no
OPENFT=no
DIRECT_CONNECT=no

# **********   working Networks, enabled by default

DONKEY=yes
BITTORRENT=yes
GNUTELLA=yes
GNUTELLA2=yes
FASTTRACK=yes
FILETP=yes

# **********   predefined configure variations

MULTINET=yes
MINIMUM=no

# **********   Features, enabled by default

DONKEY_SUI=yes
DONKEY_SUI_FILE=DonkeySui2
GD=yes
BZIP2=yes
MAGIC=yes
ICONV=yes
USE_PTHREAD=yes

# **********

BATCH=no
FORCE_OCAML=no
FORCE_MINGW=no
PROFILE=no
DEBUG=no
GUI=no
CHECKBOUNDS=false

# **********                                                                                  **********
# **********                Network and Feature configure switches and dependency             **********
# **********                                                                                  **********

AC_ARG_ENABLE(multinet,      [  --disable-multinet      allows you to only compile support for eDonkey (incl. Overnet and Kademlia)], [MULTINET="$enableval"])
AC_ARG_ENABLE(minimum,       [  --enable-minimum        compile MLDonkey with minimum features and Networks (eDonkey, iconv and thread enabled)], [MINIMUM="$enableval"])
AC_ARG_ENABLE(minimum,       [  --enable-minimum=all    disable all optional features and Networks excepting eDonkey], [MINIMUM="$enableval"])

if [ test ! "$MULTINET" = "yes" ] || [ test "$MINIMUM" = "yes" ] || [ test "$MINIMUM" = "all" ]; then
  GNUTELLA=no
  GNUTELLA2=no
  FASTTRACK=no
  FILETP=no
  BITTORRENT=no
fi
if [ test "$MINIMUM" = "yes" ] || [ test "$MINIMUM" = "all" ]; then
  DONKEY_SUI=no
  GD=no
  BZIP2=no
  MAGIC=no
fi
if [ test "$MINIMUM" = "all" ]; then
  USE_PTHREAD=no
  ICONV=no
fi

#AC_ARG_ENABLE(soulseek,      [  --disable-soulseek      allows you to remove support for SoulSeek], [SOULSEEK="$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"])

AC_ARG_ENABLE(donkey,        [  --disable-donkey        allows you to remove support for eDonkey], [DONKEY="$enableval"])
AC_ARG_ENABLE(donkeysui,     [  --disable-donkeysui     allows you to remove support for eMule SecureUserIdent], [DONKEY_SUI="$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(fasttrack,     [  --disable-fasttrack     allows you to remove support for FastTrack], [FASTTRACK="$enableval"])

AC_ARG_ENABLE(batch,         [  --enable-batch          reply YES to all queries in this script], [BATCH="$enableval"])
AC_ARG_ENABLE(force-ocaml,   [  --enable-force-ocaml    force usage of self-compiled Ocaml], [FORCE_OCAML="$enableval"])
AC_ARG_ENABLE(ocamlver,      [  --enable-ocamlver=VER   force usage of specific Ocaml version (3.08.1|CVS), ], [REQUIRED_OCAML="$enableval"])
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
fi

AC_ARG_ENABLE(profile,       [  --enable-profile        allows you to profile mlnet using gprof], [PROFILE="$enableval"])
AC_ARG_ENABLE(debug,         [  --enable-debug          allows you to compile mlnet with debug symbols], [DEBUG="$enableval"])
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

AC_ARG_ENABLE(pthread,       [  --disable-pthread       allows you to disable pthread support in mldonkey, hurts performance!], [USE_PTHREAD="$enableval"])
AC_ARG_ENABLE(pthread-lib,   [  --enable-pthread-lib    legacy option], [USE_PTHREAD="$enableval"])
AC_ARG_ENABLE(iconv,         [  --disable-iconv         disable the use of iconv], [ICONV="$enableval"])
AC_ARG_ENABLE(gd,            [  --disable-gd            disable the use of gd], [GD="$enableval"])
AC_ARG_ENABLE(bzip2,         [  --disable-bzip2         disable the use of bzip2], [BZIP2="$enableval"])
AC_ARG_ENABLE(magic,         [  --disable-magic         disable the use of libmagic (GNU file)], [MAGIC="$enableval"])

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

OCAML_PATH=

echo -e "\n--------------------------------"
echo "     Checking system tools."
echo "--------------------------------"

AC_PROG_RANLIB
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

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

AC_PATH_PROG(PERL, perl, perl)
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"

echo "----------------------------------------"
echo "     Checking system tools finished."
echo -e "----------------------------------------\n"

echo "--------------------------------"
echo "     Checking Ocaml compiler."
echo "--------------------------------"

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_TOOL(OCAMLC,ocamlc,ocamlrun ocamlc)

BUILD_OCAML=no
if [ test -z "$OCAMLC" ] || [ test "$REQUIRED_OCAML" = "CVS" ]; then
   BUILD_OCAML=yes
else
  OCAMLVERSION=`$OCAMLC -v | sed -n -e 's|.*version* *\(.*\)$|\1|p' `
  case "$OCAMLVERSION" in
    "$REQUIRED_OCAML"*) ;;
    3.09.0*) ;;
    3.09.1*) ;;
    3.09.3*) ;;
    3.08.4*) ;;
    3.08.3*) ;;
    *)
        echo "Need build"
        BUILD_OCAML=yes
        ;;
  esac
fi

if [ test "$BUILD_OCAML" = "yes" ] || [ test "$FORCE_OCAML" = "yes" ]; then

  if [ test "$SYSTEM" = "mingw" ]; then
    echo "********         Objective-Caml $REQUIRED_OCAML is required               *********" 1>&2;
    echo "********     no valid Ocaml compiler found on your system        *********" 1>&2;
    echo "********   on MinGW there is no possibility to let configure     *********" 1>&2;
    echo "********   build the Ocaml compiler with the included script     *********" 1>&2;
    echo "********    check http://mldonkey.sourceforge.net/Windows        *********" 1>&2;
    echo "********   for instuctions how to compile Ocaml under MinGW      *********" 1>&2;
    exit 1
  else  
    
  echo "********  Objective-Caml $REQUIRED_OCAML is required  *********" 1>&2;
  if [ test "$WGET" = "" ] && [ test "$REQUIRED_OCAML" != "CVS" ]; 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 <&1; else i=yes; fi
  case "$i" in
   y* | Y*)
      cd $PATCH_DIR
      if [ test "$REQUIRED_OCAML" = "CVS" ]; then
        rm -rf $BUILD_DIR
        mkdir -p $BUILD_DIR
        cd $BUILD_DIR
        cvs -d:pserver:anoncvs:""@camlcvs.inria.fr:/caml login
        cvs -z3 -d:pserver:anoncvs@camlcvs.inria.fr:/caml co -P ocaml
        cd ocaml
      else
        if test ! -f ocaml-"$REQUIRED_OCAML".tar.gz; then
          echo Downloading ...
          $WGET http://caml.inria.fr/pub/distrib/ocaml-"$DOWNLOAD_OCAML_MAJOR"/ocaml-"$REQUIRED_OCAML".tar.gz
        fi
        if test ! -f ocaml-"$REQUIRED_OCAML".tar.gz; then
          $WGET http://caml.inria.fr/pub/distrib/ocaml-3.08/ocaml-"$REQUIRED_OCAML".tar.gz
        fi
        if test ! -f ocaml-"$REQUIRED_OCAML".tar.gz; then
          echo "********        download Ocaml $REQUIRED_OCAML not successful, try do download it manually       *********" 1>&2;
          echo "********        from http://caml.inria.fr/pub/distrib/                                   *********" 1>&2;
          echo "********        and copy the tar.gz into $PATCH_DIR              *********" 1>&2;
          exit 1
        fi

        echo Uncompressing ...
        mkdir -p $BUILD_DIR
        cd $BUILD_DIR
        rm -rf ocaml-"$REQUIRED_OCAML"
        gzip -cd $PATCH_DIR/ocaml-"$REQUIRED_OCAML".tar.gz | tar vxf -
        cd ocaml-"$REQUIRED_OCAML"
        if test -f $PATCH_DIR/ocaml-"$REQUIRED_OCAML".patch; then
          echo Patching ...
          patch -p1 < $PATCH_DIR/ocaml-"$REQUIRED_OCAML".patch
        fi
      fi
      echo Configuring ...
      ./configure -prefix $LOCAL_DIR -host $host -cc $CC
      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
      if [ test "$REQUIRED_OCAML" = "CVS" ]; then
        rm -rf ocaml
      else
        rm -rf ocaml-"$REQUIRED_OCAML"
      fi
      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
fi

if test "$OCAMLC" = "$LOCAL_OCAML"/ocamlc.opt ; then
  OCAML_PATH="$LOCAL_OCAML"/
  OCAMLOPT=$LOCAL_OCAML/ocamlopt.opt
  OCAMLDOC=$LOCAL_OCAML/ocamldoc
  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
  OCAMLDOC=$LOCAL_OCAML/ocamldoc
  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(OCAMLDOC, ocamldoc, ocamldoc)
  AC_CHECK_PROG(OCAMLMKTOP, ocamlmktop, ocamlmktop)
  AC_CHECK_PROG(CAMLP4, camlp4, camlp4)
fi
fi

OCAMLVERSION=`$OCAMLC -v | sed -n -e 's|.*version* *\(.*\)$|\1|p' `
OCAMLLIB=`$OCAMLC -where`
REBUILD_OCAML=no
case "$OCAMLVERSION" in
    "$REQUIRED_OCAML"*) ;;
    3.09.0*) ;;
    3.09.1*) ;;
    3.09.3*) ;;
    3.08.4*) ;;
    3.08.3*) ;;
    *)
      if [ test "$REQUIRED_OCAML" != "CVS" ]; then
        REBUILD_OCAML=yes
      fi
      ;;
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

echo "-------------------------------------------"
echo "     Checking Ocaml $OCAMLVERSION finished."
echo "-------------------------------------------"


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

echo -e "\n----------------------------------"
echo "     Checking system headers."
echo "----------------------------------"

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

])

# 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)
AC_CHECK_FUNCS(setrlimit getrlimit strerror_r strerror)
AC_CHECK_HEADERS(byteswap.h,,)

# various header files
AC_CHECK_HEADERS([sys/utsname.h])
AC_CHECK_HEADERS(arpa/inet.h,,)
AC_CHECK_HEADERS([sys/types.h sys/time.h sys/resource.h netinet/in_systm.h netinet/in.h netinet/ip.h],,,
[#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.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
])
echo "-------------------------------------------"
echo "     Checking system headers finished."
echo -e "-------------------------------------------\n"
echo "-----------------------------------"
echo "     Checking system libraries."
echo "-----------------------------------"
echo "----- checking zlib (required)"
AC_CHECK_LIB(z,inflate,,[echo "Zlib missing, did you install zlib and zlib-developer packages?"; exit 1])
AC_CHECK_HEADERS(zlib.h,,[echo "Zlib missing, did you install zlib and zlib-developer packages?"; exit 1])
AC_CHECK_LIB(z,zlibVersion,[AC_DEFINE(HAVE_ZLIBVERSION, 1,)])

if test "$BZIP2" != "no"; then
  echo "----- checking bzlib (optional)"
  AC_CHECK_HEADERS(bzlib.h,[AC_CHECK_LIB(bz2,BZ2_bzReadOpen,[BZIP2=yes],[BZIP2=no])],[BZIP2=no])
fi

if test "$BZIP2" = "yes"; then
  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_NOTICE(bzlib not available)
fi

if test "$MAGIC" != "no"; then
  echo "----- checking libmagic (GNU file) (optional)"
  AC_CHECK_HEADERS(magic.h,
    [AC_CHECK_LIB(magic,magic_file,
      [AC_CHECK_DECL([MAGIC_ERROR], [MAGIC=yes], [MAGIC=no],[#include <magic.h>])],
      [MAGIC=no])],
    [MAGIC=no])
fi

if test "$MAGIC" = "yes"; then
  MAGICLIB=Magic_magic
else
  MAGICLIB=Magic_nomagic
  AC_MSG_NOTICE(libmagic not available)
fi

GD_JPG=no
GD_PNG=no
if test "$GD" != "no"; then
  echo "----- checking gd (optional)"
  GD=no
  LIBGD=no
  SAVE_LIBS=$LIBS
  SAVE_CFLAGS=$CFLAGS
  SAVE_LDFLAGS=$LDFLAGS
  AC_CHECK_PROG(GDLIBCONFIG, gdlib-config, gdlib-config)
  if test "$ac_cv_prog_GDLIBCONFIG" = "gdlib-config"; then
    GD_LIBS=`$GDLIBCONFIG --libs`
    GD_LIBS2="$LIBS -lgd $GD_LIBS"
    LIBS=$GD_LIBS2
    GD_CFLAGS=`$GDLIBCONFIG --cflags`
    CFLAGS="$CFLAGS $GD_CFLAGS"
    GD_LDFLAGS=`$GDLIBCONFIG --ldflags`
    LDFLAGS="$LDFLAGS $GD_LDFLAGS"
    AC_MSG_CHECKING(for libgd > 2.0.14)
    GDVERSION=`$GDLIBCONFIG --version`
    GDMAJORVERSION=`$GDLIBCONFIG --majorversion`
    GDMINORVERSION=`$GDLIBCONFIG --minorversion`
    GDREVISION=`$GDLIBCONFIG --revision`
    if [ [ $GDMAJORVERSION = 2 ] && [ $GDMINORVERSION = 0 ] && [ $GDREVISION -gt 14 ]]; then
	AC_MSG_RESULT($GDVERSION)
	LIBGD=yes
    else
	AC_MSG_RESULT(no (found $GDVERSION))
	LIBGD=no
    fi
  else
    LIBGD=yes
  fi

  if test "$LIBGD" = "yes"; then
    AC_CHECK_HEADERS(gd.h,[LIBGD=yes],[LIBGD=no])
  fi

  if test "$LIBGD" = "yes"; then
    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
  fi

  if test "$LIBGD" = "yes"; then
    LIBPNG=no
# saving current variables
    SAVE_LIBS_PNG=$LIBS
    SAVE_CFLAGS_PNG=$CFLAGS
    SAVE_LDFLAGS_PNG=$LDFLAGS
    AC_CHECK_PROG(PNGLIBCONFIG, libpng-config, libpng-config)
    if test "$ac_cv_prog_PNGLIBCONFIG" = "libpng-config"; then
# check libpng-config contents and update system variables
      PNG_LIBS=`$PNGLIBCONFIG --libs`
      LIBS="$LIBS $PNG_LIBS"
      PNG_CFLAGS=`$PNGLIBCONFIG --cflags`
      CFLAGS="$CFLAGS $PNG_CFLAGS"
      PNG_LDFLAGS=`$PNGLIBCONFIG --ldflags`
      LDFLAGS="$LDFLAGS $PNG_LDFLAGS"
    fi
    AC_CHECK_LIB(png, png_create_read_struct , [LIBPNG=yes])
# restore variables
    LIBS=$SAVE_LIBS_PNG
    CFLAGS=$SAVE_CFLAGS_PNG
    LDFLAGS=$SAVE_LDFLAGS_PNG

    if test "$LIBPNG" = "no"; then
      AC_MSG_NOTICE(png support not available)
    else
      LIBGD_PNG=no
      LIBS=$GD_LIBS2
      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
	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
        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=$GD_LIBS2
      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
	GD_JPG=yes
        AC_DEFINE(HAVE_GD_JPG, 1, [Define to 1 if you have jpg support in libgd.])
        AC_MSG_RESULT(yes)
      else
        AC_MSG_RESULT(no)
      fi
    fi
  else
    AC_MSG_NOTICE(libgd not available)
  fi
  LIBS=$SAVE_LIBS
  CFLAGS=$SAVE_CFLAGS
  LDFLAGS=$SAVE_LDFLAGS
fi

if test "$GD" != "no"; then
  GDGRAPHICS=DriverGraphics_gd
else
  GDGRAPHICS=DriverGraphics_nogd
fi

if test "$ICONV" = "yes"; then
  echo "----- checking iconv (optional)"
  AM_ICONV
  if test "$am_cv_func_iconv" != "yes"; then
    AC_DEFINE(DISABLE_ICONV, 1, [Define to 1 if you want to replace iconv and related with stubs.])
    ICONV=no
  else
  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)),
  [echo "Your iconv implementation is incomplete"; exit 1]))
  AC_CHECK_LIB(charset,locale_charset)
  AC_CHECK_HEADERS([libcharset.h])
  AC_CHECK_HEADERS([langinfo.h])
  AC_CHECK_HEADERS([locale.h])
  fi
else
  AC_DEFINE(DISABLE_ICONV, 1, [Define to 1 if you want to replace iconv and related with stubs.])
  ICONV=no
fi

if test "$USE_PTHREAD" = "no"; then
 PTHREAD_LIBS=""
 echo "You disabled thread support, this will hurt MLDonkey performance!"
else
  echo "----- checking thread support (optional, strongly advised)"
  ACX_PTHREAD
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*)
           echo "----- GNU C Library version"
           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

echo "---------------------------------------------"
echo "     Checking system libraries finished."
echo -e "---------------------------------------------\n"

CXX_VERSION=
CRYPTOPPFLAGS=
echo "--------------------------------------"
echo "     Checking activated networks."
echo "--------------------------------------"

  if test "$DONKEY" = "yes"; then
    AC_MSG_CHECKING(eDonkey)
    if test -d ../src/networks/donkey; then
      AC_MSG_RESULT(yes)
      if test "$DONKEY_SUI" = "yes"; then
	AC_PROG_CXX

	AC_CHECK_TOOL(NEWCXX, [$CXX], [no])
	AC_MSG_CHECKING(eMule SUI)
	if test x"$NEWCXX" = x"no"
	then
	  AC_MSG_RESULT(no)
	  CXX=
	  DONKEY_SUI=no
	else
	  AC_MSG_RESULT(yes)
	  ACX_CHECK_CXX_FLAGS(-fno-omit-frame-pointer,no_omit_frame_pointer, CRYPTOPPFLAGS="-fno-omit-frame-pointer")
	  ACX_CHECK_CXX_FLAGS(-mno-omit-leaf-frame-pointer,no_omit_leaf_frame_pointer, CRYPTOPPFLAGS="$CRYPTOPPFLAGS -mno-omit-leaf-frame-pointer")
	  CXX=$NEWCXX
	  CXX_VERSION=`$CXX -dumpversion`
	  DONKEY_SUI_FILE=DonkeySui1
	  AC_DEFINE(HAVE_CRYPTOPP, 1,)
	fi
      fi
#      AC_MSG_CHECKING(eDonkey server)
#      if test -d ../src/networks/server; then
#        AC_MSG_RESULT(no)
#        DONKEY_SERVER=no
#      else
#        AC_MSG_RESULT(no)
#        DONKEY_SERVER=no
#      fi
    else
      DONKEY=no
      DONKEY_SUI=no
      AC_MSG_RESULT(no)
    fi
  else
    DONKEY_SUI=no
  fi

  if test "$BITTORRENT" = "yes"; then
    AC_MSG_CHECKING(BitTorrent)
    if test -d ../src/networks/bittorrent; then
      AC_MSG_RESULT(yes)
    else
      BITTORRENT=no
      AC_MSG_RESULT(no)
    fi
  fi

  if test "$FILETP" = "yes"; then
    AC_MSG_CHECKING(FileTP)
    if test -d ../src/networks/fileTP; then
      AC_MSG_RESULT(yes)
    else
      FILETP=no
      AC_MSG_RESULT(no)
    fi
  fi

  if test "$GNUTELLA" = "yes"; then
    AC_MSG_CHECKING(Gnutella)
    if test -d ../src/networks/gnutella; then
      AC_MSG_RESULT(yes)
    else
      GNUTELLA=no
      AC_MSG_RESULT(no)
    fi
  fi

  if test "$GNUTELLA2" = "yes"; then
    AC_MSG_CHECKING(G2)
    if test -d ../src/networks/gnutella2; then
      AC_MSG_RESULT(yes)
    else
      GNUTELLA2=no
      AC_MSG_RESULT(no)
    fi
  fi

  if test "$FASTTRACK" = "yes"; then
    AC_MSG_CHECKING(Fasttrack)
    if test -d ../src/networks/fasttrack; then
      AC_MSG_RESULT(yes)
    else
      FASTTRACK=no
      AC_MSG_RESULT(no)
    fi
  fi

  if test "$DIRECT_CONNECT" = "yes"; then
    AC_MSG_CHECKING(DirectConnect)
    if test -d ../src/networks/direct_connect; then
      AC_MSG_RESULT(yes)
    else
      DIRECT_CONNECT=no
      AC_MSG_RESULT(no)
    fi
  fi

  if test "$SOULSEEK" = "yes"; then
    AC_MSG_CHECKING(Soulseek)
    if test -d ../src/networks/soulseek; then
      AC_MSG_RESULT(yes)
    else
      SOULSEEK=no
      AC_MSG_RESULT(no)
    fi
  fi

  if test "$OPEN_NAPSTER" = "yes"; then
    AC_MSG_CHECKING(OpenNapster)
    if test -d ../src/networks/opennap; then
      AC_MSG_RESULT(yes)
    else
      OPEN_NAPSTER=no
      AC_MSG_RESULT(no)
    fi
  fi

  if test "$OPENFT" = "yes"; then
    AC_MSG_CHECKING(OpenFT)
    if test -d ../src/networks/openFT; then
      AC_MSG_RESULT(yes)
    else
      OPENFT=no
      AC_MSG_RESULT(no)
    fi
  fi
echo "-----------------------------------------------"
echo "     Checking activated networks finished."
echo "-----------------------------------------------"

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

# 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)"
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

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(CXXFLAGS)
AC_SUBST(LDFLAGS)
AC_SUBST(CC)
AC_SUBST(CPP)
AC_SUBST(CXX)
AC_SUBST(FIX_BROKEN_CPP)
AC_SUBST(CONFIG_INCLUDES)
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(OCAMLDOC)
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(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(DONKEY_SUI)
AC_SUBST(CRYPTOPPFLAGS)
AC_SUBST(DONKEY_SUI_FILE)
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(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(GD)
AC_SUBST(GD_JPG)
AC_SUBST(GD_PNG)
AC_SUBST(GDGRAPHICS)
AC_SUBST(GD_LIBS)
AC_SUBST(GD_CFLAGS)
AC_SUBST(GD_LDFLAGS)
AC_SUBST(BZIP2)
AC_SUBST(MAGIC)
AC_SUBST(MAGICLIB)
BUILD_SYSTEM="`uname -s` `uname -m` `uname -r`"
AC_SUBST(BUILD_SYSTEM)
AC_SUBST(GLIBC_VERSION)
AC_SUBST(CC_VERSION)
AC_SUBST(CXX_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 \
   ../src/utils/lib/magic.ml \
   ../src/networks/donkey/donkeySui.ml \
   ../src/daemon/driver/driverGraphics.ml \
   ../packages/rpm/mldonkey.spec \
   ../packages/rpm/Makefile \
   ../packages/slackware/mldonkey.options)
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
  if test "$DONKEY_SUI" = "yes"; then
    echo "enabled (eMule SUI enabled)"
  else
    echo "enabled (eMule SUI disabled)"
  fi
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 " - zlib (required)   enabled"

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

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 " - libmagic          "
if test "$MAGIC" = "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 "\nCompilers:"
echo -e " - Ocaml version     $OCAMLVERSION"
echo -e " - $CC version       $CC_VERSION"
if test "x$CXX" != "x"; then
  echo -e " - $CXX version       $CXX_VERSION"
fi
echo -e "\nNow execute '$GNU_MAKE' to start compiling. 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"
if test "$DONKEY_SUI" = "yes"; then
  echo -e "\nCompiling CryptoPP.cc can take several minutes, on slow machines up to half an hour."
fi
