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

AC_INIT(aclocal.m4)
AC_CONFIG_HEADER(config.h)

dnl Checks for optional features.
AC_ARG_ENABLE(debug, [  --enable-debug          turn on debugging [default=no]])
test yes = "$enable_debug" || : ${CFLAGS=""}
AC_ARG_WITH(gnustep,
[  --with-gnustep          use the GNUstep makefile package for building
                          and installing the library [default=no]])
test yes = "$with_gnustep" || with_gnustep=no
AC_ARG_WITH(extra-prefix,
[  --with-extra-prefix     add non-standard include and library path])
if test no != "${with_extra_prefix-no}"; then
CPPFLAGS="$CPPFLAGS -I$with_extra_prefix/include"
LDFLAGS="$LDFLAGS -L$with_extra_prefix/lib"
fi

dnl Checks for programs.
AC_PROG_CC
AC_PROG_CPP
if test no = "$with_gnustep"; then
AC_PROG_INSTALL
AC_PROG_LIBTOOL
fi
AM_PATH_GTK(1.2.6,,, gthread)
if test no != "$GTK_CONFIG"; then
GTK_PREFIX=`"$GTK_CONFIG" $gtk_config_args --prefix`
AC_SUBST(GTK_PREFIX)
fi

AM_PATH_LIBGLADE
if test no != "$LIBGLADE_CONFIG"; then
for flag in $LIBGLADE_CFLAGS; do
GTK_CFLAGS=`echo "$GTK_CFLAGS " | sed "s#$flag ##g"`
done
for flag in $LIBGLADE_LIBS; do
GTK_LIBS=`echo "$GTK_LIBS " | sed "s#$flag ##g"`
done
GTK_CFLAGS="$LIBGLADE_CFLAGS $GTK_CFLAGS"
GTK_LIBS="$LIBGLADE_LIBS $GTK_LIBS"
AC_DEFINE(HAVE_LIBGLADE)
fi

if test yes = "$with_gnustep"; then
LDFLAGS="$LDFLAGS `echo " $GTK_LIBS" | sed 's/ -l[[^ ]]*//g'`"
GTK_LIBS=`echo " $GTK_LIBS" | sed 's/ -[[^l]][[^ ]]*//g'`
fi

if test no = "$with_gnustep"; then

    if test yes = "$enable_debug"; then
    CFLAGS="$CFLAGS -DDEBUG"
    test "$GCC" && CFLAGS="$CFLAGS -Wall"
    else
    test "$GCC" && : ${CFLAGS:="-pipe -Wall -O2"}
    fi

    dnl I should use AC_TRY_COMPILE here, but it does not work.
    my_CPPFLAGS=$CPPFLAGS
    CPPFLAGS="$CPPFLAGS -pthread"
    AC_CACHE_CHECK(whether $CC accepts -pthread, ac_cv_prog_cc_pthread,
    AC_TRY_CPP(, ac_cv_prog_cc_pthread=yes, ac_cv_prog_cc_pthread=no))
    CPPFLAGS=$my_CPPFLAGS

fi

dnl Checks for compiler characteristics.
AC_CHECK_SIZEOF(int, 4)
AC_CHECK_SIZEOF(long, 4)
AC_CHECK_SIZEOF(float, 4)
AC_CHECK_SIZEOF(void *, SIZEOF_LONG)

dnl Checks for header files.
AC_CHECK_HEADERS(unistd.h)
AC_CACHE_CHECK(for Notifications in NSThread.h, ac_cv_nsthread_notification,
AC_EGREP_HEADER(NSWillBecomeMultiThreadedNotification, Foundation/NSThread.h,
	ac_cv_nsthread_notification=yes; AC_DEFINE(HAVE_NSTHREAD_NOTIFICATION),
	ac_cv_nsthread_notification=no))

dnl Checks for library functions.
AC_CHECK_FUNCS(getcwd)

if test no = "$with_gnustep"; then

    dnl Checks for libraries.
    dnl FIXME gtk-config should provide thread information separately.
    if test $ac_cv_prog_cc_pthread = yes; then
    CFLAGS="$CFLAGS -pthread" LDFLAGS="$LDFLAGS -pthread"
    else
    AC_CHECK_LIB(pthread, pthread_create, PTHREAD=-lpthread,
    AC_CHECK_LIB(pthreads, pthread_create, PTHREAD=-lpthreads))
    fi
    AC_CHECK_LIB(c, sched_yield, :,
    AC_CHECK_LIB(rt, sched_yield, PTHREAD="$PTHREAD -lrt",
    AC_CHECK_LIB(posix4, sched_yield, PTHREAD="$PTHREAD -lposix4")))
    AC_SUBST(PTHREAD)

    AC_CHECK_LIB(socket, socket)
    AC_CHECK_LIB(c, gethostbyname, :,
    AC_CHECK_LIB(nsl, gethostbyname))

    AC_CHECK_LIBM
    AC_CHECK_LIB(dl, dlopen)
    AC_CHECK_LIB(Foundation, NSLog,,, -lobjc $LIBM $PTHREAD)
    if test $ac_cv_lib_Foundation_NSLog = no; then
    AC_CHECK_LIB(gnustep-base, NSLog,,, -lobjc $LIBM $PTHREAD)
    fi

fi

AC_OUTPUT(config.mk gtk-tool.make GToolKit/Makefile
	  Examples/Makefile Examples/calc/Makefile Examples/p/Makefile)

if test yes = "$with_gnustep"; then
for f in */Makefile Examples/*/Makefile; do cp "$f.gnustep" "$f"; done
fi
