AC_INIT(ical_tcl.C)
AC_PREREQ(2.0)

ICAL_VERSION="2.0p2"
AC_SUBST(ICAL_VERSION)

############################################################################
# Create config files
AC_CONFIG_HEADER(config.h)

############################################################################
# With debugging malloc?

AC_ARG_WITH(mdebug, [  --with-mdebug           use a debugging malloc],
	MALLOC_DEBUG=-DMALLOC_DEBUG
	MALLOC_LIB="-lcxx /usr/local/lib/libmalloc_dbg.a",
	MALLOC_DEBUG=""
	MALLOC_LIB="")
AC_SUBST(MALLOC_DEBUG)
AC_SUBST(MALLOC_LIB)

############################################################################
# Check for programs

AC_PROG_INSTALL
AC_PROG_RANLIB
AC_LN_S
AC_PROGRAMS_PATH(MAILER,Mail mail,Mail)

AC_PROG_CC
AC_PROG_CXX

###########################################################################
# System variants

AC_ISC_POSIX

############################################################################
# Installation location.
#
# Pick this now because it is useful when looking for libraries and headers.

AC_PREFIX(wish)

############################################################################
# Find Tcl/Tk

# Base set of directories to search
dirs="/usr/local /usr"
if test -n "$prefix"; then
   dirs="$prefix $dirs"
   fi

# Find Tcl library
AC_MSG_CHECKING(for tcl library)
tcllibdir=
if test -n "$tcllib"; then
   tcllibdir="$tcllib"
   tcllibver=
   tcllib="-L$tcllib -ltcl"
   fi
for d in $dirs; do
    if test -n "$tcllibdir"; then break; fi
    for vnum in 7.4 ""; do
	if test -n "$tcllibdir"; then break; fi
	for suff in .a .so ".so.*"; do
	    if test -n "$tcllibdir"; then break; fi
	    if test -f "$d/lib/libtcl$vnum$suff"; then
		tcllibdir="$d/lib"
		tcllibver="$vnum"
		tcllib="-L$tcllibdir -ltcl$vnum"
	    fi
	done
    done
done
if test -z "$tcllibdir"; then AC_MSG_ERROR(not found); fi
AC_MSG_RESULT($tcllibdir)
AC_SUBST(tcllib)
AC_SUBST(tcllibdir)
AC_SUBST(tcllibver)

# Find tcl.h
AC_MSG_CHECKING(for tcl.h)
for d in $dirs; do
    if test -n "$tclinc"; then break; fi
    if test -f "$d/include/tcl.h"; then
       tclinc="$d/include"
       break
    fi
done
if test -z "$tclinc"; then AC_MSG_ERROR(not found); fi
AC_MSG_RESULT($tclinc)
if test "$tclinc" = "/usr/include"; then tclinc=; else tclinc="-I$tclinc"; fi
AC_SUBST(tclinc)

# Find tcl scripts
AC_MSG_CHECKING(for tcl script files)
for d in $dirs; do
    if test -n "$tclscripts"; then break; fi
    for f in lib/tcl7.4 lib/tcl; do
        if test -f "$d/$f/init.tcl"; then
	   tclscripts="$d/$f"
	   break
	fi
    done
done
if test -z "$tclscripts"; then AC_MSG_ERROR(not found); fi
AC_MSG_RESULT($tclscripts)
AC_SUBST(tclscripts)

# Find Tk library
AC_MSG_CHECKING(for tk library)
tklibdir=
if test -n "$tklib"; then
   tklibdir="$tklib"
   tklibver=
   tklib="-L$tklib -ltk"
   fi
for d in $dirs; do
    if test -n "$tklibdir"; then break; fi
    for vnum in 4.0 ""; do
	if test -n "$tklibdir"; then break; fi
	for suff in .a .so ".so.*"; do
	    if test -n "$tklibdir"; then break; fi
	    if test -f "$d/lib/libtk$vnum$suff"; then
		tklibdir="$d/lib"
		tklibver="$vnum"
		tklib="-L$tklibdir -ltk$vnum"
	    fi
	done
    done
done
if test -z "$tklibdir"; then AC_MSG_ERROR(not found); fi
AC_MSG_RESULT($tklibdir)
AC_SUBST(tklib)
AC_SUBST(tklibdir)
AC_SUBST(tklibver)

# Find tk.h
AC_MSG_CHECKING(for tk.h)
for d in $dirs; do
    if test -n "$tkinc"; then break; fi
    if test -f "$d/include/tk.h"; then
       tkinc="$d/include"
       break
    fi
done
if test -z "$tkinc"; then AC_MSG_ERROR(not found); fi
AC_MSG_RESULT($tkinc)
if test "$tkinc" = "/usr/include"; then tkinc=; else tkinc="-I$tkinc"; fi
if test "$tkinc" = "$tclinc"; then tkinc=; fi
AC_SUBST(tkinc)

# Find tk scripts
AC_MSG_CHECKING(for tk script files)
for d in $dirs; do
    if test -n "$tkscripts"; then break; fi
    for f in lib/tk4.0 lib/tk; do
        if test -f "$d/$f/tk.tcl"; then
	   tkscripts="$d/$f"
	   break
	fi
    done
done
if test -z "$tkscripts"; then AC_MSG_ERROR(not found); fi
AC_MSG_RESULT($tkscripts)

# Now actually generate the list of script files with "tk.tcl" at front
files=""
for f in $tkscripts/*.tcl; do
    if test "$f" = "$tkscripts/tk.tcl"; then
       files="$f $files"
    else
       files="$files $f"
    fi
done
tkscripts="$files"
AC_SUBST(tkscripts)

#############################################################################
# Check Tcl/Tk versions

AC_REQUIRE_CPP()
ac_save_cppflags="$CPPFLAGS"
CPPFLAGS="$tclinc $tkinc $CPPFLAGS"

AC_MSG_CHECKING(for up-to-date Tcl version)
AC_EGREP_CPP(good_tcl_version,
[#include <tcl.h>
#if ((TCL_MAJOR_VERSION == 7) && (TCL_MINOR_VERSION >= 4))
    good_tcl_version
#endif
#if (TCL_MAJOR_VERSION > 7)
    good_tcl_version
#endif],is_good_tcl_version=yes,is_good_tcl_version=no)
AC_MSG_RESULT($is_good_tcl_version)
if test "$is_good_tcl_version" = "no"; then exit 1; fi

AC_MSG_CHECKING(for up-to-date Tk version)
AC_EGREP_CPP(good_tk_version,
[#include <tk.h>
#if (TK_MAJOR_VERSION >= 4)
    good_tk_version
#endif],is_good_tk_version=yes,is_good_tk_version=no)
AC_MSG_RESULT($is_good_tk_version)
if test "$is_good_tk_version" = "no"; then exit 1; fi

CPPFLAGS="$ac_save_cppflags"

############################################################################
# X11 locations

AC_PATH_XTRA

# The following code was cribbed from the Tk 4.0 configure.in
not_really_there=""
if test "$no_x" = ""; then
    if test "$x_includes" = ""; then
	AC_TRY_CPP([#include <X11/Intrinsic.h>], , not_really_there="yes")
    else
	if test ! -r $x_includes/X11/Intrinsic.h; then
	    not_really_there="yes"
	fi
    fi
fi
if test "$no_x" = "yes" -o "$not_really_there" = "yes"; then
    echo checking for X11 header files
    XINCLUDES=
    AC_TRY_CPP([#include <X11/Intrinsic.h>], , XINCLUDES="nope")
    if test "$XINCLUDES" = nope; then
        dirs="/usr/unsupported/include /usr/local/include /usr/X386/include /usr/include/X11R4 /usr/X11R5/include /usr/include/X11R5 /usr/openwin/include /usr/X11/include /usr/sww/include"
        for i in $dirs ; do
	    if test -r $i/X11/Intrinsic.h; then
	        XINCLUDES=" -I$i"
	    fi
        done
    fi
    if test "$XINCLUDES" = nope; then
       echo "Warning:  couldn't find any X11 include files."
    else
	X_CFLAGS="$X_CFLAGS $XINCLUDES"
    fi
fi

if test "$no_x" = yes; then
    XLIBSW=nope
    if test "$XLIBSW" = nope; then
	dirs="/usr/unsupported/lib /usr/local/lib /usr/X386/lib /usr/lib/X11R4 /usr/X11R5/lib /usr/lib/X11R5 /usr/openwin/lib /usr/X11/lib /usr/sww/X11/lib"
	for i in $dirs ; do
	    if test -r $i/libX11.a; then
		XLIBSW="-L$i -lX11"
	    fi
	done
    fi
    if test "$XLIBSW" = nope ; then
	echo "Warning:  couldn't find the X11 library archive."
    else
	X_LIBS="$X_LIBS $XLIBSW"
    fi
fi

#############################################################################
# Check for library functions/etc..

AC_TYPE_PID_T
AC_CHECK_SIZEOF(long)
AC_CHECK_HEADERS(string.h sys/utsname.h limits.h)

AC_CHECK_FUNCS(fsync strerror uname)
AC_REPLACE_FUNCS(gethostname)

AC_LANG_SAVE
AC_LANG_CPLUSPLUS

#############################################################################
# Macro to check for prototypes

AC_DEFUN(ICAL_CHECK_PROTO,
[changequote(,)dnl
ac_tr_func=HAVE_`echo $1 | tr '[a-z]' '[A-Z]'`_PROTO
changequote([,])dnl
AC_MSG_CHECKING(for $1 prototype)
AC_CACHE_VAL(ical_cv_proto_$1,[
AC_TRY_RUN([
#include <$2>
static void* ptr = &$1;
int main() {return 0;}
],ical_cv_proto_$1=yes,ical_cv_proto_$1=no,ical_cv_proto_$1=no)])
AC_MSG_RESULT($ical_cv_proto_$1)
if test "$ical_cv_proto_$1" = "yes"; then
   AC_DEFINE_UNQUOTED(${ac_tr_func})
   fi])dnl

ICAL_CHECK_PROTO(strerror,string.h)
ICAL_CHECK_PROTO(sys_errlist,errno.h)
ICAL_CHECK_PROTO(gettimeofday,sys/time.h)
ICAL_CHECK_PROTO(gethostname,unistd.h)
ICAL_CHECK_PROTO(getpid,unistd.h)
ICAL_CHECK_PROTO(strncasecmp,string.h)
ICAL_CHECK_PROTO(gethostid,unistd.h)

# gethostid
AC_MSG_CHECKING(for working gethostid)
AC_TRY_RUN([/* check for working gethostid */
#ifndef HAVE_GETHOSTID_PROTO
extern "C" long gethostid(void);
#endif
int main() {
    return ((gethostid() == 0) ? 1 : 0);
}
],AC_DEFINE(HAVE_GETHOSTID),AC_MSG_RESULT(no),AC_MSG_RESULT(no))

AC_LANG_RESTORE

############################################################################
# Generate list of files

AC_CHECKING(for tcllib script files)
tcl_files=""
for f in ${srcdir}/tcllib/*.tcl; do
    tcl_files="$tcl_files $f"
    done
AC_SUBST(tcl_files)

AC_CHECKING(for ical script files)
ical_files=""
for f in ${srcdir}/*.tcl; do
    ical_files="$ical_files $f"
    done
AC_SUBST(ical_files)

############################################################################
# Support for automatic reconfiguration

CONFIGURE="$0 $configure_args"

MFILES="Makefile startup.tcl calendar/Makefile time/Makefile types/Makefile"
MFILES_IN=
for f in $MFILES; do
  MFILES_IN="$MFILES_IN $srcdir/$f.in"
  done

AC_SUBST(CONFIGURE)
AC_SUBST(MFILES)
AC_SUBST(MFILES_IN)

############################################################################
# Build configured files

AC_OUTPUT($MFILES)
