AC_PREREQ(2.59)

AC_INIT([pslib],[0.11.0],[http://pan-starrs.ifa.hawaii.edu/bugzilla])
AC_CONFIG_SRCDIR([pslib.pc.in])
AC_CANONICAL_TARGET

AM_INIT_AUTOMAKE([1.7 foreign dist-bzip2])
AC_CONFIG_HEADERS([src/config.h])
AM_MAINTAINER_MODE

PSLIB_LT_VERSION="0:11:0"
AC_SUBST(PSLIB_LT_VERSION,$PSLIB_LT_VERSION)

dnl ------------------- PERL options ---------------------
  AC_ARG_WITH(perl,
    [AS_HELP_STRING(--with-perl=FILE,Specify location of PERL executable.)],
    [AC_CHECK_PROG(PERL, $withval, $withval)],
    [AC_CHECK_PROG(PERL, perl, `which perl`)])
    if test "$PERL" == ""
    then
      AC_MSG_ERROR([PERL is required.  Use --with-perl to specify its install location.])
    fi
    AC_SUBST(PERL,$PERL)

SUBDIR="etc src test" dnl don't include 'swig', as it is optional

SRCPATH='${top_srcdir}/src'
SRCDIRS="sys astro db fft fits imageops math mathtypes types xml"
# escape two escapes at this level so \\ gets passed to the shell and \ to perl
SRCINC=`echo "${SRCDIRS=}" | ${PERL} -pe "s|(\w+)|-I\\\\${SRCPATH=}/\1|g"`
SRCINC="-I${SRCPATH=} ${SRCINC=}"
SRCSUBLIBS=`echo "${SRCDIRS=}" | ${PERL} -pe "s|(\w+)|\1/libpslib\1.la|g"`
AC_SUBST(SRCSUBLIBS,${SRCSUBLIBS=})
AC_SUBST(SRCINC,${SRCINC=})
AC_SUBST(ABS_SRCDIR,`pwd`) dnl assuming configure is run in top directory -- is there something better?
AC_DISABLE_STATIC
AC_PROG_MAKE_SET

dnl handle debug building
AC_ARG_ENABLE(optimize,
  [AS_HELP_STRING(--enable-optimize,enable compiler optimization)],
  [AC_MSG_RESULT(compile optimization enabled)
   CFLAGS="${CFLAGS=} -O2"],
  [AC_MSG_RESULT([compile optimization disabled])
   CFLAGS="${CFLAGS=} -O0 -g"]
)

dnl handle profiler building
AC_ARG_ENABLE(profile,
  [AS_HELP_STRING(--enable-profile,enable compiler profiler information inclusion)],
  [AC_MSG_RESULT(compile optimization enabled)
   CFLAGS="${CFLAGS=} -fprofile-arcs -ftest-coverage -pg"]
)

AC_LANG(C)
AC_PROG_CC

dnl enable largefile supports
AC_SYS_LARGEFILE

dnl enable POSIX/XSI and C99 compliance
CFLAGS="${CFLAGS=} -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=200112L -std=c99"

AC_PROG_INSTALL
AC_PROG_LIBTOOL

AC_PREFIX_DEFAULT([/usr/local])
test "$prefix" = NONE && prefix=/usr/local

dnl check for standard language functionality
AC_CHECK_HEADERS([fcntl.h float.h limits.h stdint.h stdlib.h string.h sys/time.h unistd.h])
AC_C_CONST
AC_C_INLINE
AC_HEADER_STDBOOL
AC_HEADER_SYS_WAIT
AC_HEADER_TIME
AC_STRUCT_TM
AC_TYPE_SIZE_T

dnl Checks for library functions.
AC_FUNC_ERROR_AT_LINE
AC_FUNC_FORK
AC_FUNC_MALLOC
AC_FUNC_MEMCMP
AC_FUNC_REALLOC
AC_FUNC_STRFTIME
AC_FUNC_STRTOD
AC_FUNC_VPRINTF
AC_CHECK_FUNCS([floor gethostname gettimeofday memmove memset mkdir pow regcomp sqrt strchr strcspn strerror strrchr strstr strtol strtoul strtoull])

dnl Add the src directories to the include path


dnl ----------------- MYSQL options --------------------
dnl MYSQL_LDFLAGS="-L${libdir}/mysql"
dnl MYSQL_CFLAGS="-I${includedir}/mysql"
AC_ARG_ENABLE(mysql,
 [AS_HELP_STRING(--disable-mysql,Disable MySQL functionality)],
 [MYSQL_REQ=$enableval],
 [MYSQL_REQ="yes"])

if test "$MYSQL_REQ" == "yes"
then
  AC_MSG_RESULT([enable building MySQL functionality])
  AC_ARG_WITH(mysql_config,
    [AS_HELP_STRING(--with-mysql_config=FILE,Specify location of MYSQL configuration script.)],
    [MYSQL_CONFIG=$withval],
    [MYSQL_CONFIG=`which mysql_config`])
  AC_CHECK_FILE($MYSQL_CONFIG,[],
    [AC_MSG_ERROR([MYSQL 4.1.2 or newer is required.  http://www.mysql.com or use --with-mysql_config to specify location of ocnfiguration script.])])

  AC_MSG_CHECKING([MySQL cflags])
  MYSQL_CFLAGS="`${MYSQL_CONFIG} --include`"
  PSLIB_CFLAGS="${PSLIB_CFLAGS=} ${MYSQL_CFLAGS}"
  AC_MSG_RESULT([${MYSQL_CFLAGS}])

  AC_MSG_CHECKING([MySQL link options])
  MYSQL_LIBS="`${MYSQL_CONFIG} --libs`"
  PSLIB_LIBS="${PSLIB_LIBS=} ${MYSQL_LIBS}"
  AC_MSG_RESULT([${MYSQL_LIBS}])
  AC_SUBST(TST_PSDB,"tst_psDB") dnl enable the test for psDB

dnl AC_RUN_IFELSE() uses CFLAGS to find mysql_version.h
  TMP_CFLAGS=${CFLAGS}
  CFLAGS=${MYSQL_CFLAGS}
  AC_RUN_IFELSE([AC_LANG_PROGRAM(
[[#include <stdlib.h>
#include <stdio.h>
#include <mysql_version.h>
]],[[int v=MYSQL_VERSION_ID;
    if (v < 40102) {
    exit(1);
    } else {
    exit(0);
    }
]])], [], [AC_MSG_ERROR([MySQL 4.1.2 or newer is required.  Obtain it at  http://www.mysql.com or use --with-mysql to specify location.])])
  CFLAGS=${TMP_CFLAGS}
else
  dnl FIXME
  CFLAGS="${CFLAGS=} -DOMIT_PSDB"
  PSLIB_CFLAGS="${PSLIB_CFLAGS=} -DOMIT_PSDB"
  AC_MSG_RESULT([disable building MySQL functionality])
fi

AC_SUBST([MYSQL_CFLAGS])

dnl ----------------- CFITSIO options --------------------
AC_ARG_WITH(cfitsio,
[AS_HELP_STRING(--with-cfitsio=DIR,Specify location of CFITSIO.)],
[CFITSIO_CFLAGS="-I$withval/include"
 CFITSIO_LDFLAGS="-L$withval/lib"])
AC_ARG_WITH(cfitsio-include,
[AS_HELP_STRING(--with-cfitsio-include=DIR,Specify CFITSIO include directory.)],
[CFITSIO_CFLAGS="-I$withval"])
AC_ARG_WITH(cfitsio-lib,
[AS_HELP_STRING(--with-cfitsio-lib=DIR,Specify CFITSIO library directory.)],
[CFITSIO_LDFLAGS="-L$withval"])
PSLIB_CFLAGS="${PSLIB_CFLAGS=} ${CFITSIO_CFLAGS}"
PSLIB_LIBS="${PSLIB_LIBS=} $CFITSIO_LDFLAGS -lcfitsio -lm"

dnl Store off CFLAGS/LDFLAGS so that they can be restored after tests
TMP_CFLAGS=${CFLAGS}
TMP_LDFLAGS=${LDFLAGS}

CFLAGS=${PSLIB_CFLAGS}
LDFLAGS=${PSLIB_LIBS}
AC_CHECK_HEADERS([fitsio.h],[],
   [AC_MSG_ERROR([CFITSIO headers not found.  Obtain CFITSIO at  http://heasarc.gsfc.nasa.gov/docs/software/fitsio or use --with-cfitsio to specify location.])]
)
TMP_LIBS=${LIBS}
AC_CHECK_LIB(cfitsio,ffopen,[],
  [AC_MSG_ERROR([CFITSIO library not found.  Obtain it at  http://heasarc.gsfc.nasa.gov/docs/software/fitsio or use --with-cfitsio to specify location.])]
)
dnl Now check if CFITSIO supports fits_open_diskfile, i.e., is at least version 2.501
AC_CHECK_LIB(cfitsio,ffdkopen,
  [CFITSIO_DISKFILE=1],
  [AC_MSG_WARN([The CFITSIO library version is rather old.  Suggested version is 2.501 or greater.])
   CFITSIO_DISKFILE=0]
)
LIBS=${TMP_LIBS}

dnl restore the CFLAGS/LDFLAGS
CFLAGS=${TMP_CFLAGS}
LDFLAGS=${TMP_LDFLAGS}

AC_DEFINE_UNQUOTED([CFITSIO_DISKFILE],${CFITSIO_DISKFILE},[Define to 1 if you have fits_open_diskfile in CFITSIO])
AC_SUBST([CFITSIO_CFLAGS])

dnl ------------------ FFTW3 options ---------------------
AC_ARG_WITH(fftw3,
[AS_HELP_STRING(--with-fftw3=DIR,Specify location of FFTW version 3.)],
[FFTW3_CFLAGS="-I$withval/include"
 FFTW3_LDFLAGS="-L$withval/lib"])
AC_ARG_WITH(fftw3-include,
[AS_HELP_STRING(--with-fftw3-include=DIR,Specify FFTW version 3 include directory.)],
[FFTW3_CFLAGS="-I$withval"])
AC_ARG_WITH(fftw3-lib,
[AS_HELP_STRING(--with-fftw3-lib=DIR,Specify FFTW version 3 library directory.)],
[FFTW3_LDFLAGS="-L$withval"])
PSLIB_CFLAGS="${PSLIB_CFLAGS=} ${FFTW3_CFLAGS}"
PSLIB_LIBS="${PSLIB_LIBS=} $FFTW3_LDFLAGS -lfftw3f"

dnl Store off CFLAGS/LDFLAGS so that they can be restored after tests
TMP_CFLAGS=${CFLAGS}
TMP_LDFLAGS=${LDFLAGS}

CFLAGS="${TMP_CFLAGS} ${PSLIB_CFLAGS}"
LDFLAGS="${TMP_LDFLAGS} ${PSLIB_LIBS}"
AC_CHECK_HEADERS([fftw3.h],[],
  [AC_MSG_ERROR([FFTW version 3 (--withfloat) headers not found.  Obtain it at http://www.fftw.org/or use --with-fftw3 to specify location.])]
)
TMP_LIBS=${LIBS}
AC_CHECK_LIB(fftw3f,fftwf_plan_dft_2d,[],
  [AC_MSG_ERROR([FFTW version 3 (--withfloat) library not found.  Obtain it at http://www.fftw.org/or use --with-fftw3 to specify location.])]
)
LIBS=${TMP_LIBS}

dnl restore the CFLAGS/LDFLAGS
CFLAGS=${TMP_CFLAGS}
LDFLAGS=${TMP_LDFLAGS}

AC_SUBST([FFTW3_CFLAGS])

dnl -------------------- GSL options ---------------------
AC_ARG_WITH(gsl-config,
[AS_HELP_STRING(--with-gsl-config=FILE,Specify location of gsl-config.)],
[GSL_CONFIG=$withval],
[GSL_CONFIG=`which gsl-config`])
AC_CHECK_FILE($GSL_CONFIG,[],
    [AC_MSG_ERROR([GSL is required.  Obtain it at http://www.gnu.org/software/gsl or use --with-gsl-config to specify location.])])

AC_MSG_CHECKING([GSL cflags])
GSL_CFLAGS="`${GSL_CONFIG} --cflags`"
AC_MSG_RESULT([${GSL_CFLAGS}])

AC_MSG_CHECKING([GSL ldflags])
GSL_LDFLAGS="`${GSL_CONFIG} --libs`"
AC_MSG_RESULT([${GSL_LDFLAGS}])

PSLIB_CFLAGS="${PSLIB_CFLAGS=} ${GSL_CFLAGS}"
PSLIB_LIBS="${PSLIB_LIBS=} ${GSL_LDFLAGS}"

AC_SUBST([GSL_CFLAGS])

dnl ------------------- XML2 options ---------------------
AC_ARG_WITH(xml2-config,
[AS_HELP_STRING(--with-xml2-config=FILE,Specify location of xml2-config.)],
[XML_CONFIG=$withval],
[XML_CONFIG=`which xml2-config`])
AC_CHECK_FILE($XML_CONFIG,[],
    [AC_MSG_ERROR([GNOME XML C parser is required.  Obtain it at http://www.xmlsoft.org or use --with-xml2-config to specify location.])])

AC_MSG_CHECKING([xml2 cflags])
XML_CFLAGS="`${XML_CONFIG} --cflags`"
AC_MSG_RESULT([${XML_CFLAGS}])

AC_MSG_CHECKING([xml2 ldflags])
XML_LDFLAGS="`${XML_CONFIG} --libs`"
AC_MSG_RESULT([${XML_LDFLAGS}])

PSLIB_CFLAGS="${PSLIB_CFLAGS=} ${XML_CFLAGS}"
PSLIB_LIBS="${PSLIB_LIBS=} ${XML_LDFLAGS}"

AC_SUBST([XML_CFLAGS])

dnl ------------------- SWIG options ---------------------
AC_ARG_ENABLE(perlmodule,
 [AS_HELP_STRING(--enable-perlmodule,Enables creation of perl module)],
 [SWIG_REQ="$enableval"],
 [SWIG_REQ="no"])

if test "$SWIG_REQ" == "yes"
then
  AC_MSG_RESULT([enable building perl module])
  AC_ARG_WITH(swig,
  [AS_HELP_STRING(--with-swig=FILE,Specify location of SWIG executable.)],
  [SWIG=$withval],
  [SWIG=`which swig`])
  AC_SUBST(SWIG,$SWIG)

  AC_CHECK_FILE($SWIG,[],
     [AC_MSG_ERROR([SWIG is required.  Obtain it at www.swig.org or use --with-swig to specify its install location.])])
  SUBDIR="${SUBDIR=} swig"

dnl ---------------- PERLPREFIX option -------------------
  AC_ARG_WITH(perlprefix,
    [AS_HELP_STRING(--with-perlprefix=DIR,Specify prefix of Perl Module's installation. (default=PREFIX))],
    [PERL_PREFIX="$withval"],
    [PERL_PREFIX="$prefix"])
    AC_SUBST(PERL_PREFIX,$PERL_PREFIX)
    AC_SUBST(PERL_INSTALLSYTLE,[`perl '-V:installstyle'`])

else
  AC_MSG_RESULT([disable building perl module])
fi

dnl for MacOSX suppport -- check if sqrtf is in mx library.
TMP_LIBS=${LIBS}
AC_CHECK_LIB(mx,sqrtf,
  [PSLIB_LIBS="${PSLIB_LIBS=} -lmx"])
LIBS=${TMP_LIBS}
AC_CHECK_FUNC(atoll, [],
  [AC_MSG_ERROR([The C99 function, atoll, is required; update your compiler version?])])

AC_CHECK_PROG(doxygen,[doxygen],[true],[false])
AM_CONDITIONAL(DOXYGEN, test x$doxygen = xtrue)


dnl ------- restore CFLAGS / LDFLAGS (tests done) --------
CFLAGS="${CFLAGS} -Wall -Werror"
AC_MSG_RESULT([CFLAGS is ${CFLAGS}])
AC_MSG_RESULT([LDFLAGS is ${LDFLAGS}])

dnl ---------------- export variables --------------------

AC_SUBST(SUBDIR,[$SUBDIR])
AC_SUBST(SRCDIRS,[$SRCDIRS])
AC_SUBST(PSLIB_LIBS,[$PSLIB_LIBS])
AC_SUBST(PSLIB_CFLAGS,[$PSLIB_CFLAGS])

AC_SUBST(PS_LANG,[en])

AC_CONFIG_FILES([
  Makefile
  src/Makefile
  src/sys/Makefile
  src/astro/Makefile
  src/db/Makefile
  src/fft/Makefile
  src/fits/Makefile
  src/imageops/Makefile
  src/math/Makefile
  src/mathtypes/Makefile
  src/types/Makefile
  src/xml/Makefile
  test/Makefile
  test/sys/Makefile
  test/astro/Makefile
  test/db/Makefile
  test/fft/Makefile
  test/fits/Makefile
  test/imageops/Makefile
  test/math/Makefile
  test/mathtypes/Makefile
  test/types/Makefile
  test/xml/Makefile
  test/FullUnitTest
  etc/pslib/Makefile
  etc/Makefile
  pslib-config
  pslib.pc
  Doxyfile
])

if test "$SWIG_REQ" == "yes"
then
  AC_CONFIG_FILES([ swig/Makefile ])
fi

AC_OUTPUT
