AC_ARG_ENABLE(scripting,
    AC_HELP_STRING([--enable-scripting],
                   [build scripting library (Kross) [default=yes]]),
    compile_kross=$enableval, compile_kross=yes)
AM_CONDITIONAL(compile_kross, test "x$compile_kross" = "xyes")

###############################
# Check if Python is installed.

if test "x$compile_kross" = "xyes" ; then
    #KDE_CHECK_PYTHON(2.3)
    KDE_CHECK_PYTHON
fi

# Compile the Kross python plugin only if both, $LIBPYTHON and
# $PYTHONINC, are defined.
AM_CONDITIONAL(compile_kross_python,
               test -n "$LIBPYTHON" && test -n "$PYTHONINC")

###############################
# Check for Ruby

if test "x$compile_kross" = "xyes" ; then
  AC_CHECK_PROGS([RUBY], [ruby ruby1.8 ruby18], ruby)

  if test -n "$RUBY"; then
    AC_MSG_CHECKING(for Ruby dirs)
    RUBY_ARCHDIR=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"archdir"@:>@)'`
    RUBY_SITEARCHDIR=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"sitearchdir"@:>@)'`
    RUBY_SITEDIR=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"sitelibdir"@:>@)'`
    RUBY_INCLUDEDIR=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"rubyincludedir"@:>@)'`
    RUBY_LIBDIR=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"libdir"@:>@)'`
    RUBY_LIBRUBYARG=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"LIBRUBYARG_SHARED"@:>@)'`
    RUBY_ENABLESHARED=`$RUBY -r rbconfig -e 'printf("%s",Config::CONFIG@<:@"ENABLE_SHARED"@:>@)'`
    AC_MSG_RESULT([
                   archdir $RUBY_ARCHDIR,
                   sitearchdir $RUBY_SITEARCHDIR,
                   sitedir $RUBY_SITEDIR,
                   includedir $RUBY_INCLUDEDIR,
                   libdir $RUBY_LIBDIR,
                   librubyarg $RUBY_LIBRUBYARG,
                   rubysharedenabled $RUBY_ENABLESHARED])
    AC_SUBST(RUBY_ARCHDIR)
    AC_SUBST(RUBY_SITEARCHDIR)
    AC_SUBST(RUBY_SITEDIR)
    AC_SUBST(RUBY_INCLUDEDIR)
    AC_SUBST(RUBY_LIBDIR)
    AC_SUBST(RUBY_LIBRUBYARG)
    AC_SUBST(RUBY_ENABLESHARED)

    AC_MSG_CHECKING(for Ruby header)

    if test ! -r $RUBY_INCLUDEDIR/ruby.h; then
      # if $RUBY_INCLUDEDIR is not valid try to use $RUBY_ARCHDIR
      RUBY_INCLUDEDIR=$RUBY_ARCHDIR
    fi

    if test ! -r $RUBY_INCLUDEDIR/ruby.h; then
      RUBY_LIBDIR=""
      AC_MSG_RESULT([not found])
    else
      AC_MSG_RESULT([found]) # header

      AC_MSG_CHECKING(Ruby shared library)
      if test "x$RUBY_ENABLESHARED" != "xyes" ; then
        AC_MSG_RESULT([shared library not found])
        RUBY_LIBDIR="" 
      else
        if test -z "$RUBY_LIBRUBYARG" ; then
          AC_MSG_RESULT([link argument not found])
          RUBY_LIBDIR="" 
        else
          AC_MSG_RESULT([found]) # shared library link arg

          AC_MSG_CHECKING([if C++ program with ruby can be compiled])
          AC_LANG_SAVE
          AC_LANG_CPLUSPLUS
          ac_save_CXXFLAGS="$CXXFLAGS"
          CXXFLAGS="$CXXFLAGS -I$RUBY_INCLUDEDIR"
          AC_CACHE_VAL(ruby_build,
          [
            AC_TRY_COMPILE([
                #include <ruby.h>
                #include <version.h>
            ],[
            
#if(RUBY_VERSION_MAJOR==1 && RUBY_VERSION_MINOR == 8 && RUBY_VERSION_TEENY <= 1)
#error "need at least ruby 1.8.2\n"
#endif

                ruby_init();
                return 0;
            ], ruby_build=yes,
            ruby_build=no)
          ])
          AC_MSG_RESULT($ruby_build)
          if test "$ruby_build" = "no"; then
            RUBY_LIBDIR=""
          fi
          CXXFLAGS="$ac_save_CXXFLAGS"
          AC_LANG_RESTORE
        fi # have ruby shared lib argument
      fi # have shared lib
    fi # have ruby header
  fi # have ruby
fi # compiling kross

AM_CONDITIONAL(compile_kross_ruby, test -n "$RUBY_LIBDIR")
