version =
  open(File.join($srcdir, "mod_ruby.h")).grep(/MOD_RUBY_STRING_VERSION/)[0]
$MAJOR, $MINOR, $TEENY = version.scan(/mod_ruby\/(\d+).(\d+).(\d+)/)[0] #/
AC_SUBST("MAJOR")
AC_SUBST("MINOR")
AC_SUBST("TEENY")

AC_PROG_INSTALL()

AC_MSG_CHECKING("whether we are using gcc")
if $CC == "gcc" || `#{$CC} -v 2>&1` =~ /gcc/
  $using_gcc = true
  $CFLAGS += " -Wall"
else
  $using_gcc = false
end
AC_MSG_RESULT($using_gcc)

AC_MSG_CHECKING("Ruby version")
AC_MSG_RESULT(RUBY_VERSION)
if RUBY_VERSION < "1.6"
  AC_MSG_ERROR("mod_ruby requires Ruby 1.6.x or later.")
end
if RUBY_VERSION < "1.6.4"
  AC_MSG_WARN("mod_ruby recommends Ruby 1.6.4 or later.")
end

AC_MSG_CHECKING("for static Apache module support")
AC_ARG_WITH("apache",
"  --with-apache=DIR       Build static Apache module.  DIR is the path
                          to the top-level Apache source directory") { |withval|
  if withval == "yes"
    AC_MSG_ERROR("You need to specify a directory with --with-apache")
  end
  $APACHE_SRCDIR = File.expand_path(withval)
  unless File.file?("#{$APACHE_SRCDIR}/src/include/httpd.h")
    AC_MSG_ERROR("Unable to locate #{withval}/src/include/httpd.h")
  end
  $APACHE_INCLUDES = "-I#{$APACHE_SRCDIR}/src/include -I#{$APACHE_SRCDIR}/src/os/unix"
  $TARGET = "libruby.a"
  $INSTALL_TARGET = "install-static"
  st = File.stat($APACHE_SRCDIR)
  $APACHE_SRC_UID = st.uid
  $APACHE_SRC_GID = st.gid
  AC_MSG_RESULT("yes")
}.if_not_given {
  AC_MSG_RESULT("no")
}

AC_MSG_CHECKING("for dynamic Apache module support")
AC_ARG_WITH("apxs",
"  --with-apxs[=FILE]      Build shared Apache module.  FILE is the optional
                          pathname to the Apache apxs tool; [apxs]") { |withval|
  if withval == "yes"
    $APXS = "apxs"
  else
    $APXS = withval
  end
}.if_not_given {
  unless $TARGET
    $APXS = "apxs"
  end
}

if $APXS
  $CFLAGS += " " + `#{$APXS} -q CFLAGS`.chomp
  $APACHE_INCLUDES = "-I" + `#{$APXS} -q INCLUDEDIR`.chomp
  $APACHE_LIBEXECDIR = `#{$APXS} -q LIBEXECDIR`.chomp
  if $? != 0
    AC_MSG_ERROR("failed to exec #{$APXS}")
  end
  $TARGET = "mod_ruby.so"
  $INSTALL_TARGET = "install-shared"
  AC_MSG_RESULT("yes")
else
  AC_MSG_RESULT("no")
end

AC_SUBST("TARGET")
AC_SUBST("INSTALL_TARGET")
AC_SUBST("APACHE_SRCDIR")
AC_SUBST("APACHE_INCLUDES")
AC_SUBST("APACHE_LIBEXECDIR")
AC_SUBST("APACHE_SRC_UID")
AC_SUBST("APACHE_SRC_GID")

case PLATFORM
when /-aix/
  $DLDFLAGS = "-Wl,-bE:mod_ruby.imp -Wl,-bI:httpd.exp -Wl,-bM:SRE -Wl,-bnoentry -lc"
  open("mod_ruby.imp", "w") do |ifile|
    ifile.write <<EOF
#!
ruby_module
EOF
  end
  print <<EOF
To build mod_ruby on the AIX platform, you need to have the apache
export file `httpd.exp' in the current directory.
Please copy <apache-src-directory>/support/httpd.exp to the current
directory before making `mod_ruby.so'.
EOF
#'
end

AC_MSG_CHECKING("for eruby")
$ENABLE_ERUBY = false
AC_ARG_ENABLE("eruby",
	      "  --enable-eruby          enable eruby") { |enableval|
  if enableval == "yes"
    $ENABLE_ERUBY = true
    $LIBERUBYARG = "-leruby"
    AC_DEFINE("USE_ERUBY")
  end
}
AC_MSG_RESULT($ENABLE_ERUBY)

AC_ARG_WITH("eruby-includes",
	    "  --with-eruby-includes=DIR   eruby include files are in DIR") { |withval|
  if $ENABLE_ERUBY
    $ERUBY_INCLUDES = "-I#{withval}"
  end
}

AC_ARG_WITH("eruby-libraries",
	    "  --with-eruby-libraries=DIR  eruby library files are in DIR") { |withval|
  if $ENABLE_ERUBY
    $LIBERUBYARG = "-L#{withval} #{$LIBERUBYARG}"
  end
}

AC_SUBST("ERUBY_INCLUDES")
AC_SUBST("LIBERUBYARG")

librubyarg = $LIBRUBYARG.dup
Config.expand(librubyarg)
$MODULE_LIBS = "#{$LIBERUBYARG} #{librubyarg} #{$LIBS}"
AC_SUBST("MODULE_LIBS")

AC_OUTPUT("Makefile",
	  "libruby.module")

# Local variables:
# mode: Ruby
# tab-width: 8
# End:
