dnl     Autoconf requirements
dnl     `AC_INIT(PACKAGE, VERSION, BUG-REPORT-ADDRESS)'
dnl     information on the package
dnl     checks for programs
dnl     checks for libraries
dnl     checks for header files
dnl     checks for types
dnl     checks for structures
dnl     checks for compiler characteristics
dnl     checks for library functions
dnl     checks for system services
dnl     `AC_CONFIG_FILES([FILE...])'
dnl     `AC_OUTPUT'


dnl program

AC_INIT([bist],[0.5.2],[cage@katamail.com],[bist])
AC_CONFIG_MACRO_DIR([m4])
AC_USE_SYSTEM_EXTENSIONS

AM_INIT_AUTOMAKE([-Wall foreign])


AM_GNU_GETTEXT([external])


AC_SUBST([VERSION],AC_PACKAGE_VERSION)
AC_SUBST([TARNAME],AC_PACKAGE_TARNAME)
AC_SUBST([ELF],AC_PACKAGE_NAME)
AC_LANG([C++])

AC_PROG_CXX([g++-4.6 g++-4.5 g++-4.4 g++-4.3 g++-4.2 g++ c++])

AC_PATH_PROG([SHELL],[bash])

AC_LIBTOOL_DLOPEN
AC_DISABLE_STATIC

AC_PROG_LIBTOOL


AC_CHECK_PROG(INSTALL, [install], [TRUE],
          [FALSE], [$PATH], [])

AC_CHECK_PROG([MSGFMT], [msgfmt], [TRUE],
          [FALSE], [$PATH:/usr/libexec:/usr/sbin:/usr/etc:etc], [])

AC_CHECK_PROG([PYTHON], [python], [TRUE],
          [FALSE], [$PATH:/usr/libexec:/usr/sbin:/usr/etc:etc], [])

AC_CHECK_PROG([PKGCONFIG], [pkg-config], [TRUE],
          [FALSE], [$PATH:/usr/libexec:/usr/sbin:/usr/etc:etc], [])

AC_CHECK_PROG([FLTKCONFIG], [fltk-config], [TRUE],
          [FALSE], [$PATH:/usr/libexec:/usr/sbin:/usr/etc:etc], [])


if [[ $FLTKCONFIG = "FALSE" ]]; then
	AC_MSG_ERROR([cannot find fltk-config!], [1])
fi

if [[ $PKGCONFIG = "FALSE" ]]; then
	AC_MSG_ERROR([cannot find pkg-config!], [1])
fi

if [[ $PYTHON = "FALSE" ]]; then
	AC_MSG_WARN([cannot find python interpreter but do not worry it is not not mandatory to run bist!], [1])
fi

if [[ $MSGFMT = "FALSE" ]]; then
	AC_MSG_WARN([cannot find msgfmt, this is a problem only if you want to update traslations files!], [1])	
fi

AC_CHECK_PROG([CTAGS_PROG], [ctags], [TRUE],
          [FALSE], [$PATH:/usr/libexec:/usr/sbin:/usr/etc:etc], [])

AC_SUBST([HOME],[$HOME])

AC_ARG_ENABLE([static], 
	[AC_HELP_STRING([--enable-static],[enable static linking with fltk. Default: disabled])], 
	[AC_SUBST([CSTATIC],[TRUE])],
        [AC_SUBST([CSTATIC],[FALSE])])


if [[[ "$enable_static" = "yes" ]]]; then
    AC_SUBST([LIBS],[$LIBS\ `fltk-config --use-images --ldstaticflags`])
else
    AC_SUBST([LIBS],[$LIBS\ `fltk-config --use-images --ldflags`])
fi

AC_MSG_CHECKING([pkg-config --exists cairo])

pkg-config --exists cairo

if test $? -ne 0 ; then

AC_MSG_ERROR([pkg --exists cairo failed! do you have cairo header installed?]);

else
AC_MSG_RESULT([OK])
fi

AC_MSG_CHECKING([pkg-config --exists pangocairo])

pkg-config --exists pangocairo

if test $? -ne 0 ; then

AC_MSG_ERROR([pkgconfig --exists pangocairo failed! do you have pango header installed?]);

else
AC_MSG_RESULT([OK])
fi


AC_SUBST([LIBS],[$LIBS\ `pkg-config  --libs cairo`])

AC_SUBST([LIBS],[$LIBS\ `pkg-config --libs pangocairo`])


AC_ARG_ENABLE([debug], 
	[AC_HELP_STRING([--enable-debug],[enable debugging output. Default: disabled])], 
	[AC_SUBST([DEBUG],[DEBUG])],
        [AC_SUBST([DEBUG],[NODEBUG])])

dnl AC_SUBST([CPPFLAGS],[$CPPFLAGS\ \-rdynamic])


AC_SUBST([CPPFLAGS],[$CPPFLAGS\ -I./pluginclude\ -I.\ -I./include\ -I../images\ -pedantic\ `fltk-config --cxxflags`])

AC_SUBST([CPPFLAGS],[$CPPFLAGS\ `pkg-config --cflags cairo`])

AC_SUBST([CPPFLAGS],[$CPPFLAGS\ `pkg-config --cflags pangocairo`])

opt=`echo "$CPPFLAGS" | grep  Wall`

if [[[ $opt = "" ]]]; then
AC_SUBST([CPPFLAGS],[$CPPFLAGS\ -Wall])	
fi


opt=`echo "$CXXFLAGS" | grep  "O[[0-9]]"`

if [[[ $opt = "" ]]]; then
AC_SUBST([CXXFLAGS],[$CXXFLAGS\ -O3])	
fi

opt=`echo "$CXXFLAGS" | grep  "\-g"`

if [[[ $opt = "" ]]]; then
AC_SUBST([CXXFLAGS],[$CXXFLAGS\ -g])	
fi

AC_SUBST([CXXFLAGS],[$CXXFLAGS\ -rdynamic])


dnl header

AC_CHECK_HEADERS([ncurses.h],[AC_SUBST([USE_CURSES],[TRUE])],[AC_SUBST([USE_CURSES],[FALSE])])

AC_CHECK_HEADERS([X11/xpm.h],[],[AC_MSG_ERROR([cannot find xpm.h header file!])])

AC_CHECK_HEADERS([libintl.h],[],[AC_MSG_ERROR([cannot find libintl.h header file!])])
AC_CHECK_HEADERS([cairo/cairo.h],[],[AC_MSG_ERROR([cannot find cairo.h header file!])])


dnl checking for pointer size

AC_CHECK_SIZEOF([int *])


if [[ $USE_CURSES = "TRUE" ]]; then
   AC_SUBST([LIBS],[$LIBS\ -lncurses])
fi



opt=`echo "$LIBS" | grep  fltk-images`

if [[[ $opt = "" ]]]; then
AC_SUBST([LIBS],[$LIBS\ -lfltk_images])	
fi

opt=`echo "$LIBS" | grep  lpthread`

if [[[ $opt = "" ]]]; then
AC_SUBST([LIBS],[$LIBS\ -lpthread])	
fi

AC_MSG_CHECKING([if fltk library works])
AC_RUN_IFELSE([AC_LANG_PROGRAM([[#include <FL/Fl.H>
#include <FL/Fl_Window.H>]],
[[Fl::run();]])],
[AC_MSG_RESULT([it works!])], 
[AC_MSG_ERROR([fltk do not works], [1])],
[AC_MSG_ERROR([fltk do not works], [1])])


dnl AC_MSG_CHECKING([if libplot library works])

dnl AC_SUBST([LIBS],[$LIBS\ -lplot])

dnl AC_RUN_IFELSE([#include <cstdio>
dnl #include <plot.h>
dnl int main(int argc, char** argv){ plPlotterParams *plotter_params;
dnl     /* set a Plotter parameter */
dnl     plotter_params = pl_newplparams ();}],
dnl [AC_MSG_RESULT([it works!])],
dnl [AC_MSG_ERROR([libplot do not works], [1])],
dnl [AC_MSG_ERROR([libplot do not works], [1])])




AC_MSG_CHECKING([if expat library works])

AC_SUBST([LIBS],[$LIBS\ -lexpat])

AC_RUN_IFELSE([AC_LANG_PROGRAM([#include <cstdio>
#include <expat.h>],
[XML_Parser p = XML_ParserCreate(NULL)])],
[AC_MSG_RESULT([it works!])],
[AC_MSG_ERROR([libexpat do not works], [1])],
[AC_MSG_ERROR([libexpat do not works], [1])])




AC_SUBST([LIBS],[$LIBS\ -ldl])



AC_MSG_CHECKING([if dl library works])
AC_RUN_IFELSE([AC_LANG_PROGRAM([#include <dlfcn.h>],
[dlerror()])],
[AC_MSG_RESULT([it works!])],
[AC_MSG_ERROR([libdl do not works], [1])],
[AC_MSG_ERROR([libdl do not works], [1])])

AC_MSG_CHECKING([if cairo library works])
AC_RUN_IFELSE([AC_LANG_PROGRAM([#include <cairo/cairo.h>],
[cairo_t* cr])],
[AC_MSG_RESULT([it works!])],
[AC_MSG_ERROR([libcairo do not works], [1])],
[AC_MSG_ERROR([libcairo do not works], [1])])



AC_CHECK_LIB([Xpm],[XpmCreatePixmapFromData], [AC_SUBST([LIBS],[$LIBS\ -lXpm])],
          [AC_MSG_ERROR([libXpm do not works], [1])], [])

AC_CHECK_LIB([X11],[XOpenDisplay], [],
             [AC_MSG_ERROR([no!], [1])], [])





AC_SUBST([LIB_DIR_CONFIG],[$libdir])


dnl expanding exec_prefix

PATTERN_EXEC_PREFIX="exec_prefix(.*)"
PATTERN_GET_LAST_DIR="/.*$"
if [[[ "$LIB_DIR_CONFIG" =~ $PATTERN_EXEC_PREFIX ]]]; then
   if [[[ $exec_prefix = "NONE" ]]]; then
      [[[ "$LIB_DIR_CONFIG"  =~  $PATTERN_GET_LAST_DIR ]]];
      if [[[ $prefix = "NONE" ]]]; then
        ac_libdir=/usr/local$BASH_REMATCH
        AC_SUBST([LIB_DIR_CONFIG_PATH],[$ac_libdir])
dnl        echo "LIBDIRR_NONE!!   $ac_libdir --"
      else
       ac_libdir=$prefix$BASH_REMATCH
        AC_SUBST([LIB_DIR_CONFIG_PATH],[$ac_libdir])
dnl        echo "prefix yes eprefix no!!   $ac_libdir --"
      fi
   else
      [[[ "$LIB_DIR_CONFIG"  =~ $PATTERN_GET_LAST_DIR ]]];
      ac_libdir=$exec_prefix$BASH_REMATCH
      AC_SUBST([LIB_DIR_CONFIG_PATH],[$ac_libdir])
dnl      echo "LIBDIRR  eprefix yes  $LIB_DIR_CONFIG"
   fi
fi




AC_CONFIG_FILES([Makefile po/Makefile.in src/Makefile])
AC_CONFIG_HEADER([include/config.h])

AC_CONFIG_SUBDIRS([pluginsrc/TREcalc])
AC_CONFIG_SUBDIRS([pluginsrc/fetch_nist_database])
AC_OUTPUT	













