AC_INIT(mysql-workbench, 5.2.38)
AC_CONFIG_SRCDIR(backend/wbpublic/grt/common.h)
AM_INIT_AUTOMAKE([1.9 tar-ustar])

AC_DISABLE_STATIC

AC_CONFIG_HEADER(config.h)

AC_ARG_ENABLE(debug,
[  --disable-debug   do not compile with debugging symbols], enable_debug=$enableval, enable_debug=no)

if test x"$enable_debug" = xyes; then
  debug_flags="-ggdb3 -DENABLE_DEBUG -DDEBUG"
#  optimize_flags="-O0"
#else
#  optimize_flags="-O2"
fi


AC_ARG_ENABLE(codecoverage,
[  --enable-codecoverage   instrument code for code coverage stats], enable_codecoverage=yes, enable_codecoverage=no)

if test x"$enable_codecoverage" = xyes; then
  codecoverage_flags="-fprofile-arcs -ftest-coverage"
else
  codecoverage_flags=""
fi


# Disable GRT header generation by default so that simple builds will not touch files in repo
AC_ARG_ENABLE(grt-generation,
[  --enable-grt-generation  regenerate headers used by GRT], generate_headers=$enableval, generate_headers=no)
AM_CONDITIONAL(BUILD_GRT_HEADERS, test "$generate_headers" = yes)


# Set some common flags for gcc
CFLAGS="$CFLAGS -Wextra -Wall -Wno-unused -Wsign-compare $debug_flags $optimize_flags $codecoverage_flags -DTIXML_USE_STL=1"
CXXFLAGS="$CXXFLAGS -Wextra -Wall -Wno-unused -Wno-deprecated $debug_flags $optimize_flags $codecoverage_flags -DTIXML_USE_STL=1"
LDFLAGS="$LDFLAGS $codecoverage_flags"

AC_CANONICAL_HOST
AC_PROG_CC
AC_PROG_CXX
AM_PROG_LIBTOOL
AC_LANG(C++)

AC_C_BIGENDIAN

AC_CHECK_TOOL(PKG_CONFIG, pkg-config)




AC_ARG_WITH(libzip-includes,
[  --with-libzip-includes=<path> specifies libzip includes path],
		[ac_libzip_includes="$withval"], [ac_libzip_includes="no"])

AC_ARG_WITH(libzip-libs,
[  --with-libzip-libs=<path> specifies libzip libs],
		[ac_libzip_libs="$withval"], [ac_libzip_libs="no"])



AC_CHECK_FUNCS(strtol strtoll strtold strtoull strtoimax strtoumax)

AC_ARG_ENABLE(se,
[  --enable-se       set edition to SE],
  std_edition=$enableval, std_edition=no)

if test x$std_edition = xyes; then
     echo -en "#ifndef __WB_CONFIG_H__\n#define __WB_CONFIG_H__\n#define EDITION_SE\n#define COMMERCIAL_CODE\n#endif\n" > wb_config.h
else
     echo -en "#ifndef __WB_CONFIG_H__\n#define __WB_CONFIG_H__\n#undef COMMERCIAL_CODE\n#define EDITION_OSS\n#endif\n" > wb_config.h
fi

AC_CHECK_HEADERS([sys/select.h])

AC_CHECK_HEADERS([tr1/unordered_set tr1/unordered_map])


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

have_zip="no"
if test x"$ac_libzip_libs" = x"no"; then
    PKG_CHECK_MODULES(ZIP, [libzip])
    have_zip="yes"
else
    ZIP_CFLAGS=-I$ac_libzip_includes
    ZIP_LIBS=-I$ac_libzip_libs
    have_zip="yes"
fi

if test x"$have_zip" = x"no"; then
echo "Libzip can not be found. Either add libzip.pc path to PKG_CONFIG_PATH, or specify includes and libs manually"
exit 1
fi

AC_SUBST(ZIP_CFLAGS)
AC_SUBST(ZIP_LIBS)

if test "$IS_MACOSX" = "yes"; then
PKG_CHECK_MODULES(GLIB, [glib-2.0 gthread-2.0 gmodule-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 gthread-2.0 gmodule-2.0 libxml-2.0 >= 2.6.2])
fi
AC_SUBST(GLIB_CFLAGS)
AC_SUBST(GLIB_LIBS)

PKG_CHECK_MODULES(SIGC, [sigc++-2.0])
AC_SUBST(SIGC_CFLAGS)
AC_SUBST(SIGC_LIBS)

PKG_CHECK_MODULES(GNOME_KEYRING, [gnome-keyring], 
                  [PKG_CHECK_EXISTS([gnome-keyring < 1.0.0], 
                                    [AC_DEFINE_UNQUOTED(HAVE_OLD_GNOME_KEYRING, 1, "")], 
                                    [AC_DEFINE_UNQUOTED(HAVE_GNOME_KEYRING, 1, "")]
                                   )
                  ],
                  [PKG_CHECK_MODULES(GNOME_KEYRING, 
                                     [gnome-keyring-1],
                                     [PKG_CHECK_EXISTS([gnome-keyring-1 < 1.0.0], 
                                                                       [AC_DEFINE_UNQUOTED(HAVE_OLD_GNOME_KEYRING, 1, "")], 
                                                                       [AC_DEFINE_UNQUOTED(HAVE_GNOME_KEYRING, 1, "")]
                                                      )
                                     ],
                                     [GNOME_KEYRING_CFLAGS=""
                                      GNOME_KEYRING_LIBS=""
                                     ]
                                    )
                  ]
                 )
AC_SUBST(GNOME_KEYRING_CFLAGS)
AC_SUBST(GNOME_KEYRING_LIBS)

PKG_CHECK_MODULES(GNOME, [gtkmm-2.4 >= 2.12])

AC_SUBST(GNOME_CFLAGS)
AC_SUBST(GNOME_LIBS)

PKG_CHECK_MODULES(SQLITE3, [sqlite3])
AC_SUBST(SQLITE3_CFLAGS)
AC_SUBST(SQLITE3_LIBS)

#PKG_CHECK_MODULES(WEBKIT1, [webkit-1.0])
#AC_SUBST(WEBKIT1_CFLAGS)
#AC_SUBST(WEBKIT1_LIBS)

# Boost
AC_ARG_WITH(boost-includes,
[  --with-boost-includes=<path> specifies Boost includes path],
		[ac_boost_inc="$withval"], [ac_boost_inc="no"])

if test x"$ac_boost_inc" != x"no"; then
  CFLAGS+=" -I$ac_boost_inc "
  CXXFLAGS+=" -I$ac_boost_inc "
  (
    CPPFLAGS+=" -I$ac_boost_inc "
    AC_CHECK_HEADER(boost/foreach.hpp, , [AC_MSG_ERROR("BOOST library is missing")])
  )
else
AC_CHECK_HEADER(boost/foreach.hpp, , [AC_MSG_ERROR("BOOST library is missing")])
fi

dnl not used for now
dnl AC_MSG_CHECKING([For boost libraries])
dnl AC_LANG(C++)
dnl BOOST_LIBS="-lboost_filesystem -lboost_system"
dnl SAVED_LDFLAGS=$LDFLAGS
dnl LDFLAGS="$LDFLAGS $BOOST_LIBS"
dnl AC_LINK_IFELSE(
dnl        [AC_LANG_PROGRAM([#include <boost/filesystem.hpp>],
dnl          [boost::filesystem::is_directory(".")])],
dnl          [AC_MSG_RESULT([yes])],
dnl          [AC_MSG_ERROR([One or more of boost_filesystem, boost_system is not installed.])])
dnl LDFLAGS=$SAVED_LDFLAGS
BOOST_LIBS=""
AC_SUBST(BOOST_LIBS)

#
# mysql headers
#
AC_ARG_WITH(mysql-config,
[  --with-mysql-config=<path> specifies how to run the mysql_config script [default=mysql_config]],
		ac_mysql_config_path="$withval", ac_mysql_config_path="mysql_config")

AC_MSG_CHECKING([for mysql headers and libraries])
MYSQL_INCLUDE=`$ac_mysql_config_path --cflags`
MYSQL_LIBS=`$ac_mysql_config_path --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])
else
	AC_MSG_RESULT(CFLAGS="$MYSQL_INCLUDE"   LIBS="$MYSQL_LIBS")
fi
AC_SUBST(MYSQL_INCLUDE)
AC_SUBST(MYSQL_LIBS)


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

PKG_CHECK_MODULES(LIBUUID, uuid, AC_DEFINE([HAVE_LIBUUID], 1, [uuid library]))
CFLAGS="$CFLAGS $UUID_CFLAGS"

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

PKG_CHECK_MODULES(LUA, lua5.1, , [PKG_CHECK_MODULES(LUA, [lua >= 5.1])])

ENABLED_MODULES="$ENABLED_MODULES -DENABLE_LUA_MODULES"

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

GRT_CFLAGS="$GRT_CFLAGS $LUA_CFLAGS"
GRT_LIBS="$GRT_LIBS $LUA_LIBS"

# We need either python 2.6 or 2.7 for MySQL Utilities
AC_CHECK_PROG(PYTHON26, python2.6, [yes])
AC_CHECK_PROG(PYTHON27, python2.7, [yes])
# Python headers
AC_MSG_CHECKING(for Python libraries)
PYTHON_LIBS=[$(python -c "from distutils import sysconfig
import os 
cfg=sysconfig.get_config_vars()
lib = os.path.join(cfg['LIBDIR'], cfg['LDLIBRARY'])
if os.path.exists(lib):
    print \"-L\" + cfg['LIBDIR'] + \" -l\" + cfg['LDLIBRARY'].lstrip(\"lib\").rstrip(\".so\")")]
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)
  AC_MSG_ERROR([Python C development headers/libraries not found])
else
  ENABLED_MODULES="$ENABLED_MODULES -DENABLE_PYTHON_MODULES"
  AC_MSG_RESULT($PYTHON_LIBS)
fi
AC_SUBST(PYTHON_CFLAGS)
AC_SUBST(PYTHON_LIBS)

GRT_CFLAGS="$GRT_CFLAGS $PYTHON_CFLAGS"
GRT_LIBS="$GRT_LIBS $PYTHON_LIBS"


GRT_CFLAGS="$GRT_CFLAGS $ENABLED_MODULES"

AC_SUBST(GRT_CFLAGS)
AC_SUBST(GRT_LIBS)


dnl Check whether mysql-utilities and connector sources are present
AC_ARG_ENABLE(mysql-utilities,
[  --enable-mysql-utilities  whether the command line MySQL Utilities should be built and installed],
                        bundle_utilities="$enableval", bundle_utilities=no)

if test x$bundle_utilities = xyes; then
    if test "$ac_cv_prog_PYTHON26" != "yes" -a "$ac_cv_prog_PYTHON27" != "yes"; then
        AC_MSG_ERROR([mysql-utilities enabled, but required Python 2.6 or 2.7 not found])
        exit 1
    fi
    AC_MSG_CHECKING(mysql-utilities)
    if ! test -d ext/mysql-utilities; then
        AC_MSG_RESULT([optional mysql-utilities repository not found, skipping])
        bundle_utilities=no
    elif ! test -d ext/connector-python; then
        AC_MSG_RESULT([optional connector-python repository not found, skipping])
        bundle_utilities=no
    else
        MYSQL_UTILITIES_PROGRAMS=[$(python -c 'import os;print " ".join([f[:-3] for f in os.listdir("ext/mysql-utilities/scripts") if f.endswith(".py")])')]
        AC_SUBST(MYSQL_UTILITIES_PROGRAMS)
        AC_MSG_RESULT([$MYSQL_UTILITIES_PROGRAMS])
        bundle_utilities=yes
    fi
fi

AC_ARG_WITH(cairo-prefix,
[  --with-cairo-prefix=<path> specifies cairo location],
		ac_cairo_prefix="$withval", ac_cairo_prefix="no")

if test x"$ac_cairo_prefix" != x"no"; then
CAIRO_CFLAGS="-I$ac_cairo_prefix"/include
CAIRO_LIBS="-I$ac_cairo_prefix"/lib
fi

PKG_CHECK_MODULES(CAIRO, [cairo >= 1.5.12 gl])

# check for opengl and glx headers
#AC_CHECK_HEADERS([GL/gl.h GL/glx.h], [], AC_MSG_ERROR([OpenGL headers not found]))

#AC_CHECK_LIB(GL, glNewList, CAIRO_LIBS="$CAIRO_LIBS -lGL", AC_MSG_ERROR([OpenGL library not found]))


# check if ctemplate is installed in system, otherwise use the bundled one
AC_CHECK_HEADER(ctemplate/template.h, ac_system_ctemplate=yes, ac_system_ctemplate=no)

AC_ARG_WITH(bundled-ctemplate,
[  --with-bundled-ctemplate  force use of bundled ctemplate library],
    ac_bundled_ctemplate=yes, ac_bundled_ctemplate=no)

if test "$ac_bundled_ctemplate" = yes; then
  AC_MSG_RESULT([Using bundled ctemplate])
  CTEMPLATE_CFLAGS="-I\$(top_srcdir)/ext/ctemplate/ctemplate-src/src"
  CTEMPLATE_LIBS="\$(top_srcdir)/ext/ctemplate/ctemplate-src/libctemplate_wb.la"
else
  if test "$ac_system_ctemplate" = yes; then
    CTEMPLATE_LIBS=-lctemplate
  else
    AC_MSG_ERROR([Library ctemplate not found in system. Install package libctemplate-devel, ctemplate-devel or libctemplate-dev])
  fi
fi
AC_SUBST(CTEMPLATE_CFLAGS)
AC_SUBST(CTEMPLATE_LIBS)


#AM_CONDITIONAL(BUILD_JAVA, test "x$ac_found_jni" = "xyes")
#AM_CONDITIONAL(BUILD_PHP, test "x$enable_php" = "xyes")
AM_CONDITIONAL(USE_READLINE, test "x$use_readline" = "xyes")
#AM_CONDITIONAL(BUILD_FASTCGI, test "x$enable_cgi" = "xyes")
AM_CONDITIONAL(BUILD_CTEMPLATE, test "x$ac_system_ctemplate" = "xyes")
AM_CONDITIONAL(BUILD_SE, test "x$std_edition" = "xyes")
AM_CONDITIONAL(BUNDLE_UTILITIES, test "x$bundle_utilities" = "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,[yes])
if test x"$PCRE_LIBS" = x; then
        AC_MSG_ERROR([Could not find pcre-config script. Make sure the pcre libraries are installed])
else
        PCRE_LIBS=`pcre-config --libs`
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([[#include <pcre.h>
int main()
{
  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; 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])


######################################################################################3



# 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"])


AC_CONFIG_FILES([Makefile
build/mysql-workbench.spec
res/Makefile
res/grt/Makefile
res/grtdata/Makefile
res/wbdata/Makefile
res/mysql.profiles/Makefile
res/sqlidedata/Makefile
ext/Makefile
ext/scintilla/gtk/Makefile
ext/cppconn/Makefile
ext/cppconn/driver/Makefile
ext/vsqlite++/Makefile
ext/vsqlite++/src/sqlite/Makefile
backend/Makefile
backend/wbpublic/Makefile
backend/wbprivate/Makefile
images/Makefile
images/icons/Makefile
images/icons/linux/Makefile
images/icons/linux/48x48/Makefile
images/icons/linux/128x128/Makefile
images/icons/linux/16x16/Makefile
images/grt/Makefile
images/ui/Makefile
images/toolbar/Makefile
images/cursors/Makefile
images/admin/Makefile
images/home/Makefile
library/Makefile
library/canvas/Makefile
library/canvas/src/Makefile
library/grt/Makefile
library/grt/src/Makefile
library/base/Makefile
library/sshtunnel/Makefile
library/sql-parser/Makefile
library/sql-parser/source/Makefile
library/forms/Makefile
library/forms/swig/Makefile
library/dbc/Makefile
tools/Makefile
tools/genobj/Makefile
tools/genwrap/Makefile
modules/Makefile
modules/db/Makefile
modules/db.mysql/Makefile
modules/db.mysql.query/Makefile
modules/db.mysql.sqlparser/Makefile
modules/db.mysql.sqlide/Makefile
modules/grt.forms/Makefile
modules/wb.model/Makefile
modules/wb.mysql.import/Makefile
modules/wb.validation/Makefile
modules/wb.mysql.validation/Makefile
modules/db.checks/Makefile
modules/wb.utils/Makefile
plugins/Makefile
plugins/db.mysql/frontend/Makefile
plugins/db.mysql.diff.reporting/frontend/Makefile
plugins/db.mysql.editors/linux/Makefile
plugins/wb.admin/frontend/Makefile
plugins/wb.doclib/Makefile
plugins/wb.model.editors/linux/Makefile
plugins/wb.model.reporting/frontend/Makefile
plugins/wb.printing/linux/Makefile
plugins/wb.model.snippets/Makefile
plugins/wb.bugreport/Makefile
frontend/Makefile
frontend/linux/Makefile
frontend/linux/sqlide/Makefile
frontend/linux/linux_utilities/Makefile
frontend/linux/workbench/Makefile
generated/Makefile
testing/tut/Params.inc
])


AC_OUTPUT()

if test "$ac_system_ctemplate" = no; then
(
  cd ./ext/ctemplate/ctemplate-src &&
  autoreconf -i -s; 
  ./configure --prefix=$prefix --libdir=$libdir/mysql-workbench --disable-dependency-tracking --disable-dynamic
)    
fi
