AC_PREREQ(2.57)

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

AM_INIT_AUTOMAKE([1.6 foreign dist-bzip2])
AM_CONFIG_HEADER(src/config.h)
AM_MAINTAINER_MODE

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

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

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"]
)

case $host in
  *-apple-darwin*)
    CFLAGS="${CFLAGS=} -Wno-long-double"
    FLIBS="${FLIBS} -lg2c"
    ;;
esac

AC_LANG(C)
AC_PROG_CC

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

AC_LANG_PUSH(Fortran 77)
AC_PROG_F77([gfortran g77])
AC_F77_WRAPPERS
AC_LANG_POP(Fortran 77)

AC_PROG_INSTALL
AC_PROG_LIBTOOL

AC_PREFIX_DEFAULT([`pwd`])
test "$prefix" = NONE && prefix=`pwd`

dnl Setup the info for psLib for pkg-config
PSLIB_CFLAGS="-I${includedir}"
PSLIB_LIBS="${FLIBS=}"

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

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 ----------------- 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} --cflags`"
  AC_MSG_RESULT([${MYSQL_CFLAGS}])
  CFLAGS="${CFLAGS=} ${MYSQL_CFLAGS}"

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

  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="${CFLAGS=} -DBUILD_PSDB"
  PSLIB_CFLAGS="${PSLIB_CFLAGS=} -DBUILD_PSDB"
else
  AC_MSG_RESULT([disable building MySQL functionality])
fi

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"])
CFLAGS="${CFLAGS=} $CFITSIO_CFLAGS"
dnl LDFLAGS="${LDFLAGS} $CFITSIO_LDFLAGS -lcfitsio"
PSLIB_LIBS="${PSLIB_LIBS=} $CFITSIO_LDFLAGS -lcfitsio"
AC_CHECK_HEADERS([fitsio.h],[],
   [AC_MSG_ERROR([CFITSIO is required.  Obtain it at  http://heasarc.gsfc.nasa.gov/docs/software/fitsio or use --with-cfitsio to specify location.])])

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"])
CFLAGS="${CFLAGS=} $FFTW3_CFLAGS"
dnl LDFLAGS="${LDFLAGS=} $FFTW3_LDFLAGS -lfftw3f"
PSLIB_LIBS="${PSLIB_LIBS=} $FFTW3_LDFLAGS -lfftw3f"
AC_MSG_CHECKING([FFTW header files])
AC_CHECK_HEADERS([fftw3.h],[],
   [AC_MSG_ERROR([FFTW version 3 with float support is required.  Obtain it at http://www.fftw.org/or use --with-fftw3 to specify location.])])

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}])
CFLAGS="${CFLAGS=} ${GSL_CFLAGS}"
dnl LDFLAGS="${LDFLAGS=} `${GSL_CONFIG} --libs`"
PSLIB_LIBS="${PSLIB_LIBS=} `${GSL_CONFIG} --libs`"

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}])
CFLAGS="${CFLAGS=} ${XML_CFLAGS}"
dnl LDFLAGS="${LDFLAGS=} `${XML_CONFIG} --libs`"
PSLIB_LIBS="${PSLIB_LIBS=} `${XML_CONFIG} --libs`"

dnl ------------------- SWIG options ---------------------
AC_ARG_ENABLE(perlmodule,
 [AS_HELP_STRING(--disable-perlmodule,Disable creation of perl module)],
 [SWIG_REQ="$enableval"],
 [SWIG_REQ="yes"])

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 ------------------- 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)

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, check if sqrtf is in mx library.
AC_CHECK_LIB(mx,sqrtf,
  [PSLIB_LIBS="${PSLIB_LIBS=} -lmx"])
AC_CHECK_FUNC(atoll, [],
  [AC_MSG_ERROR([The C99 function, atoll, is required; update your compiler?])])

DOXYGEN="doxygen Doxyfile"
AC_CHECK_PROG(DOXYGEN,doxygen,[echo Doxygen not detected in configure stage.])
AC_SUBST(DOXYGEN,$DOXYGEN)

CFLAGS="${CFLAGS=} -Wall -Werror"

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

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

AC_CONFIG_FILES([
  Makefile
  src/Makefile
  src/astronomy/Makefile
  src/collections/Makefile
  src/dataManip/Makefile
  src/dataIO/Makefile
  src/image/Makefile
  src/sysUtils/Makefile
  test/FullUnitTest
  test/Makefile
  test/astronomy/Makefile
  test/collections/Makefile
  test/dataManip/Makefile
  test/dataIO/Makefile
  test/image/Makefile
  test/sysUtils/Makefile
  etc/pslib/Makefile
  etc/Makefile
  pslib-config
  pslib.pc
  Doxyfile
])

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

AC_OUTPUT
