dnl Process this file with autoconf to produce a configure script.
AC_INIT(main.c)
AC_CONFIG_HEADER(config.h)
dnl Grab the package and version from the top debian/changelog entry.

AC_ARG_WITH(platform,
[  --with-platform	  platform name, as detected by AppRun],
[AC_SUBST(with_platform)],
[
AC_MSG_ERROR([

Please do not run 'configure' manually - instead, run the AppRun script with
the --compile option, like this:
	$ OroboROX/AppRun --compile

Thank you.
])
])

if test -z "$PKG_CONFIG"; then
  PKG_CONFIG=pkg-config
fi
AC_SUBST(PKG_CONFIG)

AC_MSG_CHECKING(that pkg-config runs)
if "$PKG_CONFIG" --version > /dev/null 2>&1 ; then
  AC_MSG_RESULT(yes)
else
  AC_MSG_RESULT(no)
  AC_MSG_ERROR([*** $PKG_CONFIG missing ***

\'$PKG_CONFIG --version\' failed to run - make sure it is inside one of the
directories in your PATH environment variable!

pkg-config comes with the developement packages for GTK+-2.2, available at:

    http://www.gtk.org])
fi

dnl check for required packages
AC_DEFUN(ROX_REQUIRE, [
AC_MSG_CHECKING(that $1 (version >= $2) is installed)
if "$PKG_CONFIG" --atleast-version=$2 $1 ; then
  AC_MSG_RESULT(yes)
else
  AC_MSG_RESULT(no)
  if "$PKG_CONFIG" --exists $1 ; then
    AC_MSG_ERROR(Current version is only `"$PKG_CONFIG" "$1" --modversion`)
  else
    AC_MSG_ERROR(Package is not installed)
  fi
fi
])

dnl check for optional packages
AC_DEFUN(ROX_USE, [
AC_MSG_CHECKING(that $1 (version >= $2) is installed)
if "$PKG_CONFIG" --atleast-version=$2 $1 ; then
  AC_MSG_RESULT(yes)
else
  AC_MSG_RESULT(no)
  if "$PKG_CONFIG" --exists $1 ; then
    echo "Current version is only" `"$PKG_CONFIG" "$1" --modversion`
  else
    echo "Package is not installed"
  fi
  echo "Not using $1."
fi
])

ROX_REQUIRE(glib-2.0, 2.0)
dnl ROX_REQUIRE(libstartup-notification-1.0 0.4)
ROX_REQUIRE(xft, 2.0.0)

have_xft=false
if test $have_xft = false ; then
XFT_LIBS="`pkg-config --libs xft`"
AC_CHECK_LIB(Xft, main,AC_DEFINE(HAVE_XFT_H, 1, [Have XFT lib ?]) XFT_LIB=[-lXft],, $X_CFLAGS $X_PRE_LIBS $XFT_LIBS $X_EXTRA_LIBS $X_LIBS)
AC_SUBST(XFT_LIB)

XFTFLAG=
enable_xft="no"
if test "x$XFT_LIB" = "x-lXft"; then
	XFTFLAG="-DXFT `pkg-config --cflags freetype2`"
	enable_xft="yes"
fi
else
enable_xft="yes"
XFTFLAG="-DXFT `pkg-config --cflags xft`"
XFT_LIBS="`pkg-config --libs xft`"
LIBS="$LIBS $XFT_LIBS"
fi

AC_MSG_CHECKING([whether to enable xft font support])
AC_ARG_ENABLE(xft,
	  [  --enable-xft            enable xft font support (default=auto)],
	  if test "x$enable_xft" = "xno"; then
		 XFTFLAG=
	  fi)

if test -n "$XFTFLAG"; then
AC_MSG_RESULT([yes])
CFLAGS="$XFTFLAG $CFLAGS"
USE_LIBS="$USE_LIBS xft"
else
AC_MSG_ERROR([***OroboROX needs xft font support to build!***])
fi



dnl Checks for programs.
AC_PROG_CC

AC_C_CONST
AC_C_INLINE

AC_PATH_XTRA
AC_HEADER_STDC
AC_CHECK_HEADERS(unistd.h sys/param.h sys/time.h time.h sys/mkdev.h sys/sysmacros.h string.h memory.h fcntl.h dirent.h sys/ndir.h ndir.h alloca.h locale.h )

AC_CHECK_FUNCS(gethostname)

dnl getopt_long may be in an addtional library
AC_CHECK_LIB(gnugetopt, getopt_long)
AC_CHECK_FUNCS(getopt_long)


AC_HEADER_MAJOR
AC_FUNC_ALLOCA
AC_STRUCT_TM
AC_STRUCT_ST_BLOCKS
AC_FUNC_CLOSEDIR_VOID
AC_CHECK_FUNCS(mkfifo)
AC_CHECK_FUNC(mknod)

AC_CHECK_LIB(X11, main,, AC_MSG_ERROR([libX11 is missing from your system: Make sure you have both the  X Window System libraries and development headers correctly installed]), $X_CFLAGS $X_PRE_LIBS $X_EXTRA_LIBS $X_LIBS)
AC_CHECK_LIB(Xext, main,, AC_MSG_ERROR([The X Window System installed on this machine does not support the shape extension]), $X_CFLAGS $X_PRE_LIBS $X_EXTRA_LIBS $X_LIBS)
AC_CHECK_HEADERS(X11/Xpoll.h)
AC_CHECK_LIB(Xpm, main,, AC_MSG_ERROR([libXpm is missing from your system: Make sure you have both the Xpm libraries and development headers correctly installed]), $X_CFLAGS $X_PRE_LIBS $X_EXTRA_LIBS $X_LIBS)
AC_CHECK_LIB(Xxf86vm, main,AC_DEFINE(HAVE_XF68VMLIB_H, 1, [Have XF86VM lib ?]) XF86VM_LIB=[-lXxf86vm],, $X_CFLAGS $X_PRE_LIBS $X_EXTRA_LIBS $X_LIBS)

STARTUP_NOTIFICATION_VERSION=0.4

AC_ARG_ENABLE(startup-notification,   [  --disable-startup-notification             disable startup notification support, for embedded/size-sensitive custom non-GNOME builds],,enable_startup_notification=auto)


if test "x$enable_startup_notification" = "xno"; then
	have_startup_notification=no
else
	if test x$enable_startup_notification = xyes; then
   		have_startup_notification=yes
   		echo "startup-notification support forced on"
	fi
	AC_MSG_CHECKING([Startup notification library >= $STARTUP_NOTIFICATION_VERSION])
	if $PKG_CONFIG --atleast-version $STARTUP_NOTIFICATION_VERSION libstartup-notification-1.0; then
  	 	have_startup_notification=yes
	else
   		have_startup_notification=no
	fi
fi
AC_MSG_RESULT($have_startup_notification)


if test x$have_startup_notification = xyes; then
  echo "Building with libstartup-notification"
  AC_DEFINE(HAVE_STARTUP_NOTIFICATION, , [Building with startup notification support])
  USE_LIBS="$USE_LIBS libstartup-notification-1.0"
else
  echo "Building without libstartup-notification"
fi

# Check for Xinerama extension (Solaris impl or Xfree impl)
oroborox_save_cppflags="$CPPFLAGS"
CPPFLAGS="$CPPFLAGS $X_CFLAGS"
ARCH=`uname -s`
case "$ARCH" in
  "SunOS"*)
      # Check for solaris
      use_solaris_xinerama=yes
      AC_CHECK_LIB(Xext, XineramaGetInfo,
		    use_solaris_xinerama=yes, use_solaris_xinerama=no,
		    $X_CFLAGS $X_PRE_LIBS $X_EXTRA_LIBS $X_LIBS)
      if test "x$use_solaris_xinerama" = "xyes"; then
	  AC_CHECK_HEADER(X11/extensions/xinerama.h,
	    [AC_DEFINE(HAVE_SOLARIS_XINERAMA, 1,
		[Have Solaris implementation of Xinerama])
	     AC_DEFINE(HAVE_XINERAMA, 1, [Have Xinerama])],
	    use_solaris_xinerama=no)
      fi	
      AC_MSG_CHECKING([for Xinerama support on Solaris])
      AC_MSG_RESULT($use_solaris_xinerama);
      ;;
  *)
      # Check for XFree
      use_xfree_xinerama=yes
      AC_CHECK_LIB(Xinerama, XineramaQueryExtension,
	  [AC_CHECK_HEADER(X11/extensions/Xinerama.h, 
	    [X_EXTRA_LIBS="$X_EXTRA_LIBS -lXinerama"
	      AC_DEFINE(HAVE_XFREE_XINERAMA, 1,
		[Have XFree86 implementation of Xinerama])
	      AC_DEFINE(HAVE_XINERAMA, 1, [Have Xinerama])],
	      use_xfree_xinerama=no,
	      [#include <X11/Xlib.h>])],
	  use_xfree_xinerama=no, $X_CFLAGS $X_PRE_LIBS $X_EXTRA_LIBS $X_LIBS)
      AC_MSG_CHECKING([for Xinerama support on XFree86])
      AC_MSG_RESULT($use_xfree_xinerama);	    
      ;;
esac
# Check for the RANDR extension
AC_CHECK_LIB(Xrandr, XRRUpdateConfiguration,
  [AC_CHECK_HEADER(X11/extensions/Xrandr.h,
      # RANDR requires RENDER
      [X_EXTRA_LIBS="$X_EXTRA_LIBS -lXrender -lXrandr"
      AC_DEFINE(HAVE_RANDR, 1, [Have the Xrandr extension library])],
      :, [#include <X11/Xlib.h>])], : ,
   $X_LIBS -lXrandr -lXrender $X_EXTRA_LIBS)
CPPFLAGS="$oroborox_save_cppflags"

LIBS="$LIBS $X_LIBS -lX11 $X_EXTRA_LIBS $XF86VM_LIB"
CFLAGS="$CFLAGS $X_CFLAGS"


AC_CHECK_FUNCS(malloc setlocale strchr strdup strrchr strstr)

AC_MSG_CHECKING([whether to turn on debugging])
AC_ARG_ENABLE(debug,
              [  --enable-debug          turn on debugging (default=no)],
              if test "x$enable_debug" = "xyes"; then
                DEBUGFLAG="-g -DDEBUG"
              fi)

if test -n "$DEBUGFLAG"; then
  AC_MSG_RESULT([yes])
  CFLAGS="$DEBUGFLAG $CFLAGS"
else
  AC_MSG_RESULT([no])
fi


dnl Extract version info from AppInfo.xml
AC_MSG_CHECKING(extracting version information)
[
VERSION=`sed -n 's/^.*<Version>\([.0-9]*\).*<\/Version>.*$/\1/p' $APP_DIR/AppInfo.xml`
]
AC_DEFINE_UNQUOTED(VERSION, "$VERSION")
AC_MSG_RESULT(version $VERSION)

ISODATE=`date +%Y-%m-%d`
AC_SUBST(ISODATE)

AC_SUBST(USE_LIBS)

AC_OUTPUT(Makefile)
