dnl Process this file with autoconf to produce a configure script.
AC_INIT(16bit.h)
AC_CONFIG_HEADER(conf.h)

AC_PROG_MWCC
MAPFLAGS="-Wl,-cref -Wl,-Map -Wl,@OUTFILE@.map"
ARFLAGS="rcsu"
DEPFLAGS="-MD"

dnl Checks for programs.
AC_PROG_CC
if test "$LD" = ""; then
LD=$CC
fi
AC_PROG_YACC
AC_PROG_LEX
AC_PROG_MAKE_SET
AC_MSG_CHECKING(perl)
if perl -e print; then PERL=perl;
elif /usr/bin/perl -e print; then PERL=/usr/bin/perl;
elif /usr/local/bin/perl -e print; then PERL=/usr/local/bin/perl;
else AC_MSG_ERROR(not found, you will need to hand-edit gtkcallbacks.c if you change v9t9.glade)
PERL=true
fi
if test "$PERL" != ""; then
AC_MSG_RESULT($PERL)
fi
AC_SUBST(PERL)

dnl get top-level directory
TOP=`pwd`
AC_SUBST(TOP)

dnl typical modules
MODSRCS="$MODSRCS video_null.c keyboard_null.c sound_null.c"
MODSRCS="$MODSRCS dsr_emudisk.c dsr_realdisk.c dsr_rs232.c"
AC_DEFINE(NULL_VIDEO)
AC_DEFINE(NULL_KEYBOARD)
AC_DEFINE(NULL_SOUND)
AC_DEFINE(EMU_DISK_DSR)
AC_DEFINE(REAL_DISK_DSR)
AC_DEFINE(REAL_RS232_DSR)

dnl Check for host OS -- this is a generic check only
AC_MSG_CHECKING(host operating system)
HOSTOS=

AC_TRY_COMPILE(,[#if !defined(__unix__) && !defined(__unix)
#error not unix
#endif],
AC_DEFINE(UNDER_UNIX)
HOSTOS="Unix"
SRCS="$SRCS unixmain.c"
INCDIRS="$INCDIRS $TOP/Modules/Unix"
OSLIBSRCS="$OSLIBSRCS Posix.c"
LIBS="$LIBS -lpthread"
UNIXMODSRCS="$UNIXMODSRCS dsr_rs232_unix.c"
)

if test "$HOSTOS" = ""; then
AC_TRY_COMPILE(,[#if !defined(_WIN32)
#error not _WIN32
#endif], 
AC_DEFINE(UNDER_WIN32)
AC_DEFINE(USE_MM_TIMER)
AC_DEFINE(WIN32_VIDEO) AC_DEFINE(WIN32_KEYBOARD) 
AC_DEFINE(WIN32_SOUND) AC_DEFINE(USE_MM_TIMER)
HOSTOS="Win32"
SRCS="$SRCS winmain.c"
INCDIRS="$INCDIRS $TOP/source/Modules/Win32"
OSLIBSRCS="$OSLIBSRCS Win32.c"
UNIXMODSRCS="$UNIXMODSRCS dsr_rs232_win32.c"
)
fi

if test "$HOSTOS" = ""; then
AC_TRY_COMPILE(,[#if !defined(macintosh)
#error not macintosh
#endif], 
AC_DEFINE(UNDER_MACOS)
HOSTOS="MacOs"
SRCS="$SRCS macosmain.c"
INCDIRS="$INCDIRS $TOP/source/Modules/MacOS"
OSLIBSRCS="$OSLIBSRCS MacOS.c"
)
AC_MSG_ERROR(nothing is ported to MacOS yet, this is just a stub)
fi

if test "$HOSTOS" = ""; then
AC_MSG_ERROR(could not determine host operating system)
fi

AC_SUBST(HOSTOS)
AC_MSG_RESULT($HOSTOS)

dnl checks for OS features
AC_CYGWIN
AC_OBJEXT
AC_SYS_LONG_FILE_NAMES

dnl Checks for libraries.
AC_CHECK_LIB(readline, readline,
	[LIBS="$LIBS -lreadline -ltermcap"; AC_DEFINE(HAVE_READLINE)],,-ltermcap)
AC_CHECK_LIB(png, png_create_write_struct,
	[LIBS="$LIBS -lpng -lz"; AC_DEFINE(WITH_LIB_PNG)],,-lz)

AC_CHECK_LIB(vga, vga_init, 
	AC_DEFINE(LINUX_SVGA_VIDEO) AC_DEFINE(LINUX_SVGA_KEYBOARD)
	LIBS="$LIBS -lvga"
	SRCS="$SRCS svgaloop.c"
	UNIXMODSRCS="$UNIXMODSRCS keyboard_svgalib.c video_svgalib.c")
AC_CHECK_HEADERS(linux/soundcard.h, 
	AC_DEFINE(OSS_SOUND)
	UNIXMODSRCS="$UNIXMODSRCS sound_oss.c")
AC_CHECK_HEADERS(sys/kd.h, 
	AC_DEFINE(LINUX_SPEAKER_SOUND)
	UNIXMODSRCS="$UNIXMODSRCS sound_speaker.c")
AC_CHECK_HEADERS(esd.h,
	AC_DEFINE(ESD_SOUND)
	UNIXMODSRCS="$UNIXMODSRCS sound_esd.c"
	LIBS="$LIBS -lesd")

AC_PATH_X
if test "$no_x" = ""; then
AC_DEFINE(X_WIN_VIDEO)
AC_DEFINE(X_WIN_KEYBOARD)
SRCS="$SRCS xlibloop.c"
UNIXMODSRCS="$UNIXMODSRCS video_X.c keyboard_X.c"
INCS="$INCS $x_includes"
LIBS="$LIBS -L$x_libraries -lX11 -lXext -lXi"
fi

if test '( "$HOSTOS" = "Unix" && "$no_x" = "" )' || test "$HOSTOS" = "Win32" ; then
AC_CHECK_LIB(gtk, gtk_init,
has_gtk=yes
GTKSRCS="gtkloop.c gtkinterface.c gtksupport.c gtkcallbacks.c gtk_v99filesel.c"
GTKMODSRCS="video_gtk.c keyboard_gtk.c"
GTKLIBS="-lgtk -lgdk -lglib -lgmodule -ldl"
,,-lgdk -lglib)

AC_ARG_WITH(gtk,
[  --with-gtk[=ARG]        use the GTK interface (ARG=yes)],
[if test "$withval" = "yes"; then
if test "$has_gtk" = "yes"; then
AC_DEFINE(GTK_KEYBOARD) AC_DEFINE(GTK_VIDEO)
SRCS="$SRCS $GTKSRCS" MODSRCS="$MODSRCS $GTKMODSRCS" LIBS="$LIBS $GTKLIBS"
else
AC_MSG_ERROR(GTK not found or not installed.)
fi
fi],
[
AC_DEFINE(GTK_KEYBOARD) AC_DEFINE(GTK_VIDEO)
SRCS="$SRCS $GTKSRCS" MODSRCS="$MODSRCS $GTKMODSRCS" LIBS="$LIBS $GTKLIBS"
])

fi  # hostos==unix, hostos==win32

dnl Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS(fcntl.h limits.h malloc.h sys/ioctl.h sys/time.h unistd.h)
dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_STRUCT_TM

dnl Checks for library functions.
AC_FUNC_ALLOCA
AC_FUNC_FNMATCH
AC_PROG_GCC_TRADITIONAL
AC_FUNC_MEMCMP
AC_TYPE_SIGNAL
AC_FUNC_UTIME_NULL
AC_FUNC_VPRINTF
AC_CHECK_FUNCS(getcwd gethostname mkdir rmdir select strcspn strdup strerror strstr strtol strcasecmp strncasecmp strupr swab)

AC_SUBST(SRCS)
AC_SUBST(INCDIRS)
AC_SUBST(MODSRCS)
AC_SUBST(UNIXMODSRCS)
AC_SUBST(WINMODSRCS)
AC_SUBST(MACMODSRCS)
AC_SUBST(OSLIBSRCS)

dnl m4_include(compiler.in) ??? i thought this existed

################

AC_SUBST(CC)
AC_SUBST(LD)
AC_SUBST(CFLAGS)
AC_SUBST(LDFLAGS)
AC_SUBST(LIBS)
AC_SUBST(DEFS)
AC_SUBST(DEPFLAGS)
AC_SUBST(MAPFLAGS)
AC_SUBST(ARFLAGS)
################

OUTFILE="$TOP/../bin/v9t9"
AC_SUBST(OUTFILE)

AC_OUTPUT(Generate/Makefile OSLib/Makefile Modules/Makefile 
	Modules/Unix/Makefile Modules/Win32/Makefile Makefile)
