AC_PREREQ(2.59)

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

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

dnl otherise AC_PROG_CC will default CFLAGS to "-g -02"
if test -z ${CFLAGS} ; then
  CFLAGS="-g -O0"
fi
AC_SUBST([CFLAGS])

AC_LANG(C)
AC_PROG_CC
dnl FIXME document why GNU_SOURCE is being set
AC_GNU_SOURCE
AC_PROG_INSTALL
AM_PROG_LIBTOOL

AC_PREFIX_DEFAULT([`pwd`])

dnl cflags --------------------------------------------------------------------
dnl FIXME document why -std=gnu99 is being set

AC_MSG_CHECKING([C99 flag])
if test "$GCC" = "yes" ; then
  AM_CFLAGS="-std=gnu99"
  AC_MSG_RESULT([-std=gnu99])
else
  AM_CFLAGS="-std=c99"
  AC_MSG_RESULT([-std=c99])
fi
AM_CFLAGS="${AM_CFLAGS=} -Wall -Werror"
AC_SUBST([AM_CFLAGS])

SRCPATH='${top_srcdir}/src'
SRCDIRS="astrom camera config detrend imcombine imsubtract objects photom"
SRCINC=`echo "${SRCDIRS=}" | ${SED} "s|\(\\w\+\)|-I\${SRCPATH=}/\1|g"`
SRCSUBLIBS=`echo "${SRCDIRS=}" | ${SED} "s|\(\\w\+\)|\1/libpsmodule\1.la|g"`
AC_SUBST([SRCSUBLIBS],${SRCSUBLIBS=})
AC_SUBST([SRCDIRS],${SRCDIRS=})
AC_SUBST([SRCINC],${SRCINC=})

dnl doxygen -------------------------------------------------------------------

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

dnl pslib ---------------------------------------------------------------------
AC_ARG_WITH(pslib-config,
[  --with-pslib-config=FILE  Specify location of psLib-config script],
[PSLIB_CONFIG=$withval])

if test -z ${PSLIB_CONFIG} ; then
  PKG_CHECK_MODULES([PSLIB], [pslib >= 0.8.0])
else
  AC_CHECK_FILE($PSLIB_CONFIG,[],
    [AC_MSG_ERROR([psLib is required.  If not in path, use --with-pslib-config to specify pslib-config script location.])])
  AC_MSG_CHECKING([PSLIB_CFLAGS])
  PSLIB_CFLAGS="`${PSLIB_CONFIG} --cflags`"
  AC_MSG_RESULT([${PSLIB_CFLAGS}])
  AC_MSG_CHECKING([PSLIB_LIBS])
  PSLIB_LIBS="`${PSLIB_CONFIG} --libs`"
  AC_MSG_RESULT([${PSLIB_LIBS}])
fi

PSMODULE_CFLAGS="${PSMODULE_CFLAGS=} ${PSLIB_CFLAGS}"
PSMODULE_LIBS="${PSMODULE_CFLAGS=} ${PSLIB_LIBS}"
dnl ---------------------------------------------------------------------------

AC_SUBST([PSMODULE_CFLAGS])
AC_SUBST([PSMODULE_LIBS])

AC_CONFIG_FILES([
  Makefile
  src/Makefile
  src/astrom/Makefile
  src/camera/Makefile
  src/config/Makefile
  src/detrend/Makefile
  src/imcombine/Makefile
  src/imsubtract/Makefile
  src/objects/Makefile
  src/photom/Makefile
  test/Makefile
  test/astrom/Makefile
  test/camera/Makefile
  test/config/Makefile
  test/detrend/Makefile
  test/imcombine/Makefile
  test/imsubtract/Makefile
  test/objects/Makefile
  test/photom/Makefile
  Doxyfile
  psmodule-config
  psmodule.pc
])

AC_OUTPUT
