dnl Process this file with autoconf to produce a configure script.
dnl Written 1996, 1997, 1998 Bernd Schmidt
dnl
dnl Updated, re-written and generally mauled 2003 Richard Drummond

AC_PREREQ(2.55)
AC_INIT(E-UAE, 0.8.29-WIP4, ,e-uae)
AC_CONFIG_SRCDIR([bootstrap.sh])
AM_CONFIG_HEADER([src/sysconfig.h])
AC_CANONICAL_TARGET
AM_INIT_AUTOMAKE(1.7 dist-bzip2 foreign)

dnl
dnl  Checks for programs.
dnl

AC_PROG_CC
AC_PROG_CPP
AC_PROG_CXX
AS_PROG_OBJC
AM_PROG_AS
AC_PROG_MAKE_SET
AC_PROG_RANLIB
AC_CHECK_TOOL(AR, ar)
AC_PROG_INSTALL
AC_PATH_PROG(MAKEDEPPRG, makedepend, not-found)
AC_PATH_PROG(FILEPRG, file, not-found)
AC_PATH_PROG(WRCPRG, wrc, not-found)
AC_PATH_PROG(RCLPRG, rcl, not-found)

AC_AIX
AC_ISC_POSIX

NR_WARNINGS=0
NR_ERRORS=0

ADDITIONAL_CFLAGS=
OPTIMIZE_CFLAGS=
WARNING_CFLAGS=
DEBUG_CFLAGS=
NO_SCHED_CFLAGS=

dnl
dnl  Checks for libraries.
dnl
dnl Replace main' with a function in -lMedia_s: (Ian!)
dnl AC_CHECK_LIB(Media_s, main, HAVE_MEDIA_LIB=yes, HAVE_MEDIA_LIB=no)
dnl Replace main' with a function in -lNeXT_s:
dnl AC_CHECK_LIB(NeXT_s, main, HAVE_NEXT_LIB=yes, HAVE_NEXT_LIB=no)
dnl AC_CHECK_LIB(moto, cos, HAVE_MOTO_LIB=yes, HAVE_MOTO_LIB=no)
dnl AC_CHECK_LIB(amiga, OpenLibrary, HAVE_AMIGA_LIB=yes, HAVE_AMIGA_LIB=n)
dnl AC_CHECK_LIB(vga, vga_setmode, HAVE_SVGA_LIB=yes, HAVE_SVGA_LIB=no)

AC_CHECK_LIB(ossaudio,_oss_ioctl, HAVE_LIBOSSAUDIO=yes, HAVE_LIBOSSAUDIO=no)

dnl  Prefer Gtk2.x over Gtk1.x if both are available
AM_PATH_GTK_2_0(2.0.0,HAVE_GTK=yes,HAVE_GTK=no)
if [[ "x$HAVE_GTK" = "xno" ]]; then
  AM_PATH_GTK(1.0.0,HAVE_GTK=yes,HAVE_GTK=no)
fi

dnl
dnl  Checks for header files.
dnl
AC_HEADER_DIRENT
AC_HEADER_STDC

AC_CHECK_HEADERS([fcntl.h sys/ioctl.h sys/time.h utime.h])
AC_CHECK_HEADERS([values.h ncurses.h curses.h sys/termios.h])
AC_CHECK_HEADERS([sys/stat.h sys/ipc.h sys/shm.h sys/mman.h])
AC_CHECK_HEADERS([sys/filio.h])

AC_CHECK_HEADERS([libraries/cybergraphics.h cybergraphx/cybergraphics.h])

AC_CHECK_HEADERS([devices/ahi.h])
AC_CHECK_HEADERS([sys/soundcard.h machine/soundcard.h sun/audioio.h sys/audioio.h])

AC_CHECK_HEADERS([machine/joystick.h])
AC_CHECK_HEADER([amigainput/amigainput.h], HAVE_AMIGAINPUT=yes, HAVE_AMIGAINPUT=no)

AC_CHECK_HEADERS([byteswap.h])

dnl
dnl  Checks for typedefs, structures, and compiler characteristics.
dnl
AC_HEADER_STDBOOL
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
AC_C_VOLATILE
AC_C_BIGENDIAN

AC_CHECK_SIZEOF(char, 1)
AC_CHECK_SIZEOF(short, 2)
AC_CHECK_SIZEOF(int, 4)
AC_CHECK_SIZEOF(long, 4)
AC_CHECK_SIZEOF(long long, 8)
AC_CHECK_SIZEOF(__int64, 8)
AC_CHECK_SIZEOF(void *)

TYPE_SOCKLEN_T

dnl
dnl  Checks for library functions.
dnl
AC_PROG_GCC_TRADITIONAL
AC_FUNC_MEMCMP
AC_TYPE_SIGNAL
AC_FUNC_UTIME_NULL
AC_CHECK_FUNCS(getcwd getopt strdup gettimeofday sigaction mkdir rmdir)
AC_CHECK_FUNCS(select strerror strstr isnan isinf setitimer alarm sync)
AC_CHECK_FUNCS(tcgetattr cfmakeraw readdir_r vprintf vsprintf vfprintf)
AC_CHECK_FUNCS(strcasecmp stricmp strcmpi)
AC_CHECK_FUNCS(nanosleep usleep sleep)

dnl AC_CHECK_FUNCS(statvfs statfs)
dnl AC_VAR_TIMEZONE_EXTERNALS - broken on *BSD
AC_CHECK_FUNCS(localtime_r timegm gmtime_r)
AC_SYS_LARGEFILE

AC_MSG_CHECKING(for bswap_16)
AC_TRY_LINK([
  #if HAVE_BYTESWAP_H
  # include <byteswap.h>
  #endif
],[
  bswap_16 (0x12);
],[
  AC_DEFINE(HAVE_BSWAP_16, 1, [Define to 1 if you have the 'bswap_16' function.])
  AC_MSG_RESULT(yes)
],[
  AC_MSG_RESULT(no)
])
AC_MSG_CHECKING(for bswap_32)
AC_TRY_LINK([
  #if HAVE_BYTESWAP_H
  # include <byteswap.h>
  #endif
],[
  bswap_32 (0x1234);
],[
  AC_DEFINE(HAVE_BSWAP_32, 1, [Define to 1 if you have the 'bswap_32' function.])
  AC_MSG_RESULT(yes)
],[
  AC_MSG_RESULT(no)
])

dnl
dnl  Checks for fsuage
dnl
AC_CHECK_HEADERS(sys/param.h sys/vfs.h sys/fs_types.h)
AC_CHECK_HEADERS(sys/mount.h, [], [],
[#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
])
jm_FILE_SYSTEM_USAGE([gl_cv_fs_space=yes], [gl_cv_fs_space=no])
if test $gl_cv_fs_space = yes; then
  AC_LIBOBJ(fsusage)
  gl_PREREQ_FSUSAGE_EXTRA
fi

AC_CACHE_SAVE

dnl
dnl  Check for libz
dnl
CHECK_ZLIB


dnl
dnl  Check for pthreads
dnl
ACX_PTHREAD(HAVE_PTHREAD=yes,HAVE_PTHREAD=no)

dnl Solaris, for example, needs additional libs to use POSIX semaphores.
if [[ "x$HAVE_PTHREAD" = "xyes" ]]; then
 SAVE_CFLAGS="$CFLAGS"
 SAVE_LIBS="$LIBS"
 CFLAGS="$PTHREAD_CFLAGS $CFLAGS"
 LIBS="$PTHREAD_LIBS"
 AC_SEARCH_LIBS(sem_init, rt posix4,,,"$SAVE_LIBS")
 PTHREAD_LIBS="$LIBS"
 CFLAGS="$SAVE_CFLAGS"
 LIBS="$SAVE_LIBS"
fi

dnl
dnl  Check for dlopen
dnl
SAVE_LIBS="$LIBS"
LIBS="$LIBS -ldl"
AC_CHECK_LIB(dl, dlopen, [
    AC_DEFINE(HAVE_DLOPEN, 1, "Define to 1 if you have 'dlopen' function)
  ], [
    LIBS="$SAVE_LIBS"
  ]
)

dnl
dnl  Check availability of OSS
dnl
HAVE_USS_SOUND=no
if [[ "x$ac_cv_header_sys_soundcard_h" = "xyes" -o "x$ac_cv_header_machine_soundcard_h" = "xyes" ]]; then
  if [[ "x$HAVE_LIBOSSAUDIO" = "xyes" ]]; then
    SAVE_LIBS="$LIBS"
    LIBS="$LIBS -lossaudio"
  fi
  dnl Avoid surprises
  AC_MSG_CHECKING(whether sys/soundcard.h or machine/soundcard.h works)
  AC_TRY_LINK([
#include "confdefs.h"
#ifdef HAVE_SYS_SOUNDCARD_H
#include <sys/soundcard.h>
#else
#include <machine/soundcard.h>
#endif
#include <sys/ioctl.h>
],
[int soundfd; ioctl (soundfd, SNDCTL_DSP_GETFMTS, 0);],
AC_MSG_RESULT(yes)
HAVE_USS_SOUND=yes,
AC_MSG_RESULT(no))
  if [[ "x$HAVE_LIBOSSAUDIO" = "xyes" ]]; then
    LIBS="$SAVE_LIBS"
  fi
fi


dnl
dnl  Find out which platform to build for
dnl
AC_MSG_CHECKING([host OS support])

dnl default is generic Unix-like OS
HOST_OS=generic
OSDEP=od-generic

case $host_os in
    linux*)
      HOST_OS=linux
      OSDEP=od-linux
      ;;
    darwin*)
      HOST_OS=darwin
      OSDEP=od-macosx
      ;;
    beos)
      HOST_OS=beos
      OSDEP=od-beos
      ;;
    amigaos)
      HOST_OS=amiga
      OSDEP=od-amiga
      ;;
    morphos)
      HOST_OS=morphos
      OSDEP=od-amiga
      ;;
    aros)
      HOST_OS=aros
      OSDEP=od-amiga
      ;;
    mingw32*)
      HOST_OS=win32
      OSDEP=od-win32
esac

AC_MSG_RESULT([$HOST_OS])

dnl
dnl  Find out what CPU arch to build for
dnl
AC_MSG_CHECKING([host cpu support])

dnl default is to use no CPU-specific features
HOST_CPU=generic
MDFPP_H=fpp-unknown.h

case $host_cpu in
     i[[3-6]]86)
       HOST_CPU=i386
       MDFPP_H=fpp-ieee.h       
       ;;
     amd64 | x86_64)
       HOST_CPU=amd64
       MDFPP_H=fpp-ieee.h       
       ;;
     powerpc)
       HOST_CPU=ppc
       MDFPP_H=fpp-ieee.h
       ;;
     m68k)
       HOST_CPU=68k
       MDFPP_H=fpp-ieee.h
       ;;
esac

AC_MSG_RESULT([$HOST_CPU])
MACHDEP="md-$HOST_CPU"

dnl
dnl  MULTIPLICATION_PROFITABLE?
dnl
dnl  We always set this for now. It's only used in the audio emulation,
dnl  where the option is to do multiplication or table look-up. On just
dnl  about any CPU it's worthwhile running E-UAE on, multiplication will be
dnl  better.

AC_DEFINE(MULTIPLICATION_PROFITABLE, 1, [Define to 1 if your CPU profitably supports multiplication.])


dnl
dnl  Find out what compiler we have
dnl
HAVE_GCC27=no
HAVE_GCC30=no
HAVE_GLIBC2=no

AC_MSG_CHECKING([for GCC 2.7 or higher])
AC_EGREP_CPP(yes,
[#if (__GNUC__ - 1 > 0 && __GNUC_MINOR__ - 1 > 5) || __GNUC__ - 1 > 1
  yes
#endif
], [AC_MSG_RESULT(yes); HAVE_GCC27=yes], AC_MSG_RESULT(no))

if [[ "x$HAVE_GCC27" = "xyes" ]]; then
  AC_MSG_CHECKING([for GCC 3.0 or higher])
  AC_EGREP_CPP(yes,
  [#if __GNUC__ - 1 > 1
    yes
  #endif
  ], [AC_MSG_RESULT(yes); HAVE_GCC30=yes], AC_MSG_RESULT(no))
fi

if test $ac_cv_prog_gcc = yes; then
  if test $HAVE_GCC27 != yes; then
    AC_MSG_WARN([Version 2.7 or better of GCC is recommended])
    NR_WARNINGS=`expr $NR_WARNINGS + 1`
  fi
else
  if test $uae_cv_prog_cc_watcom != yes; then
    AC_MSG_WARN([UAE may not compile and run correctly with your compiler])
    NR_WARNINGS=`expr $NR_WARNINGS + 1`
  fi
fi

if test $ac_cv_prog_gcc = yes; then
  if [[ "$HOST_CPU" = i386 -o "$HOST_CPU" = ppc -o "$HOST_CPU" = amd64 ]]; then
    MACHDEP="$MACHDEP-gcc"
  fi
fi

dnl
dnl  Now we know the compiler, check some compiler options
dnl
if [[ "x$HAVEGCC27" = "xyes" -a "$HOST_CPU" = "i386" ]]; then
  SAVECFLAGS=$CFLAGS
  CFLAGS="$CFLAGS -mno-schedule-prologue"
  AC_TRY_COMPILE(,int main(){return 0;}, OPTIMIZE_CFLAGS="$OPTIMIZE_CFLAGS -mno-schedule-prologue")
  CFLAGS=$SAVECFLAGS
fi
if [[ "x$HAVEGCC27" = "xyes" -a "$HOST_CPU" = "i386" ]]; then
  SAVECFLAGS=$CFLAGS
  CFLAGS="$CFLAGS -mpreferred-stack-boundary=2"
  AC_TRY_COMPILE(,int main(){return 0;}, OPTIMIZE_CFLAGS="$OPTIMIZE_CFLAGS -mpreferred-stack-boundary=2")
  CFLAGS=$SAVECFLAGS
fi
dnl If GCC supports exceptions, we don't want them.
dnl
if [[ "x$HAVEGCC27" = "xyes" -a "$HOST_CPU" = "i386" ]]; then
  SAVECFLAGS=$CFLAGS
  CFLAGS="$CFLAGS -fno-exceptions"
  AC_TRY_COMPILE(,int main(){return 0;}, OPTIMIZE_CFLAGS="$OPTIMIZE_CFLAGS -fno-exceptions")
  CFLAGS=$SAVECFLAGS
fi

dnl on OS X, we don't want position-independent code, so
dnl include the option -mdynamic-no-pic.
dnl
if [[ "$HOST_OS" = "darwin" ]]; then
  OPTIMIZE_CFLAGS="$OPTIMIZE_CFLAGS -mdynamic-no-pic"
fi

if [[ "x$ac_cv_header_features_h" = "xyes" ]]; then
  AC_MSG_CHECKING(for glibc-2.0 or higher)
  AC_EGREP_CPP(yes,
[#include <features.h>
#if __GLIBC__ - 1 >= 1
  yes
#endif
], [AC_MSG_RESULT(yes); HAVE_GLIBC2=yes], AC_MSG_RESULT(no))
fi


dnl
dnl  Generic default targets
dnl
TARGET=x11
TARGETDEP=t-unix.h
if [[ "$HAVE_PTHREAD" = "yes" ]]; then
  THREADDEP=td-posix
  THREADNAME=posix
else
  THREADDEP=td-none
  THREADNAME=none
fi
if [[ "x$no_x" != "xyes" ]]; then
  GFX_DEP=gfx-x11
  GFX_NAME=x11
else
  GFX_DEP=
  GXF_NAME=
fi
GUI_DEP=gui-none
GUI_NAME=none
GUI_LIBS=
SND_DEP=sd-none
SND_NAME=none
JOY_DEP=jd-none
JOY_NAME=none

DO_PROFILING=no
if [[ "$HOST_CPU" = "i386" ]]; then
  WANT_JIT=yes
else
  WANT_JIT=no
fi
NATMEM=no
NOFLAGS=no
WANT_DGA=no
WANT_VIDMODE=no
WANT_THREADS=dunno
NEED_THREAD_SUPPORT=no
WANT_AUTOCONFIG=dunno
WANT_SCSIEMU=no
WANT_AGA=yes
WANT_CD32=dunno
WANT_CDTV=dunno
WANT_BSDSOCK=dunno
WANT_UI=dunno
WANT_AUDIO=dunno
WANT_COMPATIBLE=yes
WANT_CYCLEEXACT=yes
WANT_CAPS=no
WANT_FDI=yes
WANT_ENFORCER=dunno

MATHLIB=-lm

dnl
dnl  Override defaults for specfic targets
dnl
if [[ "$HOST_OS" = "linux" ]]; then
  JOY_DEP=jd-linuxold
  JOY_NAME="linux"
  if [[ "$HOST_CPU" = "i386" ]]; then
    NATMEM="0x50000000"
  fi
else if [[ "$OSDEP" = "od-beos" ]]; then
  TARGET=beos
  TARGETDEP=t-beos.h
  THREADDEP=td-beos
  THREADNAME="native BeOS"
  GFX_DEP=gfx-beos
  GFX_NAME="native BeOS"
  GFX_LIBS="-lgame"
  GUI_DEP="gui-beos"
  GUI_NAME="native BeOS"
  GUI_LIBS="-lbe -ltracker"
  JOY_DEP=jd-beos
  JOY_NAME="native BeOS"
  JOY_LIBS="-ldevice"
  SND_DEP=sd-beos
  SND_NAME="native BeOS"
  SND_LIBS="-lmedia"
  WANT_BSDSOCK=no
  MATHLIB=""
  UAE_RSRCFILE="$OSDEP/uae.rsrc"
else if [[ "$OSDEP" = "od-amiga" ]]; then
  TARGET=amiga
  TARGETDEP=t-amiga.h
  THREADDEP=td-amigaos
  THREADNAME="native AmigaOS"
  GFX_DEP=gfx-amigaos
  GFX_NAME="native AmigaOS"
  GUI_DEP=gui-muirexx
  GUI_NAME="MUIRexx"
  SND_DEP=sd-amigaos
  SND_NAME="native AmigaOS/AHI"
  if [[ "x$HAVE_AMIGAINPUT" = "xyes" ]]; then
   JOY_DEP=jd-amigainput
   JOY_NAME="AmigaInput"
  else
   JOY_DEP=jd-amigaos
   JOY_NAME="Amiga lowlevel.library"
  fi
  WANT_BSDSOCK=no
else if [[ "$OSDEP" = "od-win32" ]]; then
  TARGET=win32
  TARGETDEP=t-win32.h
  WANT_BSDSOCK=no
fi
fi
fi
fi

dnl
dnl Win32 build has its own writelog.c
dnl
if [[ "$OSDEP" != "od-win32" ]]; then
   EXTRAOBJS=writelog.o
fi



dnl
dnl  Options
dnl

AC_ARG_ENABLE(profiling,       AS_HELP_STRING([--enable-profiling],       [Build a profiling (SLOW!) version]),                       [DO_PROFILING=$enableval],[])
AC_ARG_ENABLE(aga,             AS_HELP_STRING([--enable-aga],             [Enable AGA chipset emulation (default yes)]),              [WANT_AGA=$enableval],[])
AC_ARG_ENABLE(cdtv,            AS_HELP_STRING([--enable-cdtv],            [Enable CDTV emulation (default no)]),                      [WANT_CDTV=$enableval],[])
AC_ARG_ENABLE(cd32,            AS_HELP_STRING([--enable-cd32],            [Enable CD32 emulation (default no)]),                      [WANT_CD32=$enableval],[])
AC_ARG_ENABLE(cycle-exact-cpu, AS_HELP_STRING([--enable-cycle-exact-cpu], [Enable cycle-exact CPU emulation (default yes)]),          [WANT_CYCLEEXACT=$enableval],[])
AC_ARG_ENABLE(compatible-cpu,  AS_HELP_STRING([--enable-compatible-cpu],  [Enable compatible CPU emulation (default yes)]),           [WANT_COMPATIBLE=$enableval],[])
AC_ARG_ENABLE(jit,             AS_HELP_STRING([--enable-jit],             [Enable JIT compiler (currently x86 only)]),                [WANT_JIT=$enableval],[])
AC_ARG_ENABLE(natmem,          AS_HELP_STRING([--enable-natmem],          [Enable JIT direct memory support (default auto)]),         [NATMEM=$enableval],[])
AC_ARG_ENABLE(noflags,	       AS_HELP_STRING([--enable-noflags],         [Enable noflags support in JIT (default no)]),              [NOFLAGS=$enableval],[])
AC_ARG_ENABLE(threads,         AS_HELP_STRING([--enable-threads],         [Enable thread support (default auto)]),                    [WANT_THREADS=$enableval],[])
AC_ARG_ENABLE(autoconfig,      AS_HELP_STRING([--enable-autoconfig],      [Enable emulaton of autoconfig devices (default auto)]),    [WANT_AUTOCONFIG=$enableval],[])
AC_ARG_ENABLE(scsi-device,     AS_HELP_STRING([--enable-scsi-device],     [Enable emulaton of SCSI devices (default no)]),            [WANT_SCSIEMU=$enableval],[])
AC_ARG_ENABLE(bsdsock,         AS_HELP_STRING([--enable-bsdsock],         [Enable bsdsocket.library emulation]),                      [WANT_BSDSOCK=$enableval],[])
AC_ARG_ENABLE(debugger,        AS_HELP_STRING([--disable-debugger],       [Disable internal debugger/monitor (default no)]),          [WANT_DEBUGGER=$enableval],[])
AC_ARG_ENABLE(state-saving,    AS_HELP_STRING([--disable-state-saving],   [Disable support for saving state snapshots (default no)]),   [WANT_STATESAVING=$enableval],[])
AC_ARG_ENABLE(enforcer,	       AS_HELP_STRING([--enable-enforcer],        [Enable ersatz Enforcer support (default auto)]),           [WANT_ENFORCER=$enableval],[])
AC_ARG_ENABLE(action-replay,   AS_HELP_STRING([--enable-action-replay],   [Enable Action Replay cartridge emulation (default yes)]),  [WANT_ACTION_REPLAY=$enableval],[])
AC_ARG_ENABLE(dga,             AS_HELP_STRING([--enable-dga],             [X11 version: Use the DGA extension]),                      [WANT_DGA=$enableval],[])
AC_ARG_ENABLE(vidmode,         AS_HELP_STRING([--enable-vidmode],         [X11 version: Use the XF86VidMode extension]),              [WANT_VIDMODE=$enableval],[])
AC_ARG_ENABLE(ui,              AS_HELP_STRING([--enable-ui],              [Use a user interface if possible (default on)]),           [WANT_UI=$enableval],[])
AC_ARG_ENABLE(audio,	       AS_HELP_STRING([--enable-audio],           [Enable audio output (default auto)]),                      [WANT_AUDIO=$enableval],[])
AC_ARG_ENABLE(fdi,	       AS_HELP_STRING([--enable-fdi],             [Enable FDI support (default yes)]),                        [WANT_FDI=$enableval],[])

AC_ARG_WITH(sdl,
  AS_HELP_STRING([--with-sdl], [Use SDL library for low-level functions]),
  [WANT_SDL=$withval], [])
AC_ARG_WITH(sdl-sound,
  AS_HELP_STRING([--with-sdl-sound], [Use SDL library for sound]),
  [WANT_SDLSND=$withval], [])
AC_ARG_WITH(sdl-gfx,
  AS_HELP_STRING([--with-sdl-gfx], [Use SDL library for graphics]),
  [WANT_SDLGFX=$withval], [])
AC_ARG_WITH(sdl-gl,
  AS_HELP_STRING([--with-sdl-gl], [Allow GL for 2D acceleration with SDL graphics]),
  [WANT_SDLGL=$withval], [])

AC_ARG_WITH(curses,
  AS_HELP_STRING([--with-curses], [Use ncurses library for graphics]),
  [WANT_NCURSES=$withval], [])

AC_ARG_WITH(cocoa-gui,
  AS_HELP_STRING([--with-cocoa-gui], [Use Cocoa for GUI on OS X]),
  [WANT_COCOA_UI=$withval], [])

AC_ARG_WITH(libscg-prefix,
  AS_HELP_STRING([--with-libscg-prefix], [Absolute path to where libscg is installed (optional)]),
  [LIBSCG_PREFIX=$withval], [])
AC_ARG_WITH(libscg-includedir,
  AS_HELP_STRING([--with-libscg-includedir], [Absolute path to libscg headers are installed (default LIBSCG_PREFIX/include)]),
  [LIBSCG_INCLUDEDIR=$withval], [])
AC_ARG_WITH(libscg-libdir,
  AS_HELP_STRING([--with-libscg-libdir], [Absolute path to libscg libs are installed (default LIBSCG_PREFIX/lib)]),
  [LIBSCG_LIBDIR=$withval], [])

AC_ARG_WITH(alsa,
  AS_HELP_STRING([--with-alsa], [Use ALSA library for sound]),
  [WANT_ALSA=$withval],[])

dnl
dnl Some simple plausibility tests...
dnl
AC_MSG_CHECKING([configuration options])

if [[ "x$WANT_SDLGL" = "xyes" ]]; then
  WANT_SDLGFX=yes
fi

if [[ "x$WANT_SDLSND" = "xyes" -o "x$WANT_SDLGFX" = "xyes" ]]; then
  WANT_SDL=yes
fi
if [[ "x$WANT_SDL" = "xno" ]]; then
  WANT_SDLGFX=no
  WANT_SDLSND=no
  WANT_SDLGL=no
fi

if [[ "x$WANT_DGA" = "xyes" -a "x$WANT_SDLGFX" = "xyes" ]]; then
    AC_MSG_WARN([DGA support cannot be enabled for non-X11 targets!])
    NR_ERRORS=`expr $NR_ERRORS + 1`
    WANT_DGA=no
fi

if [[ "x$WANT_DGA" = "xyes" -a "x$no_x" = "xyes" ]]; then
  AC_MSG_WARN([Ignoring --enable-dga, since X was disabled or not found])
  NR_ERRORS=`expr $NR_ERRORS + 1`
  WANT_DGA=no
fi

if [[ "x$WANT_DGA" = "xno" -a "x$WANT_VIDMODE" = "xyes" ]]; then
  AC_MSG_WARN([The XF86VidMode extension can only be used in DGA mode. Disabling it])
  NR_ERRORS=`expr $NR_ERRORS + 1`
  WANT_VIDMODE=no
fi

AC_MSG_RESULT(ok)


dnl Check for X
AC_PATH_XTRA

dnl Check for ncurses
AC_CHECK_LIB(ncurses, waddch, HAVE_NCURSES_LIB=yes, HAVE_NCURSES_LIB=no)

dnl Check for SDL
if [[ "x$WANT_SDL" != "xno" ]]; then

  if [[ "$HOST_OS" = "darwin" ]]; then

    dnl Assume this is MacOS X and try to use SDL framework

    SDL_CFLAGS="-I/Library/Frameworks/SDL.framework/Headers -D_REENTRANT"
    SDL_LIBS="-framework Cocoa -framework SDL -lobjc"

    AC_MSG_CHECKING(for SDL framework)

    SAVE_CPPFLAGS=$CPPFLAGS
    SAVE_LIBS=$LIBS
    CPPFLAGS="$CPPFLAGS $SDL_CFLAGS"
    LIBS="$LIBS $SDL_LIBS"

    AC_TRY_LINK(
    [
      #include "confdefs.h"
      #include <SDL.h>
      #undef main
    ],
    [
      SDL_Init(SDL_INIT_VIDEO);
    ],
    AC_MSG_RESULT(yes)
    HAVE_SDL=yes
    WANT_COCOA_UI=yes,
    AC_MSG_RESULT(no)
    HAVE_SDL=no)

    CPPFLAGS="$SAVE_CPPFLAGS"
    LIBS="$SAVE_LIBS"
  else
    AM_PATH_SDL(1.2.0,HAVE_SDL=yes,HAVE_SDL=no)
  fi

  if [[ "x$WANT_SDL" = "xyes" -a "x$HAVE_SDL" = "xno" ]]; then
    AC_MSG_WARN([SDL support wanted, but libSDL could not be found])
    WANT_SDL=no
    NR_ERRORS=`expr $NR_ERRORS + 1`
  fi
fi


if test $ac_cv_prog_gcc = yes; then
  WARNING_CFLAGS="$WARNING_CFLAGS -Wall -Wno-unused -Wno-format -W -Wmissing-prototypes -Wstrict-prototypes"
  OPTIMIZE_CFLAGS="$OPTIMIZE_CFLAGS -fomit-frame-pointer"
fi

if [[ "x$DO_PROFILING" = "xyes" ]]; then
  if [[ "x$HAVE_GCC27" = "xyes" ]]; then
    OPTIMIZE_CFLAGS=""
    DEBUG_CFLAGS="-g -fno-inline -fno-omit-frame-pointer -pg -DUSE_PROFILING"
    LDFLAGS="-pg"
  else
    DO_PROFILING=no
    AC_MSG_WARN([Don't know how to set up profiling for your compiler])
    NR_ERRORS=`expr $NR_ERRORS + 1`
  fi
fi

UAE_CFLAGS="$OPTIMIZE_CFLAGS $DEBUG_CFLAGS $WARNING_CFLAGS"


dnl
dnl  Check CPU emulation options
dnl

dnl  Now configure the CPU emulation proper
ASMOBJS=
UAE_DEFINES="$UAE_DEFINES -DFPUEMU -DCPUEMU_0"
CPUOBJS="cpuemu_0.o"

if [[ "x$WANT_COMPATIBLE" != "xno" ]]; then
  UAE_DEFINES="$UAE_DEFINES -DCPUEMU_5"
  CPUOBJS="$CPUOBJS cpuemu_5.o"
fi

if [[ "x$WANT_CYCLEEXACT" != "xno" ]]; then
  UAE_DEFINES="$UAE_DEFINES -DCPUEMU_6"
  CPUOBJS="$CPUOBJS cpuemu_6.o"
fi


if [[ "x$HAVE_GCC27" = "xyes" -a "$HOST_CPU" = "i386" -a "x$DO_PROFILING" = "xno" ]]; then
  dnl strength-reduce is turned off not because of paranoia, but because it
  dnl actually makes the code worse in some cases on the i386 (generates too
  dnl many registers, which all end up on the stack).
  UAE_CFLAGS="$UAE_CFLAGS -fno-strength-reduce -DREGPARAM=\"__attribute__((regparm(3)))\""
  UAE_DEFINES="$UAE_DEFINES -DUNALIGNED_PROFITABLE"
else if [[ "x$HAVE_GCC27" = "xyes" -a "$HOST_CPU" = "68k" ]]; then
  UAE_CFLAGS="$UAE_CFLAGS -DREGPARAM=\"__attribute__((regparm(4)))\""
  UAE_DEFINES="$UAE_DEFINES -DUNALIGNED_PROFITABLE -DM68K_FLAG_OPT=1 -DSTATIC_INLINE=\"static inline\""
else
  UAE_CFLAGS="$UAE_CFLAGS -DREGPARAM="
fi
fi


dnl
dnl  Check whether to build with optimized CCR flags handling
dnl
if [[ "x$HAVE_GCC27" = "xyes" ]]; then
  if [[ "$HOST_CPU" = "i386" -o "$HOST_CPU" = "ppc" -o "$HOST_CPU" = "68k" ]]; then
    GENCPUOPTS="--optimized-flags"
  fi
fi


dnl
dnl  Check whether to build JIT
dnl
AC_MSG_CHECKING([whether to build JIT compiler])
JITOBJS=""
if [[ "x$WANT_JIT" != "xno" -a "$HOST_CPU" = "i386" ]]; then
  UAE_DEFINES="$UAE_DEFINES -DJIT"
  if [[ "x$NATMEM" = "xyes" ]]; then
    NATMEM="0x50000000"
  fi
  if [[ "x$NATMEM" != "xno" ]]; then
    UAE_DEFINES="$UAE_DEFINES -DNATMEM_OFFSET=$NATMEM"
  fi
  JITOBJS="compstbl.o compemu.o compemu_support.o compemu_fpp.o"
  if [[ "x$NOFLAGS" != "xno" ]]; then
    JITOBJS="$JITOBJS cpustbl_nf.o"
    JITOBJS="$JITOBJS cpuemu_nf_0.o"
    if [[ "x$WANT_COMPATIBLE" != "xno" ]]; then
      JITOBJS="$JITOBJS cpuemu_nf_5.o"
    fi
    if [[ "x$WANT_CYCLEEXACT" != "xno" ]]; then
      JITOBJS="$JITOBJS cpuemu_nf_6.o"
    fi
  fi
  AC_MSG_RESULT(yes)

  AC_MSG_CHECKING([whether assembler supports --execstack option])
  SAVE_CFLAGS="$CFLAGS"
  CFLAGS="-Wa,--execstack $CFLAGS"
  AC_TRY_COMPILE(, int main (void) {return 0;},
    [AC_MSG_RESULT(yes)
    UAE_CFLAGS="-Wa,--execstack $UAE_CFLAGS"],
    AC_MSG_RESULT(no)
  )
  CFLAGS="$SAVE_CFLAGS"
else
  AC_MSG_RESULT(no)
fi

AC_CACHE_SAVE

dnl
dnl  Check which graphics target to build
dnl
AC_MSG_CHECKING([for graphics target to build])

dnl Check whether we wanted to use ncurses
dnl
dnl First check if we need to uses ncurses because no
dnl other gfx support is available
if [[ "x$WANT_NCURSES" != "xno" -a "x$WANT_SDL" = "xno" -a "x$GFX_DEP" = "x" ]]; then
 WANT_NCURSES=yes
fi

if [[ "x$WANT_NCURSES" = "xyes" ]]; then
 if [[ "x$HAVE_NCURSES_LIB" = "xyes" ]]; then
  GFX_LIBS="-lncurses"
  GFX_CFLAGS=
  GFX_CPPFLAGS=
  GFX_DEP=gfx-curses
  GFX_NAME=ncurses
 else
  AC_MSG_WARN([ncurses support wanted, but libncurses could not be found])
 fi
fi


dnl  Check whether we actually wanted to use SDL or whether
dnl  we need to use SDL because we couldn't find any other
dnl  graphics support
dnl
if [[ "x$WANT_SDL" = "xyes" -o "x$GFX_DEP" = "x" ]]; then
  if [[ "x$HAVE_SDL" = "xyes" -a "x$WANT_SDLGFX" != "xno" ]]; then
    WANT_SDL=yes
    if [[ "x$WANT_SDLGFX" = "xyes" -o "x$GFX_DEP" = "x" ]]; then
      GFX_DEP=gfx-sdl
      GFX_NAME=SDL
      GFX_LIBS=""
      GFX_CFLAGS=""
      GFX_CPPFLAGS=""
      if [[ "x$WANT_SDLGL" = "xyes" ]]; then
        GFX_CFLAGS="-DUSE_GL"
        if [[ "x$OSDEP" = "xod-macosx" ]]; then
          GFX_LIBS="-framework OpenGL"
        else
          GFX_LIBS="-lGL -lGLU"
        fi
      fi
    fi
  fi
fi


dnl  If we got here and we still haven't found a graphics target
dnl  then bail out.
dnl
if [[ "x$GFX_DEP" = "x" ]]; then
  AC_MSG_RESULT(none)
  AC_MSG_ERROR([Could not find a useable graphics target])
fi

AC_MSG_RESULT("$GFX_NAME")

dnl
dnl  If we're using X, find out what extensions are wanted/available
dnl
if [[ "$GFX_DEP" = "gfx-x11" ]]; then
  dnl If the user wants DGA, see if we have it.
  dnl This must come after we checked for X11.

  if [[ "x$WANT_DGA" = "xyes" ]]; then
    TMP_SAVE_LIBS=$LIBS
    LIBS="$X_LIBS $LIBS"
    AC_CHECK_LIB(Xxf86dga, XF86DGAQueryExtension, HAVE_DGA=yes, HAVE_DGA=no, [ $X_PRE_LIBS -lXext -lX11 $X_EXTRA_LIBS ])
    echo $LIBS
    LIBS=$TMP_SAVE_LIBS
    if [[ "x$HAVE_DGA" = "xno" ]]; then
      AC_MSG_WARN([Could not find DGA extension, ignoring --enable-dga])
      NR_ERRORS=`expr $NR_ERRORS + 1`
      WANT_DGA=no
    else
      X_CFLAGS="$X_CFLAGS -DUSE_DGA_EXTENSION"
      X_EXTRA_LIBS="$X_EXTRA_LIBS -lXxf86dga"
    fi
  fi

  if [[ "x$WANT_VIDMODE" = "xyes" ]]; then
    TMP_SAVE_LIBS=$LIBS
    LIBS="$X_LIBS $LIBS"
    AC_CHECK_LIB(Xxf86vm, XF86VidModeQueryExtension, HAVE_VIDMODE=yes, HAVE_VIDMODE=no, [ $X_PRE_LIBS -lXext -lX11 $X_EXTRA_LIBS ])
    LIBS=$TMP_SAVE_LIBS
    if [[ "x$HAVE_VIDMODE" = "xno" ]]; then
      AC_MSG_WARN([Could not find VidMode extension, ignoring --enable-vidmode])
      NR_ERRORS=`expr $NR_ERRORS + 1`
      WANT_VIDMODE=no
    else
      X_CFLAGS="$X_CFLAGS -DUSE_VIDMODE_EXTENSION"
      X_EXTRA_LIBS="$X_EXTRA_LIBS -lXxf86vm"
    fi
  fi

  dnl Check if we can compile with XKB/XKBfile support

  AC_MSG_CHECKING(whether to compile XKB extension support)
  TMP_SAVE_LIBS=$LIBS
  TMP_SAVE_CFLAGS=$CFLAGS
  LIBS="$X_LIBS $LIBS -lxkbfile"
  CFLAGS="$CFLAGS $X_CFLAGS"
  AC_TRY_LINK([
    #include "confdefs.h"
    #include <stdio.h>
    #ifdef HAVE_SYS_TYPES_H
    # include <sys/types.h>
    #endif
    #include <X11/Xlib.h>
    #include <X11/XKBlib.h>
    #include <X11/extensions/XKBrules.h>],[
    int major, minor;
    XkbRF_RulesPtr rules;
    XkbLibraryVersion (&major, &minor);
    rules = XkbRF_Load ("dummy", "", True, True);
  ],
  AC_MSG_RESULT(yes)
  X_CFLAGS="$X_CFLAGS -DUSE_XKB"
  X_EXTRA_LIBS="$X_EXTRA_LIBS -lxkbfile"
  xkb_rules_dir_found="no"
  for xkb_rules_dir in /usr/share/X11/xkb/rules /usr/lib/X11/xkb/rules /usr/X11R6/lib/X11/xkb/rules
  do
	if test -d $xkb_rules_dir ; then
		X_CFLAGS="$X_CFLAGS -DXKB_PATH=\\\"$xkb_rules_dir/\\\""
		xkb_rules_dir_found="yes"
		break
	fi
  done
  if test "x$xkb_rules_dir_found" = "xno" ; then
  	AC_MSG_ERROR([[Could not find xkb path.]])
  fi,
  AC_MSG_RESULT(no)
  )
  LIBS=$TMP_SAVE_LIBS
  CFLAGS=$TMP_SAVE_CFLAGS

dnl See if we can at least compile SHM support

  if [[ "x$no_x" != "xyes" -a "x$ac_cv_header_sys_ipc_h" = "xyes" -a "x$ac_cv_header_sys_shm_h" = "xyes" ]]; then
    AC_MSG_CHECKING(whether the X11 MIT-SHM extension can be compiled in)
    TMP_SAVE_CFLAGS=$CFLAGS
    TMP_SAVE_LIBS=$LIBS
    CFLAGS="$CFLAGS $X_CFLAGS"
    LIBS="$X_LIBS $LIBS $X_PRE_LIBS -lX11 -lXext $X_EXTRA_LIBS"
    AC_TRY_LINK([
  #include "confdefs.h"
  #ifdef HAVE_SYS_TYPES_H
  #include <sys/types.h>
  #endif
  #include <X11/Xlib.h>
  #include <X11/Xutil.h>
  #include <X11/keysym.h>
  #include <X11/cursorfont.h>
  #include <sys/ipc.h>
  #include <sys/shm.h>
  #include <X11/extensions/XShm.h>],[
  static XShmSegmentInfo shminfo;
  static Display *display;
  XShmAttach(display, &shminfo);
  XSync(display,0);
  shmctl(shminfo.shmid, IPC_RMID, 0);
  ],
  SHM_SUPPORT_LINKS=1
  AC_MSG_RESULT(yes),
  SHM_SUPPORT_LINKS=0
  AC_MSG_RESULT(no))
  fi
  CFLAGS=$TMP_SAVE_CFLAGS
  LIBS=$TMP_SAVE_LIBS

  GFX_CFLAGS="$X_CFLAGS"
  GFX_CPPFLAGS="-DSHM_SUPPORT_LINKS=$SHM_SUPPORT_LINKS"
  GFX_LIBS="$X_LIBS $X_PRE_LIBS -lXext -lX11 $X_EXTRA_LIBS"
fi


dnl
dnl
dnl  Check which sound target to use
AC_MSG_CHECKING([for sound target to build])

if [[ "x$WANT_AUDIO" != "xno" ]]; then
  if [[ "x$SND_NAME" = "xnone" ]]; then
    if [[ "x$ac_cv_header_sys_audioio_h" = "xyes" -o "x$ac_cv_header_sun_audioio_h" = "xyes" ]]; then
      SND_DEP=sd-solaris
      SND_NAME=Solaris/NetBSD
      USE_SOUND=yes
    else if [[ "x$WANT_ALSA" = "xyes" ]]; then
      SND_DEP=sd-alsa
      SND_LIBS="-lasound"
      SND_NAME=ALSA
      USE_SOUND=yes
    else if [[ "x$HAVE_USS_SOUND" = "xyes" ]]; then
      SND_DEP=sd-uss
      if [[ "x$HAVE_LIBOSSAUDIO" = "xyes" ]]; then
        SND_LIBS="-lossaudio"
      fi
      SND_NAME=OSS
      USE_SOUND=yes
    fi
    fi
    fi
  fi

  dnl
  dnl  Did we actually want to use SDL for sound, or should we
  dnl  use it 'cos there's nothing else?
  dnl
  if [[ "x$WANT_SDLSND" = "xyes" -o "x$SND_DEP" = "x" ]]; then
    if [[ "x$HAVE_SDL" = "xyes" -a "x$WANT_SDLSND" != "xno" ]]; then
      SND_DEP=sd-sdl
      SND_LIBS=
      SND_NAME=SDL
      NEED_THREAD_SUPPORT=yes
    fi
  fi

  AC_MSG_RESULT("$SND_NAME")
else
  AC_MSG_RESULT("sound support disabled")
  SND_DEP=sd-none
  SND_LIBS=
  SND_NAME=none
fi

dnl
dnl  Did we want a GUI?
dnl
AC_MSG_CHECKING([which UI target to build])
if [[ "x$WANT_UI" = "xno" ]]; then
  GUI_DEP=gui-none
  GUI_LIBS=
  GUI_NAME=none
else
  dnl Hack for building Cocoa gui on OS X
  dnl We really should reall check for the presence of objc and Cocoa first
  if [[ "$HOST_OS" = "darwin" ]]; then
    if [[ "x$WANT_COCOA_UI" != "xno" -o "x$HAVE_GTK" = "xno" ]]; then
      GUI_DEP=gui-cocoa
      GUI_CFLAGS="$CFLAGS -DNO_MAIN_IN_MAIN_C"
      GUI_LIBS="-framework Cocoa"
      GUI_NAME="Cocoa"
    fi
  fi
  if [[ "$GUI_DEP" = "gui-none" ]]; then
    if [[ "x$HAVE_GTK" = "xno" ]]; then
      AC_MSG_WARN([UI wanted, but no UI useable target found])
      NR_ERRORS=`expr $NR_ERRORS + 1`
      WANT_UI=no
    else
      GUI_DEP=gui-gtk
      GUI_LIBS="$GTK_LIBS"
      GUI_NAME="GTK+"
      GUI_CFLAGS="$CFLAGS"
      NEED_THREAD_SUPPORT=yes
      if [[ "x$WANT_THREADS" = "xno" ]]; then
        AC_MSG_ERROR([Thread support disabled, but GTK+ UI requires threads])
      fi
    fi
  fi
fi
AC_MSG_RESULT("$GUI_NAME")


dnl
dnl  Do we want AGA emulation
dnl
AC_MSG_CHECKING([whether to build AGA emulation])
if [[ "x$WANT_AGA" != "xno" ]]; then
  UAE_DEFINES="$UAE_DEFINES -DAGA"
  AC_MSG_RESULT(yes)
else
  AC_MSG_RESULT(no)
fi


dnl
dnl  Do we want Autoconfig?
dnl
AC_MSG_CHECKING([whether to build support for Autconfig devices])
if [[ "x$WANT_AUTOCONFIG" != "xno" ]]; then
  UAE_DEFINES="$UAE_DEFINES -DAUTOCONFIG"

  dnl  Configure hard disk emulation
  UAE_DEFINES="$UAE_DEFINES -DFILESYS"
  NEED_THREAD_SUPPORT=yes
  FILESYSOBJS="filesys.o fsdb.o fsusage.o hardfile.o"
  if [[ "x$HOST_OS" != "xwin32" ]]; then
    FILESYSOBJS="$FILESYSOBJS filesys_unix.o fsdb_unix.o hardfile_unix.o"
  else
    FILESYSOBJS="$FILESYSOBJS"
  fi
  dnl  Filesys support currently requires threads, because hardfile
  dnl  support requires threads and we can't currently separate the
  dnl  two.
  dnl
  dnl  However, we don't currently build the threaded filesys implementation
  dnl  because it performs poorly.
  dnl
  dnl  UAE_DEFINES="$UAE_DEFINES -DUAE_FILESYS_THREADS"

  AC_MSG_RESULT(yes)
else
  AC_MSG_RESULT(no)
fi


dnl
dnl  Do we want SCSI emulation?
dnl  This needs tidying up. ;-)
dnl
AC_MSG_CHECKING([whether to build SCSI emulation])
if [[ "x$WANT_SCSIEMU" = "xyes" ]]; then
  if [[ "x$WANT_AUTOCONFIG" = "xno" ]]; then
    AC_MSG_RESULT(no)
    AC_MSG_WARN([Autoconfig support not enabled, so SCSI emulation cannot be enabled])
    NR_WARNINGS=`expr $NR_WARNINGS + 1`
    WANT_SCSIEMU=no
  else
    if [[ "$OSDEP" = "od-amiga" ]]; then
      AC_MSG_RESULT(yes)
      SCSIOBJS="scsiemul.o blkdev.o"
      UAE_DEFINES="$UAE_DEFINES -DSCSIEMU"
      NEED_THREAD_SUPPORT=yes
    else
    SCGLIBS="-lscg -lschily"
    if [[ "$HOST_OS" = "darwin" ]]; then
      SCGLIBS="$SCGLIBS -framework IOKit -framework CoreFoundation"
    fi
    AC_MSG_RESULT(yes)
    AC_MSG_CHECKING([for libscg and libschily])
    OLDLIBS="$LIBS"
    OLDCPPFLAGS="$CPPFLAGS"
    if [[ "x$LIBSCG_PREFIX" != "x" ]]; then
       LIBSCG_CPPFLAGS="-I$LIBSCG_PREFIX/include"
       LIBSCG_LIBS="-L$LIBSCG_PREFIX/lib"
    fi
    if [[ "x$LIBSCG_INCLUDEDIR" != "x" ]]; then
       LIBSCG_CPPFLAGS="-I$LIBSCG_INCLUDEDIR"
    fi
    if [[ "x$LIBSCG_LIBDIR" != "x" ]]; then
       LIBSCG_LIBS="-L$LIBSCG_LIBDIR $SCGLIBS"
    else
       LIBSCG_LIBS="$SCGLIBS"
    fi
    LIBS="$LIBS $LIBSCG_LIBS"
    CPPFLAGS="$CPPFLAGS $LIBSCG_CPPFLAGS"
    AC_TRY_LINK([
typedef int BOOL;
#include "scg/scgcmd.h"
#include "scg/scsitransp.h"
#include "scg/scsireg.h"],
[{SCSI *scgp = scg_open (0, 0, 0, 0, 0);}],
    [
      AC_MSG_RESULT(yes)
      SCSIOBJS="scsiemul.o blkdev.o blkdev-libscg.o"
      UAE_DEFINES="$UAE_DEFINES -DSCSIEMU"
      UAE_CPPFLAGS="$UAE_CPPFLAGS $LIBSCG_CPPFLAGS"
      UAE_LIBS="$UAE_LIBS $LIBSCG_LIBS"
      NEED_THREAD_SUPPORT=yes
    ],
    [
      AC_MSG_RESULT(no)
      AC_MSG_CHECKING([for libscg and libschily in UAE source tree])
      LIBSCG_CPPFLAGS="$OLDCPPFLAGS -I$srcdir/src/include"
      LIBSCG_LIBS="-L$srcdir/src $OLDLIBS $SCGLIBS"
      CPPFLAGS="$LIBSCG_CPPFLAGS"
      LIBS="$LIBSCG_LIBS"
      AC_TRY_LINK([
typedef int BOOL;
#include "scg/scgcmd.h"
#include "scg/scsitransp.h"
#include "scg/scsireg.h"],
[{SCSI *scgp = scg_open (0, 0, 0, 0, 0);}],
      [
        AC_MSG_RESULT(yes)
        SCSIOBJS="scsiemul.o blkdev.o blkdev-libscg.o"
        UAE_DEFINES="$UAE_DEFINES -DSCSIEMU"
        NEED_THREAD_SUPPORT=yes
        if [[ "$srcdir" != "." ]]; then
          AC_CONFIG_LINKS(src/libscg.a:src/libscg.a)
          AC_CONFIG_LINKS(src/libschily.a:src/libschily.a)
	  UAE_LIBS="$UAE_LIBS $LIBSCG_LIBS"
        else
          UAE_LIBS="$UAE_LIBS -L. $SCGLIBS"
        fi
      ],
      [
        AC_MSG_RESULT(no)

	AC_MSG_CHECKING([for Linux cdrom support via ioctl])
	LIBS="$OLDLIBS"
	CPPFLAGS="$OLDCPPFLAGS"
	AC_TRY_LINK([
#include <linux/cdrom.h>],
[{struct cdrom_generic_command cmd; ioctl (0, CDROM_SEND_PACKET, &cmd);}],
	[
	   AC_MSG_RESULT(yes)
	   SCSIOBJS="scsiemul.o blkdev.o"
	   UAE_DEFINES="$UAE_DEFINES -DSCSIEMU -DSCSIEMU_LINUX_IOCTL"
	   NEED_THREAD_SUPPORT=yes
	],
	[

	AC_MSG_RESULT(no)
        NR_WARNINGS=`expr $NR_WARNINGS + 1`
        echo "Please refer to the UAE SCSI device section in docs/README for installation instructions."
        WANT_SCSIEMU=no
	])
      ])
    LIBS="$OLDLIBS"
    CPPFLAGS="$OLDCPPFLAGS"
    ])
    fi
  fi
else
  AC_MSG_RESULT(no)
fi
if [[ "x$WANT_SCSIEMU" != "xyes" ]]; then
  SCSIOBJS="scsi-none.o"
  AC_MSG_RESULT(no)
fi


dnl
dnl  Do we want CDTV emulation ?
dnl
if [[ "x$WANT_CDTV" != "xyes" -a "x$WANT_SCSIEMU" != "xyes" ]]; then
  WANT_CDTV=no
fi
AC_MSG_CHECKING([whether to build CDTV emulation])
if [[ "x$WANT_CDTV" != "xno" ]]; then
  AC_MSG_RESULT(yes)
  if [[ "x$WANT_SCSIEMU" != "xyes" ]]; then
    AC_MSG_WARN([SCSI support is not enabled or cannot be built. Disabling CDTV emulation])
    NR_WARNINGS=`expr $NR_WARNINGS + 1`
    WANT_CDTV=no
  else
    UAE_DEFINES="$UAE_DEFINES -DCDTV"
    CDOBJS="cdrom.o"
  fi
else
  AC_MSG_RESULT(no)
fi


dnl
dnl  Do we want CD32 emulation ?
dnl
if [[ "x$WANT_CD32" != "xyes" ]]; then
  if [[ "x$WANT_SCSIEMU" != "xyes" -o "x$WANT_AGA" = "xno" ]]; then
    WANT_CD32=no
  fi
fi
AC_MSG_CHECKING([whether to build CD32 emulation])
if [[ "x$WANT_CD32" != "xno" ]]; then
  AC_MSG_RESULT(yes)
  if [[ "x$WANT_SCSIEMU" != "xyes" ]]; then
    AC_MSG_WARN([SCSI support is not enabled or cannot be built. Disabling CD32 emulation])
    NR_WARNINGS=`expr $NR_WARNINGS + 1`
    WANT_CD32=no
  else
    if [[ "x$WANT_AGA" != "xyes" ]]; then
      AC_MSG_WARN([AGA emulation is not enabled. Disabling CD32 emulation])
      NR_WARNINGS=`expr $NR_WARNINGS + 1`
      WANT_CD32=no
    else
      UAE_DEFINES="$UAE_DEFINES -DCD32"
      if [[ "x$WANT_CDTV" = "xno" ]]; then
        CDOBJS="cdrom.o"
      fi
      CDOBJS="$CDOBJS akiko.o"
    fi
  fi
else
  AC_MSG_RESULT(no)
fi


dnl
dnl  Do we want bsdsocket.library emulation?
dnl
AC_MSG_CHECKING([whether to build bsdsocket.library emulation])
if [[ "x$WANT_BSDSOCK" != "xno" ]]; then
  if [[ "x$WANT_AUTOCONFIG" = "xno" ]]; then
    AC_MSG_RESULT(no)
    AC_MSG_WARN([Autoconfig support not enabled, so bsdsocket emulation cannot be enabled])
    NR_WARNINGS=`expr $NR_WARNINGS + 1`
  else
    NEED_THREAD_SUPPORT=yes
    UAE_DEFINES="$UAE_DEFINES -DBSDSOCKET"
    BSDSOCKOBJS="bsdsocket-posix-new.o bsdsocket.o"
    AC_MSG_RESULT(yes)
  fi
else
  AC_MSG_RESULT(no)
  BSDSOCKOBJS="bsdsocket.o"
fi


dnl
dnl  Check whether we want or need threads
dnl
AC_MSG_CHECKING([whether to build with threads])
if [[ "x$WANT_THREADS" = "xyes" -o "x$NEED_THREAD_SUPPORT" = "xyes" ]]; then
  AC_MSG_RESULT(yes)
  AC_MSG_CHECKING([which thread target to build])

  if [[ "x$WANT_SDL" = "xyes" -o "$THREADDEP" = "td-none" ]]; then
    if [[ "x$HAVE_SDL" = "xyes" -a "x$WANT_SDL" != "xno" ]]; then
      WANT_SDL=yes
      THREADDEP=td-sdl
      THREADNAME=SDL
    fi
  fi

  if [[ "$THREADDEP" = "td-none" ]]; then
    AC_MSG_ERROR([Thread support wanted or required, but no thread target found])
  fi

  if [[ "$THREADDEP" = "td-posix" ]]; then
    CC="$PTHREAD_CC"
    UAE_CFLAGS="$UAE_CFLAGS $PTHREAD_CFLAGS"
    UAE_LIBS="$UAE_LIBS $PTHREAD_LIBS"

    dnl Hack to use named semaphores on OS X/darwin
    dnl This needs to be properly checked when detecting pthreads!
    if [[ "$HOST_OS" = "darwin" ]]; then
      UAE_DEFINES="$UAE_DEFINES -DUSE_NAMED_SEMAPHORES"
    fi
  fi

  UAE_DEFINES="$UAE_DEFINES -DSUPPORT_THREADS"
  AC_MSG_RESULT($THREADNAME)
else
  THREADDEP=td-none
  AC_MSG_RESULT(no)
fi


dnl
dnl  So, are we using SDL?
dnl
if [[ "x$WANT_SDL" = "xyes" ]]; then
   UAE_DEFINES="$UAE_DEFINES -DUSE_SDL"
   JOY_DEP=jd-sdl
   UAE_LIBS="$UAE_LIBS $SDL_LIBS"
   UAE_CPPFLAGS="$UAE_CPPFLAGS $SDL_CFLAGS"
fi


dnl
dnl  Check for CAPS API
dnl
WANT_CAPS=dunno
AC_ARG_WITH(caps,        AS_HELP_STRING([--with-caps],        [Build with support for CAPS/IPF disk images) (default auto)]), [WANT_CAPS=$withval],[])
AC_ARG_WITH(caps-prefix, AS_HELP_STRING([--with-caps-prefix], [Prefix where CAPS API is installed (optional)]),                 [CAPS_PREFIX=$withval],[])
if [[ "x$WANT_CAPS" != "xno" ]]; then
  SAVE_CPPFLAGS="$CPPFLAGS"
  CAPS_CPPFLAGS="-I$CAPS_PREFIX/include"
  if [[ "x$CAPS_PREFIX" != "x" ]]; then
    CPPFLAGS="$CPPFLAGS $CAPS_CPPFLAGS"
  fi
  AC_CHECK_HEADERS(caps/capsimage.h, [HAVE_CAPS=yes], [HAVE_CAPS=no])
  if [[ "$HAVE_CAPS" = "yes" ]]; then
    UAE_DEFINES="$UAE_DEFINES -DCAPS"
    UAE_CPPFLAGS="$UAE_CPPFLAGS $CAPS_CPPFLAGS"
  else
    if [[ "$WANT_CAPS" = "yes" ]]; then
      AC_MSG_WARN([CAPS support wanted, but CAPS API not found])
      WANT_CAPS=no
    fi
  fi
  CPPFLAGS="$SAVE_CPPFLAGS"
fi


dnl
dnl  Check whether FDI support wanted
dnl
if [[ "x$WANT_FDI" != "xno" ]]; then
  UAE_DEFINES="$UAE_DEFINES -DFDI2RAW"
fi


dnl
dnl  Build debugger?
dnl
AC_MSG_CHECKING([whether to build internal debugger/monitor])
if [[ "x$WANT_DEBUGGER" != "xno" ]]; then
  AC_MSG_RESULT(yes)
  UAE_DEFINES="$UAE_DEFINES -DDEBUGGER"
  DEBUGOBJS="debug.o identify.o"
else
  AC_MSG_RESULT(no)
fi


dnl
dnl  Support state saving/loading?
dnl
AC_MSG_CHECKING([whether to build support for saving/loading emulator state])
if [[ "x$WANT_STATESAVING" != "xno" ]]; then
  AC_MSG_RESULT(yes)
  UAE_DEFINES="$UAE_DEFINES -DSAVESTATE"
else
  AC_MSG_RESULT(no)
fi


dnl
dnl  Build fake enforcer?
dnl
AC_MSG_CHECKING([whether to build with enforcer])
if [[ "x$WANT_ENFORCER" != "xno" ]]; then
  if [[ "x$WANT_AUTOCONFIG" != "xno" ]]; then
    AC_MSG_RESULT(yes)
    UAE_DEFINES="$UAE_DEFINES -DENFORCER"
  else
    AC_MSG_RESULT(no)
    AC_MSG_WARN([Autoconfig support not enabled, so enforcer emulation cannot be enabled])
  fi
else
  AC_MSG_RESULT(no)
fi


dnl
dnl  Build Action Replay emulation
dnl
AC_MSG_CHECKING([whether to build Action Replay emulation])
if [[ "x$WANT_ACTION_REPLAY" != "xno" ]]; then
  AC_MSG_RESULT(yes)
  UAE_DEFINES="$UAE_DEFINES -DACTION_REPLAY"
else
  AC_MSG_RESULT(no)
fi


LIBS="$LIBS $UAE_LIBS $GUI_LIBS $GFX_LIBS $SND_LIBS $JOY_LIBS $MATHLIB"
CPPFLAGS="$CPPFLAGS $UAE_DEFINES $UAE_CPPFLAGS $GFX_CPPFLAGS"
CFLAGS="$CFLAGS $UAE_CFLAGS $GUI_CFLAGS $GFX_CFLAGS $SND_CFLAGS $JOY_CFLAGS"

AC_CONFIG_LINKS(src/target.h:src/targets/$TARGETDEP)
AC_CONFIG_LINKS(src/machdep:src/$MACHDEP)
AC_CONFIG_LINKS(src/md-fpp.h:src/include/$MDFPP_H)
AC_CONFIG_LINKS(src/osdep:src/$OSDEP)
AC_CONFIG_LINKS(src/gfxdep:src/$GFX_DEP)
AC_CONFIG_LINKS(src/sounddep:src/$SND_DEP)
AC_CONFIG_LINKS(src/threaddep:src/$THREADDEP)
AC_CONFIG_LINKS(src/joydep:src/$JOY_DEP)
AC_CONFIG_LINKS(src/guidep:src/$GUI_DEP)

AC_CONFIG_FILES([Makefile
		 src/Makefile
		 src/dms/Makefile
		 src/caps/Makefile
		 src/md-generic/Makefile
		 src/md-i386-gcc/Makefile
		 src/md-ppc-gcc/Makefile
		 src/md-ppc/Makefile
		 src/md-68k/Makefile
		 src/md-amd64-gcc/Makefile
		 src/od-generic/Makefile
		 src/od-linux/Makefile
		 src/od-amiga/Makefile
		 src/od-beos/Makefile
		 src/od-macosx/Makefile
		 src/od-macosx/Info.plist
		 src/od-win32/Makefile
		 src/td-none/Makefile
		 src/td-amigaos/Makefile
		 src/td-beos/Makefile
		 src/td-posix/Makefile
		 src/td-sdl/Makefile
		 src/td-win32/Makefile
		 src/gfx-amigaos/Makefile
		 src/gfx-beos/Makefile
		 src/gfx-x11/Makefile
		 src/gfx-sdl/Makefile
		 src/gfx-curses/Makefile
		 src/gfx-svga/Makefile
		 src/sd-alsa/Makefile
		 src/sd-amigaos/Makefile
		 src/sd-beos/Makefile
		 src/sd-none/Makefile
		 src/sd-sdl/Makefile
		 src/sd-solaris/Makefile
		 src/sd-uss/Makefile
		 src/jd-none/Makefile
		 src/jd-amigainput/Makefile
		 src/jd-amigaos/Makefile
		 src/jd-beos/Makefile
		 src/jd-linuxold/Makefile
		 src/jd-sdl/Makefile
		 src/gui-none/Makefile
		 src/gui-beos/Makefile
		 src/gui-cocoa/Makefile
		 src/gui-gtk/Makefile
		 src/gui-muirexx/Makefile
		 src/keymap/Makefile
		 src/test/Makefile
])

AM_CONDITIONAL(TARGET_BIGENDIAN, test x$ac_cv_c_bigendian = xyes)
AM_CONDITIONAL(TARGET_BEOS,  test x$OSDEP = xod-beos)
AM_CONDITIONAL(TARGET_WIN32, test x$OSDEP = xod-win32)
AM_CONDITIONAL(BUILD_MACOSX_BUNDLE, test x$OSDEP = xod-macosx)

AC_SUBST(ac_cv_c_inline)
AC_SUBST(WRCPRG)
AC_SUBST(NO_SCHED_CFLAGS)
AC_SUBST(GENCPUOPTS)
AC_SUBST(TARGET)
AC_SUBST(MACHDEP)
AC_SUBST(OSDEP)
AC_SUBST(THREADDEP)
AC_SUBST(GFX_DEP)
AC_SUBST(SND_DEP)
AC_SUBST(JOY_DEP)
AC_SUBST(GUI_DEP)
AC_SUBST(EXTRAOBJS)
AC_SUBST(CPUOBJS)
AC_SUBST(JITOBJS)
AC_SUBST(RESOBJS)
AC_SUBST(ASMOBJS)
AC_SUBST(SCSIOBJS)
AC_SUBST(CDOBJS)
AC_SUBST(DEBUGOBJS)
AC_SUBST(FILESYSOBJS)
AC_SUBST(BSDSOCKOBJS)
AC_SUBST(UAE_RSRCFILE)
AC_SUBST(SET_MAKE)
AC_SUBST(MATHLIB)
AC_SUBST(top_srcdir)

dnl  If we're not cross-compiling then, the config script in src/tools
dnl  can be called by config.status as normal
dnl
if [[ "x$cross_compiling" != "xyes" ]]; then
  AC_CONFIG_SUBDIRS(src/tools)
fi

AC_OUTPUT

dnl  However, if we are cross-compiling, then we need to call
dnl  configure in src/tools manually ourselves. This is because
dnl  autoconf currently has no support for building host tools
dnl  when cross-compiling. Okay. This is a hack. But it works. Can
dnl  anybody think of a better way of doing this?
dnl
if [[ "x$cross_compiling" = "xyes" ]]; then
  export CC=$CC_FOR_BUILD
  export CFLAGS=$CFLAGS_FOR_BUILD
  export LDFLAGS=$LDFLAGS_FOR_BUILD
  export LIBS=$LIBS_FOR_BUILD
  mkdir -p src/tools
  cd  src/tools && ../../"$srcdir"/src/tools/configure --cache-file=/dev/null --host=$build --target=$host
fi
