dnl NOTE:
dnl This 'configure' and 'Imakefile.in' are written to be directory
dnl independent (using srcdir), however, xmkmf and Imake.tmpl do not
dnl allow us to switch compilation directory.
dnl
dnl $Id: configure.in,v 1.2 2001-03-14 12:09:57 thep Exp $
AC_INIT(image/imagetypes.c)

dnl Checks for programs.
AC_PROG_MAKE_SET
AC_CANONICAL_HOST
dnl  AC_PROG_CC cannot be specified here, since imake may not like the
dnl  compiler probed by AC_PROG_CC (i.e. gcc).
dnl AC_PROG_CC
AC_PROG_CPP
AC_PROG_AWK
AC_PROG_YACC
AC_PROG_LEX
AC_PATH_PROGS(mgp_cv_path_perl, perl, no)
AC_SUBST(mgp_cv_path_perl)
AC_PATH_PROGS(mgp_cv_path_uuencode, uuencode, no)
AC_SUBST(mgp_cv_path_uuencode)
AC_PATH_PROGS(mgp_cv_path_gzip, gzip, no)
AC_SUBST(mgp_cv_path_gzip)
AC_PATH_PROGS(mgp_cv_path_uudecode, uudecode, no)
AC_PATH_PROGS(mgp_cv_path_gunzip, gunzip, no)

OPTFLAGS=
AC_SUBST(OPTFLAGS)
DEPLIBS=
AC_SUBST(DEPLIBS)

case $host_os in
freebsd3*|freebsd2.2.6*|freebsd2.2.7*|freebsd2.2.8*)
	OPTFLAGS="$OPTFLAGS -DUUDECODEOPT=\\\"-s\\\""
	;;
esac
OPTFLAGS="$OPTFLAGS -DUUDECODE=\\\"$mgp_cv_path_uudecode\\\""
OPTFLAGS="$OPTFLAGS -DGUNZIP=\\\"$mgp_cv_path_gunzip\\\""

dnl Checks for debugging option.
AC_MSG_CHECKING(if --enable-debug option specified)
AC_ARG_ENABLE(debug,
	[  --enable-debug          compile debugging information in.],
	[mgp_debug="yes"; OPTFLAGS="$OPTFLAGS -g -DDEBUG -Wall"],
	[mgp_debug="no"])
AC_MSG_RESULT($mgp_debug)

dnl Checks for locale option.
AC_MSG_CHECKING(if --enable-locale option specified)
AC_ARG_ENABLE(locale,
	[  --enable-locale         perform setlocale() on invocation.],
	[mgp_locale="yes"; OPTFLAGS="$OPTFLAGS -DUSE_SETLOCALE"],
	[mgp_locale="no"])
AC_MSG_RESULT($mgp_locale)

dnl Checks for libraries.
LIBS="-lm $LIBS"

AC_MSG_CHECKING(if --disable-freetype option specified)
AC_ARG_ENABLE(freetype,
 	[  --disable-freetype      DON'T use freetype routines.],
 	[mgp_skip_freetype="yes"; mgp_use_freetype="no"],
 	[mgp_skip_freetype="no"; mgp_use_freetype="yes"])
AC_MSG_RESULT($mgp_skip_freetype)
 
if test "$mgp_use_freetype" = "yes"; then
 	AC_MSG_CHECKING(for freetype library/header)
 	for dir in /usr/local/freetype /usr/local /opt/freetype /usr/pkg /usr; do
 		ac_cv_ft_lib=no
 		ac_cv_ft_include=no
		shlib1=`echo $dir/lib/libttf.so* | head -1 | sed -e 's/.*\///'`
		if test x$shlib1 = x; then
			$shlib1=no
		fi
 		if test -d $dir/lib -a -f $dir/lib/$shlib1; then
 			ac_cv_ft_lib="$dir/lib"
 			ac_cv_ft_libpath="$shlib1"
 		elif test -d $dir/lib -a -f $dir/lib/libttf.a; then
 			ac_cv_ft_lib="$dir/lib"
 			ac_cv_ft_libpath="libttf.a"
 		fi
 		if test -d $dir/include -a -f $dir/include/freetype.h; then
 			ac_cv_ft_include="$dir/include"
 		elif test -d $dir/include/freetype -a -f $dir/include/freetype/freetype.h; then
 			ac_cv_ft_include="$dir/include/freetype"
		fi
 		if test "$ac_cv_ft_lib" != "no" -a "$ac_cv_ft_include" != "no"; then
 			LIBS="-L$ac_cv_ft_lib $LIBS"
 			CFLAGS="-I$ac_cv_ft_include $CFLAGS"
 			break
 		fi
 	done
 	if test "$ac_cv_ft_lib" = "no" -a "$ac_cv_ft_include" = "no"; then
 		AC_MSG_RESULT(no - not using freetype)
 		mgp_use_freetype=no
 	else
 		AC_MSG_RESULT($ac_cv_ft_lib and $ac_cv_ft_include)
 	fi
fi
if test "$mgp_use_freetype" = "yes"; then
	LIBS="-L$ac_cv_ft_lib $LIBS"
	OPTFLAGS="-I$ac_cv_ft_include $OPTFLAGS"
	DEPLIBS="$ac_cv_ft_lib/$ac_cv_ft_libpath $DEPLIBS"
 	AC_CHECK_LIB(ttf, TT_Init_FreeType, [],
		[echo "Fatal: libttf.a not found"
		exit 1])
 	AC_TRY_COMPILE([#include <freetype.h>], [], [], [dnl
 		echo "Fatal: freetype.h not found"
 		exit 1])
 	AC_DEFINE(FREETYPE)
	AC_MSG_CHECKING(for num_CharMaps field in TT_Face_Properties)
 	AC_TRY_COMPILE([#include <freetype.h>],
		[TT_Face_Properties x; return x.num_CharMaps;],
		[result=yes; AC_DEFINE(HAVE_TT_FACE_PROPERTIES_CHARMAPS)],
		[result=no])
	AC_MSG_RESULT($result)
fi

AC_MSG_CHECKING(if --disable-vflib option specified)
AC_ARG_ENABLE(vflib,
	[  --disable-vflib         DON'T use vflib routines.],
	[mgp_skip_vflib="yes"; mgp_use_vflib="no"],
	[mgp_skip_vflib="no"; mgp_use_vflib="yes"])
AC_MSG_RESULT($mgp_skip_vflib)

if test "$mgp_use_vflib" = "yes"; then
	AC_MSG_CHECKING(for VFlib library/header)
	for dir in /usr/local/VFlib /usr/local/vflib /usr/local /usr/pkg /usr; do
		ac_cv_vf_libname=VFlib
		ac_cv_vf_libdir=no
		ac_cv_vf_hdrdir=no
		shlib1=`echo $dir/lib/libVFlib.so* | head -1 | sed -e 's/.*\///'`
		shlib2=`echo $dir/lib/libVFlib2.so* | head -1 | sed -e 's/.*\///'`
		if test x$shlib1 = x; then
			$shlib1=no
		fi
		if test x$shlib2 = x; then
			$shlib2=no
		fi
		if test -d $dir/lib -a -f $dir/lib/$shlib1; then
			ac_cv_vf_libdir="$dir/lib"
 			ac_cv_vf_libpath=$shlib1
		elif test -d $dir/lib -a -f $dir/lib/libVFlib.a; then
			ac_cv_vf_libdir="$dir/lib"
			ac_cv_vf_libpath="libVFlib.a"
		elif test -d $dir/lib -a -f $dir/lib/$shlib2; then
			ac_cv_vf_libname=VFlib2
			ac_cv_vf_libdir="$dir/lib"
 			ac_cv_vf_libpath=$shlib2
		elif test -d $dir/lib -a -f $dir/lib/libVFlib2.a; then
			ac_cv_vf_libname=VFlib2
			ac_cv_vf_libdir="$dir/lib"
			ac_cv_vf_libpath="libVFlib2.a"
		fi
		if test -d $dir/include -a -f $dir/include/VF.h; then
			ac_cv_vf_hdrdir="$dir/include"
		fi
		if test "$ac_cv_vf_libdir" != "no" -a "$ac_cv_vf_hdrdir" != "no"; then
			LIBS="-L$ac_cv_vf_libdir $LIBS"
			CFLAGS="-I$ac_cv_vf_hdrdir $CFLAGS"
			break
		fi
	done
	if test "$ac_cv_vf_libdir" = "no" -a "$ac_cv_vf_hdrdir" = "no"; then
		AC_MSG_RESULT(no - not using VFlib)
		mgp_use_vflib=no
	else
		AC_MSG_RESULT($ac_cv_vf_libdir and $ac_cv_vf_hdrdir)
	fi
fi
if test "$mgp_use_vflib" = "yes"; then
	LIBS="-L$ac_cv_vf_libdir -l$ac_cv_vf_libname $LIBS"
	OPTFLAGS="-I$ac_cv_vf_hdrdir $OPTFLAGS"
	DEPLIBS="$ac_cv_vf_libdir/$ac_cv_vf_libpath $DEPLIBS"
	AC_CHECK_LIB($ac_cv_vf_libname, VF_Init,
		[mgp_cv_vflib_a=$ac_cv_vf_libpath],
		[echo "Fatal: $ac_cv_vf_libpath not found"
		exit 1])
	AC_TRY_COMPILE([#include <VF.h>], [], [], [dnl
		echo "Fatal: VF.h not found"
		exit 1])
	AC_DEFINE(VFLIB)

	AC_MSG_CHECKING(for VFlib configuration file)
	AC_ARG_WITH(vfontcap, [  --with-vfontcap=PATH    use PATH as vfontcap])
	ac_cv_vf_fontcap="no"
	if test -f "$with_vfontcap"; then
		ac_cv_vf_fontcap=$with_vfontcap;
	else
		for ac_dir in /usr/local/lib /usr/local/lib/tex \
		    /usr/local/lib/VFlib /usr/local/lib/fonts/vf \
		    /usr/local/lib /usr/local/etc /etc \
		    /usr/local/libdata/vflib /usr/local/share/VFlib \
		    /usr/local/share/VFlib/* \
		    ../../VFlib* ../VFlib* VFlib*; do
			if test -d $ac_dir -a -f $ac_dir/vfontcap; then
				ac_cv_vf_fontcap=$ac_dir/vfontcap;
				break
			fi
		done
	fi
	AC_MSG_RESULT($ac_cv_vf_fontcap)
	if test "$ac_cv_vf_fontcap" = "no"; then
		echo Fatal: no vfontcap file found.  cannot continue.
		echo Please use the option --with-vfontcap=PATH to specify the path to vfontcap.
		exit 1
	fi
	AC_DEFINE_UNQUOTED(VFCAP, "$ac_cv_vf_fontcap")
	AC_SUBST(mgp_cv_vflib_a)
fi

if test "$mgp_use_freetype" = "yes"; then
	AC_MSG_CHECKING(if --enable-freetype-charset16 option specified)
	AC_ARG_ENABLE(freetype-charset16,
		[  --enable-freetype-charset16         use freetype for chaset16.],
		[mgp_use_freetype_c16="yes"],
		[mgp_use_freetype_c16="no"])
	AC_MSG_RESULT($mgp_use_freetype_c16)

	if test "$mgp_use_freetype_c16" = "yes"; then
		AC_DEFINE(FREETYPE_CHARSET16)
	fi
fi

for i in /usr/pkg /usr/local /usr; do
	if test -f $i/include/png.h; then 
		if test -f $i/lib/libpng.a ; then
			DEPLIBS="$DEPLIBS $i/lib/libpng.a"
		elif test -f $i/lib/libpng.so ; then
			DEPLIBS="$DEPLIBS $i/lib/libpng.so"
		else
			break;
		fi
		LIBS="$LIBS -L$i/lib"
		OPTFLAGS="$OPTFLAGS -I$i/include"
		AC_CHECK_LIB(png, png_write_init, 
			[LIBS="$LIBS -lpng"
			 AC_DEFINE(USE_PNG)], 
		[AC_CHECK_LIB(png, png_read_init, 
			[LIBS="$LIBS -lpng -lz"
			 AC_DEFINE(USE_PNG)],
			[], [-lz])])
		break
	fi
done

dnl Checks for header files.
AC_PATH_X
AC_MSG_CHECKING(if --enable-gif option specified)
AC_ARG_ENABLE(gif,
	[  --enable-gif            compile gif support in (need libungif).],
	[mgp_gif="yes"],
	[mgp_gif="no"])
AC_MSG_RESULT($mgp_gif)
if test $mgp_gif = "yes"; then
	AC_CHECK_LIB(ungif, DGifOpenFileHandle,
		[gif_ok=yes
		LIBS="$LIBS -lungif"
		AC_DEFINE(USE_GIF)],
	[AC_CHECK_LIB(gif, DGifOpenFileHandle,
		[gif_ok=yes
		LIBS="$LIBS -lgif"
		AC_DEFINE(USE_GIF)],
	[AC_CHECK_LIB(ungif, DGifOpenFileName,
		[gif_ok=yes
		LIBS="$LIBS -lungif"
		AC_DEFINE(USE_GIF)],
	[AC_CHECK_LIB(gif, DGifOpenFilename,
		[gif_ok=yes
		LIBS="$LIBS -lgif"
		AC_DEFINE(USE_GIF)], 
		[], [-L$x_libraries -lX11])], [-L$x_libraries -lX11])])])
	if test "x$gif_ok" != "xyes"; then
		echo 'FATAL: You need libungif to use gif images.  pass proper LIBS to configure.'
		exit 1
	fi
	for i in /usr/pkg /usr/local /usr; do
		if test -f $i/include/gif_lib.h; then
			gif_h_ok=yes
			OPTFLAGS="$OPTFLAGS -I$i/include"
			break
		fi
	done
	if test "x$gif_h_ok" != "xyes"; then
		echo 'FATAL: can not find gif_lib.h.  pass proper CPPFLAGS to configure.'
		exit 1
	fi
fi

AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS(fcntl.h strings.h sys/file.h unistd.h sys/time.h sys/select.h stdarg.h varargs.h termios.h locale.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_CHECK_TYPE(u_int, unsigned int)
AC_CHECK_TYPE(u_char, unsigned char)

dnl Checks for library functions.
AC_FUNC_MEMCMP
AC_TYPE_SIGNAL
AC_CHECK_FUNCS(poll strstr _setbufend)
AC_REPLACE_FUNCS(usleep strsep strdup mkstemp snprintf memcmp)

dnl keyword substitution.
mgp_keywords=`$mgp_cv_path_perl $srcdir/keywords.pl $srcdir/globals.c`
AC_SUBST(mgp_keywords)
 
AC_OUTPUT(Imakefile image/Imakefile sample/Imakefile
	contrib/mgp2html.pl contrib/mgp2latex.pl contrib/mgpnet mgpembed.pl)

/bin/rm -f Makefile image/Makefile

echo "be sure to generate Makefile from Imakefile, by xmkmf and make Makefiles"
