AC_DEFUN([AC_SEARCH_QWT],
[
     ac_with_Qwt3=no


    ac_search_libs="qwt-qt3 qwt"
    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/qwt"
        ;;
      *)
        if test "${build_cpu}" = "x86_64"; then
           ac_search_lib_path="$INSROOT/lib64 $QT3DIR/lib64 /usr/lib/qt-3.3/lib64 /usr/lib/qt3/lib64 /usr/lib64"
        else
           ac_search_lib_path="$INSROOT/lib $QT3DIR/lib /usr/lib/qt-3.3/lib /usr/lib/qt3/lib /usr/lib "
        fi
        ac_search_include_path="$INSROOT/include $QT3DIR/include /usr/include/qwt-qt3 /usr/lib64/qt-3.3/include /usr/lib/qt-3.3/include /usr/lib64/qt3/include /usr/lib/qt3/include"
        ;;
   esac

     AC_ARG_WITH(Qwt3,
          AC_HELP_STRING([--with-Qwt3=DIR], [root directory of Qwt3 installation]),
          ac_with_Qwt3=$withval
          if test "x${ac_with_Qwt3}" != xyes && test "x${ac_with_Qwt3}" != xno; then
               ac_Qwt3_include="-I$withval/include"
               if test "${build_cpu}" = "x86_64"; then
                  ac_Qwt3_libdir="-L$withval/lib64"
               else
                  ac_Qwt3_libdir="-L$withval/lib"
               fi
               ac_Qwt3_libs="-lqwt"
          fi
     )

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

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

     AC_ARG_WITH([Qwt3-lib-name],
          AC_HELP_STRING([--with-Qwt3-lib-name=DIR], [The Qwt3 library name, e.g. qwt, Qwt]),
          ac_with_Qwt3=$withval
          if test "x${ac_with_Qwt3}" != xyes && test "x${ac_with_Qwt3}" != xno; then
               ac_Qwt3_libs="-l$withval"
          fi
     )

  if test "x${ac_Qwt3_libdir}" == "x"; then
    for ac_qwt3_path_test in $ac_search_lib_path ; do
dnl      AC_MSG_NOTICE(Check for lib path $ac_qwt3_path_test/)
      if test -r "$ac_qwt3_path_test/"; then
         for ac_qt_lib_test in $ac_search_libs ; do
dnl            AC_MSG_NOTICE( Check for lib $ac_qwt3_path_test/lib$ac_qt_lib_test.*)
            ls $ac_qwt3_path_test/*$ac_qt_lib_test.*  >/dev/null 2>&1
            if test $? -eq 0; then
dnl              AC_MSG_NOTICE(Found it!)
	      ac_Qwt3_libdir="-L$ac_qwt3_path_test/"
	      ac_Qwt3_libs="-l$ac_qt_lib_test"
              break;
	    fi
         done
         if test "$ac_Qwt3_libs"; then
            break;
         fi
      fi
    done
    ls $ac_Qwt3_libdir/libQwt*.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_Qwt3_libdir. Otherwise Qwt application will not work!)
    fi
  fi



   if test "x${ac_Qwt3_include}" == "x"; then
    for ac_qt_path_test in $ac_search_include_path ; do
dnl      AC_MSG_NOTICE(Check for include path $ac_qt_path_test/qwt.h)
      if test -r "$ac_qt_path_test/qwt.h"; then
         ac_Qwt3_include="-I$ac_qt_path_test"
         break;
      fi
    done
   fi

    AC_SEARCH_QWT_LOCAL($ac_Qwt3_libdir, $ac_Qwt3_include, $ac_Qwt3_libs)
])


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

     if test "x$QT_LIBS" == "x"; then
         AC_SEARCH_QT
     fi

#     AC_MSG_NOTICE($QT_CXXFLAGS $QfT_LDFLAGS $QT_LIBS)
#     AC_MSG_NOTICE($1 : $2 : $3)

     if test "$1"; then
          LDFLAGS="$LDFLAGS $QT_LDFLAGS $1"
     fi
     if test "$2"; then
          CXXFLAGS="$CXXFLAGS $2 $QT_CXXFLAGS"
     fi
     if test "$3"; then
          LIBS="$QT_LIBS $3"
     fi

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


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

     if test "x$success" != xyes; then
          AC_MSG_ERROR(Qwt3 not (properly) installed. Get a (free) recent version from: http://qwt.sourceforge.net/)
     else
          if test "$1"; then
               AC_SUBST(QWT_LDFLAGS, "$1")
          fi
          if test "$2"; then
               AC_SUBST(QWT_CXXFLAGS, "$2")
          fi
	  if test "$3"; then
	       AC_SUBST(QWT_LIBS, "$3")
	  fi
     fi

])
