AC_INIT(
[PHP/Java Bridge], 
m4_bpatsubst(m4_include([../VERSION]),[
]), 
[php-java-bridge-users@lists.sourceforge.net], 
[php-java-bridge])
AC_CONFIG_AUX_DIR(.)
AC_CONFIG_LINKS(protocol.h:../protocol.h)

dnl autoconf >2.53
AC_PREREQ(2.53)
AC_CONFIG_SRCDIR(natcJavaBridge.c)

dnl automake >1.6.3
AM_INIT_AUTOMAKE([1.6.3])
AM_CONFIG_HEADER(config.h)

AC_PROG_CC
AC_PROG_CPP
AM_PROG_GCJ
AC_PROG_LIBTOOL
AC_STDC_HEADERS

AC_ARG_WITH(java,  [  --with-java[[=JAVA_HOME[[,JRE_HOME]]]]	
                          include java support. If JRE_HOME is specified, 
                          the run-time java location will be compiled into 
                          the binary. Otherwise the java executable will 
                          be searched using the PATH environment variable
			  Example: --with-java=/opt/jdk1.4,/usr/java/jre1.6], PHP_JAVA="$withval", PHP_JAVA="yes")
AC_ARG_WITH(mono,  [  --with-mono[[=/path/to/ikvmc.exe]]
                          include mono support
                          Example: --with-mono=$HOME/bin/ikvmc.exe], PHP_MONO="$withval", PHP_MONO="no")
AC_ARG_WITH(extdir,  [  --with-extdir[[=DIR]]	
                          directory which contains the shared java 
                          extension], EXTENSION_DIR="$withval", EXTENSION_DIR="`php-config --extension-dir`")
AC_ARG_ENABLE(servlet,  [  --enable-servlet[[=JAR]]	
                          JAR must be j2ee.jar or servlet.jar which 
                          contains the servlet classes], SERVLET="$enableval", SERVLET="yes")
AC_ARG_ENABLE(script,  [  --enable-script[[=JAR]]	
                          if you use JDK < 1.6 JAR must be script-api.jar 
                          which contains the jsr223 script api], SCRIPT="$enableval", SCRIPT="yes")
AC_ARG_ENABLE(faces,  [  --enable-faces[[=JAR]]	
                          JAR must be jsf-api.jar which contains the java 
                          server faces api], FACES="$enableval", FACES="yes")

if test "$PHP_JAVA" != "no" || test "$PHP_MONO" != "no"  ; then

JAVA_FUNCTION_CHECKS
PTHREADS_CHECK
PTHREADS_ASSIGN_VARS
PTHREADS_FLAGS
JAVA_CHECK_BROKEN_STDIO_BUFFERING
JAVA_CHECK_ABSTRACT_NAMESPACE
JAVA_CHECK_STRUCT_UCRED

AC_MSG_CHECKING(libtool)
libtool --version | fgrep "(GNU libtool)" ||
   AC_MSG_ERROR([Need GNU libtool, please read the INSTALL document.])

JNI_LIBS="libnatcJavaBridge.la"
AM_CPPFLAGS="${AM_CPPFLAGS} -DEXTENSION_DIR=\"\\\"\$(EXTENSION_DIR)\\\"\""
if test "$PHP_JAVA" != "yes"; then
# --with-java=/opt/compiletime/jdk,/usr/runtime/jre
PHP_JRE="`echo $PHP_JAVA | LANG=C awk -F, '{print $2}'`"
PHP_JAVA="`echo $PHP_JAVA | LANG=C awk -F, '{print $1}'`"

JAVA_INCLUDES=`for i in \`find $PHP_JAVA/include -follow -type d -print\`; do echo -n "-I$i "; done`
AM_CPPFLAGS="${AM_CPPFLAGS} $JAVA_INCLUDES"
JAVA_CHECK_JNI
if test "$have_jni" = "no"; then 
   JNI_LIBS=""
fi
else
JAVA_CHECK_JNI
if test "$have_jni" = "no"; then 
   echo "FATAL: Native compilation not supported on this system."
   echo "Install a java SDK and run: ./configure --with-java=<JAVA_HOME>"
   exit 3
fi
fi

TCP_SOCKETNAME="9167"
EXTENSION_DISPLAY_NAME="MonoBridge"
MONO_BRIDGE_EXE=MonoBridge.exe
VM_BINARIES="${MONO_BRIDGE_EXE} RunMonoBridge"

if test "$PHP_MONO" = "no"; then
  MONO_BRIDGE_EXE=
  TCP_SOCKETNAME="9267"
  EXTENSION_DISPLAY_NAME="JavaBridge"
  VM_BINARIES="java RunJavaBridge"
  MONO_BRIDGE_EXE=
fi
if test "$PHP_MONO" = "yes"; then
  PHP_MONO="`locate ikvmc.exe | head -1`"
  if test X$PHP_MONO = X; then 
   echo "ikvmc.exe not found, skipping mono JavaBridge.exe creation" >2
   PHP_MONO=
   MONO_BRIDGE_EXE=
  fi
fi

# optional faces, requires script, servlet
JAVA_BRIDGE_FACES_JAR="php-faces.jar"
if test "$FACES" = "no"; then
  JAVA_BRIDGE_FACES_JAR=""
else
  if test "$SCRIPT" = "no"; then
  SCRIPT="yes";
  fi
  if test "$SERVLET" = "no"; then
  SERVLET="yes";
  fi
fi
if test "$FACES" = "yes"; then
  FACES="../unsupported/jsf-api.jar"
fi

# optional script, requires servlet
JAVA_BRIDGE_SCRIPT_JAR="php-script.jar"
if test "$SCRIPT" = "no"; then
  JAVA_BRIDGE_SCRIPT_JAR=""
  SCRIPT=""
else
  if test "$SERVLET" = "no"; then
  SERVLET="yes";
  fi
fi
if test "$SCRIPT" = "yes"; then
  SCRIPT="script-api.jar"
fi

# optional JavaBridge.war, requires servlet
JAVA_BRIDGE_SERVLET_JAR="php-servlet.jar"
JAVA_BRIDGE_WAR="JavaBridge.war"
if test "$SERVLET" = "no"; then
  JAVA_BRIDGE_WAR=
  JAVA_BRIDGE_SERVLET_JAR=""
fi

if test "$SERVLET" = "yes"; then
  SERVLET="../unsupported/servlet-api.jar"
  if test X$SERVLET = X; then 
   echo "servlet-api.jar not found, skipping servlet JavaBridge.war creation" >2
   SERVLET=
   JAVA_BRIDGE_WAR=
  fi
fi

BACKEND_VERSION=`cat ../VERSION`
AC_CONFIG_FILES([RunJavaBridge.c RunMonoBridge.c php/java/bridge/global.properties])
AC_SUBST(AM_CPPFLAGS)
AC_SUBST(BACKEND_VERSION)
AC_SUBST(EXTENSION_DIR)
AC_SUBST(PHP_JAVA)
AC_SUBST(PHP_MONO)
AC_SUBST(TCP_SOCKETNAME)
AC_SUBST(EXTENSION_DISPLAY_NAME)
AC_SUBST(VM_BINARIES)
AC_SUBST(MONO_BRIDGE_EXE)
AC_SUBST(SERVLET)
AC_SUBST(JAVA_BRIDGE_WAR)
AC_SUBST(JAVA_BRIDGE_SERVLET_JAR)

# jni
AC_SUBST(JNI_LIBS)

# optional script
AC_SUBST(SCRIPT)
AC_SUBST(JAVA_BRIDGE_SCRIPT_JAR)
# optional faces
AC_SUBST(FACES)
AC_SUBST(JAVA_BRIDGE_FACES_JAR)
AM_CONDITIONAL([COND_GCJ], [test "$PHP_MONO" != "no" || test "$PHP_JAVA" = "yes"])
AC_OUTPUT(Makefile)

echo "-----------------------------------------------------------------"
echo "The PHP/Java Bridge backend will be compiled with:"
if test "$PHP_JAVA" = "yes"; then
echo "  JAVA: --with-java not specified."
echo "  Will use GNU gcc, gcj to compile backend to native code."
else if test "$PHP_MONO" != "no"; then
echo "  MONO: $PHP_MONO"
echo "  Will use GNU gcc, gcj and ikvmc to compile backend to CLR bytecode."
else
echo "  JAVA (compile time)    : $PHP_JAVA"
echo "  JAVA (run time)        : $PHP_JRE"
echo "  LOCAL (secure) channel : $have_jni"
echo ""
echo "  The following additional jars will be created:"
echo "  SERVLET                : $JAVA_BRIDGE_SERVLET_JAR"
echo "  SCRIPT                 : $JAVA_BRIDGE_SCRIPT_JAR"
echo "  JAVA SERVER FACES      : $JAVA_BRIDGE_FACES_JAR"
fi
fi
echo "-----------------------------------------------------------------"
echo ""

else

echo "install:" >Makefile

fi
