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.
#
# WARNING: Building Kross python plugin is now prohibited at all times,
# because it is not compatible with Python >= 3.
AM_CONDITIONAL(compile_kross_python,
               false && test -n "$LIBPYTHON" && test -n "$PYTHONINC")

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

if test "x$compile_kross" = "xyes" ; then
  AC_CHECK_PROG(RUBY_EXECUTABLE, ruby, ruby)

  if test -n "$RUBY_EXECUTABLE"; then
    AC_MSG_CHECKING(for Ruby dirs)

    RUBY_MAJOR=`${RUBY_EXECUTABLE} -r rbconfig -e "puts RbConfig::CONFIG.fetch(%q(MAJOR))"`
    RUBY_MINOR=`${RUBY_EXECUTABLE} -r rbconfig -e "puts RbConfig::CONFIG.fetch(%q(MINOR))"`

    RUBY_ARCHDIR=`${RUBY_EXECUTABLE} -r rbconfig -e "puts RbConfig::CONFIG.fetch(%q(archdir))"`
    RUBY_SITEARCHDIR=`${RUBY_EXECUTABLE} -r rbconfig -e "puts RbConfig::CONFIG.fetch(%q(sitearchdir))"`
    RUBY_SITEDIR=`${RUBY_EXECUTABLE} -r rbconfig -e "puts RbConfig::CONFIG.fetch(%q(sitelibdir))"`
    RUBY_LIBDIR=`${RUBY_EXECUTABLE} -r rbconfig -e "puts RbConfig::CONFIG.fetch(%q(libdir))"`
    RUBY_RUBYLIBDIR=`${RUBY_EXECUTABLE} -r rbconfig -e "puts RbConfig::CONFIG.fetch(%q(rubylibdir))"`
    RUBY_LIBRUBYARG=`${RUBY_EXECUTABLE} -r rbconfig -e "puts RbConfig::CONFIG.fetch(%q(LIBRUBYARG_SHARED))"`
    if test -n "$RUBY_LIBRUBYARG"; then
      RUBY_ENABLESHARED=yes
    fi

    RUBY_SERIES="$RUBY_MAJOR$RUBY_MINOR"
    RUBY_VERSION_DEFS=""
    if test "$RUBY_SERIES" -ge "19"; then
       AC_DEFINE_UNQUOTED(HAVE_RUBY_1_9, 1, [Defines if your system has Ruby 1.9.x])
       RUBY_VERSION_DEFS="${RUBY_VERSION_DEFS} -DHAVE_RUBY_1_9"
    fi
    if test "$RUBY_SERIES" -ge "30"; then
       AC_DEFINE_UNQUOTED(HAVE_RUBY_3, 1, [Defines if your system has Ruby 3.x])
       RUBY_VERSION_DEFS="${RUBY_VERSION_DEFS} -DHAVE_RUBY_3"
    fi

    if test `${RUBY_EXECUTABLE} -r rbconfig -e "puts RbConfig::CONFIG.key?(%q(rubyhdrdir))"` = "true"; then
       RUBY_INCLUDEDIR=`${RUBY_EXECUTABLE} -r rbconfig -e "puts RbConfig::CONFIG.fetch(%q(rubyhdrdir))"`
       if test `${RUBY_EXECUTABLE} -r rbconfig -e "puts RbConfig::CONFIG.key?(%q(rubyarchhdrdir))"` = "true"; then
          RUBY_ARCHINCLUDEDIR=`${RUBY_EXECUTABLE} -r rbconfig -e "puts RbConfig::CONFIG.fetch(%q(rubyarchhdrdir))"`
       else
          RUBY_ARCH=`${RUBY_EXECUTABLE} -r rbconfig -e "puts RbConfig::CONFIG.fetch(%q(arch))"`
          RUBY_ARCHINCLUDEDIR=${RUBY_INCLUDEDIR}/${RUBY_ARCH}
       fi
       RUBY_CFLAGS="-I${RUBY_INCLUDEDIR} -I${RUBY_ARCHINCLUDEDIR}"
    else
       RUBY_INCLUDEDIR=`${RUBY_EXECUTABLE} -r rbconfig -e "puts RbConfig::CONFIG.fetch(%q(archdir))"`
       RUBY_CFLAGS="-I${RUBY_INCLUDEDIR}"
    fi

    # Removes trailing slashes, if any, to avoid fail to install with recent libtool.
    RUBY_ARCHDIR=${RUBY_ARCHDIR%/}
    RUBY_SITEDIR=${RUBY_SITEDIR%/}
    RUBY_LIBDIR=${RUBY_LIBDIR%/}
    RUBY_RUBYLIBDIR=${RUBY_RUBYLIBDIR%/}

    AC_MSG_RESULT([
                    archdir $RUBY_ARCHDIR,
                    sitearchdir $RUBY_SITEARCHDIR,
                    sitedir $RUBY_SITEDIR,
                    rubylibdir $RUBY_RUBYLIBDIR,
                    libdir $RUBY_LIBDIR,
                    includedir $RUBY_INCLUDEDIR,
                    librubyarg $RUBY_LIBRUBYARG,
                    cflags $RUBY_CFLAGS])
    AC_SUBST(RUBY_ARCHDIR)
    AC_SUBST(RUBY_SITEARCHDIR)
    AC_SUBST(RUBY_SITEDIR)
    AC_SUBST(RUBY_RUBYLIBDIR)
    AC_SUBST(RUBY_LIBDIR)
    AC_SUBST(RUBY_INCLUDEDIR)
    AC_SUBST(RUBY_LIBRUBYARG)
    AC_SUBST(RUBY_CFLAGS)
    AC_SUBST(RUBY_ENABLESHARED)
    AC_SUBST(RUBY_VERSION_DEFS)


    AC_MSG_CHECKING(Ruby shared library)
    if test "x$RUBY_ENABLESHARED" != "xyes" ; then
      AC_MSG_RESULT([shared library 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 $RUBY_CFLAGS"
      AC_CACHE_VAL(ruby_build,
      [
        AC_TRY_COMPILE([
            #include <ruby.h>
        ],[
            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 shared lib
  fi # have ruby
fi # compiling kross

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