AC_PREREQ(2.59)

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

dnl this enables the building of libtap
AC_CONFIG_SUBDIRS([test/tap])

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

PSLIB_LT_VERSION="0:13: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 share test utils" dnl don't include 'swig', as it is optional

SRCPATH='${top_srcdir}/src'
SRCDIRS="sys astro db fft fits imageops jpeg math mathtypes types"   dnl 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_PROG_MAKE_SET

dnl handle optimized building
AC_ARG_ENABLE(optimize,
  [AS_HELP_STRING(--enable-optimize,enable compiler optimization)],
  [AC_MSG_RESULT(compile optimization enabled)
   CFLAGS="${CFLAGS=} -pipe -O2 -g"
   PSLIB_CFLAGS="${PSLIB_CFLAGS=} -DPS_NO_TRACE"],
  [AC_MSG_RESULT([compile optimization disabled])
    if test x"${CFLAGS}" == x; then
      CFLAGS="-pipe -O0 -g"
    fi
  ]
)

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=} -pg -g"
   LDFLAGS="${LDFLAGS=} -pg"]
)

dnl handle path coverage checking
AC_ARG_ENABLE(coverage,
  [AS_HELP_STRING(--enable-coverage,enable path coverage checking)],
  [AC_MSG_RESULT(path coverage enabled)
   CFLAGS="${CFLAGS=} -lgcov -fprofile-arcs -ftest-coverage -pg"]
)

dnl turn off trace messages
AC_ARG_ENABLE(trace,
  [AS_HELP_STRING(--disable-trace,disable psTrace functionality)],
  [AC_MSG_RESULT(psTrace disabled)
   PSLIB_CFLAGS="${PSLIB_CFLAGS=} -DPS_NO_TRACE"]
)

dnl build tests at the same time as the source code
AC_ARG_ENABLE(tests,
  [AS_HELP_STRING(--enable-tests,build tests at same time as source)],
  [AC_MSG_RESULT(test building enabled)
   tests=true],
   [tests=false])
AM_CONDITIONAL(BUILD_TESTS, test x$tests = xtrue)

AC_LANG(C)
AC_PROG_CC

dnl enable largefile supports
AC_SYS_LARGEFILE
AC_FUNC_FSEEKO

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 how to link against pthreads
ACX_PTHREAD([], [AC_MSG_ERROR([pthread support is required to build psLib])])

PSLIB_CFLAGS="${PSLIB_CFLAGS=} ${PTHREAD_CFLAGS}"
dnl PTHREAD_CFLAGS /must/ also be used in the linking of an exe.  Without
dnl adding PTHREAD_CFLAGS into PSLIB_LIBS we can't link exes on Linux.
dnl Basically this means the entire method of only linking psLib's deps in the
dnl final exe or library is flawed.
PSLIB_LIBS="${PSLIB_LIBS=} ${PTHREAD_CFLAGS} ${PTHREAD_LIBS}"

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=${CPPFLAGS}
TMP_CFLAGS=${CFLAGS}
TMP_LDFLAGS=${LDFLAGS}

CPPFLAGS=${PSLIB_CFLAGS}
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.])],[-lm]
)
dnl Now check if CFITSIO supports fits_open_diskfile, i.e., is at least version 2.501
AC_CHECK_LIB(cfitsio,ffdkopn,
  [CFITSIO_DISKFILE=1],
  [AC_MSG_WARN([The CFITSIO library version is rather old.  Suggested version is 2.501 or greater.])
   CFITSIO_DISKFILE=0],[-lm]
)
LIBS=${TMP_LIBS}

dnl restore the CFLAGS/LDFLAGS
CPPFLAGS=${TMP_CPPFLAGS}
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 CPPFLAGS/CFLAGS/LDFLAGS so that they can be restored after tests
TMP_CPPFLAGS=${CPPFLAGS}
TMP_CFLAGS=${CFLAGS}
TMP_LDFLAGS=${LDFLAGS}

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

dnl restore the CFLAGS/LDFLAGS
CPPFLAGS=${TMP_CPPFLAGS}
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 ------------------ libjpeg options ---------------------

AC_ARG_WITH(jpeg,
[AS_HELP_STRING(--with-jpeg=DIR,Specify location of libjpeg.)],
[JPEG_CFLAGS="-I$withval/include"
 JPEG_LDFLAGS="-L$withval/lib"])
AC_ARG_WITH(jpeg-include,
[AS_HELP_STRING(--with-jpeg-include=DIR,Specify libjpeg include directory.)],
[JPEG_CFLAGS="-I$withval"])
AC_ARG_WITH(fftw3-lib,
[AS_HELP_STRING(--with-jpeg-lib=DIR,Specify libjpeg library directory.)],
[JPEG_LDFLAGS="-L$withval"])

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

CFLAGS="${TMP_CFLAGS} ${JPEG_CFLAGS}"
CPPFLAGS="$CFLAGS"
LDFLAGS="${TMP_LDFLAGS} ${JPEG_LDFLAGS}"

AC_CHECK_HEADERS([jpeglib.h],[PSLIB_CFLAGS="$PSLIB_CFLAGS $JPEG_CFLAGS"
			      AC_SUBST(JPEG_CFLAGS)],
  [AC_MSG_ERROR([libjpeg headers not found.  Obtain libjpeg from http://www.ijg.org/ or use --with-jpeg to specify location.])]
)

AC_CHECK_LIB(jpeg,jpeg_CreateCompress,[PSLIB_LIBS="$PSLIB_LIBS $JPEG_LDFLAGS -ljpeg"],
  [AC_MSG_ERROR([libjpeg library not found.  Obtain libjpeg from http://www.ijg.org/ or use --with-jpeg to specify location.])]
)

dnl restore the CFLAGS/LDFLAGS in case we decide to make missing libjpeg only a warning
CPPFLAGS=${TMP_CPPFLAGS}
CFLAGS=${TMP_CFLAGS}
LDFLAGS=${TMP_LDFLAGS}

dnl ------------------- XML2 options ---------------------
dnl AC_ARG_WITH(xml2-config,
dnl [AS_HELP_STRING(--with-xml2-config=FILE,Specify location of xml2-config.)],
dnl [XML_CONFIG=$withval],
dnl [XML_CONFIG=`which xml2-config`])
dnl AC_CHECK_FILE($XML_CONFIG,[],
dnl     [AC_MSG_ERROR([GNOME XML C parser is required.  Obtain it at http://www.xmlsoft.org or use --with-xml2-config to specify location.])])
dnl
dnl AC_MSG_CHECKING([xml2 version])
dnl XML_VERSION=`xml2-config --version`
dnl XML_VERSION_major=`echo $XML_VERSION | ${PERL} -pe 's|^(\d+).*|\1|'`
dnl XML_VERSION_minor=`echo $XML_VERSION | ${PERL} -pe 's|^(\d+)\.(\d+).*|\2|'`
dnl dnl First test the minimum version of 2.6
dnl if test $XML_VERSION_major -lt 2 || ( test $XML_VERSION_major -eq 2 && test $XML_VERSION_minor -lt 6 )
dnl then
dnl 	AC_MSG_ERROR([requires libxml2 2.6.0 or greater, found $XML_VERSION.  Install newer version or use --with-xml2-config to specify another location.])
dnl else
dnl     AC_MSG_RESULT([$XML_VERSION... yes])
dnl fi
dnl
dnl AC_MSG_CHECKING([xml2 cflags])
dnl XML_CFLAGS="`${XML_CONFIG} --cflags`"
dnl AC_MSG_RESULT([${XML_CFLAGS}])
dnl
dnl AC_MSG_CHECKING([xml2 ldflags])
dnl XML_LDFLAGS="`${XML_CONFIG} --libs`"
dnl AC_MSG_RESULT([${XML_LDFLAGS}])
dnl
dnl PSLIB_CFLAGS="${PSLIB_CFLAGS=} ${XML_CFLAGS}"
dnl PSLIB_LIBS="${PSLIB_LIBS=} ${XML_LDFLAGS}"
dnl
dnl 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?])])


dnl doxygen doc generation is very, very slow so we're turing it off by default
AC_ARG_ENABLE(doxygen,
  [AS_HELP_STRING(--enable-doxygen ,enable manpage generation)],
  [AC_MSG_RESULT(doxygen enabled)
    AC_PATH_PROG([DOXYGEN], [doxygen], [missing])
  ],
  [AC_MSG_RESULT([doxygen disabled])
    doxygen=off
  ]
)
AM_CONDITIONAL([DOXYGEN], test "x$doxygen" != "xmissing" -a "x$doxygen" != "xoff")


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

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/jpeg/Makefile
  src/math/Makefile
  src/mathtypes/Makefile
  src/types/Makefile
  test/Makefile
  test/sys/Makefile
  test/astro/Makefile
  test/db/Makefile
  test/fft/Makefile
  test/fits/Makefile
  test/imageops/Makefile
  test/jpeg/Makefile
  test/math/Makefile
  test/mathtypes/Makefile
  test/types/Makefile
  test/FullUnitTest
  test/pstap/Makefile
  test/pstap/src/Makefile
  utils/Makefile
  etc/Makefile
  share/Makefile
  pslib-config
  pslib.pc
  Doxyfile
])
dnl src/xml/Makefile
dnl test/xml/Makefile


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

AC_OUTPUT
