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

orig_args="$*"

AC_INIT(fontforge/pfaedit.h)
AC_PROG_CC
AC_PROG_INSTALL
AC_DISABLE_STATIC
AC_ENABLE_SHARED
AC_CANONICAL_HOST
AC_SUBST(HOST)
HOST="$host"

gww_define_caps_for_alt="dontknow"
gww_nodynamic="no"
gww_needsstatic="no"
gww_ismac="no"
gww_needsstaticimages="no"
gww_rpath=""
AC_ARG_WITH(static-imagelibs,
 [  --with-static-imagelibs   Control whether to try using static image libaries],
 [
   if test "$withval" \!= "no" ; then
     gww_needsstaticimages="yes"
   fi
   dnl Mac doesn't ship with image libraries, so my builds should include
   dnl static versions, else users won't have them.

 ])

LDFLAGS="$LDFLAGS -L/usr/local/lib"

case "$host" in
*-apple-darwin*)
 gww_ismac="yes"
 AC_DEFINE(_CursorsMustBe16x16)
 AC_DEFINE(_Keyboard,1)
 AC_DEFINE(__Mac,1)
 gww_define_caps_for_alt="1"

dnl fink puts stuff under /sw

 CPPFLAGS="$CPPFLAGS -I/sw/include"
 LDFLAGS="$LDFLAGS -L/sw/lib"
 gww_rpath="-rpath /sw/lib"
 LIBS="/System/Library/Frameworks/CoreServices.framework/CoreServices $LIBS"
 ;;
powerpc-*-*linux*)
 AC_DEFINE(_Keyboard,3)
 ;;
*sparc*)
 AC_DEFINE(_Keyboard,2)
 ;;
*-pc-cygwin*)
 dnl building dynamic libraries on cygwin means marking each entry point
 dnl and each shared data item as exported. And marking anything borrowed
 dnl from another library as imported (stdout? strlen? fopen? malloc? X*)
 dnl Seems far too much work as I borrow freely from libc, X11, etc.

 ./configure.static
 exit

 AC_ENABLE_STATIC
 AC_DISABLE_SHARED

 AC_DEFINE(_BrokenBitmapImages)
 AC_DEFINE(_ModKeysAutoRepeat)
 AC_DEFINE(__CygWin,1)

dnl Although present on my cygwin system the Xi library does not load
dnl and crashes gdb if we attempt to use it.

 AC_DEFINE(_NO_XINPUT)

dnl on cygwin the file we dlopen is not the expected filename. We must
dnl search for a libtool .la file (libpng.la for example) to tell us
dnl what name we should dlopen. Sadly cygwin doesn't always provide
dnl these => we must link statically, or load at start up.

dnl so provide a simplistic function to check for the .la file
dnl AC_CHECK_LIB_LA(lib-name, action-if-found, action-if-missing)

AC_DEFUN([AC_CHECK_LIB_LA],
 [
  AC_CHECK_FILE(/lib/lib$1.la,[$2],[
   AC_CHECK_FILE(/usr/lib/lib$1.la,[$2],[
    AC_CHECK_FILE(/usr/local/lib/lib$1.la,[$2],[
     AC_CHECK_FILE(/usr/X11R6/lib/lib$1.la,[$2],[$3])])])])
 ])

 ;;
esac

AC_PROG_LIBTOOL

AC_ARG_WITH(capslock-for-alt,
 [  --with-capslock-for-alt   Control whether to use capslock rather than alt/meta to alter behavior of tools in outline/bitmap windows],
 [
   if test "$withval" \!= "2" ; then
     gww_define_caps_for_alt="2"
   elif test "$withval" \!= "no" ; then
     gww_define_caps_for_alt="1"
   else
     gww_define_caps_for_alt="0"
   fi
 ])
if test "$gww_define_caps_for_alt" \!= "dontknow" ; then
  AC_DEFINE_UNQUOTED(FONTFORGE_CONFIG_CAPSLOCK_FOR_ALT,$gww_define_caps_for_alt)
fi

dnl One user seems to have messed up iconv on his system and can't get it
dnl to work.

gww_uselessiconv="no"
AC_ARG_WITH(iconv,
 [  --without-iconv   Don't use the system's iconv(3) routines, use fontforge's instead],
 [
   if test "$withval" = "no" ; then
     gww_uselessiconv="yes"
   fi
 ])

AC_SUBST(FF_EXTRA_FILES)

AC_SEARCH_LIBS(dlopen, dl) dnl some systems have this in libc (Darwin uses dyld)
AC_SEARCH_LIBS(inet_aton, resolv) dnl some systems have this in libc
if test "$gww_uselessiconv" = "no" ; then
 AC_SEARCH_LIBS(libiconv, iconv) dnl linux has iconv in libc, mac,solaris have libiconv in libiconv, and a define to map iconv() to libiconv(), so we can't do the obvious check
fi

AC_CHECK_FUNC(tzset,:,AC_DEFINE(_NO_TZSET))

dnl Is there a better way to add a directory to the include path?

FreeType2_IncRoot=
if sh -c "freetype-config --prefix" >/dev/null 2>&1 ; then
 FreeType2_IncRoot=`freetype-config --prefix`
 AC_CHECK_FILE($FreeType2_IncRoot/include/freetype2,CFLAGS="$CFLAGS -I$FreeType2_IncRoot/include/freetype2/",FreeType2_IncRoot=)
fi

if test "$FreeType2_IncRoot" = "" >/dev/null 2>&1 ; then
 AC_CHECK_FILE(/usr/include/freetype2,[CFLAGS="$CFLAGS -I/usr/include/freetype2/"
     FreeType2_IncRoot="/usr"
 ],[
 AC_CHECK_FILE(/usr/local/include/freetype2,[CFLAGS="$CFLAGS -I/usr/local/include -I/usr/local/include/freetype2/"
     FreeType2_IncRoot="/usr/local/"
 ],[
 AC_CHECK_FILE(/usr/X11R6/include/freetype2,[CFLAGS="$CFLAGS -I/usr/X11R6/include -I/usr/X11R6/include/freetype2/"
     FreeType2_IncRoot="/usr/X11R6/"
 ],AC_DEFINE(_NO_FREETYPE))])])
fi

AC_CHECK_FILE(/usr/include/libxml2/libxml,[CFLAGS="$CFLAGS -I/usr/include/libxml2/"],[
 AC_CHECK_FILE(/usr/local/include/libxml2/libxml,[CFLAGS="$CFLAGS -I/usr/local/include -I/usr/local/include/libxml2/"],
  AC_DEFINE(_NO_LIBXML))])

dnl Some old versions of lib(un)gif don't work for us, they've got the
dnl Function argument in the wrong place. So two checks for gif.

AC_CHECK_LIB(ungif, DGifOpenFileName, [
echo -n checking for ExtensionBlock.Function in gif_lib.h...
AC_TRY_COMPILE([#include <gif_lib.h>], [ ExtensionBlock foo; foo.Function=3; ], echo " " yes , [echo " " no
    ac_cv_lib_ungif_DGifOpenFileName=no
    AC_DEFINE(_NO_LIBUNGIF)])
] , AC_DEFINE(_NO_LIBUNGIF))

dnl some people have installed libraries without include files.
dnl bloody useless thing to do, but check for both (don't need to check for
dnl zlib.h because we never use it directly, only used inside libpng)

AC_CHECK_LIB(z, deflateEnd, : ,AC_DEFINE(_NO_LIBPNG), -lm) 
AC_CHECK_LIB(png, png_create_read_struct, : ,[
 AC_CHECK_LIB(png12, png_create_read_struct, AC_DEFINE(_LIBPNG12), AC_DEFINE(_NO_LIBPNG), -lz -lm)], -lz -lm)
AC_CHECK_HEADER(png.h, : , AC_DEFINE(_NO_LIBPNG))
AC_CHECK_LIB(jpeg, jpeg_CreateDecompress, :  ,AC_DEFINE(_NO_LIBJPEG)) 
AC_CHECK_HEADER(jpeglib.h, : , AC_DEFINE(_NO_LIBJPEG))
AC_CHECK_LIB(tiff, TIFFOpen, : , AC_DEFINE(_NO_LIBTIFF), -lm )
AC_CHECK_HEADER(tiffio.h, : , AC_DEFINE(_NO_LIBTIFF))
case "$host" in
*-pc-cygwin*)
 AC_CHECK_LIB(xml2, xmlParseFile, : , AC_DEFINE(_NO_LIBXML), [-liconv -lz])
 ;;
*)
 AC_CHECK_LIB(xml2, xmlParseFile, : , AC_DEFINE(_NO_LIBXML))
 ;;
esac

if test "$gww_uselessiconv" = "no" ; then
 AC_CHECK_HEADERS([iconv.h])
fi

AC_CHECK_HEADERS([ieeefp.h])

dnl many systems have freetype on /usr/X11R6/lib which isn't in the standard search path

AC_CHECK_LIB(freetype, FT_New_Memory_Face, : , [
 gww_save_LDFLAGS="$LDFLAGS"
 LDFLAGS="$LDFLAGS -L/usr/X11R6/lib"
 dnl Avoid configure's cache.

 unset ac_cv_lib_freetype_FT_New_Memory_Face
 echo "${ac_cv_lib_freetype_FT_New_Memory_Face+set}"  set
 AC_CHECK_LIB(freetype, FT_New_Memory_Face,[
   gww_rpath="$gww_rpath -rpath /usr/X11R6/lib"
  ] , [
   LDFLAGS=$gww_save_LDFLAGS
   AC_DEFINE(_NO_FREETYPE)
  ])
])

gww_hasbytecode="no"
if test "$ac_cv_lib_freetype_FT_New_Memory_Face" = "yes" ; then
 AC_ARG_WITH(freetype-bytecode,
  [  --with-freetype-bytecode   Control whether we build with the bytecode interpreter],
  [
     gww_hasbytecode="$withval"
  ],
  [
    echo checking for bytecode interpreter in freetype...
    AC_CHECK_LIB(freetype, TT_RunIns, gww_hasbytecode="yes", : )
  ])
fi
if test "$gww_hasbytecode" = "yes" ; then
 AC_CHECK_LIB(pthread, pthread_create, LIBS="$LIBS -lpthread", gww_hasbytecode="no")
fi

if test \( "$gww_hasbytecode" = "yes" \) -a \( "$FreeType2_IncRoot" != "" \); then
 gww_hasbytecode="no"
 AC_ARG_WITH(freetype-src,
 [  --with-freetype-src=dir   Specify freetype source directory],
 [
    if test "$withval" != "no" >/dev/null 2>&1 ; then
	FreeType2_TT_BUILD_INC="$withval/src/truetype"
#	if test "${FreeType2_TT_BUILD_INC:0:1}" != "/" ; then
	if test `echo ${FreeType2_TT_BUILD_INC} | sed 's/\(.\).*/\1/'` != "/" ; then
		FreeType2_TT_BUILD_INC=`pwd`/$FreeType2_TT_BUILD_INC
	fi
	AC_MSG_CHECKING(for valid freetype tree)
	if test -f "$FreeType2_TT_BUILD_INC/ttobjs.h"; then
		AC_MSG_RESULT(ok)
	else
		AC_MSG_RESULT(missing ttobjs.h)
		echo "Unable to find $FreeType2_TT_BUILD_INC/ttobjs.h"
	fi
    fi
 ],
 [
	dnl guess directory
	AC_MSG_CHECKING(trying to find the freetype source directory -- be patient)
	  FreeType2_TT_SRC_DIRs=`find / -name ttobjs.h -print 2>/dev/null |grep '[freetype[^/\]*2[^/\]*/src/truetype/ttobjs.h]' | sort -r | sed -e 's/ttobjs.h//'`
	  for dir in $FreeType2_TT_SRC_DIRs; do
	   if cmp -s ${dir}../../include/freetype/freetype.h ${FreeType2_IncRoot}/freetype/freetype.h >/dev/null 2>&1 ; then
	    FreeType2_TT_BUILD_INC=$dir
	  break
	   fi
	  done
	  if test "$FreeType2_TT_BUILD_INC" = "" >/dev/null 2>&1 ; then
	   for dir in $FreeType2_TT_SRC_DIRs; do
	    has_goodft2sources="no"
	    FreeType2_TT_BUILD_INC=$dir
	   break
	   done
	  fi
	  if test "$FreeType2_TT_BUILD_INC" = "" >/dev/null 2>&1 ; then
	   echo " " no
	  else
	   echo " " probably
	  fi
 ])
 if test "$FreeType2_TT_BUILD_INC" \!= "" >/dev/null 2>&1 ; then
  if test -f $FreeType2_TT_BUILD_INC/ttdriver.h >/dev/null 2>&1 ; then
   CFLAGS="$CFLAGS -I$FreeType2_TT_BUILD_INC"
   gww_hasbytecode="yes"
   AC_DEFINE(FREETYPE_HAS_DEBUGGER)
  fi
 fi
fi

dnl older libc's don't have snprintf

AC_CHECK_FUNC(snprintf, : , AC_DEFINE(_NO_SNPRINTF))

AC_C_CONST
AC_PATH_XTRA
AC_SUBST(X_11LIB)
X_11LIB=-lX11
if test "$no_x" = "yes"; then
 X_11LIB=
else
 dnl AC_CHECK_HEADER("X11/extensions/XInput.h", : ,AC_DEFINE(_NO_XINPUT),)
 dnl AC_CHECK_HEADER("X11/extensions/XI.h", : ,AC_DEFINE(_NO_XINPUT),)
 dnl /usr/share/doc/XFree86-doc-4.2.0/Xi/lib.PS

 AC_CHECK_LIB(Xi, XOpenDevice, X_11LIB="-lXi -lX11" ,AC_DEFINE(_NO_XINPUT), $X_LIBS $X_PRE_LIBS $X_EXTRA_LIBS -lX11 ) 
 AC_CHECK_LIB(xkbui, XkbQueryExtension, X_11LIB="$X_11LIB -lxkbui" ,AC_DEFINE(_NO_XKB), $X_LIBS $X_PRE_LIBS $X_EXTRA_LIBS -lX11 -lm ) 
fi

echo -n checking for long long ...
AC_TRY_COMPILE(,[long long foo=0x400000000000;],[ AC_DEFINE(_HAS_LONGLONG)
    echo " " yes] , echo " " no )

dnl mac's don't have gettext, people have to use fink to install it

gww_has_gettext="yes"
AC_CHECK_HEADERS([libintl.h], : , gww_has_gettext="no")

gww_regular_link="no"
AC_ARG_WITH(regular-link,
 [  --with-regular-link   Don't try to dlopen libraries],
 [
    if test "$withval" != "no" >/dev/null 2>&1 ; then
	gww_regular_link="yes"
	AC_DEFINE(NODYNAMIC)
	gww_rpath=""
    fi
 ], : )

dnl If we can't/don't want to use dynamic libraries then we must link with
dnl the static libraries we've found

AC_SUBST(STATIC_LIBS)
STATIC_LIBS=
if test "$gww_nodynamic" = "yes" -o "$gww_regular_link" = "yes"; then
 if test "$gww_needsstatic" = "yes"; then
  STATIC_LIBS="$STATIC_LIBS -static"
 fi
 if test "$ac_cv_lib_ungif_DGifOpenFileName" = "yes"; then
  STATIC_LIBS="$STATIC_LIBS -lungif"
 fi
 if test "$ac_cv_lib_png_png_create_read_struct" = "yes"; then
  STATIC_LIBS="$STATIC_LIBS -lpng -lz"
 fi
 if test "$ac_cv_lib_png12_png_create_read_struct" = "yes"; then
  STATIC_LIBS="$STATIC_LIBS -lpng12 -lz"
 fi
 if test "$ac_cv_lib_tiff_TIFFOpen" = "yes"; then
  STATIC_LIBS="$STATIC_LIBS -ltiff"
 fi
 if test "$ac_cv_lib_jpeg_jpeg_CreateDecompress" = "yes"; then
  STATIC_LIBS="$STATIC_LIBS -ljpeg"
 fi
 if test "$ac_cv_lib_freetype_FT_New_Memory_Face" = "yes"; then
  STATIC_LIBS="$STATIC_LIBS -lfreetype"
 fi
 if test "$ac_cv_lib_xml2_xmlParseFile" = "yes"; then
  STATIC_LIBS="$STATIC_LIBS -lxml2"
 fi
 AC_CHECK_LIB(uninameslist, UnicodeNameAnnot, [
   STATIC_LIBS="$STATIC_LIBS -luninameslist"
   AC_DEFINE(_STATIC_LIBUNINAMESLIST) ] , AC_DEFINE(_NO_LIBUNINAMESLIST))
fi

AC_CHECK_LIB(intl, gettext, STATIC_LIBS="$STATIC_LIBS -lintl" ,HAVE_LIBINTL_H=0 )

if test "$gww_needsstaticimages" = "yes" -a "$gww_regular_link" = "no"; then
 if test "$gww_needsstatic" = "yes"; then
  STATIC_LIBS="$STATIC_LIBS -static"
 fi
 if test "$ac_cv_lib_ungif_DGifOpenFileName" = "yes"; then
  STATIC_LIBS="$STATIC_LIBS -lungif"
  AC_DEFINE(_STATIC_LIBUNGIF)
 fi
 if test "$ac_cv_lib_png_png_create_read_struct" = "yes"; then
  STATIC_LIBS="$STATIC_LIBS -lpng -lz"
  AC_DEFINE(_STATIC_LIBPNG)
 fi
 if test "$ac_cv_lib_png12_png_create_read_struct" = "yes"; then
  STATIC_LIBS="$STATIC_LIBS -lpng12 -lz"
  AC_DEFINE(_STATIC_LIBPNG)
 fi
 if test "$ac_cv_lib_tiff_TIFFOpen" = "yes"; then
  STATIC_LIBS="$STATIC_LIBS -ltiff"
  AC_DEFINE(_STATIC_LIBTIFF)
 fi
 if test "$ac_cv_lib_jpeg_jpeg_CreateDecompress" = "yes"; then
  STATIC_LIBS="$STATIC_LIBS -ljpeg"
  AC_DEFINE(_STATIC_LIBJPEG)
 fi
fi
if test "$gww_nodynamic" != "yes" -a "$gww_regular_link" != "yes" -a \
    "$gww_needsstaticimages" != "yes" ; then
case "$host" in
*-pc-cygwin*)
 if test "$ac_cv_lib_ungif_DGifOpenFileName" = "yes"; then
  AC_CHECK_LIB_LA(ungif, :, [ STATIC_LIBS="$STATIC_LIBS -lungif" ; AC_DEFINE(_STATIC_LIBUNGIF)])
 fi
 if test "$ac_cv_lib_png_png_create_read_struct" = "yes"; then
  AC_CHECK_LIB_LA(png, :, [ STATIC_LIBS="$STATIC_LIBS -lpng -lz" ; AC_DEFINE(_STATIC_LIBPNG)])
 fi
 if test "$ac_cv_lib_png12_png_create_read_struct" = "yes"; then
  AC_CHECK_LIB_LA(png12, :, [ STATIC_LIBS="$STATIC_LIBS -lpng12 -lz" ; AC_DEFINE(_STATIC_LIBPNG12)])
 fi
 if test "$ac_cv_lib_tiff_TIFFOpen" = "yes"; then
  AC_CHECK_LIB_LA(tiff, :, [ STATIC_LIBS="$STATIC_LIBS -ltiff" ; AC_DEFINE(_STATIC_LIBTIFF)])
 fi
 if test "$ac_cv_lib_jpeg_jpeg_CreateDecompress" = "yes"; then
  AC_CHECK_LIB_LA(jpeg, :, [ STATIC_LIBS="$STATIC_LIBS -ljpeg" ; AC_DEFINE(_STATIC_LIBJPEG)])
 fi
 if test "$ac_cv_lib_freetype_FT_New_Memory_Face" = "yes"; then
  AC_CHECK_LIB_LA(freetype, :, [ STATIC_LIBS="$STATIC_LIBS -lfreetype" ; AC_DEFINE(_STATIC_LIBFREETYPE)])
 fi
 if test "$ac_cv_lib_xml2_xmlParseFile" = "yes"; then
  AC_CHECK_LIB_LA(xml2, :, [ STATIC_LIBS="$STATIC_LIBS -lxml2" ; AC_DEFINE(_STATIC_LIBXML2)])
 fi
 ;;
esac
fi

AC_ARG_WITH(multilayer,
 [  --with-multilayer   Control whether we build with support for type3/svg fonts containing multilayered drawing with strokes, fills, images, etc.],
 [
    if test "$withval" != "no" >/dev/null 2>&1 ; then
	AC_DEFINE(FONTFORGE_CONFIG_TYPE3)
    fi
 ], : )

AC_ARG_WITH(devicetables,
 [  --with-devicetables   Control whether we build with support for device tables which allow fine control of kerning (etc.) at small point sizes],
 [
    if test "$withval" != "no" >/dev/null 2>&1 ; then
	AC_DEFINE(FONTFORGE_CONFIG_DEVICETABLES)
    fi
 ], AC_DEFINE(FONTFORGE_CONFIG_DEVICETABLES) )

AC_ARG_WITH(gtk,
 [  --with-gtk   Control whether we build using the gtk toolkit (does not work yet)],
 [
    if test "$withval" != "no" >/dev/null 2>&1 ; then
	AC_DEFINE(FONTFORGE_CONFIG_GTK)
    fi
 ], : )
AC_ARG_WITH(gdraw,
 [  --with-gdraw   Control whether we build using fontforge's traditional toolkit (gdraw)],
 [
    if test "$withval" != "no" >/dev/null 2>&1 ; then
	AC_DEFINE(FONTFORGE_CONFIG_GDRAW)
    fi
 ], : )
AC_ARG_WITH(noui,
 [  --with-noui   Control whether we build without any ui, just the ability to run scripts],
 [
    if test "$withval" != "no" >/dev/null 2>&1 ; then
	AC_DEFINE(FONTFORGE_CONFIG_NO_WINDOWING_UI)
    fi
 ], : )

test -d libs || mkdir libs

AC_SUBST(WFLAGS)
if test "$GCC" = yes; then
  WFLAGS="-Wmissing-prototypes -Wunused -Wimplicit -Wreturn-type -Wparentheses -Wformat -Wchar-subscripts"
else
  WFLAGS=""
fi

LIBS="$LIBS $gww_rpath"

AC_SUBST(VERSION)
VERSION=`cat VERSION`

cp Makefile.dynamic.in Makefile.in
cp Unicode/Makefile.dynamic.in Unicode/Makefile.in
cp gdraw/Makefile.dynamic.in gdraw/Makefile.in
cp fontforge/Makefile.dynamic.in fontforge/Makefile.in
AC_OUTPUT(Makefile Unicode/Makefile gdraw/Makefile fontforge/Makefile)
AC_OUTPUT(po/Makefile plugins/Makefile)
AC_OUTPUT(systemspecific)
if test -e umap >/dev/null 2>&1 ; then
  cp umap/Makefile.dynamic.in umap/Makefile.in
  AC_OUTPUT( umap/Makefile )
fi
AC_OUTPUT( fontforge.pc )

echo Configuration defines: $DEFS
echo CFLAGS: $CFLAGS
if test "$STATIC_LIBS" != ""; then
  echo Non DlOpenable or Static Libraries: $STATIC_LIBS
fi

if ! echo $PATH | tr : \\n | grep -x $prefix/bin >/dev/null ; then
 echo " *******************************************************************"
 echo " * You do not have $prefix/bin in your PATH environment variable   *"
 echo " *******************************************************************"
 echo
fi

if test "$no_x" = "yes"; then
 echo " *******************************************************************"
 echo " * This version of fontforge will only run scripts. No X libraries *"
 echo " * were found so there is NO user interface!!!!! If you want a UI  *"
 echo " * try installing X11 on your system.                              *"
 echo " * Caveat: You will probably need to install two packages, the     *"
 echo " *  base X11 package and the developer SDK package                 *"
 echo " *******************************************************************"
 case "$host" in
 *-apple-darwin*)
  echo ""
  echo "X11 is optional software on the mac, it does not install automatically"
  echo " but X11User it is on CD 3 of the OS/X install disks, and X11SDK is on"
  echo " the Xcode CD. You must install both packages for fontforge to build"
  echo " with a user interface"
  ;;
 esac
 echo
fi
if test "$gww_has_gettext" = "no"; then
 echo " *******************************************************************"
 echo " * Your system does not have the gettext package installed.  This  *"
 echo " * means that fontforge cannot localize its user interface and all *"
 echo " * messages will be in English. You might want to install libintl. *"
  if test "$gww_ismac" = "yes"; then
 echo " * Unfortunately Mac OS/X does not ship with gettext. You can find *"
 echo " * it as part of the standard fink download at:                    *"
 echo " *  http://fink.sourceforge.net/                                   *"
 fi
 echo " *******************************************************************"
fi

dnl EOF
