dnl Process this file with autoconf to produce a configure script.
AC_INIT(uCON64, 2.2.0, ucon64-main@lists.sourceforge.net)
AC_CONFIG_HEADER(config.h libdiscmage/config.h)


dnl Set target, target_cpu, target_os.
AC_CANONICAL_TARGET


dnl Checks for programs.
AC_PROG_CC(gcc egcs cc)
AC_PROG_GCC_TRADITIONAL


dnl Optional features.
AC_MSG_CHECKING(whether debug output is enabled)
AC_ARG_ENABLE(debug,
              AC_HELP_STRING([--enable-debug],
                             [enable debug output (default: no)]),
              enable_debug=$enableval,
              enable_debug=no)
if test $enable_debug = yes; then
  AC_DEFINE(DEBUG, 1, [enable debug output (default: no)])
fi
AC_MSG_RESULT($enable_debug)


AC_MSG_CHECKING(whether support for parallel port backup units is disabled)
AC_ARG_ENABLE(parallel,
              AC_HELP_STRING([--disable-parallel],
                             [disable support for parallel port backup units (default: no)]),
              enable_parallel=$enableval,
              enable_parallel=yes)
if test $enable_parallel = yes; then
  disable_parallel=no
else
  disable_parallel=yes
fi
if test $disable_parallel = no; then
  AC_DEFINE(USE_PARALLEL, 1, [enable support for parallel port backup units (default: yes)])
  USE_PARALLEL=1
fi
AC_MSG_RESULT($disable_parallel)
AC_SUBST(USE_PARALLEL)


AC_MSG_CHECKING(whether to use ppdev)
AC_ARG_ENABLE(ppdev,
              AC_HELP_STRING([--enable-ppdev],
                             [use ppdev for parallel port I/O (default: no)]),
              enable_ppdev=$enableval,
              enable_ppdev=no)
if test $enable_ppdev = yes; then
  AC_TRY_COMPILE([#include <linux/ppdev.h>], , enable_ppdev=yes, enable_ppdev=no)
fi
if test $enable_ppdev = yes; then
  AC_DEFINE(USE_PPDEV, 1, [use ppdev for parallel port I/O (default: no)])
  USE_PPDEV=1
fi
AC_MSG_RESULT($enable_ppdev)
AC_SUBST(USE_PPDEV)


AC_MSG_CHECKING(whether the use of color is disabled)
AC_ARG_ENABLE(ansi_color,
              AC_HELP_STRING([--disable-ansi-color],
                             [disable the use of color (default: no)]),
              enable_ansi_color=$enableval,
              enable_ansi_color=yes)
if test $enable_ansi_color = yes; then
  disable_ansi_color=no
else
  disable_ansi_color=yes
fi
if test $disable_ansi_color = no; then
  AC_DEFINE(USE_ANSI_COLOR, 1, [enable the use of color (default: yes)])
fi
AC_MSG_RESULT($disable_ansi_color)


AC_MSG_CHECKING(whether add-on libraries are dynamically linked)
AC_ARG_ENABLE(dload,
              AC_HELP_STRING([--disable-dload],
                             [disable dynamic loading of add-on libraries (default: no)]),
              enable_dload=$enableval,
              enable_dload=yes)
if test $enable_dload = yes; then
  disable_dload=no
else
  disable_dload=yes
fi
if test $disable_dload = no; then
  AC_DEFINE(DLOPEN, 1, [enable dynamic loading of add-on libraries (default: yes)])
  DLOPEN=1
fi
AC_MSG_RESULT($disable_dload)
AC_SUBST(DLOPEN)


dnl Optional packages.
AC_MSG_CHECKING(whether to build without libdiscmage support)
AC_ARG_WITH(libdiscmage,
            AC_HELP_STRING([--without-libdiscmage],
                           [build without libdiscmage support (default: no)]),
            with_libdiscmage=$withval,
            with_libdiscmage=yes)
if test $with_libdiscmage = yes; then
  AC_TRY_COMPILE([#include "libdiscmage/libdiscmage.h"], , without_libdiscmage=no, without_libdiscmage=yes)
else
  without_libdiscmage=yes
fi
if test $without_libdiscmage = no; then
  AC_DEFINE(USE_DISCMAGE, 1, [build with libdiscmage support (default: yes)])
  USE_DISCMAGE=1
fi
AC_MSG_RESULT($without_libdiscmage)
AC_SUBST(USE_DISCMAGE)


AC_MSG_CHECKING(whether to build with libcd64 support)
AC_ARG_WITH(libcd64,
            AC_HELP_STRING([--with-libcd64],
                           [build with libcd64 support (default: no)]),
            with_libcd64=$withval,
            with_libcd64=no)
if test $with_libcd64 = yes; then
  CFLAGS0=$CFLAGS
  CFLAGS+=" -Ibackup/libcd64"
  AC_TRY_COMPILE([#include "ultra64/host/cd64lib.h"], , with_libcd64=yes, with_libcd64=no)
  CFLAGS=$CFLAGS0
fi
if test $with_libcd64 = yes; then
  AC_DEFINE(USE_LIBCD64, 1, [build with libcd64 support (default: no)])
  USE_LIBCD64=1
  case "$target_cpu" in
    i*86 | x86_64)
      USE_LIBCD64_RAWIO=1
      ;;
  esac
elif test $disable_parallel = no; then
  dnl On OpenBSD linking to libi386 or libamd64 is necessary, but only if
  dnl  libcd64 is not enabled, because libcd64 already links to libi386 or
  dnl  libamd64. On NetBSD it is similar, but with libi386 and libx86_64.
  case "$target" in
    i*86-*-openbsd* | i*86-*-netbsd*)
      LIBIOPL_BSD="-li386"
      ;;
    x86_64-*-openbsd*)
      LIBIOPL_BSD="-lamd64"
      ;;
    x86_64-*-netbsd*)
      LIBIOPL_BSD="-lx86_64"
      ;;
  esac
fi
AC_MSG_RESULT($with_libcd64)
AC_SUBST(USE_LIBCD64)
AC_SUBST(USE_LIBCD64_RAWIO)
AC_SUBST(LIBIOPL_BSD)


AC_MSG_CHECKING(whether to build without gzip and zip support)
AC_ARG_WITH(zlib,
            AC_HELP_STRING([--without-zlib],
                           [build without gzip and zip support (default: no)]),
            with_zlib=$withval,
            with_zlib=yes)
if test $with_zlib = yes; then
  AC_TRY_COMPILE([#include <zlib.h>], , without_zlib=no, without_zlib=yes)
else
  without_zlib=yes
fi
if test $without_zlib = no; then
  AC_DEFINE(USE_ZLIB, 1, [build with gzip and zip support (default: yes)])
  USE_ZLIB=1
fi
AC_MSG_RESULT($without_zlib)
AC_SUBST(USE_ZLIB)


AC_MSG_CHECKING(whether to build with libusb support)
AC_ARG_WITH(libusb,
            AC_HELP_STRING([--with-libusb],
                           [build with libusb support (default: no)]),
            with_libusb=$withval,
            with_libusb=no)
if test $with_libusb = yes; then
  AC_TRY_COMPILE([#include <usb.h>], , usb_h=yes, usb_h=no)
else
  usb_h=no
fi
if test $with_libusb = yes; then
  if test $usb_h = yes; then
    with_libusb=yes
  else
    AC_TRY_COMPILE([#include <lusb0_usb.h>], , with_libusb=yes, with_libusb=no)
  fi
fi
if test $with_libusb = yes; then
  AC_DEFINE(USE_USB, 1, [build with libusb support (default: no)])
  USE_USB=1
fi
AC_MSG_RESULT($with_libusb)
AC_SUBST(USE_USB)


dnl Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h unistd.h byteswap.h inttypes.h sys/io.h)
dnl NOT zlib.h! Or else --with[out]-zlib gets overrriden in config.h.


dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_UID_T
AC_C_INLINE
AC_TYPE_PID_T
AC_TYPE_SIZE_T


dnl Checks for library functions.
AC_FUNC_MEMCMP
AC_FUNC_STRFTIME
AC_FUNC_VPRINTF
AC_CHECK_FUNCS(realpath clock_nanosleep strnlen)

AC_PROG_RANLIB
AC_PROG_INSTALL


AC_OUTPUT(config.mak libdiscmage/Makefile)

echo
echo NOTE: On non-Linux systems you may need to use gmake instead of make
