dnl $Header: https://svn.mpia.de/gulli/TwiceAsNice/branch/unstable/TwiceAsNice/neubau/config/m4/ice.m4 14080 2016-11-19 19:50:46Z mathar $

AC_DEFUN([AC_SEARCH_ICE],
[
  ac_with_Ice=no

  ac_Ice_libs="-lIce -lIceUtil"

case "${host}" in
    *-mingw32*)
        ac_search_path="$ICEDIR /usr/i686-pc-mingw32/sys-root/mingw"
        ac_search_path_slice="$ICEDIR/slice /usr/i686-pc-mingw32/sys-root/mingw/slice"
        ac_search_include_path="$ICEDIR/include /usr/i686-pc-mingw32/sys-root/mingw/include"
        ;;
    *)
        ac_search_path="$ICEDIR /usr /usr/local /opt /opt/local"
        ac_search_path_slice="$ICEDIR/slice /usr/slice /usr/share/slice /usr/share/Ice-3.5.1/slice /usr/share/Ice-3.4.2/slice /opt/local/share/ice/slice /usr/share/Ice/slice"
        ac_search_include_path="$ICEDIR/include /usr/include /usr/include/ice"
        ;;
esac

  AC_ARG_WITH(Ice,
    AC_HELP_STRING([--with-Ice=DIR], [root directory of Ice installation]),
    ac_with_Ice=$withval
    if test "x${ac_with_Ice}" != xyes && test "x${ac_with_Ice}" != xno; then
      ac_Ice_include="$withval/include"
      ac_Ice_libdir="$withval/lib"
      ac_Ice_bindir="$withval/bin"
      ac_Ice_slice="$withval/slice"
    fi
  )

  AC_ARG_WITH([Ice-include-dir],
    AC_HELP_STRING([--with-Ice-include-dir=DIR], [Ice header files are in DIR]),
    ac_with_Ice=$withval
    if test "x${ac_with_Ice}" != xyes && test "x${ac_with_Ice}" != xno; then
      ac_Ice_include="$withval"
    fi
  )
  AC_ARG_WITH([Ice-bin-dir],
    AC_HELP_STRING([--with-Ice-bin-dir=DIR], [Ice utilities such as moc and uic are in DIR]),
    ac_with_Ice=$withval
    if test "x${ac_with_Ice}" != xyes && test "x${ac_with_Ice}" != xno; then
      ac_Ice_bindir="$withval"
    fi
  )
  AC_ARG_WITH([Ice-lib-dir],
    AC_HELP_STRING([--with-Ice-lib-dir=DIR], [The Ice library is in DIR]),
    ac_with_Ice=$withval
    if test "x${ac_with_Ice}" != xyes && test "x${ac_with_Ice}" != xno; then
      ac_Ice_libdir="$withval"
    fi
  )
  AC_ARG_WITH([Ice-slice-dir],
    AC_HELP_STRING([--with-Ice-slice-dir=DIR], [The slIce files are in DIR]),
    ac_with_Ice=$withval
    if test "x${ac_with_Ice}" != xyes && test "x${ac_with_Ice}" != xno; then
      ac_Ice_slice="$withval"
    fi
  )

  if test "x${ac_Ice_libdir}" == "x"; then
    for ac_ice_path_test in $ac_search_path ; do
dnl     AC_MSG_NOTICE(Check for lib path $ac_ice_path_test/lib)
      if test -r "$ac_ice_path_test/lib"; then
         if test "${build_cpu}" = "x86_64"; then
dnl            AC_MSG_NOTICE(Check for lib path $ac_ice_path_test/lib64)
            ls $ac_ice_path_test/lib64/*Ice*  >/dev/null 2>&1
            if test $? -eq 0; then
dnl               AC_MSG_NOTICE(Found in $ac_ice_path_test/lib64)
               ac_Ice_libdir="$ac_ice_path_test/lib64"
               break;
            fi
dnl            AC_MSG_NOTICE(Check for lib path $ac_ice_path_test/lib/x86_64-linux-gnu)
            ls $ac_ice_path_test/lib/x86_64-linux-gnu/*Ice*  >/dev/null 2>&1
            if test $? -eq 0; then
dnl               AC_MSG_NOTICE(Found in $ac_ice_path_test/x86_64-linux-gnu)
               ac_Ice_libdir="$ac_ice_path_test/lib/x86_64-linux-gnu"
               break;
            fi
         fi
         ls $ac_ice_path_test/lib/*Ice*  >/dev/null 2>&1
         if test $? -eq 0; then
dnl                AC_MSG_NOTICE(Found in $ac_ice_path_test/lib)
              ac_Ice_libdir="$ac_ice_path_test/lib"
              break;
         fi
      fi
      if test -r "$ac_ice_path_test/lib64"; then
         if test "${build_cpu}" = "x86_64"; then
dnl            AC_MSG_NOTICE(Check for lib path $ac_ice_path_test/lib64)
            ls $ac_ice_path_test/lib64/*Ice*  >/dev/null 2>&1
            if test $? -eq 0; then
dnl               AC_MSG_NOTICE(Found in $ac_ice_path_test/lib64)
               ac_Ice_libdir="$ac_ice_path_test/lib64"
               break;
            fi
dnl            AC_MSG_NOTICE(Check for lib path $ac_ice_path_test/lib/x86_64-linux-gnu)
            ls $ac_ice_path_test/lib/x86_64-linux-gnu/*Ice*  >/dev/null 2>&1
            if test $? -eq 0; then
dnl               AC_MSG_NOTICE(Found in $ac_ice_path_test/x86_64-linux-gnu)
               ac_Ice_libdir="$ac_ice_path_test/lib/x86_64-linux-gnu"
               break;
            fi
         fi
         ls $ac_ice_path_test/lib/*Ice*  >/dev/null 2>&1
         if test $? -eq 0; then
dnl                AC_MSG_NOTICE(Found in $ac_ice_path_test/lib)
              ac_Ice_libdir="$ac_ice_path_test/lib"
              break;
         fi
      fi
    done
  fi

   if test "x${ac_Ice_include}" == "x"; then
    for ac_ice_path_test in $ac_search_include_path ; do
dnl      AC_MSG_NOTICE(Check for include path $ac_ice_path_test/Ice/Ice.h)
      if test -r "$ac_ice_path_test/Ice/Ice.h"; then
dnl         AC_MSG_NOTICE(Found in $ac_ice_path_test)
         ac_Ice_include="$ac_ice_path_test"
         break;
      fi
    done
   fi

   if test "x${ac_Ice_bindir}" == "x"; then
    for ac_ice_path_test in $ac_search_path ; do
dnl      AC_MSG_NOTICE(Check for bin path $ac_ice_path_test/bin)
      if test -r "$ac_ice_path_test/bin/slice2cpp"; then
dnl         AC_MSG_NOTICE(Found in $ac_ice_path_test/bin)
         ac_Ice_bindir="$ac_ice_path_test/bin"
         break;
      fi
    done
   fi

   if test "x${ac_Ice_slicedir}" == "x"; then
    for ac_ice_path_test in $ac_search_path_slice; do
dnl      AC_MSG_NOTICE(Check for lib path $ac_ice_path_test/Ice)
      if test -d "$ac_ice_path_test/Ice"; then
dnl         AC_MSG_NOTICE(Found in $ac_ice_path_test/Ice)
         ac_Ice_slice="$ac_ice_path_test"
         break;
      fi
    done
   fi


  AC_SEARCH_ICE_LOCAL($ac_Ice_libdir, $ac_Ice_include, $ac_Ice_bindir, $ac_Ice_libs, $ac_Ice_slice)
])


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

  gccv=`gcc -dumpversion`
  if [[ "${gccv:0:3}" == "4.4" ]] ; then
     ftmp='-ftemplate-depth-128'
  else
     ftmp='-ftemplate-depth=128'
  fi
  
  if test "$1"; then
    LDFLAGS="$LDFLAGS -L$1 "
    LIBS="$4"
  fi
  if test "$2"; then
    CXXFLAGS="$ftmp -Wall -D_REENTRANT $CXXFLAGS -I$2"
  fi
  if test "$3"; then
    PATH="$3:$PATH"
  fi

#  AC_MSG_NOTICE(Check for include path $build_os $CXXFLAGS $LDFLAGS ${host} )

  AC_LANG_PUSH(C++)
  AC_MSG_NOTICE($ac_compiler_gnu)
  AC_MSG_NOTICE($ac_link)
  AC_MSG_CHECKING(for Ice)
  AC_TRY_LINK([
    #include <Ice/Ice.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_MSG_ERROR(Ice not (properly) installed. Get a (free) recent version from: http://www.zeroc.com (Ice/Ice.h not found - LDFLAGS="$LDFLAGS -L$1 "; CXXFLAGS="-I$2";  PATH="$3"; LIBS="$4" ))
  else
    if test "$1"; then
      AC_SUBST(ICE_LDFLAGS, "-L$1")
    fi
    if test "$2"; then
      AC_SUBST(ICE_CXXFLAGS, "$ftmp -D_REENTRANT -I$2")
    fi
    if test "$4"; then
      AC_SUBST(ICE_LIBS, "$4")
    fi
  fi


# slice2cpp test
  # AC_CHECK_PROG(ac_icebindir, slice2cpp, yes)
  AC_PATH_PROG(ac_icebindir, slice2cpp, no)
  if test "x$ac_icebindir" != xno; then
    AC_SUBST(ICE_SLICE2CPP, "$ac_icebindir")
  else
    AC_MSG_ERROR(Ice not (properly) installed. Get a (free) recent version from: http://www.zeroc.com (slice2cpp not found))
  fi


# slice2py test
  AC_PATH_PROG(ac_icepybindir, slice2py, no)
  if test "x$ac_icebindir" != xno; then
    AC_SUBST(ICE_SLICE2PY, "$ac_icepybindir")
  else
    AC_MSG_ERROR(Ice not (properly) installed. Get a (free) recent version from: http://www.zeroc.com (slice2py not found))
  fi

  AC_SUBST(ICE_SLICEFILES, "-I$ac_Ice_slice")
])

