AC_DEFUN([AC_PATH_LUA], [

dnl	Based on the lua check used by yzis-M3

	HAVE_LUA=""

	AC_ARG_WITH([lua],
		AC_HELP_STRING([--without-lua], [Build without Lua libraries (default: check)]))

        AC_ARG_WITH(lua-dir,
                AC_HELP_STRING([--with-lua-dir=DIR],[where the root of Lua 5.x is installed]),
                [
                LUA="$withval"
                LUA_INCLUDES=-I"$withval"/include
                LUA_LIBS="-L$withval/lib" ])

        AC_ARG_WITH(lua-includes,
                AC_HELP_STRING([--with-lua-includes=DIR],[where the Lua includes are]),
                [ LUA_INCLUDES="-I$withval" ])

        AC_ARG_WITH(lua-libraries,
                AC_HELP_STRING([--with-lua-libraries=DIR],[where the Lua library is installed]),
                [
                LUA_LIBS="-L$withval" ])


    if test "x$with_lua" = "xno"; then
	AC_MSG_RESULT([Not using Lua])
    else
        if ! test "x$LUA" = "x"; then
                AC_MSG_RESULT(using Lua from $LUA)
        fi
        if ! test "x$LUA_LIBS" = "x"; then
                AC_MSG_RESULT(using Lua libraries in $LUA_LIBS)
        fi
        if ! test "x$LUA_INCLUDES" = "x"; then
                AC_MSG_RESULT(using Lua includes in $LUA_INCLUDES)
        fi

        dnl checking some headers first
        ac_save_CFLAGS="$CFLAGS"
        ac_save_CPPFLAGS="$CPPFLAGS"
        ac_save_LDFLAGS="$LDFLAGS"
        CFLAGS="$LUA_INCLUDES $CFLAGS"
        CPPFLAGS="$LUA_INCLUDES $CPPFLAGS"
        LDFLAGS="$LUA_LIBS $LDFLAGS"

        LUAH_FOUND=""
        AC_CHECK_HEADER(lua.h,LUAH_FOUND="true",
                        [ AC_MSG_RESULT([lua.h was not found or was not usable, Lua 5.0 headers are required !]) ]
                        )
	LUALIBH_FOUND=""
        AC_CHECK_HEADER(lualib.h,LUALIBH_FOUND="true",
                        [ AC_MSG_RESULT([lualib.h was not found or was not usable, Lua 5.0 headers are required !]) ]
                        )


        dnl find the libs name
	if test -z "$LUALIBH_FOUND" -o -z "$LUAH_FOUND"; then
		LUA_LIBS=""
	else
	        AC_CHECK_LIB(lua50,lua_version, LUA_LIBS="$LUA_LIBS -llua50 -llualib50",
	                        AC_CHECK_LIB(lua,lua_version, LUA_LIBS="$LUA_LIBS -llua -llualib",
	                                [LUA_LIBS=""
				 	AC_MSG_RESULT([Lua 5.0 libraries were not found !]) ]
        	                        )
                	)
	fi
        CFLAGS="$ac_save_CFLAGS"
        CPPFLAGS="$ac_save_CPPFLAGS"
        LDFLAGS="$ac_save_LDFLAGS"


	if test -z "$LUA_LIBS"; then
		LUA=""
		LUA_INCLUDES=""
		LUA_LIBS=""
	else
               AC_DEFINE_UNQUOTED(HAVE_LUA, 1, [Define if you have LUA > 5.0])
		HAVE_LUA="yes"
	fi
        AC_SUBST(LUA)
        AC_SUBST(LUA_INCLUDES)
        AC_SUBST(LUA_LIBS)
    fi

])

AC_PATH_LUA
