dnl Process this file with autoconf to produce a configure script. -*-m4-*-

dnl %CopyrightBegin%
dnl
dnl Copyright Ericsson AB 2008-2012. All Rights Reserved.
dnl
dnl The contents of this file are subject to the Erlang Public License,
dnl Version 1.1, (the "License"); you may not use this file except in
dnl compliance with the License. You should have received a copy of the
dnl Erlang Public License along with this software. If not, it can be
dnl retrieved online at http://www.erlang.org/.
dnl
dnl Software distributed under the License is distributed on an "AS IS"
dnl basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
dnl the License for the specific language governing rights and limitations
dnl under the License.
dnl
dnl %CopyrightEnd%

AC_INIT()

AC_CONFIG_AUX_DIRS($srcdir/autoconf)

AC_PREREQ(2.59)

## Delete previous failed configure results
if test -f ./CONF_INFO; then
   rm ./CONF_INFO
fi

if test -z "$ERL_TOP" || test ! -d $ERL_TOP ; then
  AC_CONFIG_AUX_DIRS(autoconf)
  WX_BUILDING_INSIDE_ERLSRC=false    
else
  erl_top=${ERL_TOP}
  if test -d $erl_top/erts/autoconf; then 
     AC_CONFIG_AUX_DIRS($erl_top/erts/autoconf)
     WX_BUILDING_INSIDE_ERLSRC=true
  else
     AC_CONFIG_AUX_DIRS(autoconf)
     WX_BUILDING_INSIDE_ERLSRC=false
  fi
fi

if test "X$host" != "Xfree_source" -a "X$host" != "Xwin32"; then
    AC_CANONICAL_HOST
else
    host_os=win32
fi

case $host_os in
    mingw32)
	if test "X$host" = "X"; then
	   host=win32
	fi
	;;
    *)
	;;
esac

TARGET=$host
AC_SUBST(TARGET)

AC_PROG_CC
AC_PROG_CXX
AC_PROG_RANLIB
AC_PROG_CPP

AC_MSG_NOTICE(Building for [$host_os])
WXERL_CAN_BUILD_DRIVER=true

LM_WINDOWS_ENVIRONMENT

if test  X"$MIXED_CYGWIN_VC" == X"yes" -o X"$MIXED_MSYS_VC" == X"yes"; then
   CFLAGS="-Owx"
fi

## Check that we are in 32 bits mode on darwin 
## (wxWidgets require that it currently uses 32-bits Carbon)
## Otherwise skip building wxErlang
AC_CHECK_SIZEOF(void *)

case $ac_cv_sizeof_void_p-$host_os in
    8-darwin*)
	if test X"$WX_BUILDING_INSIDE_ERLSRC" != X"true" ; then
		AC_MSG_ERROR([Can not use 64bits wxWidgets on Darwin])
	else
		echo "Can not combine 64bits erlang with wxWidgets on MacOSX, wx will not be useable" > ./CONF_INFO
		WXERL_CAN_BUILD_DRIVER=false
		AC_MSG_WARN([Can not combine 64bits erlang with wxWidgets on MacOSX, wx will not be useable])
	fi
	WXERL_CAN_BUILD_DRIVER=false
	;;
    *)
	;;
esac

PTHR_CFLAGS="-D_THREAD_SAFE -D_REENTRANT"

OBJC_CC=$CC
OBJC_CFLAGS=""

dnl NOTE: CPPFLAGS will be included in CFLAGS at the end
case $host_os in
    darwin*)
	AC_MSG_CHECKING([if compiler accepts -ObjC])
	saved_CFLAGS=$CFLAGS
  	CFLAGS="$CFLAGS -ObjC"
  	AC_TRY_COMPILE([],[;], accept_objc_flag=true, accept_objc_flag=false)
  	if test "X$accept_objc_flag" = "Xtrue"; then
	   AC_MSG_RESULT([yes])
           C_ONLY_FLAGS="-ObjC"
	else
	   dnl We are probebly trying to build with a non-Apple gcc,
	   dnl which is good as long as we do not try to build Cocoa
	   dnl code. We need an Apple compiler for just that (Objective C)
	   AC_MSG_RESULT([no])
	   AC_MSG_CHECKING([for a Cocoa compliant Objective C compiler])
	   SEARCHFOR=""
	   save_IFS=$IFS
	   IFS=:
	   set $PATH
	   IFS=$save_IFS
	   while test X"$1" != X""; do
	       dnl Add all possible paths to a real apple gcc
	       SEARCHFOR="$1/gcc-apple-4.2 $SEARCHFOR"
	       shift
	   done
	   dnl Add LLVM compilers, they will work in this case
	   SEARCHFOR="/usr/bin/clang /usr/bin/gcc $SEARCHFOR"	
	   APPLE_CC=""
	   dnl SEARCHFOR is reversed, so we want to find the last existing
	   dnl executable in the list
	   for x in $SEARCHFOR; do
	       if test -x $x; then
	       	  APPLE_CC=$x
	       fi
	   done	
	   if test X$APPLE_CC = X; then
	      AC_MSG_RESULT([no])
	      dnl Complete failure, we cannot build Cocoa code
	      if test X"$WX_BUILDING_INSIDE_ERLSRC" != X"true" ; then
		AC_MSG_ERROR([Can not find compiler to compile Cocoa applications])
	      else
		echo "Can not find compiler to compile Cocoa applications" > ./CONF_INFO
		WXERL_CAN_BUILD_DRIVER=false
		AC_MSG_WARN([Can not find compiler to compile Cocoa applications])
	      fi
	      WXERL_CAN_BUILD_DRIVER=false
	   else
	      dnl We think we found an Apple compiler and will add 
	      dnl Apple specific options
	      AC_MSG_RESULT($APPLE_CC)
	      OBJC_CC=$APPLE_CC
	      OBJC_CFLAGS="-ObjC"
	   fi
  	fi
  	CFLAGS=$saved_CFLAGS
	CPPFLAGS="$CPPFLAGS -D_MACOSX $PTHR_CFLAGS"
	;;
    mingw32)
	CFLAGS="$CFLAGS -DWIN32 -DWINVER=0x0500 -D_WINDOWS -D_UNICODE -DUNICODE"
	CPPFLAGS="$CPPFLAGS -D_WIN32_WINNT=0x0500"	
	AC_MSG_WARN([Reverting to 32-bit time_t])
	CPPFLAGS="$CPPFLAGS -D_USE_32BIT_TIME_T"
	;;
    win32)
	CFLAGS="$CFLAGS -DWIN32 -DWINVER=0x0500 -D_WINDOWS -D_UNICODE -DUNICODE"
	CPPFLAGS="$CPPFLAGS -D_WIN32_WINNT=0x0500"
	;;
    *)
	CFLAGS="$CFLAGS -Wno-deprecated-declarations"
	CPPFLAGS="$CPPFLAGS -D_GNU_SOURCE $PTHR_CFLAGS"
	;;
esac

AC_SUBST(OBJC_CC)
AC_SUBST(OBJC_CFLAGS)

case $host_os in
    darwin*)
	LDFLAGS="-bundle -flat_namespace -undefined warning -fPIC $LDFLAGS"
	# Check sizof_void_p as future will hold 64bit MacOS wx
	if test $ac_cv_sizeof_void_p = 4; then
	    LDFLAGS="-m32 $LDFLAGS"
	fi		   
	GL_LIBS="-framework OpenGL"
	;;
    win32)
        LDFLAGS="-dll $LDFLAGS"
	GL_LIBS="-lglu32 -lOpengl32"
	;;
    mingw32)
	LDFLAGS="-shared -fPIC $LDFLAGS"
	GL_LIBS="-lglu32 -lOpengl32"
	;;
    *)
	LDFLAGS="-shared -fPIC $LDFLAGS"
	GL_LIBS="-lGL -lGLU"
	;;
esac

dnl ----------------------------------------------------------------------
dnl Include CPPFLAGS in CFLAGS
dnl ----------------------------------------------------------------------

case $host_os in
    mingw32)
	DEBUG_CFLAGS="-g -Wall -DDEBUG $CFLAGS"
	CFLAGS="-g -Wall -O2 -fomit-frame-pointer -fno-strict-aliasing $CFLAGS"
	;;
    win32)
	DEBUG_CFLAGS="-g -Wall -DDEBUG $CFLAGS"
	CFLAGS="-g -Wall -O2 $CFLAGS"
	;;
    *)
	DEBUG_CFLAGS="-g -Wall -fPIC -DDEBUG $CFLAGS"
	CFLAGS="-g -Wall -O2 -fPIC -fomit-frame-pointer -fno-strict-aliasing $CFLAGS"
	;;
esac

dnl  
dnl  Opengl tests
dnl

if test X"$host_os" != X"win32" ; then
    AC_CHECK_HEADERS([GL/gl.h], [],
    	[AC_CHECK_HEADERS([OpenGL/gl.h])])
    if test X"$ac_cv_header_GL_gl_h" != Xyes &&
       	test X"$ac_cv_header_OpenGL_gl_h" != Xyes
    then
       	saved_CPPFLAGS="$CPPFLAGS"
	AC_MSG_NOTICE(Checking for OpenGL headers in /usr/X11R6)
       	CPPFLAGS="-isystem /usr/X11R6/include $CPPFLAGS"
       	$as_unset ac_cv_header_GL_gl_h
       	AC_CHECK_HEADERS([GL/gl.h])
       	if test X"$ac_cv_header_GL_gl_h" != Xyes ; then
	    AC_MSG_NOTICE(Checking for OpenGL headers in /usr/local)
	    CPPFLAGS="-isystem /usr/local/include $saved_CPPFLAGS"
	    $as_unset ac_cv_header_GL_gl_h
	    AC_CHECK_HEADERS([GL/gl.h])
	    if test X"$ac_cv_header_GL_gl_h" != Xyes ; then
		AC_MSG_WARN([No OpenGL headers found, wx will NOT be usable])
		CPPFLAGS="$saved_CPPFLAGS"
	    else 
	    	GL_LIBS="-L/usr/local/lib $GL_LIBS"
	    fi
	else 
    	    GL_LIBS="-L/usr/X11R6/lib $GL_LIBS"
       	fi
    fi
else
    AC_CHECK_HEADERS([gl/gl.h],[],[],[#include <windows.h>])
fi

if test X"$host_os" != X"win32" ; then
    AC_CHECK_HEADERS([GL/glu.h], [],
        [AC_CHECK_HEADERS([OpenGL/glu.h])])
    if test X"$ac_cv_header_GL_glu_h" != Xyes &&
        test X"$ac_cv_header_OpenGL_glu_h" != Xyes
    then
	AC_MSG_WARN([No GLU headers found, wx will NOT be usable])
    fi
else
    AC_CHECK_HEADERS([gl/glu.h],[],[],[#include <windows.h>])
fi

AC_SUBST(GL_LIBS)

CXXFLAGS="$CFLAGS $CPPFLAGS" 
CFLAGS="$CFLAGS $CPPFLAGS $C_ONLY_FLAGS"

DEBUG_CXXFLAGS="$DEBUG_CFLAGS $CPPFLAGS"
DEBUG_CFLAGS="$DEBUG_CFLAGS $CPPFLAGS $C_ONLY_FLAGS"
AC_SUBST(DEBUG_CFLAGS)
AC_SUBST(DEBUG_CXXFLAGS)


if test X"$WX_BUILDING_INSIDE_ERLSRC" != X"true" ; then
    AC_MSG_CHECKING(for erl)
    if test X$ERL != X; then 
       AC_MSG_RESULT([yes; using $ERL])
    else 
       type erl >/dev/null 2>&1
       if test $? -eq 0 ; then 
          ERL=erl
          AC_MSG_RESULT([yes])
       else 
	  AC_MSG_ERROR([Cannot find erl in path])
       fi
    fi
    AC_MSG_CHECKING(for erlc)
    if test X$ERLC != X; then 
       AC_MSG_RESULT([yes; using $ERLC])
    else 
       type erlc >/dev/null 2>&1
       if test $? -eq 0 ; then 
          ERLC=erlc
          AC_MSG_RESULT([yes])
       else 
	  AC_MSG_ERROR([Cannot find erlc in path])
       fi
    fi
    ERLANG_ROOT_DIR=`erl -noshell -eval 'io:format("~s~n",[[code:root_dir()]])' -s erlang halt`
    AC_MSG_NOTICE(ERL ROOT DIR: [$ERLANG_ROOT_DIR])
    ERLWX_VSN=`grep WX_VSN $srcdir/vsn.mk | sed 's/^.*=[ ]*//'`
else
    ERLC=erlc
    ERL=erl
    ERLANG_ROOT_DIR=$ERL_TOP
    AC_SUBST(ERLC)
fi

AC_SUBST(WX_BUILDING_INSIDE_ERLSRC)
AC_SUBST(ERLANG_ROOT_DIR)

dnl
dnl  Check for wxwidgets
dnl 
if test "$cross_compiling" = "yes"; then
    echo "Cross compilation of the wx driver is not supported yet, wx will NOT be usable" > ./CONF_INFO
    WXERL_CAN_BUILD_DRIVER=false
elif test  X"$MIXED_CYGWIN_VC" == X"no" -a X"$MIXED_MSYS_VC" == X"no"; then
    WX_VERSION=`wx-config --version`
    case $WX_VERSION in
         2.8.*)
		m4_include(wxwin-2.8.m4)
		;;
	 *)
		m4_include(wxwin-2.9.m4)
		;;
    esac

    AM_OPTIONS_WXCONFIG
    reqwx=2.8.4
    # Try to find debug libs first 
    # wxelibs=core,base,gl,aui,adv

    AC_MSG_CHECKING(for debug build of wxWidgets)

    AM_PATH_WXCONFIG($reqwx, wxWinWithGLDBG=1, wxWinWithGLDBG=0, [stc,xrc,html,adv,xml,core,base,gl,aui], [--unicode --debug=yes])
    DEBUG_WX_CFLAGS=$WX_CFLAGS
    DEBUG_WX_CXXFLAGS=$WX_CXXFLAGS
    DEBUG_WX_LIBS=$WX_LIBS
    DEBUG_WX_LIBS_STATIC=$WX_LIBS_STATIC
    AC_SUBST(DEBUG_WX_CFLAGS)
    AC_SUBST(DEBUG_WX_CXXFLAGS)
    AC_SUBST(DEBUG_WX_LIBS)
    AC_SUBST(DEBUG_WX_LIBS_STATIC)
    
    AC_MSG_CHECKING(for standard build of wxWidgets)
    AM_PATH_WXCONFIG($reqwx, wxWinWithGL=1, wxWinWithGL=0, [stc,xrc,html,adv,xml,core,base,gl,aui], [--unicode --debug=no])

    if test "x$WX_LIBS_STATIC" = "x"; then
       WX_HAVE_STATIC_LIBS=false
    else
       WX_HAVE_STATIC_LIBS=true
    fi

    wxWin=0

    if test "$wxWinWithGLDBG" = 1; then
        if test "$wxWinWithGL" = 1; then
    	   wxWin=1
	   FORCED_DEBUG_BUILD=false
    	else 
    	   wxWin=1
	   FORCED_DEBUG_BUILD=debug
	   WX_CFLAGS=$DEBUG_WX_CFLAGS
	   WX_CXXFLAGS=$DEBUG_WX_CXXFLAGS
	   WX_LIBS=$DEBUG_WX_LIBS
	   WX_LIBS_STATIC=$DEBUG_WX_LIBS_STATIC
	fi
    elif test "$wxWinWithGL" = 1; then
    	wxWin=1
	FORCED_DEBUG_BUILD=false
    fi    
    AC_SUBST(FORCED_DEBUG_BUILD)
    RC_FILE_TYPE=o
define(wx_warn_text,[
                wxWidgets must be installed on your system.
 
		Please check that wx-config is in path, the directory
		where wxWidgets libraries are installed (returned by
		'wx-config --libs' or 'wx-config --static --libs' command)
		is in LD_LIBRARY_PATH or equivalent variable and
		wxWidgets version is $reqwx or above.])

    if test "$wxWin" != 1; then
        if test X"$WX_BUILDING_INSIDE_ERLSRC" != X"true" ; then
	   AC_MSG_ERROR([wx_warn_text])
	else
	   echo "wxWidgets not found, wx will NOT be usable" > ./CONF_INFO
	   WXERL_CAN_BUILD_DRIVER=false
	   AC_MSG_WARN([wx_warn_text])
	fi
    fi
else
    AC_MSG_CHECKING(for wxWidgets in standard locations)
		  
    if test "x$MIXED_MSYS" = "xyes"; then
        CWXWIN_CONFIG=`win2msys_path.sh $wx_config_name 2>/dev/null`
    else
	CWXWIN_CONFIG=`cygpath $wx_config_name 2>/dev/null`
    fi
    CWXWIN1=`dirname $CWXWIN_CONFIG 2>/dev/null`
    CWXWIN2=`dirname $CWXWIN1 2>/dev/null`

    if test -z "$PROGRAMFILES" ; then 
       PROGRAMFILES=c:/Program Files
    fi

    
    if test "x$MIXED_MSYS" = "xyes"; then
        CWXWIN_PROG=`win2msys_path.sh "$PROGRAMFILES" 2>/dev/null`
    else
	CWXWIN_PROG=`cygpath -d "$PROGRAMFILES" | cygpath -f - 2>/dev/null`
    fi
    CWXWIN3=$CWXWIN_PROG/wxWidgets-2.8
    CWXWIN4=$CWXWIN_PROG/wxMSW-2.8
    CWX_DOCUMENTED="/opt/local/pgm/wxMSW-2.8.* /opt/local/pgm/wxWidgets-2.8.*"
    case $ac_cv_sizeof_void_p in
    	 8)
		CWX_DOCUMENTED="/opt/local64/pgm/wxMSW-2.8.* /opt/local64/pgm/wxWidgets-2.8.* $CWX_DOCUMENTED"
		;;
         *)
		true
		;;
    esac	
			
    CWXPATH="$CWXWIN1 $CWXWIN2 $CWX_DOCUMENTED $CWXWIN3.* $CWXWIN4.*"

    for dir in $CWXPATH; do
	if test -f $dir/include/wx/wx.h; then
	    WXINCLUDE_MSVC=$dir/include/msvc
	    WXINCLUDE_PLAIN=$dir/include
	    WXINCLUDE_CONTRIB=$dir/contrib/include
	    WX_CFLAGS="-EHsc -D_UNICODE -DUNICODE -I$WXINCLUDE_MSVC -I$WXINCLUDE_PLAIN -I$WXINCLUDE_CONTRIB -D__WXMSW__"
	    WX_CXXFLAGS="-TP $WX_CFLAGS"
	    WX_LIBDIR=$dir/lib/vc_lib
	    WX_RESCOMP="rc.sh -I$WXINCLUDE_PLAIN -D __WIN32__"
	    RC_FILE_TYPE=res
	    for lib in $WX_LIBDIR/wxbase*.lib $WX_LIBDIR2/wxbase*.lib; do
		maybe=`echo $lib | egrep 'wxbase[[0-9]]*u\.lib'`
		if test '!' -z "$maybe"; then
		   corelib_number=`echo $maybe | sed 's,.*\([[0-9]].\)u\.lib,\1,'`
		   break
		fi
	    done
	    if test '!' -z "$corelib_number"; then
		WXLIBNO=$corelib_number
		WX_LIBS0="wxmsw${WXLIBNO}u_stc wxmsw${WXLIBNO}u_xrc wxmsw${WXLIBNO}u_html"
		WX_LIBS1="wxmsw${WXLIBNO}u_adv wxbase${WXLIBNO}u_xml wxmsw${WXLIBNO}u_core"
		WX_LIBS2="wxbase${WXLIBNO}u wxmsw${WXLIBNO}u_gl wxmsw${WXLIBNO}u_aui"
		WX_LIBS3="wxregexu wxexpat wxtiff wxjpeg wxpng wxzlib"
		WX_SYSLIBS="winspool winmm oleaut32 ole32 gdiplus"
		WX_LIBS_STATIC="-L$WX_LIBDIR"
		for lib in $WX_LIBS0 $WX_LIBS1 $WX_LIBS2 $WX_LIBS3 $WX_SYSLIBS; do
		   WX_LIBS_STATIC="$WX_LIBS_STATIC -l$lib"
		done
		AC_MSG_RESULT([$dir])
		break
	    fi
	fi
    done
    if test -z "$WX_LIBS_STATIC"; then
	AC_MSG_RESULT([failed])
        if test X"$WX_BUILDING_INSIDE_ERLSRC" != X"true" ; then
		AC_MSG_ERROR([Cannot find core lib version for wxWidgets])
	else
		echo "No usable wxWidgets not found, wx will not be useable" > ./CONF_INFO
		WXERL_CAN_BUILD_DRIVER=false
		AC_MSG_WARN([Cannot find core lib version for wxWidgets])
	fi
    fi
    WX_HAVE_STATIC_LIBS=true
    AC_SUBST(WX_CFLAGS)
    AC_SUBST(WX_CXXFLAGS)
    AC_SUBST(WX_LIBS_STATIC)
    AC_SUBST(WX_RESCOMP)
fi

if test "$WXERL_CAN_BUILD_DRIVER" != "false"; then

AC_SUBST(WX_HAVE_STATIC_LIBS)
AC_SUBST(RC_FILE_TYPE)

AC_MSG_CHECKING(if wxwidgets have opengl support)
AC_LANG_PUSH(C++)
saved_CXXFLAGS=$CXXFLAGS
CXXFLAGS="$CXXFLAGS $WX_CXXFLAGS"

AC_COMPILE_IFELSE(
 [AC_LANG_PROGRAM([[
  #ifdef WIN32
  # include <windows.h>
  #endif
  #include "wx/wx.h"
  ]], [[
  #if wxUSE_GLCANVAS
  ;
  #else
  #error barf
  #endif
  ]])],
[HAVE_GL_SUPPORT=yes
 AC_DEFINE(HAVE_GL_SUPPORT, [1], [Define if wxwidgets have gl support])],
 HAVE_GL_SUPPORT=no)

CXXFLAGS=$saved_CXXFLAGS
AC_LANG_POP(C++)
AC_MSG_RESULT($HAVE_GL_SUPPORT)
AC_SUBST(HAVE_GL_SUPPORT)

if test X"$HAVE_GL_SUPPORT" != X"yes" ; then
   		echo "wxWidgets don't have gl support, wx will NOT be useable" > ./CONF_INFO
		WXERL_CAN_BUILD_DRIVER=false
fi

dnl Check for GLintptr
dnl We define the types glext.h so we don't depend on glext.h is updated
dnl to the latest version, but some systems defined them in gl.h 
dnl i.e Darwin and Solaris.

AC_CHECK_TYPES([GLintptr, GLintptrARB, GLchar, 
                GLcharARB, GLhalfARB, GLint64EXT], 
	       [], [], 
	       [#ifdef WIN32
		# include <windows.h>
	        # include <gl/gl.h>
	        #elif defined(HAVE_GL_GL_H)
	        # include <GL/gl.h>
                #elif defined(HAVE_OPENGL_GL_H)
		# include <OpenGL/gl.h>
		#endif 
		])
 
dnl

AC_MSG_CHECKING(GLU Callbacks uses Tiger Style)
AC_LANG_PUSH(C++)
saved_CXXFLAGS=$CXXFLAGS
CXXFLAGS="$CXXFLAGS $WX_CXXFLAGS"

AC_TRY_COMPILE([
                #ifdef WIN32
		# include <windows.h>
	        # include <gl/glu.h>
	        #elif defined(HAVE_GL_GL_H)
	        # include <GL/glu.h>
                #elif defined(HAVE_OPENGL_GL_H)
		# include <OpenGL/glu.h>
		#endif 
		#ifndef CALLBACK
		# define CALLBACK
		#endif
		void CALLBACK foo() {};
	       ],
		[
		   GLUtesselator* tess;                   
		   gluTessCallback(tess,GLU_TESS_VERTEX,(GLvoid (*)(...)) foo);
		],
		[TESS_CB_TIGER_STYLE=yes
		 AC_DEFINE(TESS_CB_TIGER_STYLE, [1], [GLU Callbacks are Tiger style])],
		TESS_CB_TIGER_STYLE=no)
AC_MSG_RESULT($TESS_CB_TIGER_STYLE)
AC_SUBST(TESS_CB_TIGER_STYLE)

dnl 
dnl Check that we wx have stc (wxStyledTextControl) headers 
dnl 

AC_CHECK_HEADERS([wx/stc/stc.h],
        [],
    	[WXERL_CAN_BUILD_DRIVER=false
	 echo "wxWidgets don't have wxStyledTextControl (stc.h), wx will NOT be useable" > ./CONF_INFO
     	 AC_MSG_WARN([Can not find wx/stc/stc.h $CXXFLAGS])      
     	],
	[#ifdef WIN32
	 # include <windows.h>
         #endif
         #include "wx/wx.h"])

dnl 
dnl Check that we can link all the libraries
dnl 

AC_MSG_CHECKING(if we can link wxwidgets programs)
saved_LIBS=$LIBS

if test X"$WX_HAVE_STATIC_LIBS" = X"true" ; then
   LIBS=$WX_LIBS_STATIC   
fi

AC_LINK_IFELSE([AC_LANG_SOURCE([
		#ifdef WIN32
		# include <windows.h>
	        # include <gl/gl.h>
	        #elif defined(HAVE_GL_GL_H)
	        # include <GL/gl.h>
                #elif defined(HAVE_OPENGL_GL_H)
		# include <OpenGL/gl.h>
		#endif 
                #include "wx/wx.h"
		#include "wx/stc/stc.h"
		#include "wx/glcanvas.h"
		])

		class MyApp : public wxApp
		{
		   virtual bool OnInit() {
		   	   // Test that we have a FromUTF8
			   // it isn't in too old wxWidgets versions
		   	   wxString test = wxString::FromUTF8((const char *)"foo");
		   	   wxStyledTextCtrl * foo = new wxStyledTextCtrl();
		   	   wxGLCanvas * bar = new wxGLCanvas((wxWindow *) NULL, -1, 
			   	      wxDefaultPosition,wxDefaultSize,0);
			   return true;
                  };
		};
		IMPLEMENT_APP(MyApp)
	       ],
	       [
		CAN_LINK_WX=yes
	       ],
	       [
		CAN_LINK_WX=no
	       ])

CXXFLAGS=$saved_CXXFLAGS
LIBS=$saved_LIBS
AC_LANG_POP(C++)
AC_MSG_RESULT($CAN_LINK_WX)

if test X"$CAN_LINK_WX" != X"yes" ; then
   		echo "Can not link the wx driver, wx will NOT be useable" > ./CONF_INFO
		WXERL_CAN_BUILD_DRIVER=false
		AC_MSG_WARN([Can not link wx program are all developer packages installed?])
fi

fi dnl - if test "$WXERL_CAN_BUILD_DRIVER" != "false"

AC_SUBST(WXERL_CAN_BUILD_DRIVER)

#############################################################################

dnl 

case $host_os in
    mingw32|win32)
	RUN_ERL=werl
	SO_EXT=.dll;;
    *)
	RUN_ERL=erl
	SO_EXT=.so;;
esac

AC_SUBST(SO_EXT)
AC_SUBST(RUN_ERL)


if test X"$WX_BUILDING_INSIDE_ERLSRC" != X"true" ; then
    dnl  Find driver directory name according to erlang
    WXERL_SYS_TYPE=`erl -noshell -eval 'io:format("~s~n",[[erlang:system_info(system_architecture)]])' -s erlang halt`
else
    WXERL_SYS_TYPE=$TARGET
fi

AC_SUBST(WXERL_SYS_TYPE)

mkdir -p $WXERL_SYS_TYPE
CONFIG_STATUS=$WXERL_SYS_TYPE/config.status

dnl

if test X"$WX_BUILDING_INSIDE_ERLSRC" != X"true" ; then
   if test X"$WXERL_CAN_BUILD_DRIVER" != X"true" ; then 
      AC_MSG_ERROR([Cannot build wxErlang driver, see ./CONF_INFO for information])
   fi
fi

AC_CONFIG_FILES([ 
	config.mk
	c_src/Makefile 
        ])

AC_OUTPUT

if test X"$WX_BUILDING_INSIDE_ERLSRC" != X"true" ; then
   AC_MSG_NOTICE()
   AC_MSG_NOTICE(--------------------------------------------------)
   AC_MSG_NOTICE(Using erlang compiler: [$ERLC])
   AC_MSG_NOTICE(wxErlang Install in: [$ERLANG_ROOT_DIR/lib/wx-$ERLWX_VSN])
   AC_MSG_NOTICE(Erlang driver in priv/[$WXERL_SYS_TYPE]/wxe_driver[$SO_EXT])
   AC_MSG_NOTICE(--------------------------------------------------)
fi

if test X"$WX_BUILDING_INSIDE_ERLSRC" = X"true" ; then
   CORES=`ls core* 2>/dev/null`
   if test X"$CORES" != X"" ; then
      echo "Configure dumped core files" > ignore_core_files
   fi
fi