### -*-m4-*-

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

# -- 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)

AC_ARG_WITH(rscheme-dir,
[  --with-rscheme-dir=<dir>  RScheme install directory ],
  RSDIR=$withval,
  if test "x$prefix" = xNONE; then
    RSDIR=/usr/local/
  else
    RSDIR="$prefix"
  fi)
AC_SUBST(RSDIR)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL
AC_LIBTOOL_DLOPEN
AM_PROG_LIBTOOL
AC_PATH_PROG(GUILE,guile,nope)
AC_PATH_PROG(RSCHEME,rs,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
    GWMOD_TARGET='guile-types.scm output-file.scm g-translate.scm sorting.scm'
    BIN_TARGET=g-wrap-guile
  fi
fi
AC_SUBST(GUILE_LIBS)
AC_SUBST(GUILE_TARGET)
AC_SUBST(GUILEMOD_TARGET)
AC_SUBST(GWMOD_TARGET)
AC_SUBST(BIN_TARGET)

if test "$RSCHEME" != nope; then
  RS_LTLIB_TARGET=libgwraprs.la
  RS_SCRIPT=g-wrap-rs
fi
AC_SUBST(RS_LTLIB_TARGET)
AC_SUBST(RS_SCRIPT)
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))

GW_RUNTIME_GUILE_INTERFACE_MAJOR_VER=2
GW_RUNTIME_GUILE_INTERFACE_REVISION=0
GW_RUNTIME_GUILE_INTERFACE_AGE=0

AC_SUBST(GW_RUNTIME_GUILE_INTERFACE_MAJOR_VER)
AC_SUBST(GW_RUNTIME_GUILE_INTERFACE_REVISION)
AC_SUBST(GW_RUNTIME_GUILE_INTERFACE_AGE)

AC_DEFINE_UNQUOTED(GW_RUNTIME_GUILE_INTERFACE_MAJOR_VER,
                   ${GW_RUNTIME_GUILE_INTERFACE_MAJOR_VER})
AC_DEFINE_UNQUOTED(GW_RUNTIME_GUILE_INTERFACE_REVISION,
                   ${GW_RUNTIME_GUILE_INTERFACE_REVISION})
AC_DEFINE_UNQUOTED(GW_RUNTIME_GUILE_INTERFACE_AGE,
                   ${GW_RUNTIME_GUILE_INTERFACE_AGE})


AC_OUTPUT([Makefile
           bin/Makefile
           doc/Makefile 
           example/Makefile
           g-wrap/Makefile
           g-wrapped/Makefile
           libruntime-guile/Makefile
           libruntime-rscheme/Makefile
           rpm/Makefile
           stale/Makefile
           stale/g-scan/Makefile
           stale/rscheme/Makefile
           test/Makefile])
