AC_DEFUN([AC_SEARCH_QT],
[
     ac_with_Qt=no
     ac_Qt_search="$INSROOT $QTDIR /opt /opt/local /usr/lib64/qt-3.3/ /usr/lib64/qt3/ /usr/lib/qt-3.3/ /usr/lib/qt3/ /usr/share/qt3 /usr /usr/local"
     if test "${build_cpu}" = "x86_64"; then
       ac_Qt_search_lib="$INSROOT/lib $QTDIR/lib /usr/lib64/qt-3.3/lib /usr/lib64/qt3/lib64 /usr/lib64 /usr/local/lib64 /opt/lib64 /opt/local/lib64"
       ac_Qt_search_include="$INSROOT/include $QT3DIR/include /usr/include/qt3 /usr/lib64/qt-3.3/include /usr/lib64/qt3/include"
     else
       ac_Qt_search_lib="$INSROOT/lib $QTDIR/lib /usr/lib /usr/lib/qt-3.3/lib /usr/lib/qt3/lib /usr/share/qt3 /usr/local/lib /opt/lib /opt/local/li /usr/share/lib/qt3"
       ac_Qt_search_include="$INSROOT/include $QT3DIR/include /usr/include/qt3 /usr/lib/qt-3.3/include /usr/lib/qt3/include"
     fi
     ac_Qt_search_plugin="$INSROOT $QTDIR/plugin /usr/lib/qt3/plugin /usr/lib64/qt-3.3/plugin /usr/lib/qt-3.3/plugin"
     ac_Qt_lib=qt-mt

dnl        if test -z "$QTDIR"; then
dnl           AC_SUBST(QTDIR, "/usr/lib/qt3")
dnl        fi
dnl     ac_Qt_include=$QTDIR/include
dnl        if test "${build_cpu}" = "x86_64"; then
dnl          ac_Qt_libdir=$QTDIR/lib64;
dnl        else
dnl          ac_Qt_libdir=$QTDIR/lib;
dnl        fi
dnl     ac_Qt_bindir=$QTDIR/bin
dnl     ac_Qt_lib=qt-mt


     AC_ARG_WITH(Qt,
          AC_HELP_STRING([--with-Qt=DIR], [root directory of Qt installation]),
          ac_with_Qt=$withval
          if test "x${ac_with_Qt}" != xyes && test "x${ac_with_Qt}" != xno; then
               ac_Qt_include="$withval/include"
               ac_Qt_libdir="$withval/lib"
               ac_Qt_bindir="$withval/bin"
          fi
     )

     AC_ARG_WITH([Qt-include-dir],
          AC_HELP_STRING([--with-Qt-include-dir=DIR], [Qt header files are in DIR]),
          ac_with_Qt=$withval
          if test "x${ac_with_Qt}" != xyes && test "x${ac_with_Qt}" != xno; then
               ac_Qt_include="$withval"
          fi
     )
     AC_ARG_WITH([Qt-bin-dir],
          AC_HELP_STRING([--with-Qt-bin-dir=DIR], [Qt utilities such as moc and uic are in DIR]),
          ac_with_Qt=$withval
          if test "x${ac_with_Qt}" != xyes && test "x${ac_with_Qt}" != xno; then
               ac_Qt_bindir="$withval"
          fi
     )
     AC_ARG_WITH([Qt-lib-dir],
          AC_HELP_STRING([--with-Qt-lib-dir=DIR], [The Qt library is in DIR]),
          ac_with_Qt=$withval
          if test "x${ac_with_Qt}" != xyes && test "x${ac_with_Qt}" != xno; then
               ac_Qt_libdir="$withval"
          fi
     )

  if test "x${ac_Qt_libdir}" == "x"; then
    for ac_qt_path_test in $ac_Qt_search_lib ; do
dnl     AC_MSG_NOTICE(Check for include $ac_qt_path_test/)
      if test -r "$ac_qt_path_test"; then
         ls $ac_qt_path_test/libqt-mt*  >/dev/null 2>&1
         if test $? -eq 0; then
            if test "${build_cpu}" = "x86_64"; then
               ac_Qt_libdir="$ac_qt_path_test/"
            else
               ac_Qt_libdir="$ac_qt_path_test/"
            fi
            break;
         fi
      fi
    done
  fi

   if test "x${ac_Qt_include}" == "x"; then
    for ac_qt_path_test in $ac_Qt_search_include; do
     AC_MSG_NOTICE(Check for include $ac_qt_path_test/qapplication.h)
      if test -r "$ac_qt_path_test/qapplication.h"; then
         ac_Qt_include="$ac_qt_path_test"
         break;
      fi
    done
   fi

   if test "x${ac_Qt_bindir}" == "x"; then
    for ac_qt_path_test in $ac_Qt_search ; do
dnl      AC_MSG_NOTICE(Check for bin path $ac_qt_path_test/bin)
      if test -r "$ac_qt_path_test/bin/moc"; then
         ac_Qt_bindir="$ac_qt_path_test/bin"
         break;
      fi
    done
   fi

   if test "x${ac_Qt_plugdir}" == "x"; then
    for ac_qt_path_test in $ac_Qt_search_plugin ; do
dnl      AC_MSG_NOTICE(Check for plugin path $ac_qt_path_test)
      if test -d "$ac_qt_path_test"; then
         ac_Qt_plugdir="$ac_qt_path_test"
         break;
      fi
    done
   fi

    AC_SEARCH_QT_LOCAL($ac_Qt_libdir, $ac_Qt_include, $ac_Qt_bindir, $ac_Qt_plugdir, $ac_Qt_lib)
])


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

     if test "$1"; then
          LDFLAGS="$LDFLAGS -L$1 -l$5"
     fi
     if test "$2"; then
          CXXFLAGS="$CXXFLAGS -I$2"
     fi
     if test "$3"; then
          PATH="$3:$PATH"
     fi

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


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

     if test "x$success" != xyes; then
          AC_MSG_ERROR(Qt not (properly) installed. Get a (free) recent version from: http://www.trolltech.com)
     else
          if test "$1"; then
               AC_SUBST(QT_LDFLAGS, "-L$1")
          fi
          if test "$2"; then
               AC_SUBST(QT_CXXFLAGS, "-DQT_THREAD_SUPPORT -I$2")
          fi
          if test "$5"; then
               AC_SUBST(QT_LIBS, "-l$5")
          fi
     fi


# moc test
     AC_PATH_PROG(ac_Qt_moc, moc, yes)
     if test "x$ac_Qt_moc" != xno; then
          AC_SUBST(QT_MOC, "$ac_Qt_moc")
     else
          AC_WARN(Qt3 moc not found!)
          AM_CONDITIONAL(HAVE_QT3, false)
     fi

# uic test
     AC_PATH_PROG(ac_Qt_uic, uic, no)
     if test "x$ac_Qt_uic" != xno; then
          AC_SUBST(QT_UIC, "$ac_Qt_uic")
     else
          AC_WARN(Qt3 uic not found!)
          AM_CONDITIONAL(HAVE_QT3, false)
     fi

        PATH=$OLD_PATH
])
