AC_INIT(ical_tcl.C)
AC_PREREQ(2.0)

############################################################################
# Standard Installation (assumes installed Tcl/Tk)
# =====================
# + Find tclConfig.sh and tkConfig.sh via tclsh/[info library]
# + Read them in to get compilation flags
# + Compile ical executable
# + Install application, shared library, all the .tcl files
# + Q: Use "package" stuff?  A: not until Tcl/Tk use it.
# 
# Stand-alone Compilation
# =======================
# Merge various .tcl files into application
#
# Shared library (for using ical as an extension)???
# ==============
# If possible on platform,
# + Compile a shared library
# + Install it
# This seems hard to do currently because we need different compilation
# flags than for normal statically linked executable, and we are not
# currently set-up to do two compilations of the source tree.

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

ICAL_VERSION="2.2"
AC_SUBST(ICAL_VERSION)

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

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

AC_ARG_ENABLE(gcc, [  --enable-gcc            allow use of gcc if available],
    [tk_ok=$enableval], [tkl_ok=no])
if test "$tk_ok" = "yes"; then
    AC_PROG_CC
else
    CC=${CC-cc}
AC_SUBST(CC)
fi

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_PROGRAM(wish)
if test -d "$prefix/bin"; then
    PATH="$prefix/bin:$PATH"
fi

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

AC_CHECKING(Tcl/Tk installation)
legal_tk_versions="8.3 8.2 8.1 8.0 4.9 4.8 4.7 4.6 4.5 4.4 4.3 4.2 4.1"
legal_tcl_versions="8.3 8.2 8.1 8.0 7.9 7.8 7.7 7.6 7.5"

#### Find tclsh
AC_ARG_WITH(tclsh,[  --with-tclsh=<path>     full path name of tclsh],
	tclsh="$withval")
if test -n "$tclsh"; then
    AC_MSG_CHECKING(for tclsh)
    AC_MSG_RESULT($tclsh)
else
    tclsh_list=
    for v in $legal_tcl_versions; do
	tclsh_list="$tclsh_list tclsh$v"
    done
    # Look for tclsh variants in $prefix/bin, and then in $PATH.
    AC_PATH_PROGS(tclsh,$tclsh_list)
fi
if test -z "$tclsh"; then
    AC_MSG_ERROR(I could not find tclsh in your search PATH.

Please supply the location of a tclsh executable by
running configure with the following option
	--with-tclsh=<full path name of tclsh>)
fi

#### Run tclsh to find tclConfig.sh
AC_MSG_CHECKING(for tclConfig.sh)
AC_ARG_WITH(tclconfig,
[  --with-tclconfig=<path> directory that contains tclConfig.sh],
tclconfig="$withval")
if test -z "$tclconfig"; then
    changequote(,)
    tclconfig=`echo 'puts stdout [file dirname [info library]]' | $tclsh`
    changequote([,])
fi
if test ! -f "$tclconfig/tclConfig.sh"; then
    AC_MSG_ERROR(I could not find tclConfig.sh.

Please specify the directory that contains tclConfig.sh
by running configure with the following option
	--with-tclconfig=<path name of directory containing tclConfig.sh>

Warning: Only Tcl versions 7.5 and later provide a tclConfig.sh file.
Make sure you have a new enough version installed on your
system.)
fi
AC_MSG_RESULT($tclconfig/tclConfig.sh)

#### Look for tkConfig.sh (in same directory as tclConfig.sh?)
AC_MSG_CHECKING(for tkConfig.sh)
AC_ARG_WITH(tkconfig,
[  --with-tkconfig=<path>  directory that contains tkConfig.sh],
tkconfig="$withval")
if test -z "$tkconfig"; then
    tkconfig="$tclconfig"
fi
if test ! -f "$tkconfig/tkConfig.sh"; then
    AC_MSG_ERROR(I could not find tkConfig.sh.

Please specify the directory that contains tkConfig.sh
by running configure with the following option
	--with-tkconfig=<path name of directory containing tkConfig.sh>

Warning: Only Tk versions 4.1 and later provide a tkConfig.sh file.
Make sure you have a new enough version installed on your system.)
fi
AC_MSG_RESULT($tkconfig/tkConfig.sh)

#### Load the Tcl/Tk configuration files 
. $tclconfig/tclConfig.sh
. $tkconfig/tkConfig.sh

if test -z "$TCL_EXEC_PREFIX"; then TCL_EXEC_PREFIX="$TCL_PREFIX"; fi
if test -z "$TK_EXEC_PREFIX"; then TK_EXEC_PREFIX="$TK_PREFIX"; fi

#### Check that the library files exist
AC_MSG_CHECKING(for installed Tcl/Tk library files)
lib1="${TCL_EXEC_PREFIX}/lib/libtcl${TCL_VERSION}.so"
lib2="${TK_EXEC_PREFIX}/lib/libtk${TK_VERSION}.so"
for f in "$lib1" "$lib2"; do
    if test ! -f "$f"; then
	AC_MSG_RESULT($f not found)
	AC_MSG_ERROR(Make sure Tcl/Tk are installed correctly on your
system and then rerun configure.)
    fi
done
AC_MSG_RESULT(ok)

#### Find the flags for linking with the Tcl/Tk libraries
if test "$TCL_LIB_VERSIONS_OK" = "ok"; then TCL_LIB_VERSIONS_OK=yes; fi
if test "$TCL_LIB_VERSIONS_OK" = "1";  then TCL_LIB_VERSIONS_OK=yes; fi

if test -z "$TCL_LIB_SPEC"; then
    if test "$TCL_LIB_VERSIONS_OK" = "yes"; then
        TCL_LIB_SPEC="-L${TCL_EXEC_PREFIX}/lib -ltcl${VERSION}${DBGX}"
    else
        TCL_LIB_SPEC="${TCL_EXEC_PREFIX}/lib/${TCL_LIB_FILE}"
    fi
fi

if test -z "$TK_LIB_SPEC"; then
    if test "$TCL_LIB_VERSIONS_OK" = "yes"; then
        TK_LIB_SPEC="-L${TK_EXEC_PREFIX}/lib -ltk${VERSION}${DBGX}"
    else
        TK_LIB_SPEC="${TK_EXEC_PREFIX}/lib/${TK_LIB_FILE}"
    fi
fi

#### Check Tcl/Tk versions

AC_REQUIRE_CPP()

AC_MSG_CHECKING(for up-to-date Tcl version in tcl.h)
AC_ARG_WITH(tclhdir,
[  --with-tclhdir=<path>   directory that contains tcl.h],
	tclhdir="$withval")
if test -z "$tclhdir"; then
    # Search for tcl.h
    tclhdir="${TCL_PREFIX}/include"
    for dir in "${TCL_PREFIX}/include" /usr/include /usr/include/tcl; do
	if test -f "$dir/tcl.h"; then
	    tclhdir="$dir"
	    break
	    fi
	done
fi

# Find tk.h
AC_ARG_WITH(tkhdir,
[  --with-tkhdir=<path>    directory that contains tk.h],
	tkhdir="$withval")
if test -z "$tkhdir"; then
    # Search for tk.h
    tkhdir="$tclhdir"
    for dir in "${TK_PREFIX}/include" /usr/include /usr/include/tcl; do
	if test -f "$dir/tk.h"; then
	    tkhdir="$dir"
	    break
	    fi
	done
fi

AC_EGREP_CPP(good_tcl_version,
[#include "$tclhdir/tcl.h"
#if ((TCL_MAJOR_VERSION == $TCL_MAJOR_VERSION) && (TCL_MINOR_VERSION == $TCL_MINOR_VERSION))
    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 in tk.h)
AC_EGREP_CPP(good_tk_version,
[#include "$tkhdir/tk.h"
#if ((TK_MAJOR_VERSION == $TK_MAJOR_VERSION) && (TK_MINOR_VERSION == $TK_MINOR_VERSION))
    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

TCL_INCDIR="$tclhdir"
TK_INCDIR="$tkhdir"

#### Export variables discovered during Tcl/Tk configuration

AC_SUBST(TCL_LIB_SPEC)
AC_SUBST(TCL_PREFIX)
AC_SUBST(TCL_EXEC_PREFIX)
AC_SUBST(TCL_LIBS)
AC_SUBST(TK_LIB_SPEC)
AC_SUBST(TK_LIBS)
AC_SUBST(TK_PREFIX)
AC_SUBST(TK_EXEC_PREFIX)
AC_SUBST(TK_XINCLUDES)
AC_SUBST(TCL_INCDIR)
AC_SUBST(TK_INCDIR)

#### Find names of script files for use in stand-alone installation

TCL_SCRIPT_DIR="$TCL_PREFIX/lib/tcl$TCL_VERSION"
if test ! -f "$TCL_SCRIPT_DIR/init.tcl"; then
    AC_MSG_ERROR(I could not find the *.tcl files that are supposed
to be installed in $TCL_SCRIPT_DIR.

Make sure your Tcl installation is complete and correct and then
rerun configure.)
fi

# Generate list of all .tcl files that need to be loaded from the Tcl library.
files=""
for f in $TCL_SCRIPT_DIR/*.tcl; do
    files="$files $f"
done
TCL_SCRIPTS="$files"

TK_SCRIPT_DIR="$TK_PREFIX/lib/tk$TK_VERSION"
if test ! -f "$TK_SCRIPT_DIR/tk.tcl"; then
    AC_MSG_ERROR(I could not find the *.tcl files that are supposed
to be installed in $TK_SCRIPT_DIR.

Make sure your Tk installation is complete and correct and then
rerun configure.)
fi

# Generate list of all .tcl files that need to be loaded from the Tk library.
#	tk.tcl should come at front
#	console.tcl should be omitted
files=""
for f in $TK_SCRIPT_DIR/*.tcl; do
    if test "$f" = "$TK_SCRIPT_DIR/tk.tcl"; then
       files="$f $files"
    elif test "$f" = "$TK_SCRIPT_DIR/console.tcl"; then
       true;
    else
       files="$files $f"
    fi
done
TK_SCRIPTS="$files"

# Generate list of all .tcl files that need to be loaded from the tcllib dir.
files=""
for f in ${srcdir}/tcllib/*.tcl; do
    files="$files $f"
    done
ICAL_TCLLIB_SCRIPTS="$files"

# Generate list of all .tcl files that need to be loaded from the ical dir.
files=""
for f in ${srcdir}/*.tcl; do
    files="$files $f"
    done
ICAL_SCRIPTS="$files"

AC_SUBST(TCL_SCRIPTS)
AC_SUBST(TK_SCRIPTS)
AC_SUBST(ICAL_TCLLIB_SCRIPTS)
AC_SUBST(ICAL_SCRIPTS)

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

AC_TYPE_PID_T
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

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

CONFIGURE="$0 $configure_args"

MFILES="Makefile startup.tcl calendar/Makefile time/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_CONFIG_SUBDIRS(types)
AC_OUTPUT($MFILES)
