AC_DEFUN([AC_SEARCH_BASDA],
[
    AC_SEARCH_NICE

    ac_with_Basda=no
    ac_search_libs="Basda"
    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(Basda,
          AC_HELP_STRING([--with-Basda=DIR], [root directory of Basda installation]),
          ac_with_Basda=$withval
          if test "x${ac_with_Basda}" != xyes && test "x${ac_with_Basda}" != xno; then
               ac_Basda_include="$withval/include"
               if test "${build_cpu}" = "x86_64"; then
                   ac_Basda_libdir="$withval/lib64"
               else
                   ac_Basda_libdir="$withval/lib"
               fi
          fi
     )

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

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

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

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


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


   if test "x${ac_Basda_slice}" == "x"; then
    for ac_basda_path_test in $ac_search_slice_path ; do
      if test -r "$ac_basda_path_test/Basda/Service.ice"; then
         ac_Basda_slice="$ac_basda_path_test"
         break;
      fi
    done
   fi

   if test "x${ac_Basda_include}" == "x"; then
    for ac_basda_path_test in $ac_search_include_path ; do
      if test -r "$ac_basda_path_test/Basda/Basda.h"; then
         ac_Basda_include="$ac_basda_path_test"
         break;
      fi
    done
   fi

   if test "x${ac_Basda_bin}" == "x"; then
    for ac_basda_path_test in $ac_search_path ; do
      if test -r "$ac_basda_path_test/bin/basda2xx.py"; then
         ac_Basda_bin="$ac_basda_path_test/bin"
         break;
      fi
    done
   fi

    AC_SEARCH_BASDA_LOCAL($ac_Basda_libdir, $ac_Basda_include, $ac_Basda_libs, $ac_Basda_slice, $ac_Basda_bin)
  fi
])


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

     if test "x$QT4_LIBS" == "x"; then
         AC_SEARCH_QT4
     fi
     
#     AC_MSG_NOTICE(dadada $1 : $2 : $3 : $4 : $5)
     
     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 Basda in $1)
     AC_TRY_LINK([
          #include <Basda/Basda.h>
     ],[
          ;
     ],[
     success=yes
     ],[
     success=no
     ])
     AC_MSG_RESULT($success)
     AC_LANG_POP()


     LDFLAGS=$OLD_LDFLAGS
     CXXFLAGS=$OLD_CXXFLAGS

     if test "x$success" != xyes; then
          AC_MSG_ERROR(Basda not (properly) installed. Get a (free) recent version from: https://svn.mpia.de/trac/gulli/TwiceAsBasda/)
          AM_CONDITIONAL(HAVE_BASDA, false)
     else
          if test "$1"; then
               AC_SUBST(BASDA_LDFLAGS, "-L$1 $BOOST_LDFLAGS $ICE_LDFLAGS")
          fi
          if test "$2"; then
               AC_SUBST(BASDA_CXXFLAGS, "-I$2  -fstrict-aliasing $BOOST_CPPFLAGS $ICE_CXXFLAGS")
               AC_SUBST(BASDA_CC_DATADIR, "$2")
               AC_SUBST(BASDA_INC, "-I$2")
          fi
          if test "$3"; then
               AC_SUBST(BASDA_LIBS, "-l$3 -lBasdaServiceState_NiceType ${NICE_LIBS}")
               AC_SUBST(BASDAQT_LIBS, "-lBasdaQt ${NICEQT_LIBS}")
          fi
          if test "$4"; then
               AC_SUBST(BASDA_SLICEFILES, "-I$4")
          fi
          if test "$5"; then
               AC_SUBST(BASDA_BIN, "$5")
               AC_SUBST(BASDA_XX, "$5/basda2xx.py")
          fi
          AM_CONDITIONAL(HAVE_BASDA, true)
     fi

])

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


  if test "$1"; then
    LDFLAGS="$LDFLAGS $BASDA_LDFLAGS"
    case "${host}" in
      *-mingw32*)
         LIBS=" $BASDA_LIBS -lBasda$1 $3"
        ;;
      *)
        LIBS=" $BASDA_LIBS -lBasda$1 $3"
        ;;
      esac
    CXXFLAGS="$CXXFLAGS $BASDA_CXXFLAGS"
  fi

  AC_LANG_PUSH(C++)
  AC_MSG_CHECKING(for Basda $1 )
  AC_TRY_LINK([
    #include <Basda/Basda.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(Basda$1 not (properly) installed.)
      AM_CONDITIONAL(HAVE_BASDA_$2, false)
  else
     case "${host}" in
      *-mingw32*)
        AC_SUBST(BASDA_$2_LIBS, "-lBasda$1 $3")
        ;;
      *)
        AC_SUBST(BASDA_$2_LIBS, "-lBasda$1 $3")
        ;;
      esac
      AM_CONDITIONAL(HAVE_BASDA_$2, true)
      AC_DEFINE(HAVE_BASDA_$2,,[define if the $1 library is available])
  fi
])


