##########################################################################
#                                                                        #
#  Copyright (C) Johannes Kanig, Stephane Lescuyer                       #
#  Jean-Christophe Filliatre, Romain Bardou and Francois Bobot           #
#                                                                        #
#  This software is free software; you can redistribute it and/or        #
#  modify it under the terms of the GNU Library General Public           #
#  License version 2.1, with the special exception on linking            #
#  described in file LICENSE.                                            #
#                                                                        #
#  This software 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.                  #
#                                                                        #
##########################################################################

# the script generated by autoconf from this input will set the following
# variables:
#   OCAMLC        "ocamlc" if present in the path, or a failure
#                 or "ocamlc.opt" if present with same version number as ocamlc
#   OCAMLOPT      "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
#   OCAMLWEB      "ocamlweb" (not mandatory)
#   OCAMLWIN32    "yes"/"no" depending on Sys.os_type = "Win32"
#   EXE           ".exe" if OCAMLWIN32=yes, "" otherwise

# The name of the package and its version
AC_INIT(mlpost,0.8.1,[],[],[])

# The compilation date
TODAY=`date`

# Check for Ocaml compilers

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

# we extract Ocaml version number and library path
OCAMLVERSION=`$OCAMLC -v | sed -n -e 's|.*version *\(.*\)$|\1|p' `
echo "ocaml version is $OCAMLVERSION"
OCAMLLIB=`$OCAMLC -v | tail -1 | cut -f 4 -d " "`
if test "$OCAMLLIB" != ${OCAMLLIB#/usr} -a \
        -d /usr/local${OCAMLLIB#/usr}; then
   OCAMLLIBLOCAL=/usr/local${OCAMLLIB#/usr}
   echo "ocaml library path is $OCAMLLIB and $OCAMLLIBLOCAL"
else
        echo "ocaml library path is $OCAMLLIB"
fi

case $OCAMLVERSION in
  3.10.0*)
    AC_MSG_ERROR(ocamlbuild is too buggy in this version. Aborting.)
    ;;
esac

# 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_PATH_PROG(OCAMLOPT,ocamlopt,no)
OCAMLBEST=byte
if test "$OCAMLOPT" = no ; then
	AC_MSG_WARN(Cannot find ocamlopt; bytecode compilation only.)
else
	AC_MSG_CHECKING(ocamlopt version)
	TMPVERSION=`$OCAMLOPT -v | sed -n -e 's|.*version *\(.*\)$|\1|p' `
	if test "$TMPVERSION" != "$OCAMLVERSION" ; then
	    AC_MSG_RESULT(differs from ocamlc; ocamlopt discarded.)
	    OCAMLOPT=no
	else
	    AC_MSG_RESULT(ok)
	    OCAMLBEST=opt
	fi
fi

# checking for ocamlc.opt
AC_PATH_PROG(OCAMLCDOTOPT,ocamlc.opt,no)
if test "$OCAMLCDOTOPT" != no ; then
	AC_MSG_CHECKING(ocamlc.opt version)
	TMPVERSION=`$OCAMLCDOTOPT -v | sed -n -e 's|.*version *\(.*\)$|\1|p' `
	if test "$TMPVERSION" != "$OCAMLVERSION" ; then
	    AC_MSG_RESULT(differs from ocamlc; ocamlc.opt discarded.)
	else
	    AC_MSG_RESULT(ok)
	    OCAMLC=$OCAMLCDOTOPT
	fi
fi

# checking for ocamlopt.opt
if test "$OCAMLOPT" != no ; then
    AC_PATH_PROG(OCAMLOPTDOTOPT,ocamlopt.opt,no)
    if test "$OCAMLOPTDOTOPT" != no ; then
	AC_MSG_CHECKING(ocamlc.opt version)
	TMPVER=`$OCAMLOPTDOTOPT -v | sed -n -e 's|.*version *\(.*\)$|\1|p' `
	if test "$TMPVER" != "$OCAMLVERSION" ; then
	    AC_MSG_RESULT(differs from ocamlc; ocamlopt.opt discarded.)
	else
	    AC_MSG_RESULT(ok)
	    OCAMLOPT=$OCAMLOPTDOTOPT
	fi
    fi
fi

# checking for camlp4o
AC_PATH_PROG(CAMLP4O,camlp4o,no)
if test "$CAMLP4O" != no ; then
  AC_MSG_CHECKING(camlp4o version)
  TMPVER=`$CAMLP4O -version`
  if test "$TMPVER" != "$OCAMLVERSION" ; then
    AC_MSG_ERROR(differs from ocamlc; Aborting.)
  else
    AC_MSG_RESULT(ok)
  fi
fi

# currently commented out because some other part of the code relies on
# camlp4o in bytecode
#AC_PATH_PROG(CAMLP4ODOTOPT, camlp4o.opt,no)
#if test "$CAMLP4ODOTOPT" != no ; then
#  AC_MSG_CHECKING(camlp4o.opt version)
#  TMPVER=`$CAMLP4ODOTOPT -version`
#  if test "$TMPVER" != "$OCAMLVERSION" ; then
#    AC_MSG_ERROR(differs from ocamlc; Aborting.)
#  else
#    AC_MSG_RESULT(ok)
#    CAMLP4O=$CAMLP4ODOTOPT
#  fi
#fi

#checking for ocamldoc
AC_PATH_PROG(OCAMLDOC,ocamldoc,no)


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

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

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

#First check that the versions for ocamlbuild are OK
AC_PATH_PROG(OCAMLBUILD, ocamlbuild, no)
if test "$OCAMLBUILD" = no; then
    AC_MSG_ERROR(Cannot find ocamlbuild.)
else
    AC_MSG_CHECKING(ocamlbuild version)
    TMPVER=`$OCAMLBUILD -version | sed -n -e 's|.*ocamlbuild *\(.*\)$|\1|p' `
    case $OCAMLVERSION in
      3.10.1|3.10.2*)
        if test "$TMPVER" != "0.1"; then
          AC_MSG_ERROR(ocamlbuild version differs from ocamlc. Aborting.)
        else
          AC_MSG_RESULT(ok)
        fi ;;
      *)
        if test "$TMPVER" != "$OCAMLVERSION" ; then
          AC_MSG_ERROR(ocamlbuild version differs from ocamlc. Aborting.)
        else
          AC_MSG_RESULT(ok)
        fi ;;
    esac
fi
# Then check that we are dealing with ocamlbuild at the right place
AC_MSG_CHECKING(ocamlbuild place)
OCAMLBUILDLIB=$(ocamlbuild -where)
if test "$OCAMLBUILDLIB" != "${OCAMLLIB}/ocamlbuild"; then
  echo "ocamlbuild present but your ocamlbuild is not compatible with your ocamlc:"
  echo "ocamlbuild : $OCAMLBUILDLIB, ocamlc : $OCAMLLIB"
  AC_MSG_ERROR(ocamlbuild not at the right place)
else
   AC_MSG_RESULT(ok)
fi

AC_PATH_PROG(OCAMLWEB,ocamlweb,true)

# platform
AC_MSG_CHECKING(platform)
if echo "let _ = Sys.os_type" | ocaml | grep -q Win32; then
    AC_MSG_RESULT(Win32)
    OCAMLWIN32=yes
    EXE=.exe
    LIBEXT=.lib
    OBJEXT=.obj
else
    AC_MSG_RESULT(not Win32)
    OCAMLWIN32=no
    EXE=
    LIBEXT=.a
    OBJEXT=.o
fi


## Where are the library we need
# we look for ocamlfind; if not present, we just don't use it to find
# libraries
AC_CHECK_PROG(USEOCAMLFIND,ocamlfind,yes,no)

if test "$USEOCAMLFIND" = yes; then
   OCAMLFINDLIB=$(ocamlfind printconf stdlib)
   OCAMLFIND=$(which ocamlfind)
   if test "$OCAMLFINDLIB" != "$OCAMLLIB"; then
   USEOCAMLFIND=no;
   echo "but your ocamlfind is not compatible with your ocamlc:"
   echo "ocamlfind : $OCAMLFINDLIB, ocamlc : $OCAMLLIB"
   fi
fi

if test "$LIBDIR" = ""; then
   if test "$USEOCAMLFIND" = yes; then
        LIBDIR=$(ocamlfind printconf destdir)/mlpost
   else
        LIBDIR=$OCAMLLIB/mlpost
   fi
fi
echo "Mlpost library will be installed in: $LIBDIR"

AC_ARG_ENABLE(cairo,
  [  --enable-cairo enable the cairo backend (requires cairo library, implies --enable-concrete)[default=yes]],,
  enable_cairo=yes)

CAIRO=no
if test "$enable_cairo" = yes; then
  # checking for mlcairo
  if test "$USEOCAMLFIND" = yes; then
     CAIROLIB=$(ocamlfind query cairo)
  fi

  if test -n "$CAIROLIB";then
     echo "ocamlfind found cairo in $CAIROLIB"
     CAIRO=yes
  else
      AC_CHECK_FILE($OCAMLLIB/cairo/cairo.cma,CAIRO=yes,CAIRO=no)
      if test "$CAIRO" = yes; then
         CAIROLIB=$OCAMLLIB/cairo/
      elif test -n "$OCAMLLIBLOCAL"; then
         AC_CHECK_FILE($OCAMLLIBLOCAL/cairo/cairo.cma,CAIRO=yes,CAIRO=no)
         if test "$CAIRO" = yes; then
         CAIROLIB=$OCAMLLIBLOCAL/cairo/
         fi
      fi
  fi
fi

AC_ARG_ENABLE(concrete,
  [  --enable-concrete enable concrete computations (requires bitstring library) [default=yes]],,
  enable_concrete=yes)

BITSTRING=no
if test "$enable_concrete" = yes; then
  if test "$USEOCAMLFIND" = yes; then
     BITSTRINGLIB=$(ocamlfind query bitstring)
  fi

  if test -n "$BITSTRINGLIB";then
     echo "ocamlfind found bitstring in $BITSTRINGLIB"
     BITSTRING=yes
  else
      AC_CHECK_FILE($OCAMLLIB/bitstring/bitstring.cma,BITSTRING=yes,BITSTRING=no)
      if test "$BITSTRING" = yes; then
         BITSTRINGLIB=$OCAMLLIB/bitstring/
      elif test -n "$OCAMLLIBLOCAL"; then
         AC_CHECK_FILE($OCAMLLIBLOCAL/bitstring/bitstring.cma,BITSTRING=yes,BITSTRING=no)
         if test "$BITSTRING" = yes; then
         BITSTRINGLIB=$OCAMLLIBLOCAL/bitstring/
         fi
      fi
  fi
fi

if test "$BITSTRING" = yes; then
  if test "$CAIRO" = yes; then
    TAGS="-tags cairo_yes,concrete_yes"
    INCLUDELIBS="-I $CAIROLIB -I $BITSTRINGLIB"
    METAREQUIRESPACKAGE="unix cairo bitstring"
  else
    CAIRO=no
    TAGS="-tag concrete_yes"
    INCLUDELIBS="-I $BITSTRINGLIB"
    METAREQUIRESPACKAGE="unix bitstring"
  fi
else
  CAIRO=no
  BITSTRING=no
  TAGS=""
  INCLUDELIBS=""
  METAREQUIRESPACKAGE="unix"
fi

#TEMPORAIRE
#CAIRO=no
#INCLUDELIBS=""


AC_ARG_ENABLE(lablgtk,
  [  --enable-lablgtk enable the cairo backend (requires cairo library, implies --enable-lablgtk)[default=yes]],,
  enable_lablgtk=yes)

LABLGTK2=no
if test "$enable_lablgtk" = yes; then
   # checking for lablgtk2
   if test "$USEOCAMLFIND" == yes; then
      LABLGTK2LIB=$(ocamlfind query lablgtk2)
   fi

   if test -n "$LABLGTK2LIB";then
      echo "ocamlfind found lablgtk2 in $LABLGTK2LIB"
   else
    AC_CHECK_FILE($OCAMLLIB/lablgtk2/lablgtk.cma,LABLGTK2=yes,LABLGTK2=no)
    if test "$LABLGTK2" = yes; then
       LABLGTK2LIB=$OCAMLLIB/lablgtk2/
    elif test -n "$OCAMLLIBLOCAL"; then
       AC_CHECK_FILE($OCAMLLIBLOCAL/lablgtk2/lablgtk2.cma,LABLGTK2=yes,LABLGTK2=no)
       if test "$LABLGTK2" = yes; then
       LABLGTK2LIB=$OCAMLLIBLOCAL/lablgtk2/
       fi
    fi
   fi
fi

##AC_CHECK_FILE($OCAMLLIB/lablgtk2/lablgtk.cma,LABLGTK2=yes,LABLGTK2=no)
## AC_CHECK_PROG(LABLGTK2,lablgtk2,yes,no) not always available (Win32)
if test -n "$LABLGTK2LIB" ; then
   LABLGTK2=yes
dnl    INCLUDEGTK2="-I $LABLGTK2LIB"
   INCLUDEGTK2="-I +lablgtk2"
else
   LABLGTK2=no
fi

# checking for cairo.lablgtk2
if test "$USEOCAMLFIND" == yes; then
   CAIROLABLGTK2LIB=$(ocamlfind query cairo.lablgtk2)
fi

if test -n "$CAIROLABLGTK2LIB";then
   echo "ocamlfind found cairo.lablgtk2 in $CAIROLABLGTK2LIB"
else
    AC_CHECK_FILE($OCAMLLIB/cairo/cairo_lablgtk.cma,
        CAIROLABLGTK2=yes,CAIROLABLGTK2=no)
    if test "$CAIROLABLGTK2" = yes; then
        CAIROLABLGTK2LIB=$OCAMLLIB/lablgtk2/
    elif test -n "$OCAMLLIBLOCAL"; then
       AC_CHECK_FILE($OCAMLLIBLOCAL/cairo/cairo_lablgtk.cma,
        CAIROLABLGTK2=yes,CAIROLABLGTK2=no)
       if test "$CAIROLABLGTK2" = yes; then
        CAIROLABLGTK2LIB=$OCAMLLIBLOCAL/cairo/
       fi
    fi
fi

if test -n "$LABLGTK2LIB" ; then
   CAIROLABLGTK2=yes
else
   CAIROLABLGTK2=no
fi

#Viewer for ps and pdf
AC_CHECK_PROGS(PSVIEWER,gv evince)
AC_CHECK_PROGS(PDFVIEWER,xpdf acroread evince)

# substitutions to perform
AC_SUBST(OCAMLC)
AC_SUBST(OCAMLOPT)
AC_SUBST(CAMLP4O)
AC_SUBST(OCAMLDOC)
AC_SUBST(OCAMLDEP)
AC_SUBST(OCAMLLEX)
AC_SUBST(OCAMLYACC)
AC_SUBST(OCAMLBEST)
AC_SUBST(OCAMLVERSION)
AC_SUBST(OCAMLWEB)
AC_SUBST(OCAMLFIND)
AC_SUBST(OCAMLBUILD)
AC_SUBST(USEOCAMLFIND)
AC_SUBST(LABLGTK2)
AC_SUBST(INCLUDEGTK2)
AC_SUBST(LABLGTK2LIB)
AC_SUBST(CAIROLABLGTK2)
AC_SUBST(CAIROLABLGTK2LIB)
AC_SUBST(TAGS)
AC_SUBST(CAIROLIB)
AC_SUBST(INCLUDELIBS)
AC_SUBST(BITSTRINGLIB)
AC_SUBST(OCAMLWIN32)
AC_SUBST(EXE)
AC_SUBST(LIBEXT)
AC_SUBST(OBJEXT)
AC_SUBST(LIBDIR)
AC_SUBST(PACKAGE_VERSION)
AC_SUBST(TODAY)
AC_SUBST(METAREQUIRESPACKAGE)

echo "---------------------------------------------------"
echo "  Mlpost library will be installed in: $LIBDIR"
echo -n "  native code compilation: "
if test "$OCAMLBEST" == "opt"; then echo "yes"; else echo "no"; fi
echo "  Support for concrete computations in mlpost: "$BITSTRING
echo "  Cairo support in mlpost: "$CAIRO
echo "  Contrib mlpost_lablgtk : "$LABLGTK2
echo "---------------------------------------------------"

# Finally create the Makefile from Makefile.in

AC_CONFIG_FILES(META version.ml Makefile myocamlbuild.ml)
AC_OUTPUT
chmod a-w Makefile
chmod a-w myocamlbuild.ml
chmod a-w META
chmod a-w version.ml
