AC_DEFUN([AC_SEARCH_NICE],
[
    ACX_PTHREAD
    AC_SEARCH_ICE

    #AC_SEARCH_QT
    #AC_SEARCH_QWT

    AC_SEARCH_QT4
    AC_SEARCH_QT4_DESIGNER

    AC_SEARCH_QT4_EXTRA(Qt3Support)
    #AC_SEARCH_QT4_EXTRA(QtDBus)
    #AC_SEARCH_QT4_EXTRA(QtAssistant)
    AC_SEARCH_QT4_EXTRA(QtNetwork)
    #AC_SEARCH_QT4_EXTRA(QtOpenGL)
    #AC_SEARCH_QT4_EXTRA(QtSql)
    #AC_SEARCH_QT4_EXTRA(QtSvg)
    #AC_SEARCH_QT4_EXTRA(QtTest)
    AC_SEARCH_QT4_EXTRA(QtUiTools)
    AC_SEARCH_QT4_EXTRA(QtXml)
    AC_SEARCH_QT4_EXTRA(QtWebKit)

    AC_SEARCH_QWT4

    AX_BOOST_BASE([1.33])

    AX_PYTHON
    AX_BOOST_PYTHON

    AX_BOOST_FILESYSTEM
    AX_BOOST_DATE_TIME
    AX_BOOST_SYSTEM
    #AX_BOOST_IOSTREAMS
    #AX_BOOST_PROGRAM_OPTIONS
    #AX_BOOST_SERIALIZATION
    AX_BOOST_REGEX
    #AX_BOOST_SIGNALS
    AX_BOOST_THREAD
    AX_BOOST_UNIT_TEST_FRAMEWORK
    AX_BOOST_XPRESSIVE      
    AX_BOOST_CIRCULARBUFFER


    ac_with_Nice=no
    ac_search_libs="Nice"
    case "${host}" in
    *-mingw32*)
     AM_CONDITIONAL(HAVE_UNIX_SYSTEM, false)
     ac_search_path="/usr/i686-pc-mingw32/sys-root/i686-pc-mingw32"
     if test "${build_cpu}" = "x86_64"; then
       ac_search_lib_path="$ac_search_path/lib64"
     else
       ac_search_lib_path="$ac_search_path/lib"
     fi
     ac_search_include_path="$ac_search_path/include "
     ac_search_slice_path="$ac_search_path/slice "
     ;;
   *)
     AM_CONDITIONAL(HAVE_UNIX_SYSTEM, true)
     ac_search_path="$INSROOT /usr /usr/local /opt /opt/local"
     if test "${build_cpu}" = "x86_64"; then
       ac_search_lib_path="$INSROOT/lib64 $INSROOT/lib /usr/lib64 /opt/local/lib64"
     else
       ac_search_lib_path="$INSROOT/lib /usr/lib"
     fi
     ac_search_include_path="$INSROOT/include /usr/include "
     ac_search_slice_path="$INSROOT/slice /usr/slice "
     ;;
   esac


     AC_ARG_WITH(Nice,
          AC_HELP_STRING([--with-Nice=DIR], [root directory of Nice installation]),
          ac_with_Nice=$withval
          if test "x${ac_with_Nice}" != xyes && test "x${ac_with_Nice}" != xno; then
               ac_Nice_include="$withval/include"
               if test "${build_cpu}" = "x86_64"; then
                   ac_Nice_libdir="$withval/lib64"
               else
                   ac_Nice_libdir="$withval/lib"
               fi
          fi
     )

     AC_ARG_WITH([Nice-include-dir],
          AC_HELP_STRING([--with-Nice-include-dir=DIR], [Nice header files are in DIR]),
          ac_with_Nice=$withval
          if test "x${ac_with_Nice}" != xyes && test "x${ac_with_Nice}" != xno; then
               ac_Nice_include="$withval"
          fi
     )

     AC_ARG_WITH([Nice-slice-dir],
          AC_HELP_STRING([--with-Nice-slice-dir=DIR], [Nice slice files are in DIR]),
          ac_with_Nice=$withval
          if test "x${ac_with_Nice}" != xyes && test "x${ac_with_Nice}" != xno; then
               ac_Nice_slice="$withval"
          fi
     )

     AC_ARG_WITH([Nice-lib-dir],
          AC_HELP_STRING([--with-Nice-lib-dir=DIR], [The Nice library is in DIR]),
          ac_with_Nice=$withval
          if test "x${ac_with_Nice}" != xyes && test "x${ac_with_Nice}" != xno; then
               ac_Nice_libdir="$withval"
          fi
     )

     AC_ARG_WITH([Nice-bin-dir],
          AC_HELP_STRING([--with-Nice-bin-dir=DIR], [The slice2cc is in DIR]),
          ac_with_Nice=$withval
          if test "x${ac_with_Nice}" != xyes && test "x${ac_with_Nice}" != xno; then
               ac_Nice_bindir="$withval"
          fi
     )


  if test "x${ac_Nice_libdir}" == "x"; then
    for ac_nice_path_test in $ac_search_lib_path ; do
      if test -r "$ac_nice_path_test/"; then
         for ac_nice_lib_test in $ac_search_libs ; do
            ls $ac_nice_path_test/lib$ac_nice_lib_test.*  >/dev/null 2>&1
            if test $? -eq 0; then
              ac_Nice_libdir="$ac_nice_path_test"
              ac_Nice_libs="$ac_nice_lib_test"
              break;
            fi
         done
         if test "$ac_Nice_libs"; then
            break;
         fi
      fi
    done




   if test "x${ac_Nice_slice}" == "x"; then
    for ac_nice_path_test in $ac_search_slice_path ; do
      if test -r "$ac_nice_path_test/Nice/Util/CompletionVar.ice"; then
         ac_Nice_slice="$ac_nice_path_test"
         break;
      fi
    done
   fi

   if test "x${ac_Nice_include}" == "x"; then
    for ac_nice_path_test in $ac_search_include_path ; do
      if test -r "$ac_nice_path_test/Nice/Nice.h"; then
         ac_Nice_include="$ac_nice_path_test"
         break;
      fi
    done
   fi

   if test "x${ac_Nice_bin}" == "x"; then
    for ac_nice_path_test in $ac_search_path ; do
      if test -r "$ac_nice_path_test/bin/nice2cc.py"; then
         ac_Nice_bin="$ac_nice_path_test/bin"
         break;
      fi
    done
   fi

    AC_SEARCH_NICE_LOCAL($ac_Nice_libdir, $ac_Nice_include, $ac_Nice_libs, $ac_Nice_slice, $ac_Nice_bin)
  fi
])


# subfunction for easier reuse
AC_DEFUN([AC_SEARCH_NICE_LOCAL],
[
     OLD_LDFLAGS=$LDFLAGS
     OLD_CXXFLAGS=$CXXFLAGS

     if test "x$QT4_LIBS" == "x"; then
         AC_SEARCH_QT4
     fi
     
 dnl AC_MSG_NOTICE($1 $2 $3 $4)
     
     if test "$1"; then
          LDFLAGS="$LDFLAGS -L$1 $BOOST_LDFLAGS $ICE_LDFLAGS"
     fi
     if test "$2"; then
          CXXFLAGS="$CXXFLAGS -I$2 $BOOST_CPPFLAGS $ICE_CXXFLAGS"
     fi
     if test "$3"; then
          LDADD="$LDFLAGS -l$3"
     fi

     AC_LANG_PUSH(C++)
     AC_MSG_CHECKING(for Nice in $1)
     AC_TRY_LINK([
          #include <Nice/Nice.h>
     ],[
          ;
     ],[
     success=yes
     ],[
     success=no
     ])
     AC_MSG_RESULT($success)
     AC_LANG_POP()


     LDFLAGS=$OLD_LDFLAGS
     CXXFLAGS=$OLD_CXXFLAGS
     AM_CONDITIONAL(HAVE_NICE, true)

     if test "x$success" != xyes; then
          AC_MSG_ERROR(Nice not (properly) installed. Get a (free) recent version from: https://svn.mpia.de/trac/gulli/TwiceAsNice/)
     else
          if test "$1"; then
               AC_SUBST(NICE_LDFLAGS, "-L$1 $BOOST_LDFLAGS $ICE_LDFLAGS")
          fi
          if test "$2"; then
               AC_SUBST(NICE_CXXFLAGS, "-I$2 $BOOST_CPPFLAGS $ICE_CXXFLAGS $PTHREAD_CXXFLAGS")
               AC_SUBST(NICE_CC_DATADIR, "$2")
               AC_SUBST(NICE_INC, "-I$2")
          fi
          if test "$3"; then
               AC_SUBST(NICE_SLICEFILES, "-I$4")
          fi
          if test "$3"; then
               AC_SUBST(NICE_LIBS, "-l$3 ${ICE_LIBS} ${BOOST_SYSTEM_LIB} ${PTHREAD_LIBS}")
               AC_SUBST(NICEQT_LIBS, "-lNiceQt ${QT4_LIBS}")
               AC_SUBST(NICEPY_LIBS, "-lNicePythonProperty")
          fi
          if test "$5"; then
               AC_SUBST(NICE_BIN, "$5")
               AC_SUBST(NICE_CC, "$5/nice2cc.py")
          fi
     fi

])
