AC_INIT(mysql-gui-common, 0.6.1)
AC_CONFIG_SRCDIR(source/linux/myg_gtkutils.cc)
AM_INIT_AUTOMAKE(AC_PACKAGE_NAME, AC_PACKAGE_VERSION)

AC_CONFIG_HEADER(config.h)


# Set some common flags for gcc
CFLAGS="$CFLAGS -Wall -Wsign-compare"
CXXFLAGS="$CXXFLAGS -Wall"

AC_CANONICAL_HOST
AC_PROG_CC
AC_PROG_CXX
PKG_PROG_PKG_CONFIG
AM_PROG_LIBTOOL

AC_CHECK_TOOL(PKG_CONFIG, pkg-config)



AC_CHECK_HEADERS([sys/select.h])


if test "$(uname -s)" == "Darwin"; then
IS_MACOSX=yes
else
IS_MACOSX=no
fi


AC_ARG_ENABLE(i18n,
[  --disable-i18n	do not add support for internationalization (requires GNU gettext)],
	enable_i18n=$enableval, enable_i18n=yes)

if test x$enable_i18n = xyes; then
    #
    # Gettext
    #
    GETTEXT_PACKAGE="$PACKAGE_NAME"
    AC_SUBST(GETTEXT_PACKAGE)
    AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE,"$GETTEXT_PACKAGE",[Translation domain used])
    dnl Add the languages which your application supports here.
    ALL_LINGUAS="ja de pt_BR pl"
    AM_GLIB_GNU_GETTEXT
fi

#
# Gtk stuff
#

AC_ARG_ENABLE(gtk,
[  --disable-gtk        do not build Gtk related libraries],
        enable_gtk=$enableval, enable_gtk=yes)

#
# gtk/libglade libraries
#

if test "$IS_MACOSX" = "yes"; then
PKG_CHECK_MODULES(GLIB, [glib-2.0])
GLIB_CFLAGS="$GLIB_CFLAGS -I/usr/include/libxml2"
GLIB_LIBS="$GLIB_LIBS -lxml2 -framework CoreFoundation"
else
PKG_CHECK_MODULES(GLIB, [glib-2.0 libxml-2.0 >= 2.6.2])
fi
AC_SUBST(GLIB_CFLAGS)
AC_SUBST(GLIB_LIBS)

if test "$IS_MACOSX" = "yes"; then
  PKG_CHECK_MODULES(GTHREAD, [gthread-2.0])
  GTHREAD_CFLAGS="$GTHREAD_CFLAGS -I/usr/include/libxml2"
  GTHREAD_LIBS="$GTHREAD_LIBS -lxml2 -framework CoreFoundation"
else
  PKG_CHECK_MODULES(GTHREAD, [gthread-2.0 libxml-2.0 >= 2.6.2])
fi
AC_SUBST(GTHREAD_CFLAGS)
AC_SUBST(GTHREAD_LIBS)

if test "$enable_gtk" = "yes"; then
  PKG_CHECK_MODULES(GTK, [gtk+-2.0])
  AC_SUBST(GTK_CFLAGS)
  AC_SUBST(GTK_LIBS)

  PKG_CHECK_MODULES(GNOME, [libglade-2.0 gtkmm-2.4])
  AC_SUBST(GNOME_CFLAGS)
  AC_SUBST(GNOME_LIBS)
fi

#
# mysql headers
#
AC_CHECK_PROG(MYSQL_INCLUDE,mysql_config,`mysql_config --cflags`)
AC_CHECK_PROG(MYSQL_LIBS,mysql_config,`mysql_config --libs_r`)

if test -z "$MYSQL_INCLUDE"; then
        AC_MSG_ERROR([Could not find mysql_config script. Make sure the mysql client libraries are installed])
fi
AC_SUBST(MYSQL_INCLUDE)
AC_SUBST(MYSQL_LIBS)


ENABLED_MODULES=""
GRT_CFLAGS=""
GRT_LIBS=""

#
# GRT Generic RunTime Environment
#
AC_ARG_ENABLE(grt,
[  --enable-grt		enable GRT module loaders (default: no)],
	enable_grt=$enableval, enable_grt=no)

if test "$enable_grt" = yes; then

# ........................................................................
# Java
# ........................................................................
AC_ARG_ENABLE(java-modules,
[  --enable-java-modules	enable Java module loader support (default: yes)],
	enable_java=$enableval, enable_java=yes)

if test "$enable_java" = yes; then
  # JNI headers
  JNI_CFLAGS=
  JNI_LDFLAGS=

  AC_ARG_WITH(java-includes, 
  [  --with-java-includes 	directory where JDK include files are located],
  	[JNI_CFLAGS="-I$withval -I`dirname $withval/*/jni_md.h`" ])

  AC_ARG_WITH(java-ldflags, 
  [  --with-java-ldflags 		linker flags for linking with JVM],
  	[JNI_LDFLAGS="$withval"])

  save_cppflags="$CPPFLAGS"
  CPPFLAGS="$JNI_CFLAGS"
  AC_CHECK_HEADER(jni.h, ac_found_jni=yes, ac_found_jni=no)
  CPPFLAGS=$save_cppflags
  save_ldflags="$LDFLAGS"
  LDFLAGS="$JNI_LDFLAGS"
  AC_CHECK_LIB(jvm, JNI_GetDefaultJavaVMInitArgs, ac_found_jvm=yes, ac_found_jvm=no)
  LDFLAGS="$save_cppflags"
  if test x$ac_found_jvm = xyes; then
    JNI_LDFLAGS="$JNI_LDFLAGS -ljvm"
    ENABLED_MODULES="$ENABLED_MODULES"
  else
    JNI_CFLAGS="$JNI_CFLAGS -DDLOAD_JVM"
  fi
  if test x$ac_found_jni = xyes; then
    ENABLED_MODULES="$ENABLED_MODULES -DENABLE_JAVA_MODULES"
  fi
  GRT_CFLAGS="$GRT_CFLAGS $JNI_CFLAGS"
  GRT_LIBS="$GRT_LIBS $JNI_LDFLAGS"
  AC_SUBST(JNI_LDFLAGS)
fi

#......................................................................
# Lua Stuff
#......................................................................

LUA_CFLAGS=
LUA_LDFLAGS=
AC_ARG_WITH(lua-includes,
[  --with-lua-includes		directory where Lua headers are located],
	[LUA_CFLAGS=-I$withval])
AC_ARG_WITH(lua-libs,
[  --with-lua-libs              directory where Lua libraries are located],
        [LUA_LDFLAGS="-L$withval -llua50 -llualib50"])

if test x"$LUA_CFLAGS$LUA_LDFLAGS" = "x"; then
  AC_CHECK_PROG(LUA_CFLAGS,lua-config,`lua-config --include`)
  AC_CHECK_PROG(LUA_LDFLAGS,lua-config,`lua-config --libs`)
fi

if test x"$LUA_LDFLAGS" = "x"; then
  AC_CHECK_LIB(lua, lua_open, ac_lua_found=yes, ac_lua_found=no, $LUA_LDFLAGS)

  if test $ac_lua_found = no; then
    AC_MSG_ERROR([lua 5.0 is required to build GRT support but was not found.
Please specify its location with --with-lua-includes and --with-lua-libs or
install it if it's not.])
  fi
  LUA_LDFLAGS="-llua -llualib"

  save_cppflags=$CPPFLAGS
  CPPFLAGS="$LUA_CFLAGS $CPPFLAGS"
  AC_CHECK_HEADER(lua.h, ac_found_lua=yes, ac_found_lua=no)
  CPPFLAGS=$save_cppflags
fi

ENABLED_MODULES="$ENABLED_MODULES -DENABLE_LUA_MODULES"


LUA_LDFLAGS="$LUA_LDFLAGS -lexpat"


# Check if slang present and if so, build newt
SLANG_LIB=""
AC_CHECK_LIB(slang-utf8, SLang_init_tty, SLANG_LIB="-lslang-utf8",
	AC_CHECK_LIB(slang, SLang_init_tty, SLANG_LIB="-lslang", [], "-L/sw/lib"), "-L/sw/lib")
AC_SUBST(SLANG_LIB)

# Check if we need -luuid
AC_CHECK_LIB(uuid, uuid_generate, [LUA_LDFLAGS="$LUA_LDFLAGS -luuid"])

GRT_CFLAGS="$GRT_CFLAGS $LUA_CFLAGS"
GRT_LIBS="$GRT_LIBS $LUA_LDFLAGS"
AC_SUBST(LUA_LDFLAGS)

# PHP headers
AC_ARG_ENABLE(php-modules,
[  --enable-php-modules 		whether to support PHP scripting],
	enable_php=$enableval, enable_php=no)

if test $enable_php = yes; then
  AC_MSG_CHECKING(for php)
  PHP_LIBS="[$(php-config --libs)]"
  if test "$PHP_LIBS" = ""; then
    AC_MSG_RESULT(not found)
  else
# php-config --prefix ?
    PHP_LIBS="/usr/lib/php5/lib/libphp5.a -L[$(php-config --extension-dir)] $PHP_LIBS"
    PHP_CFLAGS=[$(php-config --includes)]
    ENABLED_MODULES="$ENABLED_MODULES -DENABLE_PHP_MODULES"
    AC_MSG_RESULT($PHP_LIBS)
  fi
fi
GRT_CFLAGS="$GRT_CFLAGS $PHP_CFLAGS"
GRT_LIBS="$GRT_LIBS $PHP_LIBS"

# Python headers
AC_ARG_ENABLE(python-modules,
[  --enable-python-modules	  whether to support Python scripting],
	enable_python=$enableval, enable_python=no)

if test $enable_python = yes; then
  AC_MSG_CHECKING(for Python)
  PYTHON_LIBS=[$(python -c "from distutils import sysconfig
print '/'.join(sysconfig.get_config_vars('LIBPL','LIBRARY'))+' '+sysconfig.get_config_var('LIBS')+' '+sysconfig.get_config_var('LINKFORSHARED')")]
  PYTHON_CFLAGS=[$(python -c "from distutils import sysconfig
print sysconfig.get_python_inc()")]
  PYTHON_CFLAGS="-I$PYTHON_CFLAGS"
  if test "$PYTHON_LIBS" = ""; then
    AC_MSG_RESULT(not found)
  else
    ENABLED_MODULES="$ENABLED_MODULES -DENABLE_PYTHON_MODULES"
    AC_MSG_RESULT($PYTHON_LIBS)
  fi
fi
GRT_CFLAGS="$GRT_CFLAGS $PYTHON_CFLAGS"
GRT_LIBS="$GRT_LIBS $PYTHON_LIBS"

# FastCGI
AC_ARG_ENABLE(fastcgi,
[  --enable-fastcgi                whether to include fastcgi support],
	enable_cgi=yes, enable_cgi=no)

if test $enable_cgi = yes; then
  AC_CHECK_HEADER(fastcgi.h, , [AC_MSG_ERROR("fastcgi.h header not found")])
  GRT_LIBS="$GRT_LIBS -lfcgi"
  ENABLED_MODULES="$ENABLED_MODULES -DENABLE_FASTCGI"
fi

# enable_grt
fi

AC_SUBST(GRT_CFLAGS)
AC_SUBST(GRT_LIBS)
AC_SUBST(ENABLED_MODULES)

AC_ARG_ENABLE(readline,
[ --enable-readline	use readline in grtsh],
	use_readline=$enableval, use_readline=no)

#
# Generic Canvas
#
AC_ARG_ENABLE(canvas,
[  --enable-canvas      enable Generic Canvas build (default: no)],
        enable_canvas=$enableval, enable_canvas=no)

if test "$enable_canvas" = yes; then
  AC_CHECK_PROG(GCDEPS_CFLAGS, freetype-config, `freetype-config --cflags`)
  AC_CHECK_PROG(GCDEPS_LIBS, freetype-config, `freetype-config --libs`)

  if test "$GCDEPS_CFLAGS$GCDEPS_LIBS" = ""; then
    PKG_CHECK_MODULES(GCDEPS, [freetype2])
  fi

  AC_CHECK_HEADERS([GL/gl.h GL/glu.h], ac_opengl_headers=yes, ac_opengl_headers=no)
  if test $ac_opengl_headers = no; then
    AC_MSG_ERROR([OpenGL headers not found. Please install OpenGL development files.])
  fi
fi
AC_SUBST(GCDEPS_CFLAGS)
AC_SUBST(GCDEPS_LIBS)



AM_CONDITIONAL(BUILD_GTK, test "x$enable_gtk" = "xyes")
AM_CONDITIONAL(BUILD_GRT, test "x$enable_grt" = "xyes")
AM_CONDITIONAL(BUILD_JAVA, test "x$ac_found_jni" = "xyes")
AM_CONDITIONAL(BUILD_GC, test "x$enable_canvas" = "xyes")
AM_CONDITIONAL(BUILD_PHP, test "x$enable_php" = "xyes")
AM_CONDITIONAL(BUILD_NEWT, ! test "x$SLANG_LIB" = "x")
AM_CONDITIONAL(USE_READLINE, test "x$use_readline" = "xyes")
AM_CONDITIONAL(BUILD_FASTCGI, test "x$enable_cgi" = "xyes")

#
# pcre
#
AC_CHECK_PROG(PCRE_CFLAGS,pcre-config,[yes])
if test "$PCRE_CFLAGS" != yes; then
        AC_MSG_ERROR([Could not find pcre-config script. Make sure the pcre libraries are installed])
else
        PCRE_CFLAGS=`pcre-config --cflags`
fi
AC_SUBST(PCRE_CFLAGS)

AC_CHECK_PROG(PCRE_LIBS,pcre-config,`pcre-config --libs`)
if test x"$PCRE_LIBS" = x; then
        AC_MSG_ERROR([Could not find pcre-config script. Make sure the pcre libraries are installed])
fi
AC_SUBST(PCRE_LIBS)


# check if pcre has support for unicode
AC_MSG_CHECKING([for unicode support in pcre])
ac_savelibs=$LIBS
ac_savecpp=$CPPFLAGS
LIBS=`pcre-config --libs`
CPPFLAS=`pcre-config --cflags`
AC_RUN_IFELSE([AC_LANG_PROGRAM([#include <pcre.h>], [
  char *err= NULL;
  int o;
  if (pcre_compile("(?:\\\\pL(?:\\\\pL|\\\\pN|\\\\pM|\\\\p{Pc}|\\\\p{Pd}|\\\\pS}|_)*)", PCRE_UTF8, &err, &o, NULL)) 
    return 0;
  return 1;
])], [HAVE_UNICODE_PCRE=1], [HAVE_UNICODE_PCRE=0])
if test "$HAVE_UNICODE_PCRE" == 1; then
  AC_MSG_RESULT(yes)
else
  AC_MSG_RESULT(no)
fi
CPPFLAGS=$ac_savecpp
LIBS=$ac_savelibs
AC_DEFINE(HAVE_UNICODE_PCRE, [], [whether pcre was compiled with unicode properties support])


# path mangling
DLEXT=.so
if test "$IS_MACOSX" = "yes"; then
	DLEXT=.dylib
	DLMAKE="gcc -arch i386 -arch ppc -dynamiclib -flat_namespace -undefined suppress"
elif test "$(uname -s)" = "SunOS"; then
	DLMAKE="gcc -shared -mimpure-text"
else
	DLMAKE="gcc -shared"
fi 
AC_SUBST(DLEXT)
AC_SUBST(DLMAKE)

DATADIRNAME=`echo $datadir|sed -e "s@${prefix}/@@" -e 's@${prefix}/@@'`
AC_SUBST(DATADIRNAME)

commondirname="common"
AC_ARG_WITH(commondirname, 
	[  --with-commondirname		sets the common data directory name],
	[commondirname="$withval"])

# name of the common data directory
MA_COMMONDIR=$commondirname
AC_SUBST(MA_COMMONDIR)

# installation directory for datafiles
mydatadir='$(datadir)'/mysql-gui/$commondirname
AC_SUBST(mydatadir)

AC_CONFIG_FILES([Makefile
library/Makefile
library/base-library/Makefile
library/base-library/source/Makefile
library/utilities/Makefile
library/utilities/source/Makefile
library/generic-runtime/Makefile
library/generic-runtime/newt/Makefile
library/generic-runtime/source/Makefile
library/grt-wb-controller/Makefile
library/grt-wb-controller/source/Makefile
library/grt-modules/Makefile
library/grt-modules/source/Makefile
library/generic-canvas/Makefile
library/generic-canvas/source/Makefile
library/generic-canvas/ftgl/Makefile
library/generic-canvas/ftgl/src/Makefile
library/mysql-resultset/Makefile
library/mysql-resultset/source/Makefile
library/sql-parser/Makefile
library/sql-parser/source/Makefile
source/Makefile 
source/linux/Makefile
source/java/Makefile
source/lua/Makefile
source/php/Makefile
source/php/modules/Makefile
tools/Makefile
tools/grtsh/Makefile
images/Makefile
images/png/Makefile
images/icons/Makefile
images/icons/png/Makefile
images/grt/Makefile
images/grt/icons/Makefile
images/grt/icons/db/Makefile
images/grt/structs/Makefile
res/Makefile
po/Makefile.in
res/grt/Makefile
res/linux/Makefile
grt_config])

AC_OUTPUT()
