dnl Copyright (C) 2002-2004 artofcode LLC. All rights reserved.
  
dnl This software is provided AS-IS with no warranty, either express or
dnl implied.

dnl This software is distributed under license and may not be copied,
dnl modified or distributed except as expressly authorized under the terms
dnl of the license contained in the file LICENSE in this distribution.

dnl For more information about licensing, please refer to
dnl http://www.ghostscript.com/licensing/. For information on
dnl commercial licensing, go to http://www.artifex.com/licensing/ or
dnl contact Artifex Software, Inc., 101 Lucas Valley Road #110,
dnl San Rafael, CA  94903, U.S.A., +1(415)492-9861.

dnl $Id: configure.ac,v 1.28.2.8 2004/09/08 06:30:15 giles Exp $
dnl Process this file with autoconf to produce a configure script

dnl ------------------------------------------------
dnl Initialization and Versioning
dnl ------------------------------------------------

AC_INIT
AC_PREREQ(2.52)
AC_CONFIG_SRCDIR(src/gs.c)

GS_VERSION_MAJOR=8
GS_VERSION_MINOR=15
GS_VERSION_PATCH=0
GS_REVISIONDATE=20050811

GS_VERSION=`expr $GS_VERSION_MAJOR \* 10000 + $GS_VERSION_MINOR \* 100 + $GS_VERSION_PATCH`

if test $GS_VERSION_MINOR -lt 10; then
	GS_VERSION_MINOR0=0$GS_VERSION_MINOR
else
	GS_VERSION_MINOR0=$GS_VERSION_MINOR
fi

AC_SUBST(GS_REVISIONDATE, $GS_REVISIONDATE)
AC_SUBST(GS_VERSION_MAJOR, $GS_VERSION_MAJOR)
AC_SUBST(GS_VERSION_MINOR, $GS_VERSION_MINOR)
AC_SUBST(GS_VERSION_MINOR0, $GS_VERSION_MINOR0)
AC_SUBST(GS_VERSION_PATCH, $GS_VERSION_PATH)
AC_SUBST(GS_VERSION, $GS_VERSION)

dnl --------------------------------------------------  
dnl Check for programs
dnl --------------------------------------------------  

dnl AC_PROG_CC likes to add '-g -O2' to CFLAGS. however,
dnl we ignore those flags and construct our own.
save_cflags=$CFLAGS
AC_PROG_CC
AC_PROG_CPP
CFLAGS=$save_cflags

AC_PROG_RANLIB
#AC_PROG_INSTALL

dnl --------------------------------------------------
dnl Set build flags based on environment
dnl --------------------------------------------------

#AC_CANONICAL_HOST

if test $ac_cv_prog_gcc = yes; then
    cflags_to_try="-Wall -Wstrict-prototypes \
-Wmissing-declarations -Wmissing-prototypes \
-Wcast-qual -Wwrite-strings \
-fno-builtin -fno-common"
    optflags_to_try="-O2"
else
    cflags_to_try=
    optflags_to_try="-O"
fi

AC_ARG_ENABLE(debug, [  --enable-debug          turn on debugging [default=no]])
if test x$enable_debug = xyes; then
    optflags_to_try="-g"
    CFLAGS="-DDEBUG $CFLAGS"
fi

AC_MSG_CHECKING([supported compiler flags])
old_cflags=$CFLAGS
echo
for flag in $optflags_to_try; do
    CFLAGS="$CFLAGS $flag"
    AC_TRY_COMPILE(, [return 0;], [
	echo "   $flag"
	GCFLAGS="$GCFLAGS $flag"
    ])
    CFLAGS=$old_cflags
done
for flag in $cflags_to_try; do
	CFLAGS="$CFLAGS $flag"
	AC_TRY_COMPILE(, [return 0;], [
		echo "   $flag"
		GCFLAGS="$GCFLAGS $flag"
	])
	CFLAGS=$old_cflags
done
AC_MSG_RESULT([ ...done.])

dnl --------------------------------------------------
dnl Check for headers
dnl --------------------------------------------------

AC_HEADER_DIRENT
AC_HEADER_STDC
AC_CHECK_HEADERS([errno.h fcntl.h limits.h malloc.h memory.h stdlib.h string.h strings.h sys/ioctl.h sys/param.h sys/time.h syslog.h unistd.h])

# for gdev3b1.c (AT&T terminal interface)
AC_CHECK_HEADER([sys/window.h])

dnl --------------------------------------------------
dnl Check for typedefs, structures, etc
dnl --------------------------------------------------

AC_C_CONST
AC_C_INLINE
AC_TYPE_MODE_T
AC_TYPE_OFF_T
AC_TYPE_SIZE_T
AC_STRUCT_ST_BLOCKS
AC_HEADER_TIME
AC_STRUCT_TM

dnl see if we're on a system that puts the *int*_t types 
dnl from stdint.h in sys/types.h
if test "$ac_cv_header_stdint_h" != yes; then
    AC_CHECK_TYPES([int8_t, int16_t, int32_t, uint8_t, uint16_t, uint32_t],,,[#include <sys/types.h>])
    if test "$ac_cv_type_uint8_t" = yes; then
        AC_DEFINE([SYS_TYPES_HAS_STDINT_TYPES])
	GCFLAGS="$GCFLAGS -DSYS_TYPES_HAS_STDINT_TYPES"
    fi
fi

dnl we aren't interested in all of DEFS, so manually insert
dnl the flags we care about
if test "$ac_cv_c_const" != yes; then
	GCFLAGS="$GCFLAGS -Dconst="
fi
if test "$ac_cv_header_stdint_h" = yes; then
	GCFLAGS="$GCFLAGS -DHAVE_STDINT_H"
fi

dnl try to find a 64 bit type for devicen color index
uint64_type="none"
 AC_CHECK_SIZEOF(unsigned long int)
 if test $ac_cv_sizeof_unsigned_long_int = 8; then
	uint64_type="unsigned long int"
 else
  AC_CHECK_SIZEOF(unsigned long long)
  if test $ac_cv_sizeof_unsigned_long_long = 8; then
	uint64_type="unsigned long long"
  else
   AC_CHECK_SIZEOF(unsigned __int64)
   if test $ac_cv_sizeof_unsigned___int64 = 8; then
	uint64_type="unsigned __int64"
   else
    AC_CHECK_SIZEOF(u_int64_t)
    if test $ac_cv_sizeof_u_int64_t = 8; then
	uint64_type="u_int64_t"
    fi
   fi
  fi
 fi
dnl we don't need to do anything if a 64-bit type wasn't found
dnl the code falls back to a (probably 32-bit) default
if test "$uint64_type" != "none"; then
	GCFLAGS="$GCFLAGS -DGX_COLOR_INDEX_TYPE=\"$uint64_type\""
fi


dnl --------------------------------------------------
dnl Check for libraries
dnl --------------------------------------------------

AC_CHECK_LIB(m, cos)
dnl AC_CHECK_LIB(pthread, pthread_create)
AC_CHECK_FUNC(iconv_open)
AC_CHECK_LIB(dl, dlopen)

AC_MSG_CHECKING([for local jpeg library source])
dnl At present, we give the local source priority over the shared
dnl build, so that the D_MAX_BLOCKS_IN_MCU patch will be applied.
dnl A more sophisticated approach would be to test the shared lib
dnl to see whether it has already been patched.
LIBJPEGDIR=src
if test -f jpeg/jpeglib.h; then
	AC_MSG_RESULT([jpeg])
	SHARE_LIBJPEG=0
	LIBJPEGDIR=jpeg
elif test -f jpeg-6b/jpeglib.h; then
	AC_MSG_RESULT([jpeg-6b])
	SHARE_LIBJPEG=0
	LIBJPEGDIR=jpeg-6b
else
	AC_MSG_RESULT([no])
	AC_CHECK_LIB(jpeg, jpeg_set_defaults, [
	  AC_CHECK_HEADERS([jpeglib.h], [SHARE_LIBJPEG=1])
	])
fi
if test -z "$SHARE_LIBJPEG"; then
  AC_MSG_ERROR([I wasn't able to find a copy
  of the jpeg library. This is required for compiling
  ghostscript. Please download a copy of the source,
  e.g. from http://www.ijg.org/, unpack it at the
  top level of the gs source tree, and rename
  the directory to 'jpeg'.
  ])
fi
AC_SUBST(SHARE_LIBJPEG)
AC_SUBST(LIBJPEGDIR)
dnl check for the internal jpeg memory header
AC_MSG_CHECKING([for jmemsys.h])
if test -r $LIBJPEGDIR/jmemsys.h; then
  AC_MSG_RESULT([yes])
else
  AC_MSG_RESULT([no])
  AC_DEFINE([DONT_HAVE_JMEMSYS_H], 1, 
    [define if the libjpeg memory system prototypes aren't available])
fi

dnl these are technically optional

AC_MSG_CHECKING([for local zlib source])
dnl zlib is needed for language level 3, and libpng
# we must define ZLIBDIR regardless because libpng.mak does a -I$(ZLIBDIR)
# this seems a harmless default
ZLIBDIR=src
if test -d zlib; then
	AC_MSG_RESULT([yes])
	SHARE_ZLIB=0
	ZLIBDIR=zlib
else
	AC_MSG_RESULT([no])
	AC_CHECK_LIB(z, deflate, [
	  AC_CHECK_HEADERS(zlib.h, [SHARE_ZLIB=1])
	])
fi
if test -z "$SHARE_ZLIB"; then
  AC_MSG_ERROR([I did not find a copy of zlib on your system.
  Please either install it, or unpack a copy of the source in a
  local directory named 'zlib'. See http://www.gzip.org/zlib/
  for more information.
  ])
fi
AC_SUBST(SHARE_ZLIB)
AC_SUBST(ZLIBDIR)

dnl png for the png output device; it also requires zlib
LIBPNGDIR=src
PNGDEVS=''
AC_MSG_CHECKING([for local png library source])
if test -f libpng/pngread.c; then
	AC_MSG_RESULT([yes])
	SHARE_LIBPNG=0
	LIBPNGDIR=libpng
	PNGDEVS='$(DD)png16m.dev $(DD)pnggray.dev $(DD)pngmono.dev $(DD)png256.dev $(DD)png16.dev $(DD)pngalpha.dev'
else
	AC_MSG_RESULT([no])
	AC_CHECK_LIB(png, png_check_sig, [
	  AC_CHECK_HEADERS(png.h, [
		SHARE_LIBPNG=1
		PNGDEVS='$(DD)png16m.dev $(DD)pnggray.dev $(DD)pngmono.dev $(DD)png256.dev $(DD)png16.dev $(DD)pngalpha.dev'
	  ], [SHARE_LIBPNG=0])
	], [SHARE_LIBPNG=0], [-lz])
fi
if test -z "$PNGDEVS"; then
  AC_MSG_NOTICE([disabling png output devices])
fi
AC_SUBST(SHARE_LIBPNG)
AC_SUBST(LIBPNGDIR)
AC_SUBST(PNGDEVS)

dnl look for CUPS...
AC_ARG_ENABLE(cups, [  --enable-cups           turn on CUPS support, default=yes])

CUPSDEV=""
CUPSINCLUDE=""
CUPSCONFIG="${CUPSCONFIG:=}"
cups_datadir="/usr/share/cups"
cups_serverroot="/etc/cups"
cups_serverbin="/usr/lib/cups"

if test -d pstoraster; then
    if test x$enable_cups != xno; then
	AC_PATH_PROG(CUPSCONFIG,cups-config)
	if test "x$CUPSCONFIG" != x; then
	    dnl Use values from CUPS config...
#	    LIBS="`$CUPSCONFIG --ldflags` `$CUPSCONFIG --static --image --libs | sed -e '1,$s/-lssl//'` $LIBS"
	    LIBS="`$CUPSCONFIG --ldflags` `$CUPSCONFIG --image --libs` $LIBS"
	    CFLAGS="`$CUPSCONFIG --cflags` $CFLAGS"
	    cups_serverroot="`$CUPSCONFIG --serverroot`"
	    cups_serverbin="`$CUPSCONFIG --serverbin`"
	    cups_datadir="`$CUPSCONFIG --datadir`"
	    CUPSINCLUDE="include pstoraster/cups.mak"
	    CUPSDEV="\$(DD)cups.dev"
	fi
    fi
fi

AC_SUBST(CUPSDEV)
AC_SUBST(CUPSCONFIG)
AC_SUBST(CUPSINCLUDE)
AC_SUBST(cups_datadir)
AC_SUBST(cups_serverroot)
AC_SUBST(cups_serverbin)

dnl look for IJS implementation
AC_ARG_WITH(ijs, AC_HELP_STRING([--with-ijs],[include IJS driver support]))
dnl set safe defaults
    IJSDIR=src
    IJSDEVS=''
if test x$with_ijs != xno; then
    AC_MSG_CHECKING([for local ijs library source])
    if test -d ijs; then
        AC_MSG_RESULT([yes])
        IJSDIR=ijs
        IJSDEVS='$(DD)ijs.dev'
    else
        AC_MSG_RESULT([no])
    fi
fi
AC_SUBST(IJSDIR)
AC_SUBST(IJSDEVS)

dnl look for jbig2dec
AC_ARG_WITH(jbig2dec, AC_HELP_STRING([--with-jbig2dec],[include JBIG2 decode support]))
JBIG2DIR=src
SHARE_JBIG2=0
JBIG2DEVS=''
if test x$with_jbig2dec != xno; then
  AC_MSG_CHECKING([for local jbig2dec library source])
  for d in jbig2dec jbig2dec-0.2 jbig2dec-0.3; do
    test -d "$d" && JBIG2DIR=$d && break
  done
  if test "x$JBIG2DIR" != xsrc; then
    AC_MSG_RESULT([$JBIG2DIR])
  else
    AC_MSG_RESULT([no])
    AC_CHECK_LIB([jbig2dec], [jbig2_page_out], [
	SHARE_JBIG2=1
    ], [
	AC_MSG_WARN([disabling support for JBIG2 files])
        with_jbig2dec=no
    ])
  fi
fi
if test x$with_jbig2dec != xno; then
  if test x$ac_cv_header_stdint_h != xyes; then
    AC_MSG_WARN([JBIG2 support requires stdint types which do not seem to be available.])
  else
    JBIG2DEVS='$(PSD)jbig2.dev'
  fi
fi

AC_SUBST(JBIG2DIR)
AC_SUBST(SHARE_JBIG2)
AC_SUBST(JBIG2DEVS)

dnl look for omni implementation
AC_ARG_WITH([omni], AC_HELP_STRING([--with-omni],
	[build the omni driver]))
dnl set safe defaults
OMNIDEVS=''
INCLUDEOMNI=yes
GLIB_VERSION=no
if test x$with_omni != xno; then
    dnl
    dnl Test for glib-2.0 package.
    dnl
    AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
    AC_MSG_CHECKING(for pkg-config)
    if test "$PKG_CONFIG" = "no" ; then
        AC_MSG_RESULT(no)
    else
        AC_MSG_RESULT(yes)
        AC_MSG_CHECKING(for gmodule-2.0)
	if $PKG_CONFIG --exists gmodule-2.0 ; then
            AC_MSG_RESULT(yes)
            GLIB_CFLAGS=`$PKG_CONFIG --cflags gmodule-2.0`
            GLIB_LIBS=`$PKG_CONFIG --cflags gmodule-2.0`
            GLIB_VERSION=2.0
        else
            AC_MSG_RESULT(no)
        fi
    fi

    dnl
    dnl Test for glib package.
    dnl
    if test "$GLIB_VERSION" = "no" ; then
        AC_PATH_PROG(GLIB_CONFIG, glib-config, no)
        AC_MSG_CHECKING([for glib])
        if test "$GLIB_CONFIG" = "no"; then
            AC_MSG_RESULT(no)
        else
            AC_MSG_RESULT(yes)
            GLIB_VERSION=1.2
            GLIB_CFLAGS=`$GLIB_CONFIG --cflags gmodule`
            GLIB_LIBS=`$GLIB_CONFIG --libs gmodule`
        fi
    fi

    dnl
    dnl If all tests pass, then include omni support!
    dnl
    AC_MSG_CHECKING([if Omni support can be compiled in])
    if test "$GLIB_VERSION" = "no" ; then
        AC_MSG_RESULT(no)
    else
        AC_MSG_RESULT([yes])

        OMNIDEVS='$(DD)omni.dev'
        CFLAGS="$CFLAGS $GLIB_CFLAGS"
        LIBS="$LIBS $GLIB_LIBS"

        if test -n "$GCC"; then
	    LIBS="$LIBS -lstdc++"
	fi
    fi
fi
AC_SUBST(OMNIDEVS)

dnl optional X11 for display devices
AC_PATH_XTRA

X_LDFLAGS=""
X_CFLAGS=""
X_DEVS=""
X_LIBS=""

if test x$no_x != xyes; then
	if test "$x_libraries" = "/usr/lib"; then
		echo "Ignoring X library directory \"$x_libraries\" requested by configure."
		x_libraries="NONE"
	fi
	if test ! "$x_libraries" = "NONE" -a ! "$x_libraries" = ""; then
		LDFLAGS="-L$x_libraries $LDFLAGS"
		X_LDFLAGS="-L$x_libraries"
		if test "$uname" = "SunOS"; then
			X_LDFLAGS="$X_LDFLAGS -R$x_libraries"
		fi
	fi

	if test "$x_includes" = "/usr/include"; then
		echo "Ignoring X include directory \"$x_includes\" requested by configure."
		x_includes="NONE"
	fi
	if test ! "$x_includes" = "NONE" -a ! "$x_includes" = ""; then
		X_CFLAGS="-I$x_includes"
	fi

	SAVELIBS="$LIBS"

	AC_CHECK_LIB(X11,XOpenDisplay)
	AC_CHECK_LIB(Xext,XdbeQueryExtension)
	AC_CHECK_LIB(Xt,XtAppCreateShell)

	LIBS="$SAVELIBS"

	if test "$ac_cv_lib_Xt_XtAppCreateShell" = yes; then
		X_DEVS="\$(DD)x11.dev \$(DD)x11alpha.dev \$(DD)x11cmyk.dev \$(DD)x11mono.dev \$(DD)x11_.dev \$(DD)x11alt_.dev \$(DD)x11cmyk2.dev \$(DD)x11cmyk4.dev \$(DD)x11cmyk8.dev \$(DD)x11rg16x.dev \$(DD)x11rg32x.dev \$(DD)x11gray2.dev \$(DD)x11gray4.dev"
		X_LIBS="Xt Xext X11"
	fi
fi

AC_SUBST(X_LDFLAGS)
AC_SUBST(X_CFLAGS)
AC_SUBST(X_LIBS)
AC_SUBST(X_DEVS)

dnl executible name
AC_ARG_WITH(gs, AC_HELP_STRING([--with-gs=NAME],
	[name of the ghostscript executible [[gs]]]),
	[GS="$with_gs"],[GS='gs'])
AC_SUBST(GS)

dnl do we compile the postscript initialization files into Ghostscript?
COMPILE_INITS="0"
AC_ARG_ENABLE(compile-inits, AC_HELP_STRING([--enable-compile-inits],
       [compile initialization files into Ghostscript]),[
               if test x$enable_compile_inits = xyes; then
                       COMPILE_INITS="1"
               fi])
AC_SUBST(COMPILE_INITS)

dnl look for drivers to compile...
AC_ARG_WITH(drivers,
[  --with-drivers=LIST     Drivers to support, separated by commas.
                          Either list the drivers or use aliases:
                          ALL      = all drivers
                          FILES    = all file format drivers
                          PRINTERS = all printer drivers
                          Printers:
                          APPLE    = all Apple printers
                          BROTHER  = all Brother printers
                          CANON    = all Canon printers
                          EPSON    = all Epson printers
                          HP       = all HP printers
                          IBM      = all IBM printers
                          JAPAN    = older japanese printers
                          LEXMARK  = all Lexmark printers
                          OKI      = all OKI printers
			  PCLXL    = all PCL XL/6 printers
                          File formats:
                          BMP      = Output to bmp files
                          FAX      = Output to fax files
                          JPEG     = Output to JPEG files
                          PBM      = Output to PBM/PNM
                          PCX      = Output to PCX
			  PNG      = Output to PNG
                          PS       = Output to PostScript/PDF
                          TIFF     = Output to TIFF
                          You can mix both variants, e.g.
                          --with-drivers=HP,stcolor would build HP drivers and
                          the Epson stcolor driver.
                          Aliases must be uppercase (a 3rd party driver might
                          have the same name).
                          Default: ALL],drivers="$withval",drivers="ALL")

AC_ARG_WITH(driversfile,
[  --with-driversfile=FILE Drivers to support from file, separated by newlines.],
driversfile="$withval",driversfile="")

if test "x$driversfile" != x; then
	# Add drivers from file...
	drivers="`tr '\n' ',' <$driversfile`"
fi

dnl Check which drivers we'd like to support.
P_DEVS=""
F_DEVS=""

dnl Known printers
HP_DEVS="cdj500 djet500 djet500c dnj650c cljet5pr deskjet laserjet ljetplus ljet2p ljet3 ljet3d ljet4 ljet4d lj4dith lj5mono lj5gray cdeskjet cdjcolor cdjmono cdj550 pj pjxl pjxl300 lp2563 paintjet pjetxl cljet5 cljet5c pxlmono pxlcolor cdj670 cdj850 cdj880 cdj890 cdj970 cdj1600 chp2200 pcl3 hpdjplus hpdjportable hpdj310 hpdj320 hpdj340 hpdj400 hpdj500 hpdj500c hpdj510 hpdj520 hpdj540 hpdj550c hpdj560c hpdj600 hpdj660c hpdj670c hpdj680c hpdj690c hpdj850c hpdj855c hpdj870c hpdj890c hpdj1120c lj3100sw"
PCLXL_DEVS="pxlmono pxlcolor"
EPSON_DEVS="eps9high eps9mid epson epsonc escp lp8000 lq850 photoex st800 stcolor alc1900 alc2000 alc4000 alc4100 alc8500 alc8600 alc9100 lp3000c lp8000c lp8200c lp8300c lp8500c lp8800c lp9000c lp9200c lp9500c lp9800c epl2050 epl2050p epl2120 epl2500 epl2750 epl5800 epl5900 epl6100 epl6200 lp1800 lp1900 lp2200 lp2400 lp2500 lp7500 lp7700 lp7900 lp8100 lp8300f lp8400f lp8600 lp8600f lp8700 lp8900 lp9000b lp9100 lp9200b lp9300 lp9400 lp9600 lp9600s"
CANON_DEVS="bj10e bj200 bjc600 bjc800 lbp8 lips3 bjcmono bjcgray bjccmyk bjccolor"
LEXMARK_DEVS="lxm5700m lx5000 lxm3200 lex2050 lex3200 lex5700 lex7000"
BROTHER_DEVS="hl7x0 hl1240 hl1250"
APPLE_DEVS="appledmp iwhi iwlo iwlq"
IBM_DEVS="ibmpro jetp3852"
OKI_DEVS="oki182 okiibm oki4w"
JAPAN_DEVS="lips4 lips4v ljet4pjl lj4dithp dj505j picty180 lips2p bjc880j pr201 pr150 pr1000 pr1000_4 jj100 bj10v bj10vh mj700v2c mj500c mj6000c mj8000c fmpr fmlbp ml600 lbp310 lbp320 md50Mono md50Eco md1xMono escpage lp2000 npdl rpdl"
MISC_PDEVS="uniprint ap3250 atx23 atx24 atx38 coslw2p coslwxl cp50 declj250 fs600 imagen lj250 m8510 necp6 oce9050 r4081 sj48 tek4696 t4693d2 t4693d4 t4693d8 dl2100 la50 la70 la75 la75plus ln03 xes md2k md5k gdi samsunggdi oprp opvp"

dnl Known file formats
BMP_DEVS="bmpmono bmpgray bmpsep1 bmpsep8 bmp16 bmp256 bmp16m bmp32b bmpa16 bmpa16m bmpa256 bmpa32b bmpamono bmpasep1 bmpasep8"
FAX_DEVS="cfax dfaxlow dfaxhigh fax tfax tiffg3 tiffg32d tiffg4 faxg3 faxg32d faxg4"
JPEG_DEVS="jpeg jpeggray"
PNG_DEVS="png16 png16m png256 pngalpha pnggray pngmono"
TIFF_DEVS="tiffs tiff12nc tiff24nc tiffcrle tifflzw tiffpack"
PCX_DEVS="pcxmono pcxgray pcx16 pcx256 pcx24b pcxcmyk pcx2up"
PBM_DEVS="pbm pbmraw pgm pgmraw pgnm pgnmraw pnm pnmraw ppm ppmraw pkm pkmraw pksm pksmraw pam"
PS_DEVS="psdf psdcmyk psdrgb pdfwrite pswrite epswrite psgray psmono psrgb bbox"
MISC_FDEVS="ccr cgm24 cgm8 cgmmono cif inferno mag16 mag256 mgr4 mgr8 mgrgray2 mgrgray4 mgrgray8 mgrmono miff24 plan9bm sgirgb sunhmono bit bitrgb bitcmyk devicen spotcmyk xcf"

while test -n "$drivers"; do
	if echo $drivers |grep "," >/dev/null; then
		THIS="`echo $drivers |sed -e 's/,.*//'`"
		drivers="`echo $drivers |sed -e \"s/$THIS,//\"`"
	else
		THIS=$drivers
		drivers=""
	fi
	case "$THIS" in
	ALL)
		# ALL = PRINTERS + FILES...
		if test -z "$drivers"; then
			drivers="PRINTERS,FILES"
		else
			drivers="$drivers,PRINTERS,FILES"
		fi
		;;
	PRINTERS)
		P_DEVS="$P_DEVS $CANON_DEVS $EPSON_DEVS $HP_DEVS $LEXMARK_DEVS $BROTHER_DEVS $APPLE_DEVS $IBM_DEVS $OKI_DEVS $JAPAN_DEVS $MISC_PDEVS"
		;;
	FILES)
		F_DEVS="$F_DEVS $BMP_DEVS $FAX_DEVS $JPEG_DEVS $PNG_DEVS $TIFF_DEVS $PCX_DEVS $PBM_DEVS $PS_DEVS $MISC_FDEVS"
		;;
	APPLE)
		# All Apple printers
		P_DEVS="$P_DEVS $APPLE_DEVS"
		;;
	BMP)
		# BMP file format
		F_DEVS="$F_DEVS $BMP_DEVS"
		;;
	CANON)
		# All Canon printers
		P_DEVS="$P_DEVS $CANON_DEVS"
		;;
	EPSON)
		# All Epson printers
		P_DEVS="$P_DEVS $EPSON_DEVS"
		;;
	FAX)
		# Fax file formats
		F_DEVS="$F_DEVS $FAX_DEVS"
		;;
	JPEG)
		# Jpeg file formats
		F_DEVS="$F_DEVS $JPEG_DEVS"
		;;
	PNG)
		# PNG file formats
		F_DEVS="$F_DEVS $PNG_DEVS"
		;;
	TIFF)
		# TIFF file formats
		F_DEVS="$F_DEVS $TIFF_DEVS"
		;;
	PCLXL)
		# PCL XL/PCL 6 drivers
		F_DEVS="$F_DEVS $PCLXL_DEVS"
		;;
	PCX)
		# PCX file formats
		F_DEVS="$F_DEVS $PCX_DEVS"
		;;
	PBM)
		# PBM file formats
		F_DEVS="$F_DEVS $PBM_DEVS"
		;;
	HP)
		# All HP printers
		P_DEVS="$P_DEVS $HP_DEVS"
		;;
	LEXMARK)
		# All Lexmark printers
		P_DEVS="$P_DEVS $LEXMARK_DEVS"
		;;
	BROTHER)
		# All Brother printers
		P_DEVS="$P_DEVS $BROTHER_DEVS"
		;;
	OKI)
		# All OKI printers
		P_DEVS="$P_DEVS $OKI_DEVS"
		;;
	IBM)
		# All IBM printers
		P_DEVS="$P_DEVS $IBM_DEVS"
		;;
	JAPAN)
		# All old japanese printers
		P_DEVS="$P_DEVS $JAPAN_DEVS"
		;;
	PS)
		# PostScript/PDF writing
		F_DEVS="$F_DEVS $PS_DEVS"
		;;
        opvp)
		# Open Vector Printing driver...
		if test x$ac_cv_lib_dl_dlopen != xno -a x$ac_cv_func_iconv_open != xno; then
			P_DEVS="$P_DEVS opvp"
		else
			AC_MSG_WARN(Unable to include opvp driver due to missing prerequisites...)
		fi
		;;
	*)
		# It's a driver name (or a user messup)
		P_DEVS="$P_DEVS `echo $THIS |sed -e 's,\.dev$,,'`"
		;;
	esac
done
# Make sure we don't have any duplicates in there, add $(DD)foo.dev constructs
if test -n "$P_DEVS"; then
	PRINTERS=`(for i in $P_DEVS; do echo '$(DD)'${i}.dev; done) | sort | uniq | tr '\012' ' '`
fi
if test -n "$F_DEVS"; then
	FILES=`(for i in $F_DEVS; do echo '$(DD)'${i}.dev; done) | sort | uniq | tr '\012' ' '`
fi
AC_SUBST(PRINTERS)
AC_SUBST(FILES)

dnl Dynamic device support.
DYNAMIC_CFLAGS=""
DYNAMIC_DEVS=""
DYNAMIC_FLAGS=""
DYNAMIC_LDFLAGS=""
DYNAMIC_LIBS=""

AC_ARG_ENABLE(dynamic, [  --enable-dynamic        enable dynamically loaded drivers (default=no)],
[	case `uname` in
		Linux*)
		DYNAMIC_CFLAGS="-fPIC"
		DYNAMIC_DEVS="\$(GLOBJDIR)/X11.so"
		DYNAMIC_FLAGS="-DGS_DEVS_SHARED -DGS_DEVS_SHARED_DIR=\\\"\$(gssharedir)\\\""
		DYNAMIC_LDFLAGS="-fPIC -shared"
		DYNAMIC_LIBS="-rdynamic -ldl"
		X_DEVS=""
		;;
		*)
		AC_MSG_ERROR(Sorry, dynamic driver support not available on this platform!)
		;;
	esac
])

AC_SUBST(DYNAMIC_CFLAGS)
AC_SUBST(DYNAMIC_DEVS)
AC_SUBST(DYNAMIC_FLAGS)
AC_SUBST(DYNAMIC_LDFLAGS)
AC_SUBST(DYNAMIC_LIBS)

dnl Threading...
THREADLIBS=""
THREADSYNC="nosync"

AC_ARG_ENABLE(threads, [  --enable-threads        enable threaded output (default=no)],
[	case `uname` in
		Linux* | IRIX* | SunOS*)
		THREADSYNC="posync"
		THREADLIBS="-lpthread"
		;;
		*BSD)
		THREADSYNC="posync"
		THREADLIBS="-pthread"
		;;
		*)
		AC_MSG_ERROR(Sorry, threading support not available on this platform!)
		;;
	esac
])

AC_SUBST(THREADLIBS)
AC_SUBST(THREADSYNC)

dnl look for default font path...
AC_ARG_WITH(fontpath, [  --with-fontpath         set font path for gs],fontpath="$withval",fontpath="")

dnl Fix "prefix" variable...
if test "x$prefix" = xNONE; then
	prefix=/usr/local
fi

dnl Fix "datadir" variable...
if test "x$datadir" = 'x${prefix}/share'; then
	datadir="$prefix/share"
fi

dnl Fix "fontpath" variable...
if test "x$fontpath" = "x"; then
	# These font directories are used by various Linux distributions...
	fontpath="$datadir/fonts/default/ghostscript"
	fontpath="${fontpath}:$datadir/fonts/default/Type1"
	fontpath="${fontpath}:$datadir/fonts/default/TrueType"

	# These font directories are used by IRIX...
	fontpath="${fontpath}:/usr/lib/DPS/outline/base"

	# These font directories are used by Solaris...
	fontpath="${fontpath}:/usr/openwin/lib/X11/fonts/Type1"
	fontpath="${fontpath}:/usr/openwin/lib/X11/fonts/TrueType"
fi

AC_SUBST(fontpath)

dnl --------------------------------------------------
dnl Check for library functions
dnl --------------------------------------------------

AC_CHECK_FUNCS([mkstemp], [HAVE_MKSTEMP=-DHAVE_MKSTEMP])
AC_SUBST(HAVE_MKSTEMP)

AC_CHECK_FUNCS([hypot], [HAVE_HYPOT=-DHAVE_HYPOT])
AC_SUBST(HAVE_HYPOT)

AC_FUNC_FORK
AC_PROG_GCC_TRADITIONAL
AC_FUNC_MALLOC
AC_FUNC_MEMCMP
AC_TYPE_SIGNAL
AC_FUNC_STAT
AC_FUNC_VPRINTF
AC_CHECK_FUNCS([bzero dup2 floor gettimeofday memchr memmove memset mkdir mkfifo modf pow putenv rint setenv sqrt strchr strerror strrchr strspn strstr])

dnl --------------------------------------------------
dnl Force ANSI mode when compiling with GCC to prevent
dnl common GCC-isms from sneaking into the code...
dnl --------------------------------------------------
#### TODO: Apparently ANSI mode also causes things not to work, with
####       black backgrounds and all sorts of drawing bugs.  I haven't
####       been able to track the problem down yet (GCC bug?  glibc
####       bug?  Something else?) but not adding this fixes the problem.
#if test -n "$GCC"; then
#	GCFLAGS="-std=c89 $GCFLAGS"
#fi

dnl --------------------------------------------------
dnl Do substitutions
dnl --------------------------------------------------

AC_SUBST(OPT_CFLAGS)
AC_SUBST(GCFLAGS)
AC_OUTPUT(Makefile lib/gs_init.ps pstoraster/pstopxl pstoraster/pstoraster)

chmod +x pstoraster/pstopxl pstoraster/pstoraster
