#General stuff
AC_INIT(libapogee, 3.0.2986, support@ccd.com)
AM_INIT_AUTOMAKE(libapogee, 3.0.2986)
AM_INIT_AUTOMAKE([-Wall -Werror])
AC_CONFIG_SRCDIR([apogee/ApogeeCam.cpp])
AC_CONFIG_HEADER([config.h])
AC_CONFIG_MACRO_DIR([m4])

#set library version string
#have to update version 3 different places
#in this one file...no good
AC_SUBST([LIBAPOGEE_SO_VERSION], [3:0:0])
AC_SUBST([LIBAPOGEE_RELEASE_VERSION], [3.0.2986])
AC_SUBST([LIBAPOGEE_VERSION_DIR], [3.0])

#subsitution for the configuration files
#directory
AC_SUBST([CFG_DIR], ["$sysconfdir"/Apogee])

LT_INIT([dlopen])

# Checks for programs.
AC_PROG_CXX
AC_PROG_INSTALL
AC_PROG_MAKE_SET

# doxygen checking
FLM_PROG_TRY_DOXYGEN

AM_CONDITIONAL([HAVE_DOXYGEN], [test -n "$DOXYGEN"])
AS_IF([test -n "$DOXYGEN"], [AC_CONFIG_FILES([doc/doxygen/zenith])])

# Checks for standard header files.
AC_CHECK_HEADERS([stdint.h])

#checks for libcurl
have_curl=no
AC_CHECK_LIB([curl], [curl_easy_init], [have_curl=yes])

if test "x${have_curl}" = xno; then
	AC_MSG_ERROR( [
	------------------------------------
	  The curl library is required to 
	  build libapogee. Please install
	  libcurl4-openssl-dev to clear this
	  error.
	------------------------------------])
fi

have_curl_hdr=no
AC_CHECK_HEADERS([curl/curl.h], [have_curl_hdr=yes])
if test "x${have_curl_hdr}" = xno; then
	AC_MSG_ERROR( [
	------------------------------------
	  The curl header files are required
	  to build libapogee. Please install
	  libcurl4-openssl-dev to clear this
	  error.
	------------------------------------])
fi

#checks for libusb-1.0
have_libusb=no
AC_CHECK_LIB([usb-1.0], [libusb_init], [have_libusb=yes])

if test "x${have_libusb}" = xno; then
	AC_MSG_ERROR( [
	------------------------------------
	  The libusb-1.0 is required to build 
	  libapogee. Please install
	  libusb-1.0.0-dev to clear this error.
	------------------------------------])
fi

have_libusb_hdr=no
AC_CHECK_HEADERS([libusb-1.0/libusb.h], [have_libusb_hdr=yes])

if test "x${have_libusb_hdr}" = xno; then
	AC_MSG_ERROR( [
	------------------------------------
	  The libusb-1.0 header files are
	  to build libapogee. Please install
	  libusb-1.0.0-dev to clear this error.
	------------------------------------])
fi

#checks for boost
AX_BOOST_BASE([1.33.1], [], AC_MSG_ERROR( [
	------------------------------------
	  Checking for boost base failed.
	  The boost regex is required to
	  build libapogee. Please install
	  libboost-regex-1.33.1-dev or
	  greater to clear this error.
	------------------------------------]) )

AX_BOOST_REGEX

#determine if the pyLibApg wrapper
#should be built too
AC_ARG_ENABLE([python-wrapper],
	[AS_HELP_STRING([--enable-python-wrapper],
	[enable building the python wrapper])],
	[BUILD_PYLIBAPG=${enableval}], [BUILD_PYLIBAPG=no])

AM_CONDITIONAL([HAVE_PYLIBAPG], [test "x${BUILD_PYLIBAPG}" = xyes])

#make the python wrapper if specified
if test "x${BUILD_PYLIBAPG}" = xyes; then
	AC_MSG_NOTICE([Adding pyLibapogee to the build])
	AM_PATH_PYTHON(2.6)
	AX_PKG_SWIG([1.3.40], [], AC_MSG_ERROR( [
		------------------------------------
		  Swig not found. Swig is required
		  for buiding the pyLibapogee wrapper.
		------------------------------------]) )
		
	AX_SWIG_ENABLE_CXX
	AX_SWIG_PYTHON
	AX_PYTHON_MODULE([numpy],[])
	
	if test "x${HAVE_PYMOD_NUMPY}" = xno; then
		AC_MSG_ERROR( [
		------------------------------------
		  The numpy is a required Python
		  module for the pyLibapogee wrapper.
		------------------------------------])
	fi

fi

#determine if the java LibApg wrapper
#should be built too
AC_ARG_ENABLE([java-wrapper],
	[AS_HELP_STRING([--enable-java-wrapper],
	[enable building the java wrapper])],
	[BUILD_JLIBAPG=${enableval}], [BUILD_JLIBAPG=no])

#putting this inside the if java wrapper
#generates an error, out here it is fine
#and doesn't seem to cause any issues
AM_PROG_GCJ

AM_CONDITIONAL([HAVE_JLIBAPG], [test "x${BUILD_JLIBAPG}" = xyes])

#make the java wrapper if specified
if test "x${BUILD_JLIBAPG}" = xyes; then
	AC_MSG_NOTICE([Adding jLibapogee to the build])
	
	#swig checks
	AX_PKG_SWIG(1.3.40)
	AX_SWIG_ENABLE_CXX
	
	#java checks
	AX_PROG_JAVA
	
	if test "x$JAVAC" = x; then
		AC_MSG_ERROR( [
		------------------------------------
		  Java needed
		------------------------------------])
	fi
	
	AX_JNI_INCLUDE_DIR
	for JNI_INCLUDE_DIR in $JNI_INCLUDE_DIRS
	do
		JNI_CPPFLAGS="$JNI_CPPFLAGS -I$JNI_INCLUDE_DIR"
	done
	
	# Export the paths so that the makefile gets them
	AC_SUBST(JNI_CPPFLAGS, $JNI_CPPFLAGS)
	
	AX_PROG_JAR
	
fi

#determine if we should use the alternative
#bulkio error handling method
AC_ARG_ENABLE([alt-bulkio-checking],
	[AS_HELP_STRING([--enable-alt-bulkio-checking],
	[allow usb bulkio transfers that are 512 or less bytes short to complete without error])],
	[USE_ALT_BULKIO_CHECK=${enableval}], [USE_ALT_BULKIO_CHECK=no])

if test "x${USE_ALT_BULKIO_CHECK}" = xyes; then
	AC_MSG_NOTICE([Adding -DALT_BULKIO_CHECK to build])
	ALT_BULKIO_DEFINE=-DALT_BULKIO_CHECK
else
	ALT_BULKIO_DEFINE=
fi
AC_SUBST([ALT_BULKIO_DEFINE])

#making the udev rules file dir prefix
#configurable
#file install will go to udev-prefix/udev/rules.d
AC_ARG_WITH([udev-prefix],
	[AS_HELP_STRING([--with-udev-prefix],
	[specify the prefix for udev rules installation, udev-prefix/udev/rules.d])],
	[UDEV_DIR="$withval/udev/rules.d"],
	[UDEV_DIR='/etc/udev/rules.d'])

AC_SUBST([UDEV_DIR])

#list the makefiles
AC_CONFIG_FILES([Makefile apogee/Makefile apogee/linux/Makefile
		setup/Makefile cfg/Makefile cfg/camera/Makefile examples/Makefile
		doc/Makefile doc/doxygen/Makefile pyLibapogee/Makefile pyLibapogee/src/Makefile
		pyLibapogee/examples/Makefile jLibapogee/Makefile jLibapogee/src/Makefile
		jLibapogee/examples/Makefile])

AC_OUTPUT

echo "
  Apogee Imaging Systems Library ($PACKAGE_NAME) version $PACKAGE_VERSION
  Prefix.........: $prefix
  udev dir.......: $UDEV_DIR
  C++ Compiler...: $CXX $CXXFLAGS $CPPFLAGS $BOOST_CPPFLAGS
  Linker.........: $LD $LDFLAGS $BOOST_LDFLAGS $LIBS 
  Python Wrapper.: $BUILD_PYLIBAPG
  Java Wrapper...: $BUILD_JLIBAPG
  Doxygen........: ${DOXYGEN:-NONE}
"
