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

AC_INIT(lib/src/version.h)
AC_PREREQ(2.12)
AC_CONFIG_HEADER(config.h)
AC_CONFIG_AUX_DIR($srcdir/tools)

PACKAGE=gretl

VERSION=`grep const $srcdir/lib/src/version.h | sed -e 's/^.*"\(.*\)";$/\1/'`
LIBGRETL_CURRENT=`grep LIBGRETL_CURRENT $srcdir/lib/src/version.h | awk '{ print $NF }'`
LIBGRETL_REVISION=`grep LIBGRETL_REVISION $srcdir/lib/src/version.h | awk '{ print $NF }'`
LIBGRETL_AGE=`grep LIBGRETL_AGE $srcdir/lib/src/version.h | awk '{ print $NF }'`
LIBVERSION="$LIBGRETL_CURRENT.$LIBGRETL_AGE.$LIBGRETL_REVISION"

AC_DEFINE_UNQUOTED(PACKAGE, "$PACKAGE")
AC_DEFINE_UNQUOTED(VERSION, "$VERSION")
AC_DEFINE_UNQUOTED(LIBVERSION, "$LIBVERSION")
AC_SUBST(PACKAGE)
AC_SUBST(VERSION)
AC_SUBST(LIBVERSION)

AC_SUBST(LIBGRETL_CURRENT)
AC_SUBST(LIBGRETL_REVISION)
AC_SUBST(LIBGRETL_AGE)

echo "configuring for $PACKAGE $VERSION (library version $LIBVERSION)"

dnl
dnl Variables controlling the gretl build
dnl
build_gui="yes"
have_readline="no"
new_readline="no"
have_zlib="no"
have_libole2="NA"
have_gtk="no"
have_gtksourceview="no"
have_gnome="no"
have_gtkextra="NA"
have_gmp="no"
have_lapack="no"
seek_inet_addr="no"
png_comments="yes"
highlight="yes"
prefer_xml1="no"
have_tramo="yes"
have_x12a="yes"
have_audio="no"
have_gnu_regex="no"
have_libxml2="no"
use_gtksourceview="no"
have_xslt="no"
MAKE="make"
XML_LIBS=

AC_CHECK_HEADERS(libintl.h,
	try_nls="yes",
	try_nls="no"
)

if test [ "$try_nls" = "yes" ]
then
   AM_GNU_GETTEXT([external])
fi

dnl
dnl Process options supplied to the configure script
dnl
AC_ARG_ENABLE(gui,
[  --enable-gui		  Enable gui [default=yes]],
[if test "$enableval" = "yes"
then
  build_gui=yes
else
  build_gui=no
fi])

AC_ARG_ENABLE(png-comments,
[  --enable-png-comments   Enable reading of PNG comment data [default=auto]],
[if test "$enableval" = "yes"
then
  png_comments=yes
else
  png_comments=no
fi])

AC_ARG_ENABLE(highlight,
[  --enable-highlight   Syntax highlighting for scripts (gtk2 only) [default=yes]],
[if test "$enableval" = "yes"
then
  highlight=yes
else
  highlight=no
fi])

AC_ARG_WITH(gmake,
[  --with-gmake         Use GNU make explicitly [default=no]],
[ case "$withval" in
  yes)
        MAKE="gmake"
        ;;
  *)
        ;;
  esac ],
)
AC_SUBST(MAKE)

AC_ARG_WITH(readline,
[  --with-readline        Build with libreadline support [default=auto]],
if test "x${withval}" = "xno"
then
  try_readline=no
else
  try_readline=yes
fi,
try_readline=yes)

AC_ARG_WITH(gtk2,
[  --with-gtk2            Build with gtk 2.0 support [default=auto]],
if test "x${withval}" = "xno"
then
  try_gtk2=no
else
  try_gtk2=yes
fi,
try_gtk2=yes)

AC_ARG_WITH(libxml1,
[  --with-libxml1          Prefer libxml1 to libxml2 [default=auto]],
if test "x${withval}" = "xyes"
then
  prefer_xml1=yes
fi,
prefer_xml1=no)

AC_ARG_WITH(gnome,
[  --with-gnome            Build with gnome support [default=auto]],
if test "x${withval}" = "xno"
then
  try_gnome=no
else
  try_gnome=yes
fi,
try_gnome=yes)

gnome_prefix="NA"
AC_ARG_WITH(gnome_install,
[  --with-gnome-install=DIR  where to install gnome-specific files [default=auto]],
if test -n "${withval}"
then
  gnome_prefix=${withval}
fi,)

AC_ARG_WITH(libole2,
[  --with-libole2   Use installed libole2 (gtk 1.2 only) [default=auto]],
if test "x${withval}" = "xno"
then
  try_ole=no
  have_libole2=no
else
  try_ole=yes
fi,
try_ole=yes)

AC_ARG_WITH(gtksourceview,
[  --with-gtksourceview   Use installed gtksourceview (gtk 2.0 only) [default=auto]],
if test "x${withval}" = "xno" || test "$try_gnome" = "no"
then
  try_installed_sourceview=no
else
  try_installed_sourceview=yes
fi,
try_installed_sourceview=yes)

AC_ARG_WITH(gtkextra,
[  --with-gtkextra   Use installed gtkextra (gtk 1.2 only) [default=auto]],
if test "x${withval}" = "xno"
then
  try_gtkextra=no
  have_gtkextra=no
else
  try_gtkextra=yes
fi,
try_gtkextra=yes)

AC_ARG_WITH(gmp,
[  --with-gmp   Use Gnu Multiple Precision library [default=auto]],
if test "x${withval}" = "xno"
then
  try_gmp=no
else
  try_gmp=yes
fi,
try_gmp=yes)

AC_ARG_WITH(x-12-arima,
[  --with-x-12-arima     include X-12-ARIMA support [default=yes]],
if test "$withval" = "no"
then
  have_x12a="no"
else
  AC_DEFINE(HAVE_X12A)
  HAVE_X12A=1
fi,
AC_DEFINE(HAVE_X12A)
HAVE_X12A=1)
AC_SUBST(HAVE_X12A)

AC_ARG_WITH(tramo-seats,
[  --with-tramo-seats    include TRAMO/SEATS support [default=yes]],
if test "$withval" = "no"
then
  have_tramo="no"
else
  AC_DEFINE(HAVE_TRAMO)
  HAVE_TRAMO=1
fi,
AC_DEFINE(HAVE_TRAMO)
HAVE_TRAMO=1)
AC_SUBST(HAVE_TRAMO)

AC_ARG_WITH(audio,
[  --with-audio    include audio graph support (experimental)],
if test "$withval" = "yes"
then
  AC_DEFINE(HAVE_AUDIO)
  HAVE_AUDIO=1
  AC_SUBST(HAVE_AUDIO)
  have_audio=yes
fi,
have_audio=no)

AC_PROG_CC

AC_DEFINE_UNQUOTED(OS_TYPE, "$host_os")
AC_PROG_INSTALL

AC_DISABLE_STATIC
AC_LIBTOOL_DLOPEN
AC_PROG_LIBTOOL

dnl We want these before the checks, so the checks can modify their values.
test -z "$CFLAGS" && CFLAGS= auto_cflags=1
test -z "$CC" && cc_specified=yes

dnl
dnl if the user hasn't specified CFLAGS, then
dnl   if compiler is gcc, then use -O2 and some warning flags
dnl
if test -n "$auto_cflags"; then
  if test -n "$GCC"; then
    CFLAGS="$CFLAGS -Wall"
  fi
fi

AC_PROG_LN_S

AC_CHECK_LIB(m, sin)
AC_CHECK_LIB(c, fopen)

dnl Checks for header files, etc.
AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS(unistd.h dirent.h fnmatch.h)
AC_C_CONST
AC_C_LONG_DOUBLE 
AC_CHECK_SIZEOF(int)
AC_TYPE_SIGNAL 
AC_C_BIGENDIAN 

dnl
dnl Check for libreadline for use with gretlcli
dnl
if test "$try_readline" = "yes" ; then
   AM_PATH_READLINE(have_readline="yes", have_readline="no")
fi

dnl
dnl See if we need to do anything special about "inet_addr"
dnl
INETLIB=''
AC_CHECK_LIB(c, inet_addr,,seek_inet_addr="yes",)
if test "$seek_inet_addr" = yes; then
  AC_CHECK_LIB(bind, inet_addr,INETLIB="-lbind",)
  if test "$INETLIB" != "-lbind"; then
    AC_CHECK_LIB(nsl, inet_addr, INETLIB="-lnsl",
    AC_MSG_ERROR(Can't find a definition of inet_addr),)
  fi
fi
AC_SUBST(INETLIB)

dnl
dnl Check for zlib
dnl
AC_CHECK_LIB(z, gzopen,have_zlib="yes" ; \
  ZLIB="-lz" ; AC_DEFINE(HAVE_ZLIB),,)
AC_SUBST(have_zlib)

dnl
dnl See if GMP lib is available
dnl
if test x"${try_gmp}" != xno ; then
  AM_PATH_GMP(4.0.1, have_gmp="yes" ; AC_DEFINE(ENABLE_GMP))
fi

dnl
dnl Check for LAPACK 
dnl Apple _will_ be different
dnl
LAPACK_CFLAGS="-I${srcdir}/lib/src"
case $host_os in
  *darwin*) 
    LAPACK_LIBS="-Wl,-framework -Wl,vecLib"
    AC_DEFINE(OSX_BUILD)
  ;; 
esac
AM_PATH_LAPACK(, have_lapack="yes")

dnl
dnl Check for FLITE?
dnl
if test "$have_audio" = "yes" ; then
  AM_PATH_FLITE(, 
    have_flite="yes"
    AC_DEFINE(HAVE_FLITE)
  )
fi

dnl
dnl Check for gnuplot, and its PNG capacity
dnl
AC_DEFUN([AC_PROG_GNUPLOT],
[AC_CHECK_PROG(GNUPLOT,gnuplot,yes)])

AC_PROG_GNUPLOT
test x"${GNUPLOT}" = xyes && AC_DEFINE(HAVE_GNUPLOT) 
if test x"${GNUPLOT}" = xyes ; then
  have_gnuplot=yes
fi

gnuplot_png=no
if test "$build_gui" = "yes" ; then
  AC_MSG_CHECKING(for PNG support in gnuplot)
  echo "set term png" | `which gnuplot` 2>/dev/null && gnuplot_png=yes
  if test "$gnuplot_png" = yes ; then
    AC_MSG_RESULT(yes)
    AC_DEFINE(GNUPLOT_PNG)
  else
    AC_MSG_RESULT(no)
    echo
    echo "* gretl needs PNG support in gnuplot."
    echo "* The current version of gnuplot is available from www.gnuplot.info"
    exit
  fi
fi

dnl
dnl Check for latex executable
dnl
AC_DEFUN([AC_PROG_LATEX],
[AC_CHECK_PROG(LATEX,latex,yes)])

AC_PROG_LATEX
test x"${LATEX}" = xyes && AC_DEFINE(HAVE_LATEX)
if test x"${LATEX}" = xyes ; then
  have_latex=yes
else
  have_latex=no
fi

dnl
dnl Preliminary check for pkg-config
dnl
if test "$try_gtk2" = "yes" ; then
   if test -z "$PKG_CONFIG"; then
      AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
   fi
   if test "$PKG_CONFIG" = "no" ; then
      echo "*** pkg-config not found, can't build with gtk 2.0" 
      try_gtk2="no"
   fi
fi

dnl
dnl Check for GTK 2.0, if appropriate
dnl
orig_CFLAGS=$CFLAGS
if test "$build_gui" = "yes" && test "$try_gtk2" = "yes" ; then
    PKG_CHECK_MODULES(GTK, gtk+-2.0,
      CFLAGS="$CFLAGS $GTK_CFLAGS"
      LIBS="$LIBS $GTK_LIBS"
      have_gtk="2.0"
      AC_DEFINE(USE_GTK2),
      have_gtk="no"      
    )
fi

dnl if the above succeeds, check separately for glib 2.0 flags
if test "$have_gtk" = "2.0" ; then
   PKG_CHECK_MODULES(GLIB, glib-2.0,,,)
fi

dnl ...and try for gtksourceview flags, unless prohibited
if test "$have_gtk" = "2.0" && test "$try_installed_sourceview" = "yes" ; then
     PKG_CHECK_MODULES(GTKSOURCEVIEW, gtksourceview-1.0,
     have_gtksourceview="yes"
     use_gtksourceview="yes"
     AC_DEFINE(USE_GTKSOURCEVIEW),
     have_gtksourceview="no"
    )
fi

dnl Fallback on supplied gtksourceview? Check for libxml2 and gtk >= 2.2.0
if test "$have_gtk" = "2.0" && test "$highlight" = "yes" && \
   test "$have_gtksourceview" = "no" ; then
     PKG_CHECK_MODULES(XML, libxml-2.0 >= 2.5.0,
     have_libxml2="yes",
     have_libxml2="no")
     PKG_CHECK_MODULES(GTK2, gtk+-2.0 >= 2.2.0,,
     highlight="no")
fi

dnl Now try using supplied gtksourceview if preconditions are met
if test "$highlight" = "yes" && \
   test "$have_gtksourceview" = "no" && \
   test "$have_libxml2" = "yes" ; then
     AC_CHECK_FUNCS([re_compile_pattern re_compile_fastmap re_search re_match],
     AC_DEFINE(USE_GTKSOURCEVIEW)
     use_gtksourceview="yes",
     use_gtksourceview="no")
fi

dnl Or fall back on GTK 1.2
if test "$build_gui" = "yes" && test "$have_gtk" = "no" ; then
   AM_PATH_GTK(1.2.3,have_gtk="1.2",have_gtk="no")
   if test "$have_gtk" = "no" ; then
      exit
   fi
   CFLAGS="$CFLAGS $GTK_CFLAGS"
   LIBS="$LIBS $GTK_LIBS"
   GLIB_CFLAGS=`glib-config --cflags`
   GLIB_LIBS=`glib-config --libs`
   if test $try_gtkextra = yes ; then
      AM_PATH_GTK_EXTRA(0.99.15,have_gtkextra="yes",have_gtkextra="no")
   fi
fi

dnl record the results of the above tests
AC_SUBST(build_gui)
AC_SUBST(have_gtk)
AC_SUBST(have_gmp)
AC_SUBST(have_gtksourceview)
AC_SUBST(use_gtksourceview)
AC_SUBST(have_audio)
AC_SUBST(GLIB_CFLAGS)
AC_SUBST(GLIB_LIBS)
AC_SUBST(GTKSOURCEVIEW_CFLAGS)
AC_SUBST(GTKSOURCEVIEW_LIBS)

dnl
dnl Should we try to use an installed version of libole2?
dnl
if test $try_ole = "yes" && test $have_gtk = "1.2" ; then
    AM_PATH_LIBOLE2(0.2.4,
        have_libole2="yes",
        have_libole2="no"
    )
fi

dnl
dnl Check for libpng (first with pkg-config, then fallback)
dnl
if test $build_gui = "no" ; then
   png_comments = "no"
fi

if test $png_comments = "yes" ; then
    PKG_CHECK_MODULES(PNG, libpng,
        png_failed="no",
	png_failed="yes"
   )
fi
if test $png_comments = "yes" && test $png_failed = "yes" ; then
    PKG_CHECK_MODULES(PNG, libpng12,
        png_failed="no",
	png_failed="yes"
   )
fi
if test $png_comments = "yes" && test $png_failed = "yes" ; then
   AC_CHECK_LIB(png, png_init_io,png_failed="no",png_failed="yes",-lz)
   PNG_CFLAGS=
   PNG_LIBS="-lz -lpng"
   AC_SUBST(PNG_CFLAGS)
   AC_SUBST(PNG_LIBS)
fi

if test $png_comments = "yes" ; then
  if test $png_failed = "yes" ; then
     echo "* gretl wants but did not find libpng."
     echo "* You can turn off this dependency by configuring with the option"
     echo "* --disable-png-comments"
     exit
  else
    AC_DEFINE(PNG_COMMENTS)
  fi
fi

dnl
dnl Check for gdk-pixbuf support
dnl
if test ${have_gtk} = "2.0" ; then
    PKG_CHECK_MODULES(GDK_PIXBUF, gdk-pixbuf-2.0 >= 0.7.0,
        pixbuf_failed="no",
	pixbuf_failed="yes"
   )
fi
if test ${have_gtk} = "1.2" ; then
    AM_PATH_GDK_PIXBUF(,
        pixbuf_failed="no",
        pixbuf_failed="yes"
    )
fi

if test ${pixbuf_failed} = "yes" ; then
    echo "* gretl wants but did not find the gdk-pixbuf library for PNG graphs."
    exit
fi

dnl
dnl Check for gnome, if we haven't been told not to
dnl
if test "$build_gui" = "yes" && test "$try_gnome" = "yes" ; then
dnl If we have gtk 2.0, test for gnome 2.0
    if test "$have_gtk" = "2.0" ; then
        PKG_CHECK_MODULES(GNOME, libgnomeui-2.0 libgnomeprint-2.2 libgnomeprintui-2.2 gconf-2.0,
           AC_DEFINE(USE_GNOME)
           have_gnome="2.0"
           if test "${gnome_prefix}" = "NA" ; then
               gnome_prefix=`pkg-config --variable=prefix libgnome-2.0`
           fi,
           have_gnome="no"
        )
        if test "$have_gnome" = "2.0" ; then
            PKG_CHECK_MODULES(XML, libxml-2.0 >= 2.5.0)
	    AC_PATH_PROG(GCONFTOOL, gconftool-2, no)
	    if test x"$GCONFTOOL" = xno; then
              AC_MSG_ERROR([gconftool-2 executable not found in your path])
            else
	      AM_GCONF_SOURCE_2
            fi
        fi 
    else
dnl Still trying for gnome, but we don't have gtk 2.0 so test for gnome 1.4  
       AC_DEFUN([AC_PROG_GNOME],
       [AC_CHECK_PROG(GNOME,gnome-config,yes)])

       AC_PROG_GNOME
       GNOME_CFLAGS=''
       GNOME_LIBS=''
       if test x"${GNOME}" = xyes ; then
           AM_PATH_GNOME_PRINT(0.1.0,
             AC_DEFINE(USE_GNOME)
             have_gnome="1.4"
             if test "${gnome_prefix}" = "NA" ; then
                gnome_prefix="`gnome-config --prefix`"
             fi
             GNOME_CFLAGS=`gnome-config --cflags gnomeui print`
             GNOME_LIBS=`gnome-config --libs gnomeui print`
             XML_CFLAGS=`gnome-config --cflags xml`
             XML_LIBS=`gnome-config --libs xml`,
             echo "* gretl will be built without gnome support"
           )
       fi
    fi
    AC_SUBST(have_gnome)
    AC_SUBST(gnome_prefix)
    AC_SUBST(GNOME_CFLAGS)
    AC_SUBST(GNOME_LIBS)
fi
dnl End of gnome check

dnl If libxml is not already provided by gnome, test for it separately
if test x"${XML_LIBS}" = x ; then
  if test "$have_gtk" = "2.0" ; then
    PKG_CHECK_MODULES(XML, libxml-2.0 >= 2.5.0)
  else 
    XMLCONF=''
    if test "$prefer_xml1" = "yes" ; then
      AC_CHECK_PROGS(XMLCONF,xml-config xml2-config,)
    else
      AC_CHECK_PROGS(XMLCONF,xml2-config xml-config,)
    fi
    if test x"${XMLCONF}" != x ; then
      XML_CFLAGS=`$XMLCONF --cflags`
      XML_LIBS=`$XMLCONF --libs`
    fi
  fi
fi

dnl Check for libxslt (for the helpfiles)
PKG_CHECK_MODULES(XSLT, libxslt >= 1.0.15,	
  have_xslt="no",
  have_xslt="yes"
)

dnl record results on various libraries
AC_SUBST(XML_CFLAGS)
AC_SUBST(XML_LIBS)
AC_SUBST(XSLT_CFLAGS)
AC_SUBST(XSLT_LIBS)
AC_SUBST(GDK_PIXBUF_CFLAGS)
AC_SUBST(GDK_PIXBUF_LIBS) 
AC_SUBST(LIBOLE_CFLAGS)
AC_SUBST(LIBOLE_LIBS)

dnl Determine installation prefix
if test "${prefix}" = "NONE" ; then
   prefix="/usr/local"
fi
AC_DEFINE_UNQUOTED(GRETL_PREFIX, "${prefix}")

dnl Create output
CFLAGS="$orig_CFLAGS"
AC_OUTPUT([ 
Makefile 
gretl_sh
gretl-config
gretl.pc 
gui/Makefile
gui/debug
gui/gtkextra-lite/Makefile
gui2/Makefile
gui2/debug
gui2/gtkextra-lite/Makefile
gui2/gtksourceview/Makefile
gui2/omf-install/Makefile
gnome/Makefile
gnome/gretl.keys 
cli/Makefile 
cli/debug
lib/Makefile 
plugin/Makefile
po/Makefile.in 
share/Makefile
share/bcih/Makefile
tests/Makefile
extra/Makefile
redhat/gretl.spec
doc/commands/Makefile
])

dnl Make links for gtk-1.2 build
if test "${have_gtk}" = "1.2" ; then
  echo -n "creating links for gtk-1.2 build... "
  $srcdir/tools/mkguilinks $srcdir
  echo "done"
fi

if test "${have_zlib}" = "no" ; then
echo "
Please install zlib (compression library) and then reconfigure gretl.
zlib is available via http://www.info-zip.org/pub/infozip/zlib/
"

elif test x"${XML_LIBS}" = x ; then
echo "
Please install libxml and then reconfigure gretl.
libxml is available from http://xmlsoft.org/
"

elif test "${have_lapack}" = "no" ; then
echo "
Please install lapack and then reconfigure gretl.
Lapack is available from http://www.netlib.org/lapack/
"

else
echo "
Configuration:
  
  Installation path:                      ${prefix}
  Use readline library:                   ${have_readline}
  Use gnuplot for graphs:                 ${have_gnuplot}
  Use PNG for gnuplot graphs:             ${gnuplot_png}
  Use LaTeX for typesetting output:       ${have_latex}
  Gnu Multiple Precision support:         ${have_gmp}
  LAPACK support:                         ${have_lapack}
  Build with GTK version:                 ${have_gtk}
  Script syntax highlighting:             ${use_gtksourceview}
  Use installed gtksourceview:            ${have_gtksourceview}
  Use installed libole2:                  ${have_libole2}
  Use installed gtkextra:		  ${have_gtkextra}
  Build with gnome support:               ${have_gnome}
  Gnome installation prefix:              ${gnome_prefix}
  X-12-ARIMA support:                     ${have_x12a}
  TRAMO/SEATS support:                    ${have_tramo}
  Experimental audio support:             ${have_audio}
  

Now type '$MAKE' to build gretl.
"
fi

