#
# autoconf input for Objective Caml programs
# Modified by the lablgtk2 development team
# Original copyright (C) 2001 Jean-Christophe Filliātre
#   from a first script by Georges Mariano
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License version 2, as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See the GNU Library General Public License version 2 for more details
# (enclosed in the file LGPL).

# the script generated by autoconf from this input will set the following
# variables:
#    CAMLC        "ocamlc" if present in the path, or a failure
#                 or "ocamlc.opt" if present with same version number as ocamlc
#    CAMLOPT      "ocamlopt" (or "ocamlopt.opt" if present), or "no"
#   OCAMLBEST     either "byte" if no native compiler was found,
#                 or "opt" otherwise
#   OCAMLDEP      "ocamldep"
#   OCAMLLEX      "ocamllex" (or "ocamllex.opt" if present)
#   OCAMLYACC     "ocamlyac"
#   OCAMLLIB      the path to the ocaml standard library
#   OCAMLVERSION  the ocaml version number
#   OCAMLWIN32    "yes"/"no" depending on Sys.os_type = "Win32"
#   EXE           ".exe" if OCAMLWIN32=yes, "" otherwise

# check for one particular file of the sources
# ADAPT THE FOLLOWING LINE TO YOUR SOURCES!
AC_INIT(src/gtk.ml)

# Check for Ocaml compilers

# we first look for ocamlc in the path; if not present, we fail
AC_CHECK_PROG(CAMLC,ocamlc,ocamlc,no)
if test "$CAMLC" = no ; then
	AC_MSG_ERROR(Cannot find ocamlc.)
fi

# we extract Ocaml version number and library path
OCAMLVERSION=`$CAMLC -version`
echo "ocaml version is $OCAMLVERSION"
OCAMLLIB=`$CAMLC -where | tr -d '\\r'`
echo "ocaml library path is $OCAMLLIB"
LIBDIR=$OCAMLLIB

AC_ARG_WITH(libdir,
	[  --with-libdir=/path     install libs in /path/lablgtk3 and /path/stublibs],
	LIBDIR=$withval
	echo "Install dirs are : $LIBDIR/lablgtk3 and $LIBDIR/stublibs"
	echo "	Compile with
		ocamlc -I $LIBDIR/lablgtk3
	and add $LIBDIR/stublibs either to OCAMLLIB/ld.conf or
	to CAML_LD_LIBRARY_PATH",
	echo "Default install dirs are : $LIBDIR/lablgtk3 and $LIBDIR/stublibs"
	echo "Compile with ocamlc -I +lablgtk3"
	)

# then we look for ocamlopt; if not present, we issue a warning
# if the version is not the same, we also discard it
# we set OCAMLBEST to "opt" or "byte", whether ocamlopt is available or not
AC_CHECK_PROG(CAMLOPT,ocamlopt,ocamlopt,no)
OCAMLBEST=byte
if test "$CAMLOPT" = no ; then
	AC_MSG_WARN(Cannot find ocamlopt; bytecode compilation only.)
else
	AC_MSG_CHECKING(ocamlopt version)
	TMPVERSION=`$CAMLOPT -version`
	if test "$TMPVERSION" != "$OCAMLVERSION" ; then
	    AC_MSG_RESULT(differs from ocamlc; ocamlopt discarded.)
	    CAMLOPT=no
	else
	    AC_MSG_RESULT(ok)
	    OCAMLBEST=opt
	fi
fi

# checking for ocamlc.opt
#AC_CHECK_PROG(OCAMLCDOTOPT,ocamlc.opt,ocamlc.opt,no)
#if test "$OCAMLCDOTOPT" != no ; then
	#AC_MSG_CHECKING(ocamlc.opt version)
	#TMPVERSION=`$OCAMLCDOTOPT -version`
	#if test "$TMPVERSION" != "$OCAMLVERSION" ; then
	    #AC_MSG_RESULT(differs from ocamlc; ocamlc.opt discarded.)
	#else
	    #AC_MSG_RESULT(ok)
	    #CAMLC=$OCAMLCDOTOPT
	#fi
#fi

# checking for ocamlopt.opt
#if test "$CAMLOPT" != no ; then
    #AC_CHECK_PROG(OCAMLOPTDOTOPT,ocamlopt.opt,ocamlopt.opt,no)
    #if test "$OCAMLOPTDOTOPT" != no ; then
	#AC_MSG_CHECKING(ocamlc.opt version)
	#TMPVER=`$OCAMLOPTDOTOPT -version`
	#if test "$TMPVER" != "$OCAMLVERSION" ; then
	    #AC_MSG_RESULT(differs from ocamlc; ocamlopt.opt discarded.)
	#else
	    #AC_MSG_RESULT(ok)
	    #CAMLOPT=$OCAMLOPTDOTOPT
	#fi
    #fi
#fi

# ocamlrun, ocamldep, ocamllex and ocamlyacc should also be present in the path
AC_CHECK_PROG(OCAMLRUN,ocamlrun,ocamlrun,no)
if test "$OCAMLRUN" = no ; then
	AC_MSG_ERROR(Cannot find ocamlrun.)
fi

AC_CHECK_PROG(OCAMLDEP,ocamldep,ocamldep,no)
if test "$OCAMLDEP" = no ; then
	AC_MSG_ERROR(Cannot find ocamldep.)
fi

AC_CHECK_PROG(OCAMLDOC,ocamldoc,ocamldoc,no)
if test "$OCAMLDOC" = no ; then
	AC_MSG_RESULT(Cannot find ocamldoc.)
fi

AC_CHECK_PROG(OCAMLLEX,ocamllex,ocamllex,no)
if test "$OCAMLLEX" = no ; then
    AC_MSG_ERROR(Cannot find ocamllex.)
#else
#    AC_CHECK_PROG(OCAMLLEXDOTOPT,ocamllex.opt,ocamllex.opt,no)
#    if test "$OCAMLLEXDOTOPT" != no ; then
#	OCAMLLEX=$OCAMLLEXDOTOPT
#    fi
fi

AC_CHECK_PROG(OCAMLYACC,ocamlyacc,ocamlyacc,no)
if test "$OCAMLYACC" = no ; then
	AC_MSG_ERROR(Cannot find ocamlyacc.)
fi

AC_CHECK_PROG(CAMLMKTOP,ocamlmktop,ocamlmktop,no)
if test "$CAMLMKTOP" = no ; then
	AC_MSG_ERROR(Cannot find ocamlmktop.)
fi

AC_CHECK_PROG(CAMLMKLIB,ocamlmklib,ocamlmklib,no)
if test "$CAMLMKLIB" = no ; then
	AC_MSG_ERROR(Cannot find ocamlmklib.)
fi

AC_CHECK_PROG(CAMLP4O,camlp4o,camlp4o,no)
if test "$CAMLP4O" = no ; then
	AC_MSG_WARN(Cannot find camlp4o; please do not modify .ml4 files.)
fi

AC_CHECK_PROG(OCAMLFIND,ocamlfind,ocamlfind,no)
if test "`$OCAMLFIND printconf stdlib`" != "`$CAMLC -where`"; then
  AC_MSG_WARN(Ignoring ocamlfind, it uses a different OCaml installation.)
  OCAMLFIND=no
fi

if test "$OCAMLFIND" = no; then
FINDLIBDIR=""
OCAMLLDCONF=""
else
FINDLIBDIR="`ocamlfind printconf destdir | tr -d '\\r'`"
echo "$OCAMLFIND library path is $FINDLIBDIR"
OCAMLLDCONF="`ocamlfind printconf ldconf | tr -d '\\r'`"
echo "$OCAMLFIND ldconf path is $OCAMLLDCONF"
fi

if expr "$OCAMLVERSION" '>=' '4' > /dev/null ; then
   ODOC_DEF="-D OCAML_400"
fi
AC_SUBST(ODOC_DEF)

if expr "$OCAMLVERSION" '>=' '3.11' > /dev/null ; then
   HAS_PRINTEXC_BACKTRACE="-D HAS_PRINTEXC_BACKTRACE"
fi
AC_SUBST(HAS_PRINTEXC_BACKTRACE)

# Check for which kind of threads is used
AC_MSG_CHECKING(for ocaml threads)
AC_ARG_WITH(threads,
        [  --with-threads=(yes|system|vm|no)  which threads to use ],
        THREADS_LIB="$withval",
        THREADS_LIB="yes")
if (test "$THREADS_LIB" = yes || test "$THREADS_LIB" = system) && \
   test -r "$OCAMLLIB/threads/threads.cma"; then
  THREADS_LIB="system"
elif (test "$THREADS_LIB" = yes || test "$THREADS_LIB" = vm) && \
   test -r "$OCAMLLIB/vmthreads/stdlib.cma"; then
  THREADS_LIB="vm"
elif test "$THREADS_LIB" = yes || test "$THREADS_LIB" = no; then
  THREADS_LIB="no"
else
  echo; AC_MSG_ERROR(Cannot use $THREADS_LIB threads)
fi
AC_MSG_RESULT(use $THREADS_LIB threads)

# Check for dll support
HAS_DLL_SUPPORT="no"
AC_MSG_CHECKING(for ocaml dll support)
if test -r "$OCAMLLIB/stublibs/dllunix.so" ||
   test -r "$OCAMLLIB/stublibs/dllunix.dll"
then HAS_DLL_SUPPORT="yes"
fi
AC_MSG_RESULT($HAS_DLL_SUPPORT)

RANLIB=`$CAMLC -config | grep ranlib | sed -e "s/ranlib: \(.*\)/\1/" `

AC_PROG_RANLIB
# get the C compiler used by ocamlc
if test -z "$CC" ; then
  touch conftest.c
  CC=$($CAMLC -verbose -c conftest.c 2>&1 | head -1 | sed ['s/^+ \([^ ]*\) .*$/\1/'])
  echo [OCaml uses $CC to compile C files]
fi
AC_PROG_CC

AC_DEFUN([AX_CHECK_COMPILE_FLAG],
[AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_IF
AS_VAR_PUSHDEF([CACHEVAR],[ax_cv_check_[]_AC_LANG_ABBREV[]flags_$4_$1])dnl
AC_CACHE_CHECK([whether _AC_LANG compiler accepts $1], CACHEVAR, [
  ax_check_save_flags=$[]_AC_LANG_PREFIX[]FLAGS
  _AC_LANG_PREFIX[]FLAGS="$[]_AC_LANG_PREFIX[]FLAGS $4 $1"
  AC_COMPILE_IFELSE([m4_default([$5],[AC_LANG_PROGRAM()])],
    [AS_VAR_SET(CACHEVAR,[yes])],
    [AS_VAR_SET(CACHEVAR,[no])])
  _AC_LANG_PREFIX[]FLAGS=$ax_check_save_flags])
AS_VAR_IF(CACHEVAR,yes,
  [m4_default([$2], :)],
  [m4_default([$3], :)])
AS_VAR_POPDEF([CACHEVAR])dnl
])dnl AX_CHECK_COMPILE_FLAGS
OCAML_CC_EXTRA_FLAGS=
AX_CHECK_COMPILE_FLAG(-fno-unwind-tables,[OCAML_CC_EXTRA_FLAGS=-fno-unwind-tables])

# platform
AC_MSG_CHECKING(platform)
echo "print_endline Sys.os_type ;;" > conftest.ml
ac_ocaml_platform=$(ocaml conftest.ml | tr -d '\r')
AC_MSG_RESULT($ac_ocaml_platform)
if test $ac_ocaml_platform = Win32 ; then
    OCAMLWIN32=yes
    EXE=.exe
    XS=.dll
else
    OCAMLWIN32=no
    EXE=
    XS=.so
fi

# Working native Dynlink
AC_MSG_CHECKING(native dynlink)
echo "Dynlink.loadfile \"foo\";;" > test_dynlink.ml
if ($CAMLOPT -shared -o test_dynlink.cmxs test_dynlink.ml) 2> /dev/null ; then
   HAS_NATIVE_DYNLINK=yes
else
   HAS_NATIVE_DYNLINK=no
fi
rm test_dynlink.*

# GTK 3 auto configuration

GTKPKG=gtk+-3.0
AM_PATH_GTK_3_0(3.0.0, :, AC_MSG_ERROR(GTK+ is required))

dnl LABLGTK_PKG(variable, autoconf package, help string, pkgconfig package)
AC_DEFUN([LABLGTK_PKG], [
AC_ARG_WITH($2,[$3],USE_$1=$withval; FORCE_$1=yes, USE_$1=yes; FORCE_$1=no)
if test $USE_$1 = yes ; then
	PKG_CHECK_MODULES($1,$4,,[
	if test $FORCE_$1 = yes ; then
	  AC_MSG_ERROR($2 enforced but no support found)
        else
          USE_$1=no
	fi])
fi
if test $USE_$1 = yes ; then
	USE_$1=1
	$1[]PKG=$4
else
	unset USE_$1
fi
AC_SUBST(USE_$1)])

LABLGTK_PKG(GTKSPELL, gtkspell,
            [  --without-gtkspell      override autodetected gtkspell support],
            gtkspell3-3.0)

LABLGTK_PKG(GTKSOURCEVIEW3, gtksourceview3,
            [  --without-gtksourceview3      override autodetected gtksourceview 3 support],
            gtksourceview-3.0)

LABLGTK_PKG(GTKQUARTZ, quartz,
            [  --without-quartz      override autodetected quartz support],
            gtk+-quartz-3.0)

PKG_CHECK_MODULES(GTKALL,$GTKPKG $GTKSPELLPKG $GTKSOURCEVIEW3PKG $GTKQUARTZPKG)

AC_ARG_ENABLE(debug,
        [  --enable-debug          enable debug mode],
	AC_MSG_RESULT(Debug mode enabled) ; DEBUG=1,
	DEBUG=)

# substitutions to perform

AC_SUBST(EXE)
AC_SUBST(XS)
AC_SUBST(LIBDIR)
AC_SUBST(OCAMLBEST)
AC_SUBST(OCAMLWIN32)
AC_SUBST(OCAMLDEP)
AC_SUBST(OCAMLRUN)
AC_SUBST(OCAMLLEX)
AC_SUBST(OCAMLYACC)
AC_SUBST(THREADS_LIB)
AC_SUBST(HAS_DLL_SUPPORT)
AC_SUBST(HAS_NATIVE_DYNLINK)
AC_SUBST(OCAML_CC_EXTRA_FLAGS)

AC_SUBST(CAMLC)
AC_SUBST(CAMLOPT)
AC_SUBST(OCAMLDOC)
AC_SUBST(CAMLMKTOP)
AC_SUBST(CAMLMKLIB)
AC_SUBST(CAMLP4O)
AC_SUBST(OCAMLFIND)
AC_SUBST(FINDLIBDIR)
AC_SUBST(OCAMLLDCONF)
AC_SUBST(LABLGLDIR)
AC_SUBST(HAVE_SVGZ)
AC_SUBST(USE_CC)
AC_SUBST(DEBUG)

# Finally create the config.make from config.make.in
AC_OUTPUT(config.make)
chmod a-w config.make

AC_DEFUN([CONF_SUMMARY], [
echo $ECHO_N "	$1 $ECHO_C"
if test -n "$USE_$2"
then echo "	yes"
else if test "$FORCE_$2" = "yes"
     then echo "	disabled"
     else echo "	not found"
     fi
fi])

echo ; echo "LablGTK configuration:"
echo "	threads  	$THREADS_LIB"
echo "	native dynlink 	$HAS_NATIVE_DYNLINK"
CONF_SUMMARY(gtkspell, GTKSPELL)
CONF_SUMMARY(gtksourceview3, GTKSOURCEVIEW3)
CONF_SUMMARY(quartz   , GTKQUARTZ)
echo
echo $ECHO_N "	debug   $ECHO_C"
if test -n "$DEBUG" ; then echo "	yes" ; else echo "	no" ; fi
echo  "	C compiler	$CC"
echo  "	Camlp4		$CAMLP4O"
