dnl
dnl  Copyright © 1997-2018 F.Hroch (hroch@physics.muni.cz)
dnl
dnl  This file is part of Munipack.
dnl
dnl  Munipack is free software: you can redistribute it and/or modify
dnl  it under the terms of the GNU General Public License as published by
dnl  the Free Software Foundation, either version 3 of the License, or
dnl  (at your option) any later version.
dnl
dnl  Munipack is distributed in the hope that it will be useful,
dnl  but WITHOUT ANY WARRANTY; without even the implied warranty of
dnl  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dnl  GNU General Public License for more details.
dnl
dnl  You should have received a copy of the GNU General Public License
dnl  along with Munipack.  If not, see <http://www.gnu.org/licenses/>.
dnl
dnl  * Separate variable for Minpack (to prevent use of LIBS)?
dnl

AC_PREREQ([2.69])
AC_INIT([Munipack],[0.5.10],[Filip Hroch <hroch@physics.muni.cz>],
 	[munipack],[http://munipack.physics.muni.cz])

dnl define development build
dnl AC_SUBST(develop,[m4_esyscmd(echo -n $DEVELOPMENT)])
dnl if test "x$develop" = "xyes"; then
dnl AC_DEFINE(DEVELBUILD,[1],[indicates development build])
dnl AC_DEFINE_UNQUOTED(DATEBUILD,[`date +'"%F %T %Z"'`],[configure time])
dnl fi


AC_CANONICAL_BUILD

AM_INIT_AUTOMAKE

AC_PROG_CC
AC_PROG_CXX
AC_PROG_CPP
AC_PROG_FC
AC_PROG_RANLIB
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET

AC_HEADER_STDC

AC_TYPE_SIZE_T
AC_STRUCT_TM
AC_C_CONST
AC_FUNC_MALLOC

AC_FC_MODULE_FLAG
AC_FC_MODULE_OUTPUT_FLAG
AC_FC_MODULE_EXTENSION
dnl Under a modern system, bootstraping required the macros, for older one,
dnl uncomment and modify these lines (and comment out the above lines and AC_PREREQ):
dnl AC_SUBST(FC_MODEXT,[mod])
dnl AC_SUBST(FC_MODINC,[-I])
dnl AC_SUBST(FC_MODOUT,[-J])

dnl cfitsio
AC_CHECK_HEADERS([fitsio.h])
AC_CHECK_LIB([cfitsio], [ffclos],AC_SUBST(LIBCFITSIO,-lcfitsio),
    [AC_MSG_ERROR([cFITSIO library not found.])],[$CFITS_LIBS -lm])

dnl minpack
dnl AC_CHECK_HEADERS([minpack.h])
AC_CHECK_LIB([minpack], [lmder_],[],[
  AC_PROG_F77
  AC_SUBST(MINPACKDIR,[minpack])
  AC_SUBST(LDMINPACK,[-L../minpack])
])

AC_ARG_ENABLE([vo],AC_HELP_STRING([--disable-vo],[disable Virtual Observatory support]),disable_vo=yes,disable_vo=no)
AC_ARG_ENABLE([cli],AC_HELP_STRING([--disable-cli],[disable Command-line User Interface]),disable_cli=yes,disable_cli=no)
AC_ARG_ENABLE([gui],AC_HELP_STRING([--disable-gui],[disable Graphic User Interface]),disable_gui=yes,disable_gui=no)

if test "x$disable_cli" = "xno" -o "x$disable_vo" = "xno" -o "x$disable_gui" = "xno"; then

  dnl wxWidgets
  AM_OPTIONS_WXCONFIG
  AM_PATH_WXCONFIG([3.0.0], [wxWin=1])

  if test -z "$wxWin"; then

    AC_MSG_WARN([

      wxWidgets toolkit not found!

      Continuing without wxWidgets.
      These parts may be unavailable (even required on command-line):

        * Both graphical and command-line interfaces
        * Any functionality related to Virtual observatory

      Otherwise:

        Install wxWidgets on your system

        Please check: wx-config is available in PATH, a directory
        where wxWidgets libraries are installed (returned by
        'wx-config --libs' command) is in LD_LIBRARY_PATH or
        equivalent variable and appropriate wxWidgets version
        is available.
    ])

  fi
fi


wxVO="no"
wxGUI="no"
wxCLI="no"

if test "$wxWin" = 1; then

  if test "x$disable_gui" = "xno"; then
     wxGUI="yes"
     AC_SUBST(XMUNIPACKDIR,[xmunipack])
     AC_SUBST(MATPLOTDIR,[wxMathPlot])
  fi

  if test "x$disable_cli" = "xno"; then
     wxCLI="yes"
     AC_SUBST(MUNIPACKDIR,[munipack])
     AC_SUBST(WX_CXXFLAGS_CLI,[`wx-config --cxxflags base`])
     AC_SUBST(WX_LIBS_CLI,[`wx-config --libs base`])
  fi

  if test "x$disable_vo" = "xno"; then
     wxVO="yes"
     AC_SUBST(WX_LIBS_VO,[`wx-config --libs base,net,xml`])
     AC_SUBST(WX_CXXFLAGS_VO,[`wx-config --cxxflags base,net,xml`])
     AC_SUBST(VODIR,[vo])
  fi
fi

dnl enables bundle (self-installation directory)
AC_ARG_ENABLE([bundle],AC_HELP_STRING([--enable-bundle],[install to self-contained directory (given BUNDLEDIR)]),[enable_bundle=yes],[enable_bundle=no])
AM_CONDITIONAL([BUNDLE], [test "x$enable_bundle" = "xyes"])

dnl set path tree for binary distribution
AC_ARG_VAR([BUNDLEDIR],[prefix of self-contained directory [/opt/munipack]])

dnl set path tree for binary distribution
if test "x$enable_bundle" = "xyes"; then
   BUNDLEDIR=${BUNDLEDIR:-/opt/munipack}

   dnl static gfortran, gcc and g++ libraries - usefull for binary distribution
   AC_SUBST(LIBGFORTRAN,-static-libgfortran)
   AC_SUBST(LIBGCC,-static-libgcc)
   AC_SUBST(LIBSTDCPP,-static-libstdc++)
fi

dnl clean precompiled modules of fortran
AC_SUBST(CLEANFILES,[*.$FC_MODEXT])


AC_CONFIG_HEADERS([config.h])

AC_CONFIG_FILES([minpack/Makefile lib/Makefile preprocess/Makefile
   kombine/Makefile photometry/Makefile artificial/Makefile fitsut/Makefile
   list/Makefile astrometry/Makefile coloring/Makefile vo/Makefile
   munipack/Makefile wxMathPlot/Makefile xmunipack/Makefile doc/Makefile
   Makefile])

AC_OUTPUT

echo
echo " Configured Munipack:"
echo
echo "                      Virtual Observatory support enabled: ${wxVO}"
echo "                           Command line interface enabled: ${wxCLI}"
echo "                         Graphical User Interface enabled: ${wxGUI}"
echo "                                      Bundle installation: ${enable_bundle}"
if test "x$enable_bundle" = "xyes"; then
echo "                                      BUNDLEDIR= ${BUNDLEDIR}"
fi
echo
