AC_PREREQ([2.53])
AC_INIT(rlm_ruby.c)
AC_REVISION($Revision: 1.9 $)
AC_DEFUN(modname,[rlm_ruby])

if test x$with_[]modname != xno; then

	AC_PROG_CC
	AC_PROG_CPP

	AC_CHECK_PROGS(RUBYBIN, [ ruby ruby1.8 ], not-found, [${PATH}:/usr/bin:/usr/local/bin])

	if test x$RUBYBIN = xnot-found; then
		fail="ruby-binary"
	fi

	dnl extra argument: --with-rlm-ruby-lib-dir
	RB_LIB_DIR=
	AC_ARG_WITH(rlm-ruby-lib-dir,
	[  --with-rlm-ruby-lib-dir=DIR       Directory for Ruby library files []],
	[ case "$withval" in
	    no)
		AC_MSG_ERROR(Need rlm-ruby-lib-dir)
		;;
	    yes)
		;;
	    *)
		RB_LIB_DIR="$withval"
		;;
	  esac ]
	)

	dnl extra argument: --with-rlm-ruby-include-dir
	RB_INC_DIR=
	AC_ARG_WITH(rlm-ruby-include-dir,
	[  --with-rlm-ruby-include-dir=DIR   Directory for Ruby include files []],
	[ case "$withval" in
	    no)
		AC_MSG_ERROR(Need rlm-ruby-include-dir)
		;;
	    yes)
		;;
	    *)
		RB_INC_DIR="$withval"
		;;
	  esac ]
	)

	if test x$fail = x; then
		#We use fetch, cause [] are disapearing somewere
		RB_PREFIX=`${RUBYBIN} -e "require 'rbconfig'; puts Config::CONFIG.fetch('prefix')"`
		RB_EXEC_PREFIX=`${RUBYBIN} -e "require 'rbconfig'; puts Config::CONFIG.fetch('exec_prefix')"`
		changequote(<<, >>)dnl
		RB_VERSION=`${RUBYBIN} -e 'puts RUBY_VERSION'`
		changequote([, ])dnl
#		RB_LIBS=`${RUBYBIN} -e "require 'rbconfig'; puts Config::CONFIG.fetch('LIBRUBY')"`
#		RB_LIB_DIR=`${RUBYBIN} -e "require 'rbconfig'; puts Config::CONFIG.fetch('libdir')"`
#		RB_EXTRA_LIBS=`${RUBYBIN} -e "require 'rbconfig'; puts Config::CONFIG.fetch('LIBS')"`
		RB_LIBS=`${RUBYBIN} -e "require 'rbconfig'; puts Config::CONFIG.fetch('LIBRUBYARG_SHARED')"`
		RB_INC_DIR=`${RUBYBIN} -e "require 'rbconfig'; puts Config::CONFIG.fetch('includedir')"`
		RB_CFLAGS=`${RUBYBIN} -e "require 'rbconfig'; puts Config::CONFIG.fetch('CFLAGS')"`
		RB_ARCH_DIR=`${RUBYBIN} -e "require 'rbconfig'; puts Config::CONFIG.fetch('archdir')"`

		old_CFLAGS=$CFLAGS
		CFLAGS="$CFLAGS $RB_CFLAGS -I${RB_ARCH_DIR} -I${RB_INC_DIR}"
#		smart_try_dir=$RB_INC_DIR
		FR_SMART_CHECK_INCLUDE(ruby.h)
		if test "x$ac_cv_header_ruby_h" != "xyes"; then
			fail="$fail ruby.h"
			targetname=
		fi
		ruby_cflags=$CFLAGS
		CFLAGS=$old_CFLAGS

		old_LIBS=$LIBS
#		LIBS="$RB_LIBS $RB_LIB_DIR $RB_EXTRA_LIBS -lm"
		LIBS="$RB_LIBS"
#		smart_try_dir=$RB_LIB_DIR
		ruby_ldflags=$LIBS
		LIBS=$old_LIBS
		targetname=modname
#		ruby_headers="${RB_INC_DIR} ${RB_ARCH_DIR}"
	fi

else
	targetname=
	echo \*\*\* module modname is disabled.
fi

if test x"$fail" != x""; then
	if test x"${enable_strict_dependencies}" = x"yes"; then
		AC_MSG_ERROR([set --without-]modname[ to disable it explicitly.])
	else
		AC_MSG_WARN([silently not building ]modname[.])
		AC_MSG_WARN([FAILURE: ]modname[ requires: $fail.]);
		targetname=""
	fi
fi

AC_SUBST(ruby_ldflags)
AC_SUBST(ruby_cflags)
AC_SUBST(ruby_headers)
AC_SUBST(targetname)
AC_OUTPUT(Makefile)
