### -*-m4-*-

dnl Process this file with autoconf to produce a configure script.
AC_INIT(g-wrap.scm)
AM_INIT_AUTOMAKE(g-wrap, 1.3.2, no-define)
AM_CONFIG_HEADER(config.h)

AM_MAINTAINER_MODE

# -- user options --
AC_ARG_WITH(modules-dir,
[  --with-modules-dir=<dir>  Base directory for contributed Guile modules ],
  GUILEMODDIR=$withval
  G_WRAP_LOAD_DIR=$withval,
  if test "x$prefix" = xNONE; then
    GUILEMODDIR='$(GUILE_SCMDIR)'
    G_WRAP_LOAD_DIR='$(GUILE_SCMDIR)'
  else
    GUILEMODDIR='$(datadir)/guile'
    G_WRAP_LOAD_DIR='$(datadir)/guile'
  fi)
AC_SUBST(GUILEMODDIR)
AC_SUBST(G_WRAP_LOAD_DIR)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL
AC_LIBTOOL_DLOPEN
AM_PROG_LIBTOOL
AC_PATH_PROG(GUILE,guile,nope)

dnl Checks for libraries.
if test "$GUILE" != nope; then
  AC_PATH_PROG(BUILD_GUILE, guile-config)
  if test ! -n $BUILD_GUILE; then
    AC_PATH_PROG(BUILD_GUILE, build-guile)
  fi
  if test ! -n $BUILD_GUILE; then
    GUILE_LIBS='-lguile'
  else
    GUILE_LIBS=`$BUILD_GUILE link`
    CFLAGS="$CFLAGS `$BUILD_GUILE compile`"
  fi
  AC_CHECK_LIB(guile,scm_boot_guile,GUILE_SUPPORT='yes',[
  AC_MSG_WARN(Can not find Guile on the system)
  ],$GUILE_LIBS)
  if test "x$GUILE_SUPPORT" = xyes; then
    LIBS="$LIBS"&&GUILE_TARGET=libg-wrap-runtime-guile.la
    GUILEMOD_TARGET=g-wrap.scm
    BIN_TARGET=g-wrap-guile
  fi
fi
AC_SUBST(GUILE_LIBS)
AC_SUBST(GUILE_TARGET)
AC_SUBST(GUILEMOD_TARGET)
AC_SUBST(BIN_TARGET)

dnl Checks for header files.

dnl Checks for typedefs, structures, and compiler characteristics.
if test "$GUILE" != nope; then
  AC_CHECK_LIB(guile,scm_puts,AC_DEFINE(HAVE_SCM_PUTS),,$GUILE_LIBS)
  if test "$GUILE_SUPPORT" = yes; then
   AC_MSG_CHECKING(for Guile Scheme directory)
   GUILE_SCMDIR=`guile -c "(display (cdr (assq 'pkgdatadir %guile-build-info)))"`
  fi
fi
AC_SUBST(GUILE_SCMDIR)
AC_MSG_RESULT($GUILE_SCMDIR)

AC_MSG_CHECKING(if guile uses old style smobs)
AC_TRY_LINK([#include <libguile/smob.h>],
            scm_set_smob_mark,
            AC_MSG_RESULT(no),
            AC_DEFINE(GWRAP_OLD_GUILE_SMOB) AC_MSG_RESULT(yes))


###########################################################################
### Check for and set up glib

AC_PATH_PROG(GW_GLIB_CONFIG, glib-config)
AM_CONDITIONAL(GW_HAVE_GLIB, test ${GW_GLIB_CONFIG}x != x)
if test ${GW_GLIB_CONFIG}x != x
then
  AC_DEFINE(GW_HAVE_GLIB,1,[Do we have glib?])

  GW_OLDCFLAGS="$CFLAGS"
  GW_OLDLIBS="$LIBS"
  CFLAGS="`${GW_GLIB_CONFIG} --cflags` ${CFLAGS}"
  LIBS="`${GW_GLIB_CONFIG} --libs` ${GUILE_LIBS} ${LIBS}"

  ### Check size of long_long - some guile's are broken.
  AC_MSG_CHECKING(if guile long_long is at least as big as gint64)
  AC_TRY_RUN([
    #include <glib.h>
    #include <libguile/__scm.h>
    int main(int argc, char *argv[]) {
      return(!(sizeof(long_long) >= sizeof(gint64)));
    }
  ],[
    AC_MSG_RESULT(yes)
    AC_CHECK_LIB(guile, scm_long_long2num,
                 AC_DEFINE(GW_GUILE_LONG_LONG_OK,1,is sizeof(long_long) >=
                 sizeof(gint64)))
  ],[
    AC_MSG_RESULT(no)
  ])

  # One of the places this is critical is in gnc_scm_to_gint64 and inverse.
  # However, I'm sure we require this elsewhere, so don't remove this test
  # unless you've done sufficient code review/testing.
  AC_MSG_CHECKING(if unsigned long is at least as big as guint32)
  GW_OLDCFLAGS="$CFLAGS"
  CFLAGS="${GUILE_INCS} ${CFLAGS} `${GW_GLIB_CONFIG} --cflags`"
  AC_TRY_RUN([
    #include <glib.h>
    int main(int argc, char *argv[]) {
      return(!(sizeof(unsigned long) >= sizeof(guint32)));
    }
  ],[
    AC_MSG_RESULT(yes)
  ],[
    AC_MSG_RESULT(no)
    AC_MSG_ERROR(cannot continue, size of unsigned long too small.)
  ])

  CFLAGS="$GW_OLDCFLAGS"
  LIBS="$GW_OLDLIBS"
fi

LGWRAP_GLIB_MAJOR_VER=1
LGWRAP_GLIB_REVISION=0
LGWRAP_GLIB_AGE=0

AC_SUBST(LGWRAP_GLIB_MAJOR_VER)
AC_SUBST(LGWRAP_GLIB_REVISION)
AC_SUBST(LGWRAP_GLIB_AGE)

###########################################################################
### Check for and set up gtk

AC_PATH_PROG(GW_GTK_CONFIG, gtk-config)
AM_CONDITIONAL(GW_HAVE_GTK, test ${GW_GTK_CONFIG}x != x)
if test ${GW_GTK_CONFIG}x != x
then
  AC_DEFINE(GW_HAVE_GTK,1,[Do we have gtk?])
fi

###########################################################################
### Check for and set up gnome-guile/guile-gtk

AC_PATH_PROG(GW_BUILD_GUILE_GTK, build-guile-gtk)
AM_CONDITIONAL(GW_HAVE_GUILE_GTK, test ${GW_BUILD_GUILE_GTK}x != x)
if test ${GW_BUILD_GUILE_GTK}x != x
then
  AC_DEFINE(GW_HAVE_GUILE_GTK,1,[Do we have guile-gtk?])
fi

GW_WCT_INTERFACE_MAJOR_VER=1
GW_WCT_INTERFACE_REVISION=0
GW_WCT_INTERFACE_AGE=0

AC_SUBST(GW_WCT_INTERFACE_MAJOR_VER)
AC_SUBST(GW_WCT_INTERFACE_REVISION)
AC_SUBST(GW_WCT_INTERFACE_AGE)

AC_DEFINE_UNQUOTED(GW_WCT_INTERFACE_MAJOR_VER, ${GW_WCT_INTERFACE_MAJOR_VER})
AC_DEFINE_UNQUOTED(GW_WCT_INTERFACE_REVISION, ${GW_WCT_INTERFACE_REVISION})
AC_DEFINE_UNQUOTED(GW_WCT_INTERFACE_AGE, ${GW_WCT_INTERFACE_AGE})

AC_OUTPUT([Makefile
           bin/Makefile
           doc/Makefile 
           example/Makefile
           g-wrap/Makefile
           rpm/Makefile
           test/Makefile])
