dnl# -*- sh -*-
dnl# Process this file with autoconf to produce a configure script.
dnl#
dnl#Copyright (c) 1998 Michal Vitecek <fuf@fuf.sh.cvut.cz>
dnl#Copyright (c) 2001,1998 Sasha Vasko <sasha at aftercode.net>
dnl#Copyright (c) 1998 Juergen Sawinski <Juergen.Sawinski@urz.uni-heidelberg.de>
dnl#Copyright (c) 1997 1998 Guylhem AZNAR <guylhem@oeil.qc.ca>

AC_INIT(libAfterImage,1.00,as-bugs@afterstep.org,libAfterImage.tar)

AC_CONFIG_HEADER(config.h:config.h.in)

version=$PACKAGE_VERSION
version_major="0"
lib_name="LIBAFTERIMAGE"
lib_file_name=$PACKAGE_NAME

dnl# Setup command-line options

dnl# we need the standard prefixes defined early
test "x$prefix" = xNONE && prefix=$ac_default_prefix
test "x$exec_prefix" = xNONE && exec_prefix="$prefix"
eval "mandir=$mandir"
eval "libdir=$libdir"
eval "includedir=$includedir"

dnl# compile-time switches
AC_ARG_ENABLE(i18n,			[  --enable-i18n            support I18N [no] ],enable_i18n=$enableval,enable_i18n="no")
AC_ARG_ENABLE(xlocale,		[  --enable-xlocale         using X_LOCALE [no] ],enable_xlocale=$enableval,enable_xlocale="no")
AC_ARG_WITH(locale,			[  --with-locale=LOCALE     locale name you want to use ],with_locale=$withval,with_locale="")
AC_ARG_ENABLE(staticlibs,   [  --enable-staticlibs      enable linking to libAfterImage statically [yes] ],enable_staticlibs=$enableval,enable_staticlibs="yes")
AC_ARG_ENABLE(sharedlibs,   [  --enable-sharedlibs      enable linking to libAfterImage dynamically [no] ],enable_sharedlibs=$enableval,enable_sharedlibs="no")

AC_ARG_ENABLE(gdb,			[  --enable-gdb             add gdb symbols (-g) (for debugging) [no] ],enable_gdb=$enableval,enable_gdb="no")
AC_ARG_ENABLE(warn,			[  --enable-warn            turn on more compiler warnings (for debugging) [no] ],enable_warn=$enableval,enable_warn="no")
AC_ARG_ENABLE(gprof,		[  --enable-gprof           add gprof symbols (-pg) (for debugging) [no] ],enable_gprof=$enableval,enable_gprof="no")
AC_ARG_ENABLE(audit,		[  --enable-audit           memory usage audit (for debugging) [no] ],enable_audit=$enableval,enable_audit="no")
AC_ARG_ENABLE(shmimage,		[  --enable-shmimage        enable usage of MIT shared memory extention for image transfer [no] ],enable_shmimage=$enableval,enable_shmimage="no")
AC_ARG_ENABLE(shaping,		[  --enable-shaping        enable usage of MIT shaped windows extention [yes] ],enable_shaping=$enableval,enable_shaping="yes")

AC_ARG_ENABLE(mmx_optimization,
							[  --enable-mmx-optimization  enable utilization of MMX instruction set to speed up imaging operations [yes] ],enable_mmx_optimization=$enableval,enable_mmx_optimization="yes")

AC_ARG_WITH(xpm,	    	[  --with-xpm               support XPM image format [yes]])
AC_ARG_WITH(builtin_xpm,    [  --with-builtin-xpm       use builtin XPM parsing/handling instead of libXpm [yes]])
AC_ARG_WITH(xpm_includes,   [  --with-xpm-includes=DIR  use libXpm includes in DIR( when builtin XPM handling is disabled )], xpm_includes="$withval", xpm_includes=NO)
AC_ARG_WITH(jpeg,		    [  --with-jpeg              support JPEG image format [yes]])
AC_ARG_WITH(jpeg_includes,  [  --with-jpeg-includes=DIR use JPEG includes in DIR], jpeg_includes="$withval", jpeg_includes=NO)
AC_ARG_WITH(png,		    [  --with-png               support PNG image format [yes]])
AC_ARG_WITH(png_includes,   [  --with-png-includes=DIR  use PNG includes in DIR], png_includes="$withval", png_includes=NO)
AC_ARG_WITH(ungif,		    [  --with-ungif             support Uncompressed GIF image format using libungif [no]],with_ungif="$withval",with_ungif=no)
AC_ARG_WITH(gif,		    [  --with-gif               support GIF image format using libgif   [no]],with_gif="$withval",with_gif=no)
AC_ARG_WITH(gif_includes,   [  --with-gif-includes=DIR  use GIF includes in DIR], gif_includes="$withval", gif_includes=NO)
AC_ARG_WITH(builtin_ungif,  [  --with-builtin-ungif             support Uncompressed GIF image format using builtin libungif [yes]],with_builtin_ungif="$withval",with_builtin_ungif=yes)
AC_ARG_WITH(tiff,		    [  --with-tiff              support TIFF image format [yes]])
AC_ARG_WITH(tiff_includes,  [  --with-tiff-includes=DIR use TIFF includes in DIR], tiff_includes="$withval", tiff_includes=NO)

AC_ARG_WITH(tff,		    [  --with-ttf               support True Type Fonts via FreeType library [yes]])
AC_ARG_WITH(tff_includes,   [  --with-ttf-includes=DIR  use FreeType includes in DIR], ttf_includes="$withval", ttf_includes=NO)

AC_ARG_WITH(afterbase,		    [  --with-afterbase               use libAfterBase library of basic functionality [yes]])
AC_ARG_WITH(afterbase_includes, [  --with-afterbase-includes=DIR  use libAfterBase includes in DIR], afterbase_includes="$withval", afterbase_includes=NO)

dnl# Check for compiler tools

AC_PROG_CC
dnl# AC_SUBST(CC)
dnl# AC_SUBST(GCC)

AC_CYGWIN
if test "x$CYGWIN" = "x" -a "x$GCC" = "xyes" ; then
  if test -z "`echo $CFLAGS | grep '\-fPIC' 2> /dev/null`" ; then
  	CFLAGS="$CFLAGS -fPIC"
  fi
  LDFLAGS="$LDFLAGS -rdynamic"
fi


if test "x$FROM_AFTERSTEP_CONFIGURE" = "xyes"; then

dnl# AfterSTep already checked everything for us
	aaa=bbb
	dnl#  AC_PROG_INSTALL
    AC_SUBST(INSTALL)
    AC_SUBST(INSTALL_PROGRAM)
    AC_SUBST(INSTALL_DATA)
    AC_SUBST(INSTALL_LIB)
    AC_SUBST(RANLIB)
    AC_SUBST(RM)
    AC_SUBST(MV)
    AC_SUBST(CP)
    AC_SUBST(MKDIR)
    AC_SUBST(PERL)
    AC_SUBST(FIND)
    AC_SUBST(XARGS)
    AC_SUBST(LDCONFIG)
    AC_SUBST(LEX)
    AC_SUBST(LEXLIB)
    AC_SUBST(YACC)
    AC_SUBST(sgmltools)
    if test "x$have_shmimage" = "xyes"; then
		AC_DEFINE(XSHMIMAGE)
    fi
    if test "x$have_shaping" = "xyes"; then
		AC_DEFINE(SHAPE)
    fi

else

dnl# we are alone - have to do all the checks ourselves :
	dnl# Add -Wall
	if test "x$GCC" = "xyes"; then
	  if test -z "`echo $CFLAGS | grep '\-Wall' 2> /dev/null`" ; then
  		CFLAGS="$CFLAGS -Wall"
	  fi
	fi

	dnl# Add -fPIC for IA64 compilation

	dnl# Add -g
	if test "x$enable_gdb" = "xyes"; then
  		if ! test -n "`echo $CFLAGS' ' | grep '\-g ' 2> /dev/null`" ; then
			CFLAGS="$CFLAGS -g"
  		fi
	else
  		DIRTY_CFLAGS="$CFLAGS"
  		CFLAGS="-DNO_DEBUG_OUTPUT"
  		for f in $DIRTY_CFLAGS; do 
        		if test "x$f" != "x-g"; then
	   		CFLAGS="$CFLAGS $f"
			fi	 
  		done
	fi
	dnl# Add -pg
	if test "x$enable_gprof" = "xyes"; then
	  CFLAGS="$CFLAGS -pg"
	  LDFLAGS="$LDFLAGS -pg"
	fi
	dnl# Add lots more warnings
	if test "x$enable_warn" = "xyes"; then
	  CFLAGS="$CFLAGS -Wuninitialized -Wwrite-strings -Wcast-qual -Wbad-function-cast -Wpointer-arith -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wnested-externs -Wconversion -Wcomment -Wcast-align -Winline -Wshadow -Wredundant-decls -Wid-clash-31"
	fi
	AC_PROG_INSTALL
	dnl# if the path to the install program is relative, make it absolute
	currdir=`pwd`
	INSTALL=`echo $INSTALL|sed -e "s@\^\.\.@${currdir}@" -e "s@^autoconf@${currdir}/autoconf@"`
	INSTALL_PROGRAM=`echo $INSTALL_PROGRAM|sed -e "s@\^\.\.@${currdir}@" -e "s@^autoconf@${currdir}/autoconf@"`
	INSTALL_DATA=`echo $INSTALL_DATA|sed -e "s@\^\.\.@${currdir}@" -e "s@^autoconf@${currdir}/autoconf@"`
	INSTALL_LIB=`echo $INSTALL_LIB|sed -e "s@\^\.\.@${currdir}@" -e "s@^autoconf@${currdir}/autoconf@"`

	AC_PROG_RANLIB
	AC_PATH_PROG(RM, rm, rm)
	AC_PATH_PROG(MV, mv, mv)
	AC_PATH_PROG(CP, cp, cp)
	AC_PATH_PROG(MKDIR, mkdir, mkdir)
	AC_PATH_PROG(PERL, perl, perl)
	AC_PATH_PROG(FIND, find, find)
	AC_PATH_PROG(XARGS, xargs, xargs)
	AC_PATH_PROG(LDCONFIG, ldconfig, ldconfig, $PATH:/sbin:/usr/local/sbin)
	AC_CHECK_PROG(sgmltools, sgmltools, HAVE_SGMLTOOLS, NO_SGMLTOOLS)
fi

if test "x$enable_gdb" = "xno"; then
		DIRTY_CFLAGS="$CFLAGS"
  		CFLAGS="-O3"
  		for f in $DIRTY_CFLAGS; do 
       		if test "x$f" != "-O2" -a "x$f" != "-O3"; then
		   		CFLAGS="$CFLAGS $f"
			fi	 
  		done
fi


dnl# Check for MMX :
if test "x$enable_mmx_optimization" = "xyes"; then
  AC_MSG_CHECKING(for MMX support)
  if test -z "`echo $build_cpu 2>&1 | grep i*86 >/dev/null`"; then
	AC_TRY_RUN([int main(){int mmx_available = 0;
				  asm volatile ( "pushl %%eax\n\t"
                      "pushl %%ebx\n\t"
                      "pushl %%ecx\n\t"
                      "pushl %%edx\n\t"
                      "movl \$1, %%eax\n\t"
                      "cpuid\n\t"
                      "andl \$0x800000, %%edx \n\t"
					  "movl %%edx, %0\n\t"
                      "popl %%edx\n\t"
                      "popl %%ecx\n\t"
                      "popl %%ebx\n\t"
                      "popl %%eax\n\t"
                      : "=m" (mmx_available)
                      : /* no input */
					  : "ebx", "ecx", "edx"
				);return mmx_available!=0?0:1;}],
				[AC_DEFINE(HAVE_MMX,1,[Define if CPU supports MMX instructions]) AC_MSG_RESULT(yes)],AC_MSG_RESULT(no))
  else
	  AC_MSG_RESULT(skipped)
  fi
fi

dnl# Check for X :
dnl# check for X, if top level configure script did not do that yet :
if test "x$PATH_XTRA_CHECKED" != "xyes"; then
	AC_PATH_XTRA
else
  if test "x$have_x" != "xyes"; then
	  AC_DEFINE(X_DISPLAY_MISSING)
  fi
fi

user_ldflags=$LDFLAGS

if test "x$have_x" = "xyes"; then
  x_libs="$X_LIBS $X_EXTRA_LIBS $X_PRE_LIBS"
  if test "x$user_ldflags" = "x"; then
  	full_x_libs=$x_libs
  else
	full_x_libs="$user_ldflags $x_libs"
  fi
  AC_CHECK_LIB(X11, XOpenDisplay, [x_libs="-lX11 $x_libs"],,$full_x_libs)
fi



dnl# some standard checks :
dnl# let's check for endiannes of our box
AC_C_BIGENDIAN
AC_C_INLINE

dnl# Check for headers
AC_HEADER_TIME
AC_CHECK_HEADERS(sys/wait.h sys/time.h malloc.h stdlib.h unistd.h stddef.h stdarg.h errno.h)

dnl# Check for X shaped window extension
have_shmimage=no
have_xext_lib=no
if test "x$enable_shmimage" = "xyes"; then
	AC_CHECK_LIB(Xext, XShmCreateImage, [have_shmimage=yes],,$full_x_libs)
	if test "x$have_shmimage" = "xyes"; then
		AC_CHECK_HEADER(sys/ipc.h,,[have_shmimage=no])
	fi
	if test "x$have_shmimage" = "xyes"; then
		AC_CHECK_HEADER(sys/shm.h,,[have_shmimage=no])
	fi
	if test "x$have_shmimage" = "xyes"; then
		have_xext_lib=yes
		AC_DEFINE(XSHMIMAGE,1,[Define if support for shared memory XImages is available])
	fi
fi

dnl# Check for X shaped window extension
HAVESHAPE="NOSHAPE"
if test "x$enable_shaping" = "xyes"; then
  	AC_CHECK_LIB(Xext, XShapeCombineMask, [have_xext_lib=yes;AC_DEFINE(SHAPE,1,Support for shaped windows)],,$full_x_libs)
fi

if test "x$have_xext_lib" = "xyes"; then
    x_libs="$x_libs -lXext"
fi

dnl# Check for graphics libraries :

image_format="xpm"

AFTERIMAGE_LIBS=

if test "$with_xpm" = no; then
  have_xpm=disabled
else
  if test "$with_builtin_xpm" = no; then
  	SAVED_CFLAGS=$CPPFLAGS
  	if test "x$xpm_includes" != "xNO"; then
		XPM_CFLAGS="-I$xpm_includes"
		CPPFLAGS="$CPPFLAGS $XPM_CFLAGS"
  	fi
	have_libxpm=no
  	AC_CHECK_HEADER(xpm.h,have_libxpm=yes)
	if test "x$have_libxpm" = "xno"; then
		AC_CHECK_HEADER(X11/xpm.h,have_libxpm_x11="yes")
		if test "x$have_libxpm_x11" = "xyes"; then
			 AC_DEFINE(HAVE_LIBXPM_X11,1,[Define if support for XPM images should be through libXpm library in Xlib])
			 have_libxpm=yes
		fi
	fi
  	if test "x$have_libxpm" = "xyes"; then
		AC_DEFINE(HAVE_XPM,1,[Define if support for XPM images is desired])
		AC_DEFINE(HAVE_LIBXPM,1,[Define if support for XPM images should be through libXpm])
		AFTERIMAGE_LIBS="$AFTERIMAGE_LIBS -lXpm"
        fi
  	CPPFLAGS=$SAVED_CFLAGS
  else
	AC_DEFINE(HAVE_XPM,1,[Define if support for XPM images is desired])
	XPM_CFLAGS=
  fi
fi
AC_SUBST(XPM_CFLAGS)

JPEG_OBJS=
ZLIB_OBJS=
PNG_OBJS=

have_jpeg=

if test "$with_jpeg" = no; then
  have_jpeg=disabled
else
  SAVED_CFLAGS=$CPPFLAGS
  if test "x$jpeg_includes" != "xNO"; then
	JPEG_CFLAGS="-I$jpeg_includes"
	CPPFLAGS="$CPPFLAGS $JPEG_CFLAGS"
  fi
  AC_CHECK_HEADER(jpeglib.h,have_jpeg=yes)
  if test "$have_jpeg" = yes; then
	AC_DEFINE(HAVE_JPEG,1,[Define if libjpeg is available]) 
	AFTERIMAGE_LIBS="$AFTERIMAGE_LIBS -ljpeg"
  else
	AC_MSG_CHECKING(using built-in JPEG library) 
	if test -f libjpeg/jpeglib.h; then
		JPEG_OBJS='$(LIBJPEG_OBJS)'
		AC_DEFINE(HAVE_JPEG,1,[Define if libjpeg is available]) 
		AC_DEFINE(HAVE_BUILTIN_JPEG,1,[Define if using builtin libjpeg]) 
		AC_MSG_RESULT(yes)
	else
		AC_MSG_RESULT(no)
	fi
   fi	
  CPPFLAGS=$SAVED_CFLAGS
fi
AC_SUBST(JPEG_CFLAGS)

dnl# these check are needed by built-in jpeg library : 
AC_DEFINE(HAVE_PROTOTYPES,1,We always use function prototypes - not supporting old compilers)
AC_CHECK_TYPE(unsigned char)
AC_CHECK_TYPE(unsigned short)
AC_C_CONST
AC_C_CHAR_UNSIGNED

AC_CHECK_HEADERS(zlib.h,[have_zlib=yes],[have_zlib=no])

if test "$with_png" = no; then
  have_png=disabled
else
  SAVED_CFLAGS=$CPPFLAGS
  if test "$png_includes" != NO; then
	PNG_CFLAGS="-I$png_includes"
	CPPFLAGS="$CPPFLAGS $PNG_CFLAGS"
  fi
  have_png=no
  AC_CHECK_HEADER(png.h,have_png=yes)
  if test "$have_png" = yes; then
        AC_MSG_CHECKING(for png_get_sRGB in png.h)
        AC_EGREP_HEADER(png_get_sRGB,png.h,,have_png=no)
        AC_MSG_RESULT($have_png)
  fi
  if test "$have_png" = yes; then
        AC_DEFINE(HAVE_PNG,1,[Define if libpng is available])
        AFTERIMAGE_LIBS="$AFTERIMAGE_LIBS -lpng"
        image_format="png"
  else 
	AC_MSG_CHECKING(using built-in PNG libraries) 
	if test -f libpng/png.h; then
		PNG_OBJS='$(LIBPNG_OBJS)'	
	        AC_DEFINE(HAVE_PNG,1,[Define if libpng is available])
        	AC_DEFINE(HAVE_BUILTIN_PNG,1,[Define if using builtin libpng])
		AC_MSG_RESULT(yes)	     
		if test "$have_zlib" = no; then 
			AC_MSG_CHECKING(using built-in ZLIB library) 
			if test -f zlib/zlib.h; then
				ZLIB_OBJS='$(ZLIB_OBJS)'
				AC_MSG_RESULT(yes)	     
			else
				AC_MSG_RESULT(no)	     
			fi
		fi
		image_format="png"
		have_png=yes
	else
		AC_MSG_RESULT(no)
	fi
  fi
  CPPFLAGS=$SAVED_CFLAGS
fi
AC_SUBST(PNG_CFLAGS)
dnl# This is required by built in versions of zlib and libpng : 

dnl# Test for libungif first :
have_gif=no
if test "$with_gif$with_ungif" = nono; then
  have_gif=disabled
else
  SAVED_CFLAGS=$CPPFLAGS
  if test "$gif_includes" != NO; then
	GIF_CFLAGS="-I$gif_includes"
	CPPFLAGS="$CPPFLAGS $GIF_CFLAGS"
  fi
  AC_CHECK_HEADER(gif_lib.h,have_gif=yes)
  CPPFLAGS=$SAVED_CFLAGS
  if test "x$have_gif" = "xyes"; then
	AC_DEFINE(HAVE_GIF,1,[Define if libgif is available])
	if test "$with_gif" = no; then
		AFTERIMAGE_LIBS="$AFTERIMAGE_LIBS -lungif"
	else
		AFTERIMAGE_LIBS="$AFTERIMAGE_LIBS -lgif"
	fi
  fi
fi

if test "x$have_gif" != "xyes"; then
	if test "x$with_builtin_ungif" = "xyes"; then
		AC_MSG_CHECKING(using built-in UNGIF library) 
		if test -f libungif/gif_lib.h; then
			UNGIF_OBJS='$(LIBUNGIF_OBJS)'	
			AC_DEFINE(HAVE_GIF,1,[Define if libgif/ungif is available])
        		AC_DEFINE(HAVE_BUILTIN_UNGIF,1,[Define if using builtin libungif])
			AC_MSG_RESULT(yes)	     
			have_gif=yes
		else
			AC_MSG_RESULT(no)
		fi
	fi
fi

AC_SUBST(GIF_CFLAGS)

dnl# Test for libtiff :

if test "$with_tiff" = no; then
  have_tiff=disabled
else
  SAVED_CFLAGS=$CPPFLAGS
  if test "$tiff_includes" != NO; then
	TIFF_CFLAGS="-I$tiff_includes"
	CPPFLAGS="$CPPFLAGS $TIFF_CFLAGS"
  fi
  AC_CHECK_HEADER(tiffio.h,[AC_DEFINE(HAVE_TIFF,1,[Define if libtiff is available]) AFTERIMAGE_LIBS="-ltiff $AFTERIMAGE_LIBS" have_tiff=yes])
  CPPFLAGS=$SAVED_CFLAGS
fi
AC_SUBST(TIFF_CFLAGS)

if test "$have_tiff" = yes -o "$have_png" = yes ; then
  if test "x$ZLIB_OBJS" = "x" -a "$have_zlib" = yes; then 
	  AFTERIMAGE_LIBS="$AFTERIMAGE_LIBS -lz"
  fi
fi

dnl# we always need libm since we now call exp() in gaussian blur
AFTERIMAGE_LIBS="$AFTERIMAGE_LIBS -lm"

dnl# check for libfreetype

TTF_INCLUDES=

if test "x$with_ttf" = "xNO"; then
  have_ttf=disabled
else
  if test "x$ttf_includes" = "xNO"; then
    AC_MSG_CHECKING(headers required by TTF support)
    if freetype-config --cflags >/dev/null  ;then
	  present_incs="$CCFLAGS $x_includes";
	  for l in `freetype-config --cflags` ;	do
	    inc_found=no
	    for p in `echo "$present_incs"` ; do
		if test "x$l" = "x$p" ; then
    		    inc_found=yes;
		fi;
		if test "x$l" = "x-I$p" ; then
    		    inc_found=yes;
		fi;
	    done;
	    if test "x$inc_found" != "xyes" ; then
		TTF_INCLUDES="$TTF_INCLUDES $l" ;
	    fi;
	  done;
	  AC_MSG_RESULT($TTF_INCLUDES)
	  saved_CPPFLAGS=$CPPFLAGS
	  saved_CFLAGS=$CFLAGS
	  CPPFLAGS="$TTF_INCLUDES $CPPFLAGS"
	  CFLAGS="$CFLAGS $TTF_INCLUDES $XPM_CFLAGS $JPEG_CFLAGS $PNG_CFLAGS $GIF_CFLAGS $TIFF_CFLAGS $X_CFLAGS"
	  have_ft2build=no
	  AC_CHECK_HEADERS(ft2build.h,[have_ft2build=yes])
	  AC_MSG_CHECKING( freetype/freetype.h )
	  have_ttf=no
          have_freetype_freetype=no
	  have_freetype=no
	  AC_EGREP_HEADER(FT_Face,freetype/freetype.h,
	                  [have_freetype_freetype=yes],
	                  [AC_MSG_RESULT(no);AC_MSG_CHECKING(freetype.h); \
                                             AC_EGREP_HEADER(FT_Face,freetype.h,
					                   [have_freetype=yes], AC_MSG_RESULT(no))]
					  )
	  if test "x$have_ft2build" = "xno" ; then
    	  	if test "x$have_freetype_freetype" = "xyes" ; then
			AC_TRY_RUN([#include <freetype/freetype.h>
                            	int main(){FT_Face f; return 0;}],
	                   	[have_ttf=yes],AC_MSG_RESULT(no))
	  	elif test "x$have_freetype" = "xyes" ; then
			AC_TRY_RUN([#include <freetype.h>
                            	int main(){FT_Face f; return 0;}],
	                   	[have_ttf=yes],AC_MSG_RESULT(no))
	  	fi;
	  elif test "x$have_freetype_freetype" = "xyes" ; then
		AC_TRY_RUN([#include <ft2build.h> 
			    #include <freetype/freetype.h>
                            int main(){FT_Face f; return 0;}],
	                [have_ttf=yes],AC_MSG_RESULT(no))
	  elif test "x$have_freetype" = "xyes" ; then
		AC_TRY_RUN([#include <ft2build.h>
			#include <freetype.h>
                        int main(){FT_Face f; return 0;}],
	                [have_ttf=yes],AC_MSG_RESULT(no))
	  fi;		       
    	  if test "x$have_ttf" = "xyes" ; then
		AC_DEFINE(HAVE_FREETYPE,1,[Define if libFreeType is available])
		if test "x$have_freetype_freetype" = "xyes" ; then 
			AC_DEFINE(HAVE_FREETYPE_FREETYPE,1,[Define if libFreeType has freetype.h in freetype/ ])
		fi;
                AC_MSG_RESULT(yes)
	  fi;
	  CPPFLAGS=$saved_CPPFLAGS
	  CFLAGS=$saved_CFLAGS
    else
		enable_ttf=no ;
		AC_MSG_RESULT(none found, ttf disabled)
		echo "You have to install FreeType libary in order to be able to use TTF fonts."
        have_ttf=no
    fi;
  else
	TTF_INCLUDES=$ttf_includes
	AC_DEFINE(HAVE_FREETYPE,1,[Define if libFreeType is available])
    have_ttf=yes
  fi
  if test "$have_ttf" = yes; then
	  AFTERIMAGE_LIBS="`freetype-config --libs` $AFTERIMAGE_LIBS"
  fi
fi;
AC_SUBST(TTF_INCLUDES)

AC_SUBST(JPEG_OBJS)
AC_SUBST(ZLIB_OBJS)
AC_SUBST(PNG_OBJS)
AC_SUBST(UNGIF_OBJS)

AFTERIMAGE_LIBS_EXTERNAL=$AFTERIMAGE_LIBS
AC_SUBST(AFTERIMAGE_LIBS_EXTERNAL)

USE_DEFAULT_AFTERBASE=0
AFTERBASE_INCS_PATH=
AC_MSG_CHECKING(libAfterBase headers)
if test "x$with_afterbase" != "xno"; then
  have_afterbase=yes
  if test "x$afterbase_includes" = "xNO"; then
	  if test -r "../libAfterBase/astypes.h"; then
		AFTERBASE_INCS_PATH="../"
	  else
 	    AC_CHECK_HEADER(libAfterBase/astypes.h,[USE_DEFAULT_AFTERBASE=1],[have_afterbase=no])
	  fi
  else
	  AFTERBASE_INCS_PATH="$afterbase_includes/"
  fi
fi

AFTERBASE_C=afterbase.o
HAVE_AFTERBASE_FLAG=0
if test "x$have_afterbase" = "xyes"; then
    HAVE_AFTERBASE_FLAG=1
	AFTERBASE_C=
    if test "x$have_x" != "xyes" -a "x$have_gif" = "xyes"; then
    	AFTERIMAGE_LIBS="$AFTERIMAGE_LIBS -lAfterBase"
    else
    	AFTERIMAGE_LIBS="-lAfterBase $AFTERIMAGE_LIBS"
    fi
    AFTERIMAGE_APPS_LIBS="-L../ -L../../libAfterBase -lAfterImage $AFTERIMAGE_LIBS"
    AC_DEFINE(HAVE_AFTERBASE,1,[Define if libAfterBase is available])
	AC_MSG_RESULT(yes  $AFTERBASE_INCS_PATH)
else
	AFTERIMAGE_APPS_LIBS="-L../ -lAfterImage $AFTERIMAGE_LIBS_EXTERNAL"
	if test "$with_afterbase" = "no"; then
	    AC_MSG_RESULT(disabled)
	else
	    AC_MSG_RESULT(none found)
	fi
	AC_HEADER_DIRENT
	AC_CHECK_HEADERS(sys/dirent.h)
fi
AC_SUBST(AFTERBASE_INCS_PATH)
AC_SUBST(USE_DEFAULT_AFTERBASE)
AC_SUBST(HAVE_AFTERBASE_FLAG)
AC_SUBST(AFTERBASE_C)
AC_SUBST(have_afterbase)
AC_SUBST(AFTERIMAGE_APPS_LIBS)

dnl# finalizing libs listing :

CLEAN_AFTERIMAGE_LIBS=
for L in $AFTERIMAGE_LIBS; do
  if test "$L" != "-L$libdir"; then
	  CLEAN_AFTERIMAGE_LIBS="$CLEAN_AFTERIMAGE_LIBS $L"
  fi
done
AFTERIMAGE_LIBS="-L$libdir -lAfterImage $CLEAN_AFTERIMAGE_LIBS"
AC_SUBST(AFTERIMAGE_LIBS)

if test "x$enable_staticlibs" = "xyes"; then
   LIBPROG='$(LIB_STATIC)'
   LIBINSTALL="install.static"
fi
if test "x$enable_sharedlibs" = "xyes"; then
   tmp='$(LIB_SHARED)'
   LIBPROG="$LIBPROG $tmp"
   LIBINSTALL="$LIBINSTALL install.dyn"
fi

dnl# define X_LOCALE.  need Linux, NetBSD and etc.
if test "x$enable_xlocale" = "xyes"; then
  DEFINE_XLOCALE="-DX_LOCALE"
fi

dnl# Translate enable_* from "yes/no" to "1/0" (for configure.h)

if test "x$enable_i18n" = "xyes"; then AC_DEFINE(I18N,1,[Define if locale support in X is needed]) fi
if test "x$enable_audit" = "xyes"; then AC_DEFINE(DEBUG_ALLOCS,1,[Define if memory allocation logging and debugging is desired]) fi

dnl# Variable strings declaration

AC_SUBST(image_format)

AC_SUBST(CFLAGS)
AC_SUBST(version)
AC_SUBST(version_major)
AC_SUBST(lib_name)
AC_SUBST(lib_file_name)
AC_SUBST(with_locale)
AC_SUBST(user_ldflags)
AC_SUBST(x_libs)
AC_SUBST(X_CFLAGS)
AC_SUBST(LIBPROG)
AC_SUBST(LIBINSTALL)
AC_SUBST(DEFINE_XLOCALE)
AC_SUBST(sgmltools)
AC_SUBST(PERL)

dnl# Write results

dnl# AC_CONFIG_HEADER(config.h)

dnl# Common parts of the Makefile

LIBAFTERIMAGEFILES=.shared
AC_SUBST_FILE(LIBAFTERIMAGEFILES)

AC_OUTPUT(Makefile \
afterbase.h \
afterimage-libs \
afterimage-config \
apps/Makefile)

chmod 755 afterimage-libs
cp config.h apps/
