dnl Process this file with autoconf to produce a configure script.
AC_INIT(src/aim.c)
AM_CONFIG_HEADER(config.h)
AM_INIT_AUTOMAKE([gaim], [0.11.0pre12])

dnl Checks for programs.
AC_PROG_CC
AC_DISABLE_STATIC
AM_PROG_LIBTOOL
LIBTOOL="$LIBTOOL --silent"
AC_PROG_INSTALL


ALL_LINGUAS="de fr es zh_CN ko"
AM_GNU_GETTEXT

AC_CYGWIN


dnl Checks for header files.
AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS(fcntl.h sys/time.h unistd.h locale.h signal.h)
AC_CHECK_HEADERS(pthread.h,,AC_MSG_ERROR([*** POSIX thread support not found ***]))

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_STRUCT_TM

dnl Checks for library functions.
AC_TYPE_SIGNAL
AC_FUNC_STRFTIME
AC_CHECK_FUNCS(socket strdup strstr atexit getaddrinfo)
AC_TRY_COMPILE(sys/socket.h, typedef unsigned int socklen_t;, AC_DEFINE(NEED_SOCKLEN_T),)

dnl Checks for getopt in standard library
AC_CHECK_FUNCS(getopt_long , , [LIBOBJS="$LIBOBJS getopt.o getopt1.o"] ) 
AC_SUBST(LIBOBJS)

dnl This is a bad, bad hack.  I am a bad, bad man.
CFLAGS="$CFLAGS -I/usr/local/include -I/opt/include -I\$(top_srcdir)"

AC_ARG_ENABLE(distrib,,,enable_distrib=no)
AM_CONDITIONAL(DISTRIB, test "x$enable_distrib" = "xyes")
AC_ARG_ENABLE(multi,   [  --disable-multi         disable multiple connections],,enable_multi=yes)
AC_ARG_ENABLE(prpls,   [  --disable-prpls         don't build protocol plugins],,enable_prpls=yes)
AC_ARG_ENABLE(gnome,   [  --disable-gnome         compile without Gnome bits],,enable_gnome=yes)
AC_ARG_ENABLE(panel,   [  --enable-panel          compile as a GNOME applet],,enable_panel=$enable_distrib)
AM_CONDITIONAL(GNOMEAPPLET, test "x$enable_panel" = "xyes")
AC_ARG_ENABLE(esd,     [  --disable-esd           Turn off ESD (default=auto)],,enable_esd=yes)
AC_ARG_ENABLE(nas,     [  --enable-nas            Enable NAS (Network Audio System) support],,enable_nas=no)
AC_ARG_ENABLE(plugins, [  --disable-plugins       compile without plugin support],,enable_plugins=yes)
AC_ARG_ENABLE(perl,    [  --disable-perl          compile without perl scripting],,enable_perl=yes)
AC_ARG_ENABLE(debug,   [  --enable-debug          compile with debugging support],,enable_debug=no)
AC_ARG_ENABLE(screensaver,   [  --disable-screensaver   compile without X screensaver extension],,enable_xss=yes)
AC_ARG_WITH(krb4,      [  --with-krb4=PREFIX      Compile Zephyr plugin with Kerberos 4 support],kerberos="$withval",kerberos="no")
AC_ARG_ENABLE(,,,)

if test "$enable_debug" = yes ; then
	CFLAGS="$CFLAGS -Wall -g"
	AC_DEFINE(DEBUG)
fi

if test "x$enable_gnome" = "xyes" ; then
	if test "x$enable_panel" = "xyes" ; then
		GNOME_INIT(gnomeui applets)
		GNOME_X_CHECKS
		CFLAGS="$CFLAGS $GNOME_INCLUDEDIR"
		AC_DEFINE(USE_APPLET)
		AC_DEFINE(USE_GNOME)
		LIBS="$LIBS $GTK_LIBS $GNOME_LIBDIR $GNOME_APPLETS_LIBS $GNOMEUI_LIBS"
		AC_PATH_PROG(gaimpath, gaim_applet)
	else
		AC_PATH_PROG(gnomepath, gnome-config)
		AC_MSG_CHECKING(for Gnome compile flags)
		GNOME_CFLAGS=`$gnomepath gnomeui --cflags 2>/dev/null`
		if test "x$GNOME_CFLAGS" = "x" ; then
			enable_gnome=no
			AC_MSG_RESULT([Gnome not found, building without it.])
		else
			GNOME_VER=`$gnomepath --version |$sedpath 's/gnome-libs //' 2>/dev/null`
			GNOME_MAJOR=`echo $GNOME_VER |$sedpath 's/\([[0-9]*]*\).\([[0-9]*]*\).\([[0-9]*]*\)/\1/' 2>/dev/null`
			if test "x$GNOME_MAJOR" = "x0" ; then
				enable_gnome = no
				AC_MSG_RESULT([old Gnome found, building without it.])
			else
				AC_MSG_RESULT(ok)
				CFLAGS="$CFLAGS $GNOME_CFLAGS"
				LIBS="$LIBS `$gnomepath gnomeui --libs 2>/dev/null`"
				AC_DEFINE(USE_GNOME)
				GNOME_CONFIG="$gnomepath"
				AC_SUBST(GNOME_CONFIG)
				AC_PATH_PROG(gaimpath, gaim)
			fi
		fi
	fi
fi

if test "x$enable_gnome" != "xyes" ; then
	AM_PATH_GLIB(1.2.0,,AC_MSG_ERROR([
*** GLib is required to build Gaim; please make sure you have the GLib
*** development headers installed. The latest version of GLib is
*** always available at http://www.gtk.org/.]))
	AM_PATH_GTK(1.2.0,,AC_MSG_ERROR([
*** GTK+ is required to build Gaim; please make sure you have the GTK+
*** development headers installed. The latest version of GTK+ is
*** always available at http://www.gtk.org/.]),gthread)
	LIBS="$LIBS $GTK_LIBS"
	AC_PATH_PROG(gaimpath, gaim)
fi
CFLAGS="$CFLAGS $GTK_CFLAGS"

dnl Check for XScreenSaver
if test "x$enable_xss" = "xyes" ; then
	XSS_LIBS="no"
	XSS_HEADERS="no"
	AC_CHECK_LIB(Xext, XScreenSaverRegister,[XSS_LIBS=""],[],[-lX11 -lXext -lm])
	AC_CHECK_LIB(Xss, XScreenSaverRegister,[XSS_LIBS="-lXss"],[],[-lX11 -lXext -lm])
	if test \! "$XSS_LIBS" = "no"; then
		AC_TRY_COMPILE([
#include <X11/extensions/scrnsaver.h>
				],[],[],[
				AC_DEFINE(USE_SCREENSAVER)
				AC_SUBST(XSS_LIBS)
				])
	else
	       XSS_LIBS=""
	fi
else
	XSS_LIBS=""
fi




dnl This was taken straight from X-Chat.
dnl X-Chat is the greatest application ever, not only
dnl because it's a rocking IRC client but also because
dnl it's very easy to learn from.
if test "$enable_perl" = yes ; then
	AC_PATH_PROG(sedpath, sed)
	AC_PATH_PROG(perlpath, perl)
	AC_MSG_CHECKING(for Perl compile flags)
	PERL_CFLAGS=`$perlpath -MExtUtils::Embed -e ccopts 2>/dev/null`
	if test "_$PERL_CFLAGS" = _ ; then
		AC_MSG_RESULT([not found, building without perl.])
		enable_perl = no
	else
		PERL_LIBS=`$perlpath -MExtUtils::Embed -e ldopts 2>/dev/null |$sedpath 's/-lgdbm //'`
		PERL_LIBS=`echo $PERL_LIBS |$sedpath 's/-ldb //'`
		PERL_LIBS=`echo $PERL_LIBS |$sedpath 's/-lndbm //'`
		if test "$system" = "Linux"; then
			PERL_LIBS=`echo $PERL_LIBS |$sedpath 's/-lnsl //'`
			PERL_LIBS=`echo $PERL_LIBS |$sedpath 's/-lposix //'`
		fi
		PERL_LIBS=`echo $PERL_LIBS |$sedpath 's/-lc //'`
		AC_MSG_RESULT(ok)
		AC_SUBST(PERL_CFLAGS)
		AC_SUBST(PERL_LIBS)
		AC_DEFINE(USE_PERL)
		AC_CHECK_FUNCS(Perl_eval_pv)
	fi
fi

if test "$enable_nas" = yes ; then
	AC_DEFINE(NAS_SOUND)
        LIBS="$LIBS -laudio"
fi

if test "$enable_esd" = yes ; then
	AM_PATH_GESD
        if test "$no_esd" != yes ; then
        	CFLAGS="$CFLAGS $ESD_CFLAGS"
		AC_DEFINE(ESD_SOUND)
                LDADD="$LDADD $ESD_LIBS"
	else
		enable_esd=no
        fi
fi

if test "$ac_cv_cygwin" = yes ; then
	LDADD="$LDADD -static"
	CFLAGS="$CFLAGS -Wall -g"
	AC_DEFINE(DEBUG)
fi                           

AC_SUBST(CFLAGS)
AC_SUBST(LDADD)
AC_SUBST(LIBS)

if test "x$enable_multi" != "xyes" ; then
	AC_DEFINE(NO_MULTI)
	enable_multi=no
fi

if test "x$enable_plugins" = "xyes" ; then
	AC_DEFINE(GAIM_PLUGINS)
	AM_CONDITIONAL(PLUGINS, test "x$enable_plugins" = "xyes")
else
	enable_plugins=no
	enable_prpls=no
fi

if test "x$enable_prpls" = "xyes" ; then
	dnl checks for icqlib
	AC_CHECK_HEADERS(asm/byteorder.h byteswap.h endian.h machine/endian.h arpa/nameser_compat.h)
	AC_CHECK_FUNCS(bswap_32 bswap_16)

	dnl checks for jabber
	dnl AC_CHECK_SIZEOF(short)
	AC_CHECK_FUNCS(snprintf connect)
	AC_CHECK_LIB(nsl, gethostent)

	dnl checks for zephyr
	AC_DEFINE(ZEPHYR_INT32, long)
	AC_SUBST(KRB4_CFLAGS)
	AC_SUBST(KRB4_LDFLAGS)
	AC_SUBST(KRB4_LIBS)
	if test "$kerberos" != "no" ; then
		if test "$kerberos" != "yes" ; then
			KRB4_CFLAGS="-I${kerberos}/include"
			if test -d "$kerberos/include/kerberosIV" ; then
				KRB4_CFLAGS="$KRB4_CFLAGS -I${kerberos}/include/kerberosIV"
			fi
			KRB4_LDFLAGS="-L${kerberos}/lib"
		elif test -d /usr/local/include/kerberosIV ; then
			KRB4_CFLAGS="-I/usr/local/include/kerberosIV"
		elif test -d /usr/include/kerberosIV ; then
			KRB4_CFLAGS="-I/usr/include/kerberosIV"
		fi
		AC_DEFINE(ZEPHYR_USES_KERBEROS)

		orig_LDFLAGS="$LDFLAGS"
		LDFLAGS="$LDFLAGS $KRB4_LDFLAGS"
		AC_CHECK_LIB(krb4, krb_rd_req,
				[KRB4_LIBS="-lkrb4 -ldes425 -lkrb5 -lk5crypto -lcom_err"],
				[AC_CHECK_LIB(krb, krb_rd_req,
					[KRB4_LIBS="-lkrb -ldes"],
					[AC_ERROR(Kerberos 4 libraries not found)],
					-ldes)],
				-ldes425 -lkrb5 -lk5crypto -lcom_err)
		orig_LIBS="$LIBS"
		LIBS="$LIBS $KRB4_LIBS"
		AC_CHECK_FUNCS(krb_set_key krb_rd_req krb_get_lrealm)
		AC_CHECK_FUNCS(krb_get_err_text krb_log)
		LIBS="$orig_LIBS"
		LDFLAGS="$orig_LDFLAGS"
	fi
	AC_CHECK_FUNCS(gethostid lrand48)
	AC_CHECK_FUNCS(memcpy memmove random strchr strerror vprintf)
	AC_CHECK_HEADERS(malloc.h paths.h sgtty.h stdarg.h sys/cdefs.h)
	AC_CHECK_HEADERS(sys/file.h sys/filio.h sys/ioctl.h sys/msgbuf.h)
	AC_CHECK_HEADERS(sys/select.h sys/uio.h sys/utsname.h sys/wait.h)
	AC_CHECK_HEADERS(termios.h)

	AM_CONDITIONAL(PRPLS, test "x$enable_plugins" = "xyes")
else
	enable_prpls=no
fi

AC_OUTPUT([Makefile
	   src/Makefile
	   intl/Makefile
           sounds/Makefile
	   plugins/Makefile
	   plugins/yay/Makefile
	   plugins/icq/Makefile
	   plugins/msn/Makefile
	   plugins/jabber/Makefile
	   plugins/zephyr/Makefile
           pixmaps/Makefile
           libfaim/Makefile
	   po/Makefile.in 
           doc/Makefile
           m4/Makefile
	   gaim.spec
          ])

echo
echo $PACKAGE $VERSION

echo
echo Allow Multiple Connections. : $enable_multi
echo Build Protocol Plugins..... : $enable_prpls
echo
echo Build with GNOME bits...... : $enable_gnome
echo Build as GNOME applet...... : $enable_panel
echo
echo Build with Plugin support.. : $enable_plugins
echo Build with Perl support.... : $enable_perl
echo
echo Build with ESD............. : $enable_esd
echo Build with NAS............. : $enable_nas
echo
echo Print debugging messages... : $enable_debug
echo
echo Gaim will be installed in $prefix/bin.
if test "x$gaimpath" != "x" ; then
	echo Warning: You have an old copy of gaim at $gaimpath.
fi
echo
echo configure complete, now type \'make\'
echo
