bg
dnl Process this file with autoconf to produce a configure script.
AC_INIT(src)

PC_OS

dnl Checks for programs.
AC_PROG_CC
AC_PROG_FC
AC_PROG_RANLIB
AC_PROG_INSTALL

AC_SUBST(INSTALL)
AC_SUBST(INSTALL_PROGRAM)
if test -z "$INSTALL"
then
	INSTALL=cp
	INSTALL_PROGRAM=cp
else
	INSTALL_PROGRAM="$INSTALL"
fi


dnl ----------------------------------------------------------------------
dnl Check if DRS is enabled
dnl

AC_MSG_CHECKING(if DRS is enabled)
AC_ARG_ENABLE(drs,
   [  --enable-drs            Compile the DRS interface [default=yes]],
   ENABLE_DRS=$enableval,
   ENABLE_DRS=yes)

if test "X$ENABLE_DRS" = "Xyes"; then
   AC_MSG_RESULT(yes)
else
   AC_MSG_RESULT(no)
fi
AC_SUBST(ENABLE_DRS)

dnl ----------------------------------------------------------------------
dnl Check if HDF is enabled
dnl

AC_MSG_CHECKING(if HDF is enabled)
AC_ARG_ENABLE(hdf,
   [  --enable-hdf            Compile the HDF interface [default=yes]],
   ENABLE_HDF=$enableval,
   ENABLE_HDF=yes)

if test "X$ENABLE_HDF" = "Xyes"; then
   AC_MSG_RESULT(yes)
else
   AC_MSG_RESULT(no)
fi
AC_SUBST(ENABLE_HDF)
dnl ----------------------------------------------------------------------
dnl Check if GRIB2 is enabled
dnl
dnl ----------------------------------------------------------------------
dnl Checks for GRIB-2 header file.
dnl
dnl get location of GRIB-2 C header from --with-grib2inc=value
dnl

PC_CHECK_C_HEADER(GRIB2INC,/usr/local/include,[grib2inc],grib2.h, dnl
  [--with-grib2inc=DIR        grib2.h is in DIR (default [GRIB2INC])])

AC_MSG_CHECKING(if GRIB2 is enabled)
AC_ARG_ENABLE(grib2,
   [  --enable-grib2            Compile GRIB-2 support [default=no]],
   ENABLE_GRIB2=$enableval,
   ENABLE_GRIB2=no)

if test "X$ENABLE_GRIB2" = "Xyes"; then
   AC_MSG_RESULT(yes)
   GRIB2OPTS="-DGRIB2"
   AC_ARG_WITH([grib2lib],
	[AS_HELP_STRING([--with-grib2lib],[path where to look for libgrib2c])],
        [],
	[with_grib2lib=no])
   if test ${with_grib2lib} == "no" ; then
     if test "${EXTERNALS+set}" = set; then 
       GRIB2LIB="-L${EXTERNALS}/lib"
     else
       GRIB2LIB="-L${prefix}/Externals/lib"
     fi
   else
     GRIB2LIB="-L"${with_grib2lib}
   fi
   AC_ARG_WITH([pnglib],
	[AS_HELP_STRING([--with-pnglib],[path where to look for libpng])],
        [],
	[with_pnglib=no])
   if test ${with_pnglib} == "no" ; then
     if test "${EXTERNALS+set}" = set; then 
       PNGLIB="-L${EXTERNALS}/lib"
     else
       PNGLIB="-L${prefix}/Externals/lib"
     fi
   else
     PNGLIB="-L"${with_pnglib}
   fi
   AC_CHECK_LIB([png],[png_error],[PNGLIBOK="yes"],[PNGLIBOK="no"],[${GRIB2LIB} ${PNGLIB}])
   if test ${PNGLIBOK} == "no" ; then
      AC_MSG_ERROR("[Could not find a working libpng]")
   fi
   AC_ARG_WITH([jasperlib],
	[AS_HELP_STRING([--with-jasperlib],[path where to look for libjasper])],
        [],
	[with_jasperlib=no])
   if test ${with_jasperlib} == "no" ; then
     if test "${EXTERNALS+set}" = set; then 
       JASPERLIB="-L${EXTERNALS}/lib"
     else
       JASPERLIB="-L${prefix}/Externals/lib"
     fi
   else
     JASPERLIB="-L"${with_jasperlib}
   fi
   AC_CHECK_LIB([jasper],[jas_init],[JASPERLIBOK="yes"],[JASPERLIBOK="no"],[${GRIB2LIB} ${JASPERLIB} -ljpeg -lm ])
   if test ${JASPERLIBOK} == "no" ; then
      AC_MSG_ERROR("[Could not find a working libjasper]")
   fi
   AC_ARG_WITH([grib2lib],
	[AS_HELP_STRING([--with-grib2lib],[path where to look for libgrib2c])],
        [],
	[with_grib2lib=no])
   if test ${with_grib2lib} == "no" ; then
     if test "${EXTERNALS+set}" = set; then 
       GRIB2LIB="-L${EXTERNALS}/lib -lgrib2c "${PNGLIB}" -lpng "${JASPERLIB}" -ljasper -lm"
     else
       GRIB2LIB="-L${prefix}/Externals/lib -lgrib2c "${PNGLIB}" -lpng "${JASPERLIB}" -ljasper -lm"
     fi
   else
     GRIB2LIB="-L"${with_grib2lib}" -lgrib2c "${PNGLIB}" -lpng "${JASPERLIB}" -ljasper -lm"
   fi
   AC_CHECK_LIB([grib2c],[g2_getfld],[GRIB2LIBOK="yes"],[GRIB2LIBOK="no"],[${GRIB2LIB}])
   if test ${GRIB2LIBOK} == "no" ; then
      AC_MSG_ERROR("[Could not find a working libgrib2c]")
   fi
dnl in this case we also need jasper

else
   AC_MSG_RESULT(no)
   GRIB2OPTS=""
   GRIB2LIB=""
fi
AC_SUBST(GRIB2LIB)
AC_SUBST(ENABLE_GRIB2)
AC_SUBST(GRIB2OPTS)
dnl ----------------------------------------------------------------------
dnl Check if OpenDAP is enabled
dnl

AC_MSG_CHECKING(if OpenDAP is enabled)
AC_ARG_ENABLE(dap,
   [  --enable-dap            Compile with the OpenDAP interface [default=no]],
   ENABLE_DAP=$enableval,
   ENABLE_DAP=no)

if test "X$ENABLE_DAP" = "Xyes"; then
   AC_MSG_RESULT(yes)
else
   AC_MSG_RESULT(no)
fi
AC_SUBST(ENABLE_DAP)
dnl ----------------------------------------------------------------------
dnl Check if PP is enabled
dnl

AC_MSG_CHECKING(if PP is enabled)
AC_ARG_ENABLE(pp,
   [  --enable-pp             Compile the PP interface [default=yes]],
   ENABLE_PP=$enableval,
   ENABLE_PP=yes)

if test "X$ENABLE_PP" = "Xyes"; then
   AC_MSG_RESULT(yes)
   PPOPTS="-DHAVE_PP"
else
   AC_MSG_RESULT(no)
   PPOPTS=""
fi
AC_SUBST(ENABLE_PP)
AC_SUBST(PPOPTS)
dnl ----------------------------------------------------------------------
dnl Check if QL is enabled
dnl

AC_MSG_CHECKING(if QL is enabled)
AC_ARG_ENABLE(ql,
   [  --enable-ql            Compile the QL interface [default=yes]],
   ENABLE_QL=$enableval,
   ENABLE_QL=yes)

if test "X$ENABLE_QL" = "Xyes"; then
   AC_MSG_RESULT(yes); \
   QLOPTS="-DHAVE_QL"
else
   AC_MSG_RESULT(no); \
   QLOPTS=""
fi
AC_SUBST(ENABLE_QL)
AC_SUBST(QLOPTS)


dnl ----------------------------------------------------------------------
dnl Checks for header files.
dnl
dnl get location of netCDF, HDF, and DRS C header from --with-ncinc=value
dnl

PC_CHECK_C_HEADER(NCINC,/usr/local/include,[ncinc],netcdf.h, dnl
  [--with-ncinc=DIR        netcdf.h is in DIR (default [NCINC])])
dnl ----------------------------------------------------------------------
dnl
dnl Only look for hdfi.h on OSF
dnl
if test "X$ENABLE_HDF" = "Xyes"; then
  case $OS in
  	osf*)
		HDF_HEADER_OPTIONS="hdfi.h"
  		;;
  	*)
		HDF_HEADER_OPTIONS="hdf.h"
  		;;
  esac
PC_CHECK_C_HEADER(HDFINC,/usr/local/include,[hdfinc],$HDF_HEADER_OPTIONS, dnl
  [--with-hdfinc=DIR       hdf.h is in DIR (default [HDFINC])])
fi
dnl ----------------------------------------------------------------------
if test "X$ENABLE_DRS" = "Xyes"; then
  PC_CHECK_C_HEADER(DRSINC,/usr/local/include,[drsinc],drscdf.h, dnl
  [--with-drsinc=DIR       drscdf.h is in DIR (default [DRSINC])])
fi
dnl ----------------------------------------------------------------------
dnl
dnl get location of netCDF, and DRS FORTRAN header from --with-ncincf=value
dnl FORTRAN header for HDF cannot be used at this time. The -DHAVE_NETCDF
dnl compile option, cancels the HDF FORTRAN interface.
dnl

PC_CHECK_FORT_HEADER(NCINCF,$NCINC,[ncincf],netcdf.inc, dnl
  [--with-ncincf=DIR       netcdf.inc is in DIR (default [NCINCF])])
dnl ----------------------------------------------------------------------
if test "X$ENABLE_DRS" = "Xyes"; then
  PC_CHECK_FORT_HEADER(DRSINCF,$DRSINC,[drsincf],drsdef.h, dnl
  [--with-drsincf=DIR      drsdef.h is in DIR (default [DRSINCF])])
fi

dnl ----------------------------------------------------------------------
dnl Checks for libraries.
dnl
dnl set $(NCLIB) from --with-nclib=value
dnl

dnl
dnl netCDF, HDF require libnsl on Solaris
dnl
if test "X$ENABLE_HDF" = "Xyes"; then
  case "${OS}$OS_MAJOR" in
  	sunos5*)
  		HDF_LIB_OPTIONS="mfhdf -ldf -ljpeg -lz -lnetcdf -lnsl"
  		;;
  	irix6*)
  		HDF_LIB_OPTIONS="mfhdf -ldf -ljpeg -lz"
  		;;
  	linux*)
		HDF_LIB_OPTIONS="mfhdf -ldf -ljpeg -lz -lm"
  		;;
  	Windows_NT*)
		HDF_LIB_OPTIONS="mfhdf -ldf -ljpeg -lz -lm"
  		;;
  	*)
  		HDF_LIB_OPTIONS="mfhdf -ldf -ljpeg -lz"
  		;;
  esac
PC_CHECK_ARG_LIB(HDFLIB,/usr/local/lib,[hdflib],$HDF_LIB_OPTIONS,SDstart,HDFOPTS,hdf,libmfhdf.a, dnl
  [--with-hdflib=DIR       libmfhdf.a is in DIR (default [HDFLIB])])
fi

dnl
dnl DAP requires additional libraries
dnl
dnl DAP_LIB_OPTIONS="netcdf -lhdf5_hl -lhdf5 -lm -lz"

if test "X$ENABLE_DAP" = "Xyes"; then
dnl   case "${OS}$OS_MAJOR" in
dnl        darwin*)
dnl                DAP_LIB_OPTIONS="nc-dap -ldap -lstdc++ -lcurl -lz -lssl -lpthread -lxml2"
dnl                ;;
dnl        *)
dnl                DAP_LIB_OPTIONS="nc-dap -ldap -lstdc++ -lcurl -lz -lssl -lpthread -lxml2"
dnl                ;;
dnl   esac
      AC_ARG_WITH([nclib], [--with-nclib=DIR        libnetcdf.a is in DIR (default [NCLIB])],[NCLIB=$withval])
      NC_LIB_OPTIONS="netcdf $(${NCLIB}/../bin/nc-config --libs)"
dnl   PC_CHECK_ARG_LIB(DAPLIB,/usr/local/lib,[daplib],$DAP_LIB_OPTIONS,SDstart,DAPOPTS,dap,libnc-dap.a, dnl
dnl   [--with-dap=DIR       libnc-dap.a is in DIR (default [DAPLIB])])
else
 dnl ----------------------------------------------------------------------
 AC_ARG_WITH([hdf5lib], [--with-hdf5lib=DIR        libhdf5.a is in DIR (default [HDF5LIB])],[HDF5LIB=$withval])

 case "${OS}$OS_MAJOR" in
	sunos5*)
		NC_LIB_OPTIONS="netcdf -L$HDF5LIB -lhdf5_hl -lhdf5 -lnsl -lm"
  		;;
  	linux*)
		NC_LIB_OPTIONS="netcdf -L$HDF5LIB -lhdf5_hl -lhdf5 -lm -lz"
  		;;
  	*)
		NC_LIB_OPTIONS="netcdf -L$HDF5LIB -lhdf5_hl -lhdf5 -lm -lz"
  		;;
  esac
fi


PC_CHECK_ARG_LIB(NCLIB,/usr/local/lib,[nclib],$NC_LIB_OPTIONS,ncopen,NCOPTS,netcdf,libnetcdf.a, dnl
  [--with-nclib=DIR        libnetcdf.a is in DIR (default [NCLIB])])
dnl ----------------------------------------------------------------------
if test "X$ENABLE_DRS" = "Xyes"; then
   PC_CHECK_FORT_LIB(DRSLIB,/usr/local/lib,[drslib],drs,Aslun,DRSOPTS,drs,libdrs.a, dnl
  [--with-drslib=DIR       libdrs.a is in DIR (default [DRSLIB])])
fi

dnl ----------------------------------------------------------------------
dnl Checks for typedefs, structures, and compiler characteristics.

dnl Checks for library functions.

dnl ----------------------------------------------------------------------
dnl Set flags for CDMS compilation and linking
AC_SYS_LARGEFILE

GRADS_CRAY=0
GRADS_BYTEORDER=1
GRADS_IEEE2CRAY_OBJECT=
case $OS in
    aix)
        PC_ENSURE(CPPFLAGS, -Dextname)dnl
	PC_ENSURE(FFLAGS, -qextname)dnl
 	;;
    hpux*)
dnl
dnl	Note: underscores are added in fcddrs.h for HP,
dnl	  so don't use FORTRAN +ppu option
dnl
	PC_ENSURE(CPPFLAGS, -Dextname)dnl
	;;
    irix6*)
	PC_ENSURE(CFLAGS, -n32)dnl
	;;
    osf*)
dnl
dnl     Don't add -std to CFLAGS, since
dnl     f77 chokes on it
dnl
    	GRADS_BYTEORDER=0
	PC_ENSURE(CFLAGS, [-Olimit 1000])dnl
	if test "$CC" = cc; then
		CC="cc -std"
	fi
	;;
    linux)
	GRADS_BYTEORDER=0
dnl	PC_ENSURE(CPPFLAGS, -DBYTESWAP -DAbsoftUNIXFortran -D__linux_absoft) dnl
	PC_ENSURE(CPPFLAGS, -DBYTESWAP -DpgiFortran) dnl
	;;
    Windows_NT*)
	GRADS_BYTEORDER=0
dnl	PC_ENSURE(CPPFLAGS, -DBYTESWAP -DAbsoftUNIXFortran -D__linux_absoft) dnl
	PC_ENSURE(CPPFLAGS, -DBYTESWAP -DpgiFortran) dnl
	;;
    *ultrix)
    	GRADS_BYTEORDER=0
	;;
    unicos)
        GRADS_CRAY=1
	PC_ENSURE(CPPFLAGS, -DCRAY) dnl
dnl	GRADS_IEEE2CRAY_OBJECT=ieee2cray.o
	;;
    darwin*)
    	GRADS_BYTEORDER=0
        ;;
esac

AC_SUBST(GRADS_BYTEORDER)dnl
AC_SUBST(GRADS_CRAY)dnl
AC_SUBST(GRADS_IEEE2CRAY_OBJECT)dnl

echo "==================================================================="
echo "NCLIB = $NCLIB"
echo "NC_LIB_OPTIONS = $NC_LIB_OPTIONS"
echo "==================================================================="
dnl ----------------------------------------------------------------------
AC_OUTPUT( dnl
  Makefile dnl
  src/cddrs/Makefile dnl
  src/cddrs/test/Makefile dnl
  src/cdunif/Makefile dnl
  src/cdunif/cdunifpp/Makefile dnl
  src/cdunif/test/Makefile dnl
  src/db_util/Makefile dnl
  src/api/Makefile dnl
  src/api/test/Makefile
)
