#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

#AC_PREREQ(2.59)
AC_INIT(cvsgraph, 1.7.0, cvsgraph@akhphd.au.dk)
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([README])
AC_CONFIG_HEADER([config.h])
AC_CONFIG_FILES([Makefile contrib/Makefile cvsgraph.1 cvsgraph.conf.5 cvsgraph.spec contrib/mkimage.php contrib/cvsgraphwrapper.php ])

with_gd=yes

AC_DEFUN([AX_CHECK_GD], [
	AC_ARG_WITH(gd,
		AC_HELP_STRING([--with-gd(=DIR)], [use the gd library (in DIR)]),,
		with_gd=yes)


	AC_PATH_PROG(GDLIB_CONFIG, gdlib-config, , [$with_gd/bin:$PATH])
	if test -n "$GDLIB_CONFIG"; then
		GD_CFLAGS=`$GDLIB_CONFIG --cflags`
		GD_LDFLAGS=`$GDLIB_CONFIG --ldflags`
		GD_LIBS=`$GDLIB_CONFIG --libs`
	elif test -d "$with_gd"; then
		GD_CFLAGS="-I$with_gd/include"
		GD_LDFLAGS="-L$with_gd/lib"
		AC_CHECK_LIB(z, inflateReset, GD_LIBS="-lz")
		AC_CHECK_LIB(png, png_check_sig, GD_LIBS="-lpng $GD_LIBS", , $GD_LIBS)
		AC_CHECK_LIB(jpeg, jpeg_CreateCompress, GD_LIBS="-ljpeg $GD_LIBS", , $GD_LIBS)
	fi

	save_CFLAGS="$CFLAGS"
	CFLAGS="$GD_CFLAGS $CFLAGS"
	save_LDFLAGS="$LDFLAGS"
	LDFLAGS="$GD_LDFLAGS $LDFLAGS"
	save_LIBS="$LIBS"
	LIBS="$GD_LIBS $LIBS"

	AC_CHECK_LIB(gd, gdImageCreate, [
		AC_DEFINE(HAVE_GD, 1, [ Define if you have gd library. ])
		AC_CHECK_LIB(gd, gdImageGif, AC_DEFINE(HAVE_GD_GIF, 1, [ Define if GD supports gif. ]), , "$GD_LIBS")
		AC_CHECK_LIB(gd, gdImageJpeg, AC_DEFINE(HAVE_GD_JPEG, 1, [ Define if GD supports jpeg. ]), , "$GD_LIBS")
		AC_CHECK_LIB(gd, gdImagePng, AC_DEFINE(HAVE_GD_PNG, 1, [ Define if GD supports png. ]), , "$GD_LIBS")
		AC_CHECK_LIB(gd, gdImagePngEx, AC_DEFINE(HAVE_GD_PNGEX, 1, [ Define if GD supports extended png. ]), , "$GD_LIBS")
		AC_CHECK_LIB(gd, gdImageFilledArc, AC_DEFINE(HAVE_GD_FILLEDARC, 1, [ Define if GD supports filled arcs]), , "$GD_LIBS")

		if test "$ac_cv_lib_freetype_FT_Load_Glyph" = "yes"
		then
			AC_CHECK_LIB(gd, gdImageStringFT, AC_DEFINE(HAVE_GD_STRINGFT, 1, [ Define if GD supports truetype lib v2]), , "$GD_LIBS")
		fi
		if test "$ac_cv_lib_ttf_TT_Load_Glyph" = "yes"
		then
			AC_CHECK_LIB(gd, gdImageStringTTF, AC_DEFINE(HAVE_GD_STRINGTTF, 1, [ Define if GD supports truetype lib v1]), , "$GD_LIBS")
		fi
		GD_LIBS="-lgd $GD_LIBS"
	], , $GD_LIBS)

	CFLAGS="$save_CFLAGS"
	LDFLAGS="$save_LDFLAGS"
	if test -n "$GDLIB_CONFIG"; then
		LIBS="$GD_LIBS"
	else
		LIBS="$GD_LIBS $LIBS"
	fi
        AC_SUBST(GD_CFLAGS)
        AC_SUBST(GD_LDFLAGS)
        AC_SUBST(GD_LIBS)
])

# Checks for programs.
AC_PROG_CC
AC_PROG_LEX
AC_PROG_YACC
AC_PATH_PROG(ac_prog_pkg_config, pkg-config)

AC_CHECK_LIB(freetype, FT_Load_Glyph)
if test "$ac_cv_lib_freetype_FT_Load_Glyph" != "yes"
then
	AC_CHECK_LIB(ttf, TT_Load_Glyph)
fi

# find out what the GD library uses
AX_CHECK_GD

AC_CHECK_LIB(m, atan2)

# Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([getopt.h sys/wait.h gd.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_TYPE_SIZE_T
AC_HEADER_TIME

# Checks for library functions.
AC_CHECK_FUNCS([memset strdup strtol])

AC_OUTPUT
