dnl Process this file with autoconf to produce a configure script.
define([AC_CACHE_LOAD], )dnl desable caching for debugging
define([AC_CACHE_SAVE], )dnl

#=============================================================================#
# configure script for the PEGASE.HR package				      #
#
# This configuration scripts checks the availability of the Fortran compiler  #
# and determine which options to use.
# It checks if the library cfitsio is available and sets the corresponding
# loader flags.
# It generates the folowing files:
#   Makefile  (from Makefile.in)
#   include/peg_config.f (from peg_config.in)
#
# The file configure is automatically created from configure.in by the
# program autoconf. 
#=============================================================================#

AC_INIT([PEGASE.HR], - , [leborgne@iap.fr]) 

version=`cat VERSION`
AC_DEFINE_UNQUOTED(PL_VERSION, "$version")
AC_MSG_RESULT([starting configuration for PEGASE.HR package])
AC_SUBST(version)

# make sure we are using a recent autoconf version
AC_PREREQ(2.50)       
AC_COPYRIGHT([Copyright 2004 PEGASEHR development team])

AC_CONFIG_SRCDIR([src/SSPs_HR.f])                     # do we have the sources ?

#----------------------------------------------------------------------------
# Define the root directory of PEGASE.HR in Makefile and peg_config.f

PEGASE_ROOT=`pwd`;
AC_SUBST(PEGASE_ROOT)

#----------------------------------------------------------------------------
# Can we install here?

if test -f configure; then 
  test -w configure || AC_MSG_ERROR([
*******************************************************************
PEGASE.HR configuration failed : 
Permission to write files in the current directory is not granted.
Therefore, it will not be possible to install compiled files...
*******************************************************************
])
fi

#----------------------------------------------------------------------------
# Fortran 77 compiler

AC_PROG_CC

#   check the Fortran compiler to set the variables: F77 and FFLAGS
#          1) if F77 is g77 set properly FFLAGS (if not "forced") and CFORTRAN
#          2) else, if F77 is not g77, and if FFLAGS is not yet defined,
#               set FFLAGS according to $host_os, for Linux we assume that 
#               the compiler is f2c
fflags_save=$FFLAGS

AC_PROG_F77(f77 gfortran fort77 xlf g77 f90 xlf90 f2c g95 xlf ifortran if90 f95 ifort fl32 )

echo "F77 is $F77"
FFLAGS="$FFLAGS -fno-backslash -fdollar-ok -ffixed-line-length-none -fno-automatic"

AC_MSG_RESULT([setting Fortran compiler options to $FFLAGS])

AC_SUBST(F77)
AC_SUBST(FFLAGS)

#----------------------------------------------------------------------------
# cfitsio library

cd cfitsio && ./configure && make && cd ..
#----------------------------------------------------------------------------

#                                              Math library 
#export LD_LIBRARY_PATH
AC_SEARCH_LIBS(fabs, m)


###  #                         Where do we take the source from ?
###  AC_ARG_WITH(libraries,
###  [  --with-libraries[=DIRS] look for additional libraries in DIRS.
###                            This may be used to find cfitstio],
###  )
###  if test "x${with_libraries}" != "x"; then
###    CONFIGOPTIONS="--with-libraries=${with_libraries}"
###  
###    AC_MSG_CHECKING(for additional library path)
###    library_dirs=
###    ac_save_IFS=$IFS
###    IFS="${IFS}:"
###    for dir in ${with_libraries}; do
###      if test -d "$dir"; then
###       library_dirs="$library_dirs -L$dir"
###      else
###        AC_MSG_WARN([*** Library directory $dir does not exist.])
###      fi
###    done
###    IFS=$ac_save_IFS
###    AC_MSG_RESULT($library_dirs)
###    LIBS="$LIBS $library_dirs -lm"
###  fi
###  

LIBS="$LIBS -Lcfitsio -lcfitsio -lm"

AC_CHECK_LIB(cfitsio,ffukye, [], [
        echo "cfitsio not found. You have two options :"
	echo "1) Compile it from source by copy-pasting the following line:"
        echo "   cd cfitsio ; ./configure ; make distclean ; make; cd .."
        echo "   Then run ./configure again."
	echo "2) If libcfitsio.a exists on your host, specify its path with "
	echo "   the option './configure --with-libraries=mypath'"
        exit -1
        ])


#AC_CHECK_LIB(cfitsio,ffukye, ,AC_MSG_ERROR([[
#*******************************************************************
#PEGASE.HR configuration failed : 
#The cfitsio library was not found.
#If libcfitsio is installed on the current host, specify its path
#with the option --with-libraries of the configure script.
#Otherwise you may have to install it before PEGASE.HR from the
#sources: 
#   http://heasarc.gsfc.nasa.gov/docs/software/fitsio/fitsio.html
#*******************************************************************
#]]))

AC_SUBST(LIBS)
AC_SUBST(CONFIGOPTIONS)


#### Output ###########################################################

AC_OUTPUT(Makefile peg_cshrc peg_bashrc)
rm -f config.status && AC_MSG_RESULT(configure: removing ./config.status)
rm -f config.log && AC_MSG_RESULT(configure: removing ./config.log)

#######################################################################


