dnl ===============================================================================
dnl              Configure script for AMIDE
dnl ===============================================================================
AC_PREREQ(2.53)
AC_INIT([amide],[0.9.1],[amide-users@lists.sourceforge.net])

AC_CONFIG_SRCDIR(src/amide.c)
AC_CONFIG_HEADERS(amide_config.h)
AC_CONFIG_MACRO_DIR([m4])
AM_INIT_AUTOMAKE(AC_PACKAGE_NAME, AC_PACKAGE_VERSION)

AM_MAINTAINER_MODE

AC_PROG_CC
AC_PROG_CXX
AM_PROG_LIBTOOL
AC_FUNC_MKDIR

AC_CHECK_HEADERS(unistd.h, AC_DEFINE(HAVE_UNISTD_H))
AC_CHECK_SIZEOF(long,8)
AC_CHECK_SIZEOF(long long,8)

AC_CHECK_FUNCS(strptime)

dnl ================= translation =======================================

GETTEXT_PACKAGE=amide
AC_SUBST(GETTEXT_PACKAGE)
AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE, "$GETTEXT_PACKAGE", [Define the gettext package to be used])

ALL_LINGUAS="zh_CN"
AM_GLIB_GNU_GETTEXT
AC_PROG_INTLTOOL

dnl m4 required for compiling
AC_PATH_PROGS(M4, gnum4 gm4 m4, NOT_FOUND)
if test $M4 = "NOT_FOUND"; then
   echo "Could not find m4 on this system, please install to compile AMIDE."
   exit 1
fi

dnl =====================================================================
dnl pick our optimization level, OSX's version of gcc doesn't handle -O6
case "$host_os" in
darwin*) 	CFLAGS="-g -O2 -Wall";;
*)		CFLAGS="-g -O6 -Wall"
		CXXFLAGS="-g -O6 -Wall";;
esac

if test $CC = "gcc"; then
   echo -n "checking version of gcc... "
   CC_VERSION=`$CC -dumpversion`
   if (test $CC_VERSION = "2.96" || test $$CC_VERSION = "2.95.4"); then
	echo $CC_VERSION
	echo "******************************************************************"
	echo "This version of gcc is a beta compiler and often incorrectly"
	echo "compiles this program.  Please upgrade to the current version of"
	echo "gcc (3.2.1 as of this writing) or downgrade to 2.95.3"
	echo "******************************************************************"
	echo -n "continue anyway? (y/n) "
	read continue_with_bad_gcc
	if test $continue_with_bad_gcc = "y"; then
		echo "removing optimizations to reduce potential of compiler error"
		CFLAGS="-g -O0"
	else
		exit 1
	fi
   else
	echo "$CC_VERSION, good"
   fi
fi









##############################################################
### Checks for optional libraries
##############################################################

dnl check for various libraries and programs that we might need
AM_PATH_GSL(1.1.1, FOUND_LIBGSL=yes, FOUND_LIBGSL=no)
AC_CHECK_LIB(ecat, matrix_open, FOUND_LIBECAT=yes, FOUND_LIBECAT=no, -L/sw/lib)
AC_CHECK_LIB(volpack, vpGetErrorString, FOUND_VOLPACK=yes, FOUND_VOLPACK=no, -lm -L/sw/lib -L/usr/local/lib)
AM_PATH_XMEDCON(0.10.0, FOUND_XMEDCON=yes, FOUND_XMEDCON=no)
AM_PATH_LIBFAME(0.9.1, FOUND_LIBFAME=yes, FOUND_LIBFAME=no)
dnl AC_CHECK_LIB(libfame, fame_init, FOUND_LIBFAME=yes, FOUND_LIBFAME=no)

dnl switch to C++ for DCMTK library stuff - also, if pthread is on the platform, probably need that
AC_LANG_CPLUSPLUS
AC_CHECK_LIB(pthread, pthread_mutex_init, THREAD_LIBS="-lpthread", THREAD_LIBS="")
AC_CHECK_LIB(dcmdata, main, FOUND_DCMDATA=yes, FOUND_DCMDATA=no, -lofstd  -lz -L/usr/local/dicom/lib -L/sw/lib $THREAD_LIBS)


dnl Let people compile without debugging information
AC_ARG_ENABLE(
	amide_debug, 
	[  --enable-amide-debug	  Compile with some debugging messages [default=yes]], 
	enable_amide_debug="$enableval", 
	enable_amide_debug=yes)

if test $enable_amide_debug = yes; then
	echo "Compiling with AMIDE debugging messages"
	AMIDE_DEBUG_CFLAGS="-ggdb"
	AC_SUBST(AMIDE_DEBUG_CFLAGS)
	AC_DEFINE(AMIDE_DEBUG, 1, Define to compile AMIDE with debugging information)
else
	echo "Compiling without AMIDE debugging messages"
fi

dnl check if we're building on windows
AC_MSG_CHECKING([for native Win32])
case "$host" in
  *-*-mingw*)
    native_win32=yes
    ;;
  *)
    native_win32=no
    ;;
esac
AC_MSG_RESULT([$native_win32])
AM_CONDITIONAL(AMIDE_OS_WIN32, test "$native_win32" = yes)
dnl if you need to know OS in .c code, use G_PLATFORM_WIN32

dnl let people compile without the GNU Scientific Library
AC_ARG_ENABLE(
	libgsl,
	[  --enable-libgsl	  Compile with the GNU Scientific Library [default=yes]],
	enable_libgsl="$enableval",
	enable_libgsl=yes)

if (test $enable_libgsl = yes) && (test $FOUND_LIBGSL = yes); then
	echo "Compiling with GNU Scientific Library support"
	AC_DEFINE(AMIDE_LIBGSL_SUPPORT, 1, Define to compile with the GNU Scientific Library)
else
	echo "Compiling without the GNU Scientific Library"
fi


dnl Let people compile without having libecat (z_matrix_70)
AC_ARG_ENABLE(
	libecat, 
	[  --enable-libecat	  Compile with the libecat/CTI library [default=yes]],
	enable_libecat="$enableval",
	enable_libecat=yes)

if (test $enable_libecat = yes) && (test $FOUND_LIBECAT = yes); then
	echo "Compiling with libecat/CTI file support"
	AMIDE_LIBECAT_LIBS="-lecat"
	AC_SUBST(AMIDE_LIBECAT_LIBS)
	AC_DEFINE(AMIDE_LIBECAT_SUPPORT, 1, Define to compile with libecat)
else
	echo "Compiling without libecat/CTI file support"
fi



dnl Let people compile without having libmdc
AC_ARG_ENABLE(
	libmdc, 
	[  --enable-libmdc	  Compile with the xmedcon/libmdc library [default=yes]], 
	enable_libmdc="$enableval", 
	enable_libmdc=yes)

if (test $enable_libmdc = yes) && (test $FOUND_XMEDCON = yes); then
	echo "Compiling with xmedcon/libmdc file support"
	AC_DEFINE(AMIDE_LIBMDC_SUPPORT, 1, Define to compile with (X)MedCon)

	dnl 2007.10.28 - new xmedcon's should be using glib2/gtk2, dont' have to overwrite XMEDCON_LIBS
	dnl XMEDCON_LIBS="-L/usr/local/lib -L/usr/local/xmedcon/lib -lmdc"
	AC_SUBST(XMEDCON_LIBS)
else
	echo "Compiling without xmedcon/libmdc file support"
fi


dnl Let people compile without rendering/libvolpack
AC_ARG_ENABLE(
	libvolpack, 
	[  --enable-libvolpack	  Compile in libvolpack rendering support [default=yes]], 
	enable_libvolpack="$enableval", 
	enable_libvolpack=yes)

if (test $enable_libvolpack = yes) && (test $FOUND_VOLPACK = yes); then
	echo "Compiling with libvolpack rendering support "
	AMIDE_LIBVOLPACK_LIBS="-lvolpack"
	AC_SUBST(AMIDE_LIBVOLPACK_LIBS)
	AC_DEFINE(AMIDE_LIBVOLPACK_SUPPORT, 1, Define to compile with VolPack)
else
	echo "Compiling without libvolpack rendering support"
fi


dnl Let people compile without mpeg movie generation/libfame
AC_ARG_ENABLE(
	libfame,
	[  --enable-libfame   	  Compile in libfame mpeg encoding support [default=yes]], 
	enable_libfame="$enableval", 
	enable_libfame=yes)

if (test $enable_libfame = yes) && (test $FOUND_LIBFAME = yes); then
	echo "Compiling with libfame mpeg encoding support "
	AC_DEFINE(AMIDE_LIBFAME_SUPPORT, 1, Define to compile with Libfame)
else
	echo "Compiling without libfame mpeg encoding support"
fi

dnl Let people compile without the DCMTK library
AC_ARG_ENABLE(
	libdcmdata,
	[  --enable-libdcmdata	  Compile in DCMTK support for DICOM files [default=yes]], 
	enable_libdcmdata="$enableval", 
	enable_libdcmdata=yes)

if (test $enable_libdcmdata = yes) && (test $FOUND_DCMDATA = yes); then
	echo "Compiling with DCMTK support for DICOM files"
	AMIDE_LIBDCMDATA_LIBS="-L/usr/local/dicom/lib -ldcmdata -lofstd -lz $THREAD_LIBS"
	AMIDE_LIBDCMDATA_CFLAGS="-I/usr/local/dicom/include/dcmtk/dcmdata -I/usr/local/dicom/include/dcmtk/ofstd -I/usr/local/dicom/include -I/usr/include/dcmtk/dcmdata -I/usr/local/include/dcmtk/ofstd -I/sw/include/dcmtk/dcmdata -I/sw/include/dcmtk/ofstd -I/sw/include/dcmtk"
	AC_SUBST(AMIDE_LIBDCMDATA_LIBS)
	AC_SUBST(AMIDE_LIBDCMDATA_CFLAGS)
	AC_DEFINE(AMIDE_LIBDCMDATA_SUPPORT, 1, Define to compile with DCMTK)
else
	echo "Compiling without DCMTK support for DICOM files"
fi



###############################
# Check for gtk/gnome stuff
##############################

PKG_CHECK_MODULES(AMIDE_GTK,[
	glib-2.0	>= 2.10.0
	gobject-2.0	>= 2.10.0
	gtk+-2.0	>= 2.10.0
	libxml-2.0	>= 2.4.12
	libgnomecanvas-2.0 >= 2.0.0
])

## add in gnome-vfs and gconf if not on win32 
## gnome-vfs is only used by amide_gnome.c and only on unix
## gconf stuff is encapsulated in amide_gconf.c
if (test $native_win32 = no); then
   PKG_CHECK_MODULES(AMIDE_GTK_EXTRA,[
		gnome-vfs-2.0   >= 2.18.0
		gconf-2.0	>= 2.18.0
   ])
   AMIDE_GTK_LIBS="$AMIDE_GTK_LIBS $AMIDE_GTK_EXTRA_LIBS"
   AMIDE_GTK_CFLAGS="$AMIDE_GTK_CFLAGS $AMIDE_GTK_EXTRA_CFLAGS"
fi

AC_SUBST(AMIDE_GTK_LIBS)
AC_SUBST(AMIDE_GTK_CFLAGS)

dnl glib-genmarshal
AC_PATH_PROG(GLIB_GENMARSHAL, glib-genmarshal)

dnl This is installed from GTK+ 2.0's gdk-pixbuf
AC_PATH_PROG(GDK_PIXBUF_CSOURCE, gdk-pixbuf-csource)
test -z "$GDK_PIXBUF_CSOURCE" && AC_MSG_ERROR([
*** You need the gdk-pixbuf-csource tool which is installed
*** from GTK+ 2's gdk-pixbuf.
***
*** Either the location where you installed your GTK+ 2 is
*** not in your PATH or something is screwed up with your
*** GTK+ 2 installation
])


dnl libgnomecanvas anti-aliasing is buggy, and looks like it's never going to be fixed
dnl record if our version of libgnomecanvas can handle anti-aliasing
dnl PKG_CHECK_MODULES(dummy, 
dnl	[libgnomecanvas-2.0 >= 2.2.0], 
dnl	 enable_antialiasing=yes, enable_antialiasing=no)
dnl if test $enable_antialiasing = yes; then
dnl	echo "Compiling with libgnomecanvas antialiasing support"
dnl	AC_DEFINE(AMIDE_LIBGNOMECANVAS_AA, 1, Define to compile AMIDE with an antialiased canvas)
dnl else
dnl	echo "Compiling without libgnomecanvas antialiasing support"
dnl	AC_DEFINE(AMIDE_LIBGNOMECANVAS_AA, 0, Define to compile AMIDE with an antialiased canvas)
dnl fi


################################################################
# Checks for gtk-doc and docbook-tools, from gtk's configure.in
################################################################

dnl if we define --enable-gtk-doc, we build the library documentation
GTK_DOC_CHECK([1.0])

dnl the following is for the application documentation
AC_ARG_ENABLE(doc,
        [  --disable-doc           Do not build documentation.],,)

if test "x${enable_doc}" = "x" ; then
  enable_doc=yes
fi

AC_MSG_CHECKING(whether documentation should be built)
if test ${enable_doc} = no; then
  AC_MSG_RESULT([no])
else
  AC_MSG_RESULT([yes])
fi
AM_CONDITIONAL(DISABLE_DOC, test ${enable_doc} = no)

if test ${enable_doc} = yes; then
   dnl building documentatin
   GNOME_DOC_INIT

   dnl scrollkeeper-update required for document building
   AC_PATH_PROGS(SCROLLKEEPER, scrollkeeper-update, NOT_FOUND)
   if test $SCROLLKEEPER = "NOT_FOUND"; then
      echo "Could not find scrollkeeper-update on this system.  Will not compile help files."
   fi
else
  dnl not building documentation
  ENABLE_SK_TRUE="#"
  ENABLE_SK_FALSE=""  
  HAVE_GNOME_DOC_UTILS_TRUE="#"
  HAVE_GNOME_DOC_UTILS_FALSE=""
fi

AC_CHECK_PROG(DB2HTML, db2html, true, false)
AM_CONDITIONAL(HAVE_DOCBOOK, $DB2HTML)
AM_CONDITIONAL(SCROLLKEEPER_EXISTS, [test "$SCROLLKEEPER" != "NOT_FOUND"])

AC_SUBST(CPPFLAGS)
AC_SUBST(LDFLAGS)

AC_CONFIG_FILES([
Makefile
pixmaps/Makefile
src/Makefile
win32/Makefile
doc/Makefile
doc/reference/Makefile
doc/reference/version.xml
help/Makefile
etc/Makefile
etc/amide-$VERSION-1.info:etc/amide.info.in
etc/amide-$VERSION-1.iss:etc/amide.iss.in
etc/amide.spec
man/Makefile
po/Makefile.in])
AC_OUTPUT 
dnl help/C/Makefile
dnl help/es/Makefile

dnl Move the spec file so it won't get eaten by make distclean
echo "Moving SPEC file to base directory"
mv etc/amide.spec ./
