#MIN_CONFIG(3.2.0)

KDE_ENABLE_HIDDEN_VISIBILITY

AM_INIT_AUTOMAKE(ktechlab, 0.3)
AC_C_BIGENDIAN
AC_CHECK_KDEMAXPATHLEN


###################################
## BEGIN Check for gpsim version ##
###################################

AC_LANG_SAVE
AC_LANG_CPLUSPLUS
ktechlab_save_CXXFLAGS="$CXXFLAGS"

AC_ARG_WITH(glib12, [  --with-glib12           force using glib 1.2.x [default=no]])
if test "x$with_glib12" != "xyes" ; then
	glib_cflags=`pkg-config --cflags glib-2.0`
else
	glib_cflags=`glib-config --cflags`
fi
AC_SUBST(glib_cflags)

CXXFLAGS="$glib_cflags"

AC_MSG_CHECKING([for gpsim 0.21.4 availability])
AC_TRY_COMPILE(
	[
#include <gpsim/interface.h>
#include <gpsim/gpsim_interface.h>
#include <gpsim/breakpoints.h>
#include <gpsim/gpsim_time.h>
void func() { (void)cycles; (void)initialize_gpsim_core(); (void)load_symbol_file(0,0); }
	],
	[],
	AC_MSG_RESULT( yes )
	have_gpsim_0_21_4=yes,
	AC_MSG_RESULT( no )
)

AC_MSG_CHECKING([for gpsim 0.21.11 availability])
AC_TRY_COMPILE(
	[
#include <gpsim/interface.h>
#include <gpsim/gpsim_interface.h>
#include <gpsim/breakpoints.h>
#include <gpsim/gpsim_time.h>
#include <gpsim/symbol.h>
#include <gpsim/program_files.h>
void func() { (void)cycles; (void)initialize_gpsim_core(); }
	],
	[],
	AC_MSG_RESULT( yes )
	have_gpsim_0_21_11=yes,
	AC_MSG_RESULT( no )
)

AC_MSG_CHECKING([for gpsim 0.21.12 availability])
AC_TRY_COMPILE(
	[
#include <gpsim/ValueCollections.h>
	],
	[],
	AC_MSG_RESULT( yes )
	have_gpsim_0_21_12=yes,
	AC_MSG_RESULT( no )
)

AC_MSG_CHECKING([for gpsim 0.27.0 availability])
AC_TRY_COMPILE(
	[
#include <gpsim/pic-processor.h>
void func() { pic_processor *Processor; sizeof(Processor->Wreg); }
	],
	[],
	AC_MSG_RESULT( yes )
	have_gpsim_0_27_0=yes,
	AC_MSG_RESULT( no )
)

CXXFLAGS="$glib_cflags $ktechlab_save_CXXFLAGS"

AC_MSG_CHECKING([if gpsim requires C++11])
AC_TRY_COMPILE(
	[
#include <gpsim/gpsim_object.h>
void func() { gpsimObject *gpsimObj; sizeof(gpsimObj); }
	],
	[],
	AC_MSG_RESULT( no ),
	have_gpsim_0_31_0=yes
	GPSIM_CXXFLAGS="-std=c++11"
	AC_MSG_RESULT( yes )
)

CXXFLAGS="$ktechlab_save_CXXFLAGS"
AC_LANG_RESTORE

#################################
## END Check for gpsim version ##
#################################



################################
## BEGIN DO_NOT_COMPILE CHECK ##
################################

if test x$have_gpsim_0_21_12 == xyes; then
	AC_DEFINE([GPSIM_0_21_12],[],[Gpsim 0.21.12 was found])
	CXXFLAGS="$CXXFLAGS $USE_EXCEPTIONS"
else
	if test x$have_gpsim_0_21_4 != xyes; then
		if test x$have_gpsim_0_21_11 != xyes; then
			AC_DEFINE([NO_GPSIM],[],[Gpsim was not found])
			LIB_GPSIM=""
		else
			AC_DEFINE([GPSIM_0_21_11],[],[Gpsim 0.21.11 was found])
		fi
	else
		AC_DEFINE([GPSIM_0_21_4],[],[Gpsim 0.21.4 was found])
	fi
fi

if test x$have_gpsim_0_21_4 == xyes || test x$have_gpsim_0_21_11 == xyes || test x$have_gpsim_0_21_12 == xyes; then
	wi_cv_lib_readline=no
	ac_save_LIBS="$LIBS"
	# Note: $LIBCURSES is permitted to be empty.
	for LIBREADLINE in "-lreadline" "-lreadline $LIBCURSES" "-lreadline -ltermcap" "-lreadline -lncurses" "-lreadline -lcurses"
	do
		LIBS="$ac_save_LIBS $LIBREADLINE"
		LIB_GPSIM="$LIBREADLINE -lgpsim -lgpsim_eXdbm -lgpsimcli -lgpsimgui"
		AC_TRY_RUN([
			/* program */
			#include <stdio.h>
			#include <stdlib.h>
		 
			main(int argc, char **argv)
			{
				/* Note:  don't actually call readline, since it may block;
				 * We just want to see if it (dynamic) linked in okay.
				 */
				if (argc == 0)	/* never true */
					readline(0);
				exit(0);
			}
		],[
		# action if true
			wi_cv_lib_readline=yes
		],[
			# action if false
			wi_cv_lib_readline=no
	],[
		# action if cross compiling
		wi_cv_lib_readline=no
	])
	
		if test "$wi_cv_lib_readline" = yes ; then break ; fi
	done

	LIBS="$ac_save_LIBS"
fi

if test x$have_gpsim_0_27_0 == xyes; then
	AC_DEFINE([GPSIM_0_27_0],[],[Gpsim 0.27.0 was found])
fi

if test x$have_gpsim_0_31_0 == xyes; then
	AC_DEFINE([GPSIM_0_31_0],[],[Gpsim 0.31.0 was found])
fi

AC_SUBST(LIB_GPSIM)
AC_SUBST(GPSIM_CXXFLAGS)

##############################
## END DO_NOT_COMPILE CHECK ##
##############################

