dnl Process this file with autoconf to produce a configure script.
AC_INIT(gifsicle.h)
AM_INIT_AUTOMAKE(gifsicle, 1.25)
AM_CONFIG_HEADER(config.h)

AC_PROG_CC
AC_PROG_CPP
AC_C_CONST
AC_C_INLINE

build_gifview=yes
AC_ARG_ENABLE(gifview,
[  --disable-gifview       do not build gifview, a GIF viewer for X11],
if test "x$enableval" != xyes ; then
  build_gifview=no
fi)

build_gifdiff=yes
AC_ARG_ENABLE(gifdiff,
[  --disable-gifdiff       do not build gifdiff, a utility for comparing GIFs],
if test "x$enableval" != xyes ; then
  build_gifdiff=no
fi)

use_dmalloc=
AC_ARG_ENABLE(dmalloc,
[  --enable-dmalloc        build with debugging malloc library],
if test "x$enableval" = xyes ; then
  use_dmalloc=yes
fi)

AC_ARG_ENABLE(warnings,
[  --enable-warnings       compile with -Wall],
if test "x$enableval" = xyes ; then
  CC="$CC -Wall"
fi)

AC_ARG_ENABLE(all,
[  --enable-all            --enable-gifview --enable-gifdiff --enable-warnings],
if test "x$enableval" = xyes ; then
  build_gifview=yes
  build_gifdiff=yes
  CC="$CC -Wall"
fi)

ungif=
AC_ARG_ENABLE(ungif,
[  --enable-ungif          build without compression (no patent problems)],
if test "x$enableval" = xyes ; then
  ungif=yes
fi)

dnl
dnl Choose programs to build. Always build gifsicle
dnl

OTHERPROGRAMS=""
OTHERMANS=""

if test "x$build_gifview" = xyes ; then
  OTHERPROGRAMS="$OTHERPROGRAMS gifview"
  OTHERMANS="$OTHERMANS gifview.1"
  AC_PATH_XTRA
  AC_CACHE_CHECK(for gettimeofday prototype, ac_cv_gettimeofday,
[AC_TRY_COMPILE([#include <time.h>
#include <sys/time.h>],
[gettimeofday((void *)0, (void *)0);],
[AC_TRY_COMPILE([#include <time.h>
#include <sys/time.h>],
[gettimeofday((void *)0);],
ac_cv_gettimeofday=0,
ac_cv_gettimeofday=2)],
ac_cv_gettimeofday=1)])
  AC_DEFINE_UNQUOTED(GETTIMEOFDAY_PROTO, $ac_cv_gettimeofday)
else
  AC_DEFINE_UNQUOTED(X_DISPLAY_MISSING, 1)
fi

if test "x$build_gifdiff" = xyes ; then
  OTHERPROGRAMS="$OTHERPROGRAMS gifdiff"
  OTHERMANS="$OTHERMANS gifdiff.1"
fi

AC_SUBST(OTHERPROGRAMS)dnl
AC_SUBST(OTHERMANS)dnl

dnl
dnl Set up malloc library
dnl

if test "x$use_dmalloc" = xyes ; then
  AC_DEFINE(DMALLOC)
  MALLOC_O=dmalloc.o
else
  MALLOC_O=fmalloc.o
fi
AC_SUBST(MALLOC_O)dnl

dnl
dnl Set up `ungif' support
dnl

if test "x$ungif" = xyes -o "x$GIFSICLE_UNGIF" = xyes ; then
  GIFWRITE_O=ungifwrt.o
  AC_DEFINE(GIF_UNGIF)
elif test -r $srcdir/gifwrite.c ; then
  GIFWRITE_O=gifwrite.o
else
  AC_MSG_ERROR(This version of gifsicle cannot create compressed GIFs.
*** Either reconfigure with the \`--enable-ungif' option or download the
*** version with compression from http://www.lcdf.org/gifsicle/)
fi
AC_SUBST(GIFWRITE_O)

dnl
dnl random or rand, strerror, strtoul, sys/select.h
dnl

AC_CHECK_FUNC(random, random_func=random, random_func=rand)
AC_DEFINE_UNQUOTED(RANDOM, ${random_func})

AC_REPLACE_FUNCS(strerror)
AC_CHECK_FUNCS(strtoul)

AC_CHECK_HEADERS(sys/select.h)

dnl
dnl Integer typedefs
dnl

AC_CACHE_CHECK(for integer typedefs, ac_cv_int_types,
[AC_EGREP_HEADER(u_int32_t, sys/types.h, ac_cv_int_types=yes,
  AC_EGREP_HEADER(uint32_t, sys/types.h, ac_cv_int_types=close,
   ac_cv_int_types=no))])

if test "$ac_cv_int_types" = no ;
then
  # Define our own versions
  AC_CACHE_CHECK(for a 16-bit value, ac_cv_int16,
  [AC_TRY_RUN([#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
  FILE *f = fopen("conftestval", "w");
  if (!f) exit(1);
  if (sizeof(short) == 2) fprintf(f, "short\n");
  else exit(1);
  fclose(f);
  exit(0);
}], ac_cv_int16=`cat conftestval`,
  AC_MSG_ERROR(no 16-bit value found),
  ac_cv_int16=short)])

  AC_CACHE_CHECK(for a 32-bit value, ac_cv_int32,
  [AC_TRY_RUN([#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
  FILE *f = fopen("conftestval", "w");
  if (!f) exit(1);
  if (sizeof(int) == 4) fprintf(f, "int\n");
  else if (sizeof(long) == 4) fprintf(f, "long\n");
  else exit(1);
  fclose(f);
  exit(0);
}], ac_cv_int32=`cat conftestval`, 
  AC_MSG_ERROR(no 32-bit value found),
  ac_cv_int32=int)])
fi

if test "$ac_cv_int_types" = no ;
then
  AC_DEFINE_UNQUOTED(u_int16_t, unsigned $ac_cv_int16)
  AC_DEFINE_UNQUOTED(u_int32_t, unsigned $ac_cv_int32)
  AC_DEFINE_UNQUOTED(int32_t, $ac_cv_int32)
elif test "$ac_cv_int_types" = close ;
then
  AC_DEFINE(NEED_SYS_TYPES_H)
  AC_DEFINE(u_int16_t, uint16_t)
  AC_DEFINE(u_int32_t, uint32_t)
else
  AC_DEFINE(NEED_SYS_TYPES_H)
fi

dnl
dnl Output
dnl

AC_OUTPUT(Makefile)
