dnl Process this file with autoconf to produce a configure script.

AC_INIT(osb-nrcit, 0.5.1)
AC_CONFIG_SRCDIR(src/BridgeImpl.h)
AM_INIT_AUTOMAKE


dnl standard tools

AM_CONFIG_HEADER(config.h)
AC_PROG_LIBTOOL
AM_PROG_CC_STDC
AC_PROG_CXX


dnl compiler flags


dnl debugging info commandline argument
use_debug=
AC_ARG_ENABLE([debug], 
  AC_HELP_STRING([--enable-debug], [enable debug mode (default:disabled)]),
  use_debug=true,,)

if test -n "$use_debug"; then 
   CPPFLAGS="$CPPFLAGS -DDEBUG" 
else
   CPPFLAGS="$CPPFLAGS -DNDEBUG"
fi  

dnl Use -Wall if we have gcc.
changequote(,)dnl
if test "x$GCC" = "xyes"; then
  case " $CFLAGS " in
  *[\ \	]-Wall[\ \	]*) ;;
  *) CFLAGS="$CFLAGS -Wall -Werror" ;;
  esac
  case " $CXXFLAGS " in
  *[\ \	]-Wall[\ \	]*) ;;
  *) CXXFLAGS="$CXXFLAGS -Wall -Werror" ;;
  esac
fi
changequote([,])dnl


dnl standard c headers and functions


AC_HEADER_STDC
AC_HEADER_STAT
AC_HEADER_TIME
AC_CHECK_HEADERS(alloca.h vfork.h)
AC_CHECK_HEADERS(string.h strings.h float.h fnmatch.h limits.h paths.h sys/cdefs.h sys/mman.h sys/param.h sys/select.h sys/socket.h)
AC_CHECK_FUNCS(memcpy memmove gettimeofday mkstemps mkstemp random setegid seteuid socket unsetenv vsnfprintf)


dnl packages


dnl check for pkg-config
AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
if test $PKG_CONFIG = no; then
   AC_MSG_ERROR([Cannot find pkg-config, make sure it is installed and in your PATH])
fi

dnl check for common gtk modules
PKG_CHECK_MODULES(PACKAGE, [gtk+-2.0 >= 2.2.0, gdk-pixbuf-2.0 >= 2.2.0, glib-2.0 >= 2.2.0, libxml-2.0 >= 2.6.0 ])

dnl check for OSB NRCore
PKG_CHECK_MODULES(NRCORE, [osb-jscore, osb-nrcore])

dnl check for X libraries
AC_PATH_XTRA
X_LIBS="-lSM -lXt $X_LIBS"

dnl check for fontconfig
PKG_CHECK_MODULES(FONTCONFIG, [fontconfig >= 1.0.0])

dnl check for Xft
PKG_CHECK_MODULES(XFT, [xft >= 2.0.0])


dnl optional packages


dnl check for librsvg 
with_svg_default=yes
AC_ARG_WITH([svg],
  AC_HELP_STRING([--with-svg=yes/no], [enable svg image support (default: $with_svg_default)]), 
  [with_svg=$withval], [with_svg=$with_svg_default])

if test x$with_svg == xyes; then
  PKG_CHECK_MODULES(RSVG, [librsvg-2.0 >= 2.2.0])
  PACKAGE_CFLAGS="$PACKAGE_CFLAGS $RSVG_CFLAGS"
  PACKAGE_LIBS="$PACKAGE_LIBS $RSVG_LIBS"
  AC_DEFINE([HAVE_LIBRSVG], [1], [Define to 1 if you want to use librsvg svg library.])
fi

AM_CONDITIONAL(USE_LIBRSVG, test x$with_svg == xyes)

dnl check for libcurl (not really optional)
pkg_version_libcurl="7.11.0"
pkg_version_libcurl_hex="070B00"
with_libcurl_default=yes

AC_ARG_WITH([libcurl],
  AC_HELP_STRING([--with-libcurl=yes], [use libcurl (default: $with_libcurl_default)]),
  [],
  [with_libcurl=$with_libcurl_default])

AC_ARG_WITH([libcurl-prefix],
  AC_HELP_STRING([--with-libcurl-prefix=prefix], [use libcurl from a prefix (implies --with-libcurl)]),
  [with_libcurl=yes;],
  [])

if test x$with_libcurl == xyes; then
  if test -z "${with_libcurl_prefix}"; then
     curl_config_path=$PATH
  else
     curl_config_path="${with_libcurl_prefix}/bin"
  fi

  AC_PATH_PROG(curl_config, curl-config,, [${curl_config_path}])
  if test -n "${curl_config}"; then
    AC_MSG_CHECKING([for curl >= ${pkg_version_libcurl}])
    ver=`${curl_config} --version | sed -e "s/libcurl //g"`
    hex_ver=`${curl_config} --vernum | tr 'a-f' 'A-F'`
    ok=`echo "ibase=16; if($hex_ver>=$pkg_version_libcurl_hex) $hex_ver else 0" | bc`
    if test x$ok != x0; then 
      AC_MSG_RESULT([$ver])
      LIBCURL_CFLAGS="`${curl_config} --cflags`"
      LIBCURL_LIBS="`${curl_config} --libs`"
      AC_SUBST(LIBCURL_LIBS)		# needed by the osb-nrcore.pc.in
      AC_DEFINE([HAVE_LIBCURL], [1], [Define to 1 if you want to use libcurl http library.])
    else 
      AC_MSG_RESULT(FAILED)
      AC_MSG_ERROR([$ver is too old. Need version ${pkg_version_libcurl} or higher.])
    fi
  else
    AC_MSG_ERROR([curl-config not in path])
  fi  
else
   AC_MSG_ERROR([you must select network transport library (use --with-curl)])
fi

AM_CONDITIONAL(USE_LIBCURL, test x$with_libcurl = xyes)


DEFINES="-DG_DISABLE_DEPRECATED -DGDK_PIXBUF_DISABLE_DEPRECATED -DGTK_DISABLE_DEPRECATED"
DEFINES="$DEFINES -DAPPLE_CHANGES -DKWIQ -DKHTML_NO_EXCEPTIONS -DKWIQ_NO_JAVA"

CXXFLAGS="$CXXFLAGS -fno-exceptions -fno-rtti -nostdinc++"

CPPFLAGS="$CPPFLAGS $PACKAGE_CFLAGS $X_CFLAGS $XFT_CFLAGS $FONTCONFIG_CFLAGS $NRCORE_CFLAGS $LIBCURL_CFLAGS $DEFINES"
LDFLAGS="$LDFLAGS $PACKAGE_LIBS $X_LIBS $XFT_LIBS $FONTCONFIG_LIBS $LIBCURL_LIBS $NRCORE_LIBS \
-nodefaultlibs -lgcc -lc"


AC_OUTPUT([
Makefile
osb-nrcit.pc
src/Makefile
src/gtk/Makefile
src/gnet/Makefile
])

