AC_PREREQ(2.59)

AC_INIT([psmodule],[0.9.99],[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=""
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])

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="astrom config detrend imcombine imsubtract objects pslib"
# 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/libpsmodule\1.la|g"`
AC_SUBST(SRCSUBLIBS,${SRCSUBLIBS=})
AC_SUBST(SRCINC,${SRCINC=})
AC_SUBST([SRCDIRS],${SRCDIRS=})

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

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

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

AC_CHECK_LIB(jpeg,jpeg_CreateCompress,[],[AC_MSG_ERROR([jpeg library not found.])])

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.9.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/config/Makefile
  src/detrend/Makefile
  src/imcombine/Makefile
  src/imsubtract/Makefile
  src/objects/Makefile
  src/pslib/Makefile
  test/Makefile
  test/astrom/Makefile
  test/config/Makefile
  test/detrend/Makefile
  test/imcombine/Makefile
  test/imsubtract/Makefile
  test/objects/Makefile
  test/pslib/Makefile
  Doxyfile
  psmodule-config
  psmodule.pc
])

AC_OUTPUT
