AC_PREREQ(2.61)

AC_INIT([pslib],[1.1.1],[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

dnl So, libtool library versions are described by three integers:
dnl 
dnl current
dnl     The most recent interface number that this library implements.
dnl revision
dnl     The implementation number of the current interface.
dnl age
dnl     The difference between the newest and oldest interfaces that this
dnl library implements. In other words, the library implements all the
dnl interface numbers in the range from number current - age to current

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

IPP_STDCFLAGS

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

dnl turn on mem leak backtracing 
AC_ARG_ENABLE(backtrace,
  [AS_HELP_STRING(--enable-backtrace, enable memory allocation backtracing)],
  [AC_MSG_RESULT(memory allocation backtracing enabled)
    AC_DEFINE([PS_MEM_BACKTRACE], 1, [Define to 1 if you want memory backtracing])
    CFLAGS="${CFLAGS=} -rdynamic"
  ]
)
AC_CHECK_FUNCS([backtrace])

AC_LANG(C)
AC_PROG_CC_C99
dnl _GNU_SOURCE is needed for atleast strcasestr() with glibc
AC_GNU_SOURCE
AC_C_INLINE

dnl enable largefile supports
AC_SYS_LARGEFILE
AC_FUNC_FSEEKO

AC_PROG_INSTALL
AC_PROG_LIBTOOL

dnl check the systems endianness
AC_C_BIGENDIAN

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

dnl ----------------- pthread support --------------------

dnl Check for how to link against pthreads
dnl RHEL 4.0 was upset about the first arg to ACX_PTHREAD being NULL ([]) so
dnl we've added in a worthless message to keep it happy
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.

ACX_PTHREAD(
  [PSLIB_CFLAGS="${PSLIB_CFLAGS=} ${PTHREAD_CFLAGS}"
    PSLIB_LIBS="${PSLIB_LIBS=} ${PTHREAD_CFLAGS} ${PTHREAD_LIBS}"],
  [AC_MSG_ERROR([pthread support is required to build psLib])]
)

dnl ----------------- MYSQL options --------------------

AX_LIB_MYSQL([4.1.2],
  [PSLIB_CFLAGS="${PSLIB_CFLAGS=} ${MYSQL_CFLAGS} -DHAVE_PSDB"
    PSLIB_LIBS="${PSLIB_LIBS=} ${MYSQL_LDFLAGS}"],
  [PSLIB_CFLAGS="${PSLIB_CFLAGS=}"]
)

dnl ----------------- CFITSIO options --------------------

dnl save LIBS/CFLAGS/LDFLAGS
TMP_LIBS=${LIBS}
TMP_CFLAGS=${CFLAGS}
TMP_LDFLAGS=${LDFLAGS}
TMP_CPPFLAGS=${CPPFLAGS}

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"

CFLAGS="${CFLAGS=} ${CFITSIO_CFLAGS}"
LDFLAGS="${LDFLAGS=} ${CFITSIO_LDFLAGS}"

dnl Solaris needs to suck in these symbols from unusual locations
AC_SEARCH_LIBS([gethostbyname], [nsl])
AC_SEARCH_LIBS([socket], [socket])

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

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

dnl restore the LIBS/CFLAGS/LDFLAGS
LIBS=${TMP_LIBS}
CFLAGS=${TMP_CFLAGS}
LDFLAGS=${TMP_LDFLAGS}
CPPFLAGS=${TMP_CPPFLAGS}

dnl ------------------ FFTW3 options ---------------------

dnl save LIBS/CFLAGS/LDFLAGS
TMP_LIBS=${LIBS}
TMP_CFLAGS=${CFLAGS}
TMP_LDFLAGS=${LDFLAGS}
TMP_CPPFLAGS=${CPPFLAGS}

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"

CFLAGS="${CFLAGS} ${FFTW3_CFLAGS}"
CPPFLAGS=${CFLAGS}
LDFLAGS="${LDFLAGS} ${FFTW3_LDFLAGS}"

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

AC_SUBST([FFTW3_CFLAGS])

dnl restore the CFLAGS/LDFLAGS
LIBS=${TMP_LIBS}
CFLAGS=${TMP_CFLAGS}
LDFLAGS=${TMP_LDFLAGS}
CPPFLAGS=${TMP_CPPFLAGS}

dnl -------------------- GSL options ---------------------

dnl save LIBS/CFLAGS/LDFLAGS
TMP_LIBS=${LIBS}
TMP_CFLAGS=${CFLAGS}
TMP_LDFLAGS=${LDFLAGS}
TMP_CPPFLAGS=${CPPFLAGS}

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 restore the CFLAGS/LDFLAGS
LIBS=${TMP_LIBS}
CFLAGS=${TMP_CFLAGS}
LDFLAGS=${TMP_LDFLAGS}
CPPFLAGS=${TMP_CPPFLAGS}

dnl ------------------ libjpeg options ---------------------

dnl save LIBS/CFLAGS/LDFLAGS
TMP_LIBS=${LIBS}
TMP_CFLAGS=${CFLAGS}
TMP_LDFLAGS=${LDFLAGS}
TMP_CPPFLAGS=${CPPFLAGS}

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(jpeg-lib,
[AS_HELP_STRING(--with-jpeg-lib=DIR,Specify libjpeg library directory.)],
[JPEG_LDFLAGS="-L$withval"])

CFLAGS="${CFLAGS} ${JPEG_CFLAGS}"
CPPFLAGS=${CFLAGS}
LDFLAGS="${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
LIBS=${TMP_LIBS}
CFLAGS=${TMP_CFLAGS}
LDFLAGS=${TMP_LDFLAGS}
CPPFLAGS=${TMP_CPPFLAGS}

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

dnl save LIBS/CFLAGS/LDFLAGS
TMP_LIBS=${LIBS}
TMP_CFLAGS=${CFLAGS}
TMP_LDFLAGS=${LDFLAGS}
TMP_CPPFLAGS=${CPPFLAGS}

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 restore the CFLAGS/LDFLAGS
LIBS=${TMP_LIBS}
CFLAGS=${TMP_CFLAGS}
LDFLAGS=${TMP_LDFLAGS}
CPPFLAGS=${TMP_CPPFLAGS}

dnl ---------------- PERLPREFIX option -------------------

dnl save LIBS/CFLAGS/LDFLAGS
TMP_LIBS=${LIBS}
TMP_CFLAGS=${CFLAGS}
TMP_LDFLAGS=${LDFLAGS}
TMP_CPPFLAGS=${CPPFLAGS}

  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 restore the CFLAGS/LDFLAGS
LIBS=${TMP_LIBS}
CFLAGS=${TMP_CFLAGS}
LDFLAGS=${TMP_LDFLAGS}
CPPFLAGS=${TMP_CPPFLAGS}

dnl for MacOSX suppport -- check if sqrtf is in mx library.

dnl save LIBS/CFLAGS/LDFLAGS
TMP_LIBS=${LIBS}
TMP_CFLAGS=${CFLAGS}
TMP_LDFLAGS=${LDFLAGS}
TMP_CPPFLAGS=${CPPFLAGS}

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 restore the CFLAGS/LDFLAGS
LIBS=${TMP_LIBS}
CFLAGS=${TMP_CFLAGS}
LDFLAGS=${TMP_LDFLAGS}
CPPFLAGS=${TMP_CPPFLAGS}

dnl doxygen doc generation is very, very slow so we're turing it off by default

dnl save LIBS/CFLAGS/LDFLAGS
TMP_LIBS=${LIBS}
TMP_CFLAGS=${CFLAGS}
TMP_LDFLAGS=${LDFLAGS}
TMP_CPPFLAGS=${CPPFLAGS}

AC_ARG_ENABLE(doxygen,
  [AS_HELP_STRING(--enable-doxygen ,enable manpage generation)],
  [AC_MSG_RESULT(doxygen enabled)
    AC_PATH_PROG([DOXYGEN], [doxygen], [])
  ],
  [AC_MSG_RESULT([doxygen disabled])
    doxygen=off
  ]
)
AM_CONDITIONAL([HAVE_DOXYGEN], test -n "$DOXYGEN" -a "x$doxygen" != "xoff")

dnl restore the CFLAGS/LDFLAGS
LIBS=${TMP_LIBS}
CFLAGS=${TMP_CFLAGS}
LDFLAGS=${TMP_LDFLAGS}
CPPFLAGS=${TMP_CPPFLAGS}

dnl ------- enable -Werror after all of the probes have run ---------
IPP_STDOPTS

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

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([
  Doxyfile
  Makefile
  etc/Makefile
  m4/Makefile
  pslib-config
  pslib.pc
  share/Makefile
  src/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/sys/Makefile
  src/types/Makefile
  test/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/pstap/Makefile
  test/pstap/src/Makefile
  test/sys/Makefile
  test/types/Makefile
  utils/Makefile
])
dnl src/xml/Makefile
dnl test/xml/Makefile


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

AC_OUTPUT
