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
case $host_os in
	osf1*)
            AC_PROG_F77($F77, f77 g77 f2c);;
        *)
	    AC_PROG_F77;;
esac


if ( test "x$G77" = "xyes"); then
    if test ! "$fflags_save"; then
      FFLAGS="$FFLAGS -fno-backslash -fdollar-ok -ffixed-line-length-none -fno-automatic"
      AC_MSG_RESULT([setting Fortran compiler options to $FFLAGS])
    fi
    if test ! "$LF2C"; then
       AC_F77_LIBRARY_LDFLAGS
    fi


elif test "$F77"; then

    if test ! "$fflags_save"; then
      case $host_os in
	irix*)
            FFLAGS="-backslash -col120 -extend_source -Wl,-woff,84 -static -bytereclen"
	    ;;
#	osf1*)
	osf*)
	    cat <<ENDEND > tmp.f
		stop
		end
ENDEND
            $F77 -old_f77 tmp.f -o tmp 2> /dev/null && FFLAGS="-old_f77 "
            rm -f tmp.f tmp.o
            FFLAGS="$FFLAGS-assume backslash -extend_source -O1 -assume byterecl"
	    ;;
#	osf*)
#            FFLAGS="-assume backslash -extend_source -O1 -assume byterecl"
#	    ;;
	aix*)
            FFLAGS="-qcharlen=4096 -qnoescape -qfixed=132 -qxlf77=leadzero "
	    ;;
	linux*)
            FFLAGS="-f -!bs"
	    ;;
	solaris*)
# -e (lines up to 132 char); -xl (interpret \ as a normal char + several 
#     language extenstions that we do not use).
	    FFLAGS="-e -Nl99 -w -xl"
	    ;;
	sunos*)
	    FFLAGS="-e -Nl99 -w"
	    F77=${PL_ROOT}unix/f77_solaris
	    ;;
	hpux*)
	    CFLAGS="$CFLAGS -DPG_PPU" 
	    FFLAGS="+es"
            AC_MSG_WARN([compilation with f77 HP-UX !!! Untested])
	    ;;
	*)
            AC_MSG_WARN([Don't the fortran compiler on $host_os; FFLAGS not set])
	    ;;
      esac
      AC_MSG_RESULT([setting Fortran compiler options to ${FFLAGS-<no option>}])
    fi
else
    AC_MSG_ERROR([
*******************************************************************
PEGASE.HR configuration failed : 
No fortran compiler found.
*******************************************************************
])
fi

AC_SUBST(F77)
AC_SUBST(FFLAGS)

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


#----------------------------------------------------------------------------

#                                              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


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)

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


