##########################################################################
#                                                                        #
#  autoconf input for Objective Caml programs                            #
#  Copyright (C) 2001 Jean-Christophe Filliātre                          #
#    from a first script by Georges Mariano                              #
#                                                                        #
#  Script modified by Julien Signoles for the Calendar library.          #
#                                                                        #
#  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 (see <http://www.gnu.org/licenses/>).                         #
#                                                                        #
##########################################################################

# 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"
#   OCAMLLIB      the path to the ocaml standard library
#   OCAMLFIND	  "ocamlfind"
#   OCAMLVERSION  the ocaml version number
#   OCAMLDOC	  "ocamldoc" or "ocamldoc.opt" (not mandatory)
#   OCAMLWEB      "ocamlweb" (not mandatory)
#   OCAMLWC	  "ocamlwc" (not mandatory)
#   OCAMLDOT	  "ocamldot" (not mandatory)

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

# Check for Ocaml compilers

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

# we extract Ocaml version number and library path: ocaml 3.09.1 is required
OCAMLVERSION=`$OCAMLC -v | sed -n -e 's|.*version *\(.*\)$|\1|p' `
echo $ECHO_N "ocaml version is $OCAMLVERSION: $ECHO_C"
case $OCAMLVERSION in
  3.09.**) OCAMLMAJOR=3; echo "${ECHO_T}Good!";;
  3.09*) OCAMLMAJOR=3; echo "${ECHO_T}Warning: unsupported version. Compile at your own risks";;
  3.0*) echo "${ECHO_T}Unsupported version!"; exit 2;;
  3.*) OCAMLMAJOR=3; echo "${ECHO_T}Good!";;
  4.*) OCAMLMAJOR=4; echo "${ECHO_T}Good!";;
  *) echo "${ECHO_T}Unsupported version!"; exit 2;;
esac

OCAMLLIB=`$OCAMLC -where | tr -d '\r'`
echo "ocaml library path is $OCAMLLIB"

# 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(OCAMLOPT,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_CHECK_PROG(OCAMLCDOTOPT,ocamlc.opt,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_CHECK_PROG(OCAMLOPTDOTOPT,ocamlopt.opt,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

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

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

# Not mandatory tools

AC_CHECK_PROG(OCAMLDOC,ocamldoc,ocamldoc,true)
if test "$OCAMLDOC" = true ; then
    AC_MSG_WARN(Cannot find ocamldoc)
# JS: documentation generator does not work with ocamldoc.opt :(
#else
#    AC_CHECK_PROG(OCAMLDOCOPT,ocamldoc.opt,ocamldoc.opt,no)
#    if test "$OCAMLDOCOPT" != no ; then
#	OCAMLDOC=$OCAMLDOCOPT
#    fi
fi

AC_CHECK_PROG(OCAMLWEB,ocamlweb,ocamlweb,ocamlweb)
AC_CHECK_PROG(OCAMLWC,ocamlwc,ocamlwc,ocamlwc)
AC_CHECK_PROG(OCAMLDOT,ocamldot,ocamldot,ocamldot)

# platform
AC_MSG_CHECKING(platform)
#if echo "let _ = Sys.os_type" | ocaml | grep -q Win32; then
if test -z "$system" ; then
    system=`ocamlc -config 2>/dev/null|grep system|sed 's/system: //'`
    AC_MSG_RESULT(${system})
    # This may be
    # - mingw
    # - win32
    # - win64
    # - cygwin
    # - some other string means Unix
    # - empty means ocamlc does not support -config
fi

use_cygpath=0
case "$system" in
    mingw) use_cygpath=1;;
    win32) use_cygpath=1;;
    win64) use_cygpath=1;;
esac

if test ${use_cygpath} -gt 0 ; then
    OCAMLDEP="${OCAMLDEP} -slash"
fi

# extension for object and library files
AC_MSG_CHECKING(extension for object files)
OBJEXT=`ocamlc -config 2>/dev/null|grep ext_obj|sed 's/ext_obj: //'`
AC_MSG_RESULT(${OBJEXT})

AC_MSG_CHECKING(extension for library files)
LIBEXT=`ocamlc -config 2>/dev/null|grep ext_lib|sed 's/ext_lib: //'`
AC_MSG_RESULT(${LIBEXT})

# Checking workable native dynlink
HAS_NATDYNLINK=no
AC_CHECK_FILE(${OCAMLLIB}/dynlink.cmxa,HAS_NATDYNLINK=yes,HAS_NATDYNLINK=no)

if test "$HAS_NATDYNLINK" != "no" ; then
  echo "let f x y = Dynlink.loadfile \"foo\"; ignore (Dynlink.is_native); abs_float (x -. y)" > test_dynlink.ml
  if ($OCAMLOPT -shared -linkall -o test_dynlink.cmxs test_dynlink.ml) \
        2> /dev/null ; \
  then
    HAS_NATDYNLINK=yes
    AC_MSG_RESULT([native dynlink works fine. Great.])
  else
    HAS_NATDYNLINK=no
    AC_MSG_WARN([native dynlink does not work on your platform. Disabled.])
  fi
  rm -f test_dynlink.*
fi

# substitutions to perform
AC_SUBST(OCAMLC)
AC_SUBST(OCAMLOPT)
AC_SUBST(OCAMLDEP)
AC_SUBST(OCAMLFIND)
AC_SUBST(OCAMLBEST)
AC_SUBST(OCAMLMAJOR)
AC_SUBST(OCAMLLIB)
AC_SUBST(OCAMLDOC)
AC_SUBST(OCAMLWEB)
AC_SUBST(OCAMLWC)
AC_SUBST(OCAMLDOT)
AC_SUBST(OBJEXT)
AC_SUBST(LIBEXT)
AC_SUBST(HAS_NATDYNLINK)

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