dnl Process this file with autoconf to produce a configure script.
dnl Do not use autoconf-2.8 or autoconf-2.9, these versions have a serious
dnl bug.
AC_INIT(amiga/transdisk.c)

dnl Checks for programs.
AC_PROG_CC

AC_PROG_CPP
AC_PROG_MAKE_SET

AC_AIX
AC_ISC_POSIX

dnl Checks for libraries.
HAVE_BEBOX=n
dnl Replace `main' with a function in -lMedia_s: (Ian!)
AC_CHECK_LIB(Media_s, main, HAVE_MEDIA_LIB=y, HAVE_MEDIA_LIB=n)
dnl Replace `main' with a function in -lNeXT_s:
AC_CHECK_LIB(NeXT_s, main, HAVE_NEXT_LIB=y, HAVE_NEXT_LIB=n)
AC_CHECK_LIB(amiga, OpenLibrary, HAVE_AMIGA_LIB=y, HAVE_AMIGA_LIB=n)
AC_CHECK_LIB(vga, vga_setmode, HAVE_SVGA_LIB=y, HAVE_SVGA_LIB=n)
AC_CHECK_LIB(AF, AFOpenAudioConn, HAVE_AF_LIB=y, HAVE_AF_LIB=n)

AC_PATH_XTRA
AC_CONFIG_HEADER(sysconfig.h)

AC_HEADER_DIRENT
AC_HEADER_STDC
AC_CHECK_HEADERS(unistd.h fcntl.h sys/time.h sys/types.h utime.h string.h strings.h values.h)
AC_CHECK_HEADERS(sys/vfs.h sys/mount.h sys/param.h sys/statfs.h sys/statvfs.h sys/stat.h sys/types.h linux/joystick.h)

if [[ $ac_cv_header_linux_joystick_h = "yes" ]]; then
  AC_MSG_CHECKING(whether linux/joystick.h is broken)
  if grep "#include" /usr/include/linux/joystick.h >/dev/null; then
    AC_MSG_RESULT(yes)
    BROKEN_JOYSTICK_H=1
    grep -v "#include" /usr/include/linux/joystick.h >include/joystick.h
  else
    AC_MSG_RESULT(no)
    BROKEN_JOYSTICK_H=0
  fi
fi

AC_CHECK_SIZEOF(char)
AC_CHECK_SIZEOF(short)
AC_CHECK_SIZEOF(int)
AC_CHECK_SIZEOF(long)
AC_CHECK_SIZEOF(long long)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_TYPE_MODE_T
AC_TYPE_OFF_T
AC_TYPE_PID_T
AC_STRUCT_ST_BLOCKS
AC_HEADER_TIME
AC_STRUCT_TM

dnl Checks for library functions.
AC_PROG_GCC_TRADITIONAL
AC_FUNC_MEMCMP
AC_TYPE_SIGNAL
AC_FUNC_UTIME_NULL
AC_CHECK_FUNCS(gettimeofday mkdir rmdir select strerror strstr statfs)

AC_MSG_CHECKING(how many args statfs takes)
if [[ $ac_cv_func_statfs = "yes" ]]; then
  AC_TRY_COMPILE([
#include "confdefs.h"
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif
#ifdef HAVE_SYS_VFS_H
#include <sys/vfs.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_STATFS_H
#include <sys/statfs.h>
#endif
#ifdef HAVE_SYS_STATVFS_H
#include <sys/statvfs.h>
#endif],[struct statfs statbuf;statfs("/",&statbuf);],
AC_MSG_RESULT(2) 
STATFS_NO_ARGS=2,
AC_MSG_RESULT(4)
STATFS_NO_ARGS=4)
fi

AC_MSG_CHECKING(whether to use f_bavail or f_bfree)
if [[ $ac_cv_func_statfs = "yes" ]]; then
  AC_TRY_COMPILE([
#include "confdefs.h"
#ifdef HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif
#ifdef HAVE_SYS_VFS_H
#include <sys/vfs.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_STATFS_H
#include <sys/statfs.h>
#endif
#ifdef HAVE_SYS_STATVFS_H
#include <sys/statvfs.h>
#endif],[struct statfs statbuf;statbuf.f_bavail;],
AC_MSG_RESULT(f_bavail) 
STATBUF_BAVAIL=f_bavail,
AC_MSG_RESULT(f_bfree)
STATBUF_BAVAIL=f_bfree)
fi

AC_MSG_CHECKING(which target to use)
if [[ $HAVE_BEBOX = "y" ]]; then
  AC_MSG_RESULT(BeBox)
  TARGET=be
  GFXOBJS="bebox.o nogui.o"
  ac_cv_c_inline=
else
  if [[ $HAVE_AMIGA_LIB = "y" ]]; then
    AC_MSG_RESULT(AmigaOS)
    TARGET=amiga
    GFXOBJS="awin.o"
  else
    if [[ $HAVE_NEXT_LIB = "y" ]]; then
      AC_MSG_RESULT(NeXTStep)
      TARGET=next
      GFXOBJS="NeXTwin.o"
      LIBRARIES="-sectcreate __ICON __header Uae.app/Uae.iconheader -segprot __ICON r r -sectcreate __ICON app Uae.app/Uae.tiff -lMedia_s -lNeXT_s"
    else
      if [[ x$no_x = "xyes" ]]; then
        if [[ $HAVE_SVGA_LIB = "n" ]]; then
          AC_MSG_RESULT(Ummm....)
          echo "Neither X nor SVGAlib found, don't know what target to use." 
 	  exit 1
        else
          AC_MSG_RESULT(SVGAlib)
          TARGET=svgalib
 	  GFXOBJS="svga.o nogui.o"
  	  LIBRARIES=-lvga
        fi
      else
        AC_MSG_RESULT(X Window System)
        TARGET=x11
        LIBRARIES="$X_LIBS $X_PRE_LIBS -lXext -lX11 $X_EXTRA_LIBS"
        TCLGUI=yes
        AC_ARG_ENABLE(gui, --disable-gui      Don't use the Tcl/Tk GUI,[TCLGUI=$enableval],[])
        if [[ $TCLGUI = "yes" ]]; then
          AC_CHECK_PROG(TCLGUI,wish4.0,yes,no)
        else
          echo "Disabling the GUI."
        fi
        if [[ $TCLGUI = "yes" ]]; then
          GFXOBJS="xwin.o xui.o"
        else
          GFXOBJS="xwin.o nogui.o"
        fi
      fi
    fi
  fi
fi

ASMOBJS=

dnl It may be possible to use X86.S on other systems, too, but then again, it
dnl might break. Check for a system that is known to work.
dnl Gustavo says it works on linuxaout, too. We'll try this later.

if [[ "$CC" = "gcc" ]]; then
  CFLAGS="-O3 -fomit-frame-pointer -Wall -Wno-unused -Wno-format -W -Wmissing-prototypes -Wstrict-prototypes"
  AC_MSG_CHECKING(whether we are on a Linux/i386 ELF system)
  LINUXELF=n
  if MACHINE=`uname -a 2>/dev/null`; then
    cat >conftest.c << EOF
int main() { return 0; }
EOF
    case $MACHINE in
    Linux*i*86)
      gcc conftest.c -o conftest
      file conftest >conftest.file
      if grep ELF conftest.file >/dev/null; then
        LINUXELF=y
      fi
      ;;
    esac
    rm -f conftest*
  fi
  if [[ $LINUXELF = "n" ]]; then
    AC_MSG_RESULT(no)
  else
    AC_MSG_RESULT(yes)
    ASMOBJS=X86.o
    CFLAGS="$CFLAGS -DX86_ASSEMBLY"
  fi
else
  echo "Couldn't find GCC. UAE may or may not compile and run correctly."
fi

if [[ $TARGET = "x11" -o $TARGET = "svgalib" ]]; then
  dnl On a Unix system, zfile is supposed to work. Dunno about others.
  CFLAGS="$CFLAGS -DUSE_ZFILE"
fi

if [[ $TARGET = "amiga" ]]; then
  dnl Determine the cpu-type
  CFLAGS="$CFLAGS -DUSE_ZFILE -m`cpu | cut -d' ' -f2`"
fi

AC_SUBST(ac_cv_c_inline)
AC_SUBST(STATFS_NO_ARGS)
AC_SUBST(BROKEN_JOYSTICK_H)
AC_SUBST(STATBUF_BAVAIL)
AC_SUBST(LIBRARIES)
AC_SUBST(TARGET)
AC_SUBST(GFXOBJS)
AC_SUBST(ASMOBJS)

AC_OUTPUT(Makefile)
