# vim: expandtab sw=2
# Copyright (C) 2000, 2001, 2002, 2003 
# Stefan Buehler   <sbuehler@uni-bremen.de>
# Patrick Eriksson <patrick@rss.chalmers.se>
# Oliver Lemke     <olemke@uni-bremen.de>
#
#
# This file is free software; as a special exception the author gives
# unlimited permission to copy and/or distribute it, with or without 
# modifications, as long as this notice is preserved.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


AC_INIT(autogen.sh)
AM_CONFIG_HEADER(config.h)

# The ARTS release number must go here:
AM_INIT_AUTOMAKE(arts,1.0.214)

AM_MAINTAINER_MODE

#
# First of all set the default PREFIX if not given
# (needed for arts data path later)
#
if test x${prefix} = xNONE; then
  prefix="/usr/local"
fi

# C Compiler:
AC_PROG_CC
AC_PROG_CPP
# C++ Compiler:
AC_PROG_CXX
# C++ Preprocessor:
AC_PROG_CXXCPP

# Determine the name of the ranlib program
AC_PROG_RANLIB

# What's used by make install:
AC_PROG_INSTALL

# Provide standard headers:
AC_STDC_HEADERS

# Check compiler characteristics
AC_C_INLINE
AC_C_CONST
AC_TYPE_SIZE_T
AC_CHECK_SIZEOF(float, 8)
AC_CHECK_SIZEOF(double, 16)
AC_CHECK_SIZEOF(int, 4)
AC_CHECK_SIZEOF(long, 8)
AC_CHECK_SIZEOF(size_t, 8)

# Special headers that we need:
# AC_HAVE_HEADERS(getopt.h) (now included in the source)
AC_CHECK_HEADER(math.h, , AC_MSG_ERROR(required header file <math.h> missing))

#
# Check for math library
#
AC_CHECK_LIB(m, sin, ,
  AC_MSG_ERROR(function $func not found in math library) )

#
# Check for sstream header
#
AC_LANG_CPLUSPLUS
AC_CHECK_HEADER(sstream, ac_sstream=yes, ac_sstream=no)
if test x$ac_sstream = xyes; then
  AC_DEFINE_UNQUOTED(HAVE_SSTREAM, , Use system sstream include)
fi
AM_CONDITIONAL(HAVE_SSTREAM, test x$ac_sstream = xyes)


#
# HDF support
#
AC_ARG_WITH([hdf],
  [  --with-hdf=DIR          HDF binary file support DIR default is autodetect],
  [case "${withval}" in
    autodetect) ac_hdf_support=autodetect ;;
    yes) ac_hdf_support=yes ;;
     no) ac_hdf_support=no ;;
      *) ac_hdf_support=unknown ;;
   esac],
  [ac_hdf_support=autodetect])

if test x$ac_hdf_support = xunknown; then
  if test -d "${withval}/lib" -a -d "${withval}/include"; then
    LDFLAGS="$LDFLAGS -L${withval}/lib"
    CPPFLAGS="$CPPFLAGS -I${withval}/include"
    ac_hdf=yes
  else
    AC_MSG_ERROR(${withval}/lib and/or ${withval}/include not found)
    ac_hdf=no
  fi
fi

if test x$ac_hdf_support = xno; then
  ac_hdf=no
else
  AC_CHECK_LIB(sz, SZ_DecompressInit, , ac_szlib=no, )
  AC_CHECK_LIB(z, gzopen,, ac_hdf=no, )
  AC_CHECK_LIB(jpeg, jpeg_start_compress,, ac_hdf=no, -lz )
  AC_CHECK_HEADER(hdf.h, ac_hdf=yes, ac_hdf=no)
  AC_CHECK_LIB(mfhdf, hdf_close,, ac_hdf=no, -ldf -ljpeg -lz )
  AC_CHECK_LIB(df, Hopen,, ac_hdf=no, -lmfhdf -ljpeg -lz )
fi

if test x$ac_hdf = xyes; then
  AC_DEFINE_UNQUOTED(HDF_SUPPORT, , Compile with HDF support)
fi

AM_CONDITIONAL(HDF_SUPPORT, test x$ac_hdf_support = xyes)

#
# Default floating-point type
#
AC_ARG_WITH(numeric-type,
[  --with-numeric-type=ARG    Set floating-point type (default=double)],
[case "${withval}" in
  double) ac_numeric_type=double ;;
   float) ac_numeric_type=float ;;
       *) AC_MSG_ERROR(bad value ${withval} for --with-numeric-type) ;;
esac],[ ac_numeric_type=double ])

AC_MSG_RESULT(Setting Numeric type to $ac_numeric_type)
AC_DEFINE_UNQUOTED(NUMERIC, $ac_numeric_type, Default floating-point type)
if test x$ac_numeric_type = xdouble; then
  AC_DEFINE_UNQUOTED(USE_DOUBLE,, Whether double precision is in use)
fi


#
# Default index type
#
AC_ARG_WITH(index-type,
[  --with-index-type=ARG    Set index type (default=long)],
[case "${withval}" in
  short) ac_index_type=short ;;
  int)  ac_index_type=int ;;
  long)  ac_index_type=long ;;
  size_t)  ac_index_type=size_t ;;
    *) AC_MSG_ERROR(bad value ${withval} for --with-index-type) ;;
esac],[ ac_index_type=long ])

AC_MSG_RESULT(Setting Index type to $ac_index_type)
AC_DEFINE_UNQUOTED(INDEX, $ac_index_type, Default index type)


#
# This is needed to put the corrects data paths into the example files
# in doc/examples:
#
AC_ARG_WITH(arts-data,
[  --with-arts-data=ARG       Set path to the arts data files]
[                             (default=$PREFIX/share/arts-data)],
[case "${withval}" in
  yes) ac_arts_data=default ;;
   no) ac_arts_data=no ;;
    *) ac_arts_data=${withval};;
esac], [ac_arts_data=default])

if test x$ac_arts_data = xdefault; then
  ac_arts_data="${prefix}/share/arts-data"
fi

if test ! x$ac_arts_data = xno; then
  #
  # Simple check to see whether the arts-data package exists
  # at the given location
  #
  AC_MSG_CHECKING(for arts data in $ac_arts_data)
  if test -d "${ac_arts_data}"; then
    AC_MSG_RESULT(found)
  else
    AC_MSG_RESULT(not found)
    ac_arts_data=no
  fi

  if test ! x$ac_arts_data = xno; then
    AC_MSG_CHECKING(for atmospheric data in $ac_arts_data)
    if test -d "${ac_arts_data}/atmosphere"; then
      AC_MSG_RESULT(found)
    else
      AC_MSG_RESULT(not found)
      ac_arts_data=no
    fi
  fi
  
  if test ! x$ac_arts_data = xno; then
    AC_MSG_CHECKING(for spectroscopy data in $ac_arts_data)
    if test -d ${ac_arts_data}/spectroscopy; then
      AC_MSG_RESULT(found)
    else
      AC_MSG_RESULT(not found)
      ac_arts_data=no
    fi
  fi
fi

AM_CONDITIONAL(ARTS_DATA_PATH, test ! x$ac_arts_data = xno)
AC_DEFINE_UNQUOTED(ARTS_DATA_PATH, "$ac_arts_data", ARTS data path)
AC_SUBST(ac_arts_data)



#
# Enable/disable debugging
#
AC_MSG_CHECKING(whether debugging should be activated)
AC_ARG_ENABLE(debug,
[  --enable-debug   Activate debugging mode (default=no)],
[case "${enableval}" in
  yes) ac_debug=yes ;;
   no) ac_debug=no ;;
    *) AC_MSG_ERROR(bad value ${enableval} for --enable-debug) ;;
esac],[ ac_debug=$USE_MAINTAINER_MODE ])
AM_CONDITIONAL(DEBUGGING, test x$ac_debug = xyes)
AC_MSG_RESULT([$ac_debug])


#
# Enable/disable warnings
#
AC_MSG_CHECKING(whether warnings should be enabled)
AC_ARG_ENABLE(more-warnings,
[  --enable-more-warnings   Enable more warnings (default=no)],
[case "${enableval}" in
  yes) ac_more_warnings=yes ;;
   no) ac_more_warnings=no ;;
    *) AC_MSG_ERROR(bad value ${enableval} for --enable-more-warnings) ;;
esac],[ ac_more_warnings=no ])
AC_MSG_RESULT([$ac_more_warnings])


#
# Toggle documentation
#
if test x$USE_MAINTAINER_MODE = xyes; then
  ac_docgen=yes
else
  ac_docgen=no
fi
AC_MSG_CHECKING(whether documentation should be generated)
AM_CONDITIONAL(ARTS_GENDOCS, test x$ac_docgen = xyes)
AC_MSG_RESULT([$ac_docgen])


#
# Check for the required tools to build the docs
#
if test x$ac_docgen = xyes; then
    AC_CHECK_PROG(ac_doxygen, doxygen, yes, no)
    if test x$ac_doxygen = xno; then
        AC_MSG_WARN(Doxygen is required to build the documentation - Get it from http://www.doxygen.org/)
    fi

    AC_CHECK_PROG(ac_latex, latex, yes, no)
    if test x$ac_latex = xno; then
        AC_MSG_ERROR(LaTeX is required to build the documentation!!!)
    fi

    AC_CHECK_PROG(ac_pdflatex, pdflatex, yes, no)
    if test x$ac_pdflatex = xno; then
        AC_MSG_ERROR(pdfLaTeX is required to build the documentation!!!)
    fi

fi

WARNING_FLAGS=
if test x$ac_cv_cxx_compiler_gnu = xyes -o x$ac_cv_prog_gxx = xyes; then
  WARNING_FLAGS="-W -Wall -Wconversion"

  if test x$ac_more_warnings = xyes; then
    WARNING_FLAGS="$WARNING_FLAGS" #-Werror"
  fi

  CXXFLAGS="$CXXFLAGS -fno-strict-aliasing"

fi


#
# OS check
#
ac_osname=`uname`
ac_osversion=`uname -r`

AM_CONDITIONAL(OS_NAME, test "$ac_osname")
AC_DEFINE_UNQUOTED(OS_NAME, "$ac_osname", Operating system name)

AM_CONDITIONAL(OS_VERSION, test "$ac_osversion")
AC_DEFINE_UNQUOTED(OS_VERSION, "$ac_osversion", Operating system version)

case "$ac_osname" in
  Linux) CPPFLAGS="$CPPFLAGS -DLINUX" ;;
  HP-UX) CPPFLAGS="$CPPFLAGS -DHPUX" ;;
  SunOS) CPPFLAGS="$CPPFLAGS -DSUNOS" ;;
esac


#
# Compile flags switches
#
if test x$ac_debug = xyes; then
  DEBUG_FLAGS="-g"
else
  DEBUG_FLAGS=""
  CPPFLAGS="$CPPFLAGS -DNDEBUG"
fi

CFLAGS="$CFLAGS $DEBUG_FLAGS $WARNING_FLAGS"
if test x$ac_cv_cxx_compiler_gnu = xyes -o x$ac_cv_prog_gxx = xyes; then
  CXXFLAGS_FIXED="$DEBUG_FLAGS -ftemplate-depth-60 $WARNING_FLAGS"
  CXXFLAGS="$CXXFLAGS $CXXFLAGS_FIXED"
else
  CXXFLAGS_FIXED="$CXXFLAGS $DEBUG_FLAGS"
  CXXFLAGS="$CXXFLAGS_FIXED"
fi

AC_SUBST(CXXFLAGS_FIXED)

AM_CONDITIONAL(COMPILE_FLAGS, test "$CXXFLAGS")
AC_DEFINE_UNQUOTED(COMPILE_FLAGS, "$CXXFLAGS", Compile Flags)

AC_OUTPUT([
arts.spec
Makefile 
   ami/Makefile 
      ami/arts/Makefile
      ami/file/Makefile
      ami/general/Makefile
      ami/hmatrix/Makefile 
      ami/math/Makefile
      ami/path/Makefile
      ami/physics/Makefile
      ami/plot/Makefile
      ami/retrieval/Makefile
   aii/Makefile 
   doc/Makefile 
   doc/index.html
      doc/icons/Makefile 
      doc/doxygen/Makefile
      doc/doxygen/Doxyfile
      doc/doxygen/html/Makefile
      doc/uguide/Makefile 
      doc/uguide/auto_version.tex 
         doc/uguide/Figs/Makefile 
      doc/examples/Makefile 
         doc/examples/absorption_example.arts
         doc/examples/amsu_example.arts
         doc/examples/batch_example.arts
         doc/examples/clouds_example.arts
         doc/examples/cont_example.arts
         doc/examples/fullmodels_example.arts
         doc/examples/radiosonde_example.arts
         doc/examples/rt_example.arts
         doc/examples/wfs_spectro_example.arts
      doc/emacs/Makefile
   m4/Makefile 
   src/Makefile
])

AC_MSG_RESULT()
AC_MSG_RESULT([*******************************************************])
AC_MSG_RESULT([              ARTS configuration summary])
AC_MSG_RESULT()
AC_MSG_RESULT([  Maintainer mode          : $USE_MAINTAINER_MODE])
AC_MSG_RESULT([  Debugging                : $ac_debug])
AC_MSG_RESULT([  Numeric type             : $ac_numeric_type])
AC_MSG_RESULT([  Index type               : $ac_index_type])
AC_MSG_RESULT([  Regenerate documentation : $ac_docgen])
if test $ac_hdf_support = autodetect; then
  AC_MSG_RESULT([  HDF support              : $ac_hdf (autodetected)])
else
  AC_MSG_RESULT([  HDF support              : $ac_hdf])
fi
if test $ac_hdf = no; then
  AC_MSG_RESULT([  WARNING: Without HDF you cannot create binary files !!!])
fi
AC_MSG_RESULT()
AC_MSG_RESULT([  CPPFLAGS : $CPPFLAGS])
AC_MSG_RESULT([  CFLAGS   : $CFLAGS])
AC_MSG_RESULT([  CXXFLAGS : $CXXFLAGS])
AC_MSG_RESULT([  LDFLAGS  : $LDFLAGS])
AC_MSG_RESULT()
if test $ac_arts_data = no; then
  AC_MSG_RESULT([  ARTS data path : not installed])
else
  AC_MSG_RESULT([  ARTS data path : $ac_arts_data])
fi
AC_MSG_RESULT([*******************************************************])
AC_MSG_RESULT()

