AC_DEFUN([AC_SEARCH_QT4],
[
    ac_with_Qt4=no

    ac_Qt4_lib_suffix="4 46 47"

    case "${host}" in
      *-mingw32*)
        ac_search_lib_path="/usr/i686-pc-mingw32/sys-root/mingw/lib"
        ac_search_include_path="/usr/i686-pc-mingw32/sys-root/mingw/include"
        ;;
      *)
        if test "${build_cpu}" = "x86_64"; then
           ac_search_lib_path="$INSROOT/lib64 $QT4DIR/lib64 /usr/lib/x86_64-linux-gnu  /usr/lib64 /usr/lib64/qt47/lib64 /usr/lib64/qt46/lib64 /usr/lib64/qt4/lib /usr/local/lib64/qt4  /opt/local/lib64/qt4 /usr/lib/qt4"
        else
           ac_search_lib_path="$INSROOT/lib $QT4DIR/lib /usr/lib/i386-linux-gnu /usr/lib /usr/lib/qt47/lib /usr/lib /usr/lib/qt46/lib /usr/lib/qt4/lib /usr/local/lib/qt4  /opt/local/lib/qt4 /usr/lib/qt4"
        fi
        ac_search_include_path="$INSROOT/include $QT4DIR/include  /usr/include/qt4 /usr/include /usr/lib64/qt47/include /usr/lib/qt47/include /usr/lib64/qt46/include /usr/lib/qt46/include /usr/lib64/qt4/include /usr/lib/qt4/include /usr/local/include/qt4  /opt/local/include/qt4"
        ;;
   esac
   ac_search_bin_path="$INSROOT/bin $QT4DIR/bin /usr/bin /usr/lib64/qt47/bin /usr/lib/qt47/bin /usr/lib64/qt46/bin /usr/lib/qt46/bin /usr/lib64/qt4/bin /usr/lib/qt4/bin /usr/local/bin /opt /opt/local/bin"


  ac_Qt4_modules="$1"
 
  AC_ARG_WITH(Qt4,
    AC_HELP_STRING([--with-Qt4=DIR], [root directory of Qt4 installation]),
    ac_with_Qt4=$withval
    if test "x${ac_with_Qt4}" != xyes && test "x${ac_with_Qt4}" != xno; then
      ac_Qt4_include="$withval/include"
      ac_Qt4_libdir="$withval/lib"
      ac_Qt4_bindir="$withval/bin"
    fi
  )

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

  AC_ARG_WITH([Qt4-plugin-dir],
    AC_HELP_STRING([--with-Qt4-plugin-dir=DIR], [The Qt designer plugin is in DIR]),
    ac_with_Qt4=$withval
    if test "x${ac_with_Qt4}" != xyes && test "x${ac_with_Qt4}" != xno; then
      ac_Qt4_plugdir="$withval"
    fi
  )

  if test "x${ac_Qt4_libdir}" == "x"; then
    for ac_qt4_path_test in $ac_search_lib_path ; do
#      AC_MSG_NOTICE(Check for lib path $ac_qt4_path_test/)
      if test -r "$ac_qt4_path_test/"; then
         ls $ac_qt4_path_test/libQtCore*  >/dev/null 2>&1
         if test $? -eq 0; then
            ac_Qt4_libdir="$ac_qt4_path_test"
            break;
         fi
      fi
    done
    ls $ac_Qt4_libdir/libQt*.la >/dev/null 2>&1
    if test $? -eq 0 && test x`echo $build_os |sed "s/darwin.*/darwin/"` = "xdarwin"; then
	AC_WARN(Please remove all *.la files from directory $ac_Qt4_libdir. Otherwise QT application will not work!)
    fi
  fi

   if test "x${ac_Qt4_include}" == "x"; then
    for ac_qt4_path_test in $ac_search_include_path ; do
#      AC_MSG_NOTICE(Check for include path $ac_qt4_path_test/qapplication.h)
      if test -r "$ac_qt4_path_test/Qt/qapplication.h"; then
         ac_Qt4_include="$ac_qt4_path_test"
         break;
      fi
    done
   fi

   if test "x${ac_Qt4_bindir}" == "x"; then
    for ac_qt4_path_test in $ac_search_bin_path ; do
#      AC_MSG_NOTICE(Check binpath $ac_qt4_path_test)
      if test -r "$ac_qt4_path_test/moc"; then
         ac_Qt4_bindir="$ac_qt4_path_test"
         break;
      elif test -r "$ac_qt4_path_test/moc-qt4"; then
         ac_Qt4_bindir="$ac_qt4_path_test"
         break;
      fi
    done
   fi

  AC_SEARCH_QT4_LOCAL($ac_Qt4_libdir, $ac_Qt4_include, $ac_Qt4_bindir, $ac_Qt4_modules, $ac_Qt4_lib_suffix)
])



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

#  AC_MSG_NOTICE(Check for lib path $1 $2 $3 $4 $5)
  for suffix in $5; do 
       if test -e $1/libQtCore${suffix}.so; then
           ac_suffix=${suffix}
       fi 
  done

  if test "$2"; then
    CXXFLAGS="$CXXFLAGS -I$2/QtCore -I$2/QtGui -I$2"
  fi
  if test "$3"; then
    PATH="$3:$PATH"
  fi

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


  LDFLAGS=$OLD_LDFLAGS
  CXXFLAGS=$OLD_CXXFLAGS
  LIBS=$OLD_LIBS
  AM_CONDITIONAL(HAVE_QT4, true)

  if test "x$success" != xyes; then
    AC_WARN(Qt4 not (properly) installed. Get a (free) recent version from: http://www.trolltech.com)
    AM_CONDITIONAL(HAVE_QT4, false)
  else
    if test "$1"; then
      AC_SUBST(QT4_LDFLAGS, "-L$1")
      AC_SUBST(QT4_LIBS, "-lQtGui${ac_suffix} -lQtCore${ac_suffix}")
      AC_SUBST(QT4_LIBS_ALL, "$QT4_LIBS")
      AC_SUBST(QT4_LDPATH, "$1")
    fi
    if test "$2"; then
       AC_SUBST(QT4_CXXFLAGS, "-DQT_CORE_LIB -DQT_GUI_LIB -DQT_SHARED -I$2 -I$2/Qt -I$2/QtCore -I$2/QtGui")
       AC_SUBST(QT4_CXXPATH, "$2")
    fi
    if test "$5"; then
       AC_SUBST(QT4_LIB_SUFFIX, "${ac_suffix}")
    fi
  fi



# moc test
  AC_CHECK_PROGS([ac_Qt4_moc],[moc-qt4 moc],no,[$3])
  if test "x$ac_Qt4_moc" != xno; then
    AC_SUBST(QT4_MOC, "$3/$ac_Qt4_moc")
  else
    AC_WARN(Qt4 moc not found!)
    AM_CONDITIONAL(HAVE_QT4, false)
  fi

# uic test
  AC_CHECK_PROGS([ac_Qt4_uic],[uic-qt4 uic],no,[$3])
  if test "x$ac_Qt4_uic" != xno; then
    AC_SUBST(QT4_UIC, "$3/$ac_Qt4_uic")
  else
    AC_WARN(Qt4 uic not found!)
    AM_CONDITIONAL(HAVE_QT4, false)
  fi

# rcc test
  AC_CHECK_PROGS([ac_Qt4_rcc],[rcc-qt4 rcc],no,[$3])
  if test "x$ac_Qt4_rcc" != xno; then
    AC_SUBST(QT4_RCC, "$3/$ac_Qt4_rcc")
  else
    AC_WARN(Qt4 rcc not found!)
    AM_CONDITIONAL(HAVE_QT4, false)
  fi


  
PATH=$OLD_PATH
])


# qt4 designer search
AC_DEFUN([AC_SEARCH_QT4_DESIGNER],
[
  AC_ARG_ENABLE(
        QT4-Designer-Plugin,
        AC_HELP_STRING([--enable-QT4-Designer-Plugin], [Build the QT4 designer plugin (default off)]),
        ac_request_plugin=$enableval,
        ac_request_plugin=no)

    case "${host}" in
      *-mingw32*)
        ac_search_path_plugin="/usr/i686-pc-mingw32/sys-root/mingw/lib/qt4/plugin"
        ;;
      *)
        ac_search_path_plugin="$QT4DIR/plugin  /usr/lib64/qt4/plugin /usr/lib/qt4/plugin /opt/local/lib/qt4-plugins"
        ;;
    esac

   if test "x${ac_request_plugin}" = "xyes"; then
        if test "x${ac_Qt4_plugdir}" == "x"; then
                for ac_qt4_path_test in $ac_search_path_plugin ; do
dnl      AC_MSG_NOTICE(Check for plugin path $ac_qt4_path_test)
                        if test -d "$ac_qt4_path_test"; then
                                ac_Qt4_plugdir="$ac_qt4_path_test/designer"
                                break;
                        fi
                done
        fi
	   AC_SEARCH_QT4_EXTRA(QtDesigner)
   else
	   AM_CONDITIONAL(HAVE_QT4_QtDesigner, false)
       AC_DEFINE(HAVE_QT4,,[define if the Qt4 library is available])
   fi
])


AC_DEFUN([AC_SEARCH_QT4_EXTRA],
[
  OLD_LDFLAGS=$LDFLAGS
  OLD_CXXFLAGS=$CXXFLAGS
  OLD_LIBS=$LIBS
  OLD_PATH=$PATH

dnl  AC_MSG_NOTICE(Check for module $ac_Qt4_want_module)

  if test "$1"; then
    LDFLAGS="$LDFLAGS $QT4_LDFLAGS"
    if test "x$1" == "xQtUiTools" ; then
      LIBS=" $QT4_LIBS -l$1"
    else
      LIBS=" $QT4_LIBS -l$1$QT4_LIB_SUFFIX"
    fi
    CXXFLAGS="$CXXFLAGS $QT4_CXXFLAGS"
  fi

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

  LDFLAGS=$OLD_LDFLAGS
  CXXFLAGS=$OLD_CXXFLAGS
  LIBS=$OLD_LIBS

  if test "x$success" != xyes; then
      AC_WARN(Qt4 $1 not (properly) installed.)
      AM_CONDITIONAL(HAVE_QT4_$1, false)
  else
    if test "x$1" == "xQtUiTools" ; then
      AC_SUBST(QT4_LIBS_$1, "-l$1")
      AC_SUBST(QT4_LIBS_ALL, " -l$1 $QT4_LIBS_ALL")
    else
      AC_SUBST(QT4_LIBS_$1, "-l$1$QT4_LIB_SUFFIX")
      AC_SUBST(QT4_LIBS_ALL, " -l$1$QT4_LIB_SUFFIX $QT4_LIBS_ALL")
    fi
     case "${host}" in
      *-mingw32*)
        ac_search_path_plugin="/usr/i686-pc-mingw32/sys-root/mingw/lib/qt4/plugin"
        ;;
      *)
        ac_search_path_plugin="$QT4DIR/plugin  /usr/lib64/qt4/plugin /usr/lib/qt4/plugin /opt/local/lib/qt4-plugins"
        ;;
      esac
      AC_SUBST(QT4_CXXFLAGS_$1, "-I$QT4_CXXPATH/$1")
      AC_SUBST(QT4_CXXFLAGS_ALL, "$QT4_CXXFLAGS_$1 $QT4_CXXFLAGS_ALL")
      AM_CONDITIONAL(HAVE_QT4_$1, true)
      AC_DEFINE(HAVE_QT4_$1,,[define if the $1 library is available])
  fi
])
