AC_PREREQ(2.59)

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

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

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

dnl enable largefile supports
AC_SYS_LARGEFILE
AC_FUNC_FSEEKO

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

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)

SRCPATH='${top_srcdir}/src'
SRCDIRS="config concepts camera astrom detrend imcombine objects"
# 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/libpsmodules\1.la|g"`
AC_SUBST(SRCSUBLIBS,${SRCSUBLIBS=})
AC_SUBST(SRCINC,${SRCINC=})
AC_SUBST([SRCDIRS],${SRCDIRS=})

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=} -fprofile-arcs -ftest-coverage -pg"]
)

dnl CFLAGS="${CFLAGS=} -fprofile-arcs -ftest-coverage -pg"]

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

PSMODULES_CFLAGS="${PSMODULES_CFLAGS=} ${PSLIB_CFLAGS}"
PSMODULES_LIBS="${PSMODULES_LIBS=} ${PSLIB_LIBS}"
dnl ---------------------------------------------------------------------------

AC_SUBST([PSMODULES_CFLAGS])
AC_SUBST([PSMODULES_LIBS])

AC_CONFIG_FILES([
  Makefile
  src/Makefile
  src/astrom/Makefile
  src/camera/Makefile
  src/config/Makefile
  src/concepts/Makefile
  src/detrend/Makefile
  src/imcombine/Makefile
  src/objects/Makefile
  test/Makefile
  test/astrom/Makefile
  test/config/Makefile
  test/camera/Makefile
  test/concepts/Makefile
  test/detrend/Makefile
  test/imcombine/Makefile
  test/objects/Makefile
  test/pstap/Makefile
  test/pstap/src/Makefile
  Doxyfile
  psmodules-config
  psmodules.pc
])

AC_OUTPUT
