dnl Process this file with autoconf to produce a configure script.

AC_SUBST(CONSOLE_DRIVER)
AC_SUBST(SOUND_DRIVER)
AC_SUBST(FILEIO_DRIVER)
AC_SUBST(MAIN_FILE)
AC_SUBST(CROSS_PREFIX)

AC_INIT(src/core/agi.c)
AC_CONFIG_AUX_DIR(./scripts)
AC_CANONICAL_HOST

if test "${target}" = NONE -o "x${target}" = "x"; then
	target=${host}
	target_cpu=${host_cpu}
	target_vendor=${host_vendor}
	target_os=${host_os}
else
	[ target_cpu=`echo $target | sed 's/^\([^-]*\)-[^-]*-.*$/\1/'` ]
	[ target_vendor=`echo $target | sed 's/^[^-]*-\([^-]*\)-.*$/\1/'` ]
	[ target_os=`echo $target | sed 's/^[^-]*-[^-]*-\(.*\)$/\1/'` ]
fi

echo "        target: ${target}"
echo "    target CPU: ${target_cpu}"
echo " target vendor: ${target_vendor}"
echo "     target OS: ${target_os}"

if test "${target_cpu}" != "${host_cpu}" -o \
	"${target_os}" != "${host_os}"; then
	if test "x$CC" = "x"; then
		CROSS_PREFIX="${target_cpu}-${target_os}-"
		CC="${CROSS_PREFIX}gcc"
	fi
fi


AC_PROG_CC
AC_PROG_CPP
AC_PROG_RANLIB
AC_PROG_INSTALL
AC_PROG_MAKE_SET

AC_ARG_WITH(sdl,    [  --with-sdl              use the Simple DirectMedia Layer])
AC_ARG_WITH(svgalib,[  --with-svgalib          use SVGAlib for graphics])
AC_ARG_WITH(alsa,   [  --with-alsa             use ALSA for sound output])
AC_ARG_WITH(win32,  [  --with-win32            use Win32 console/sound drivers])
AC_ARG_WITH(amiga,  [  --with-amiga            use Amiga console/sound drivers])

if test "${GCC-no}" = yes; then
	CFLAGS="${CFLAGS} -Wall"
fi

MAIN_FILE="main"

case "${target_os}" in
beos*)
	CFLAGS="$CFLAGS -I/boot/develop/tools/gnupro/include"
	CPPFLAGS="$CPPFLAGS -I/boot/develop/tools/gnupro/include"
	with_sdl="yes"
	;;
nto*)
	AC_CHECK_HEADERS(Pt.h Ph.h)
	;;
cygwin*|mingw*)
	AC_CHECK_HEADERS(wingdi.h)
	MAIN_FILE="winmain"
	with_win32="yes"
	;;
palmos*)
	CFLAGS="$CFLAGS -DPALMOS"
	MAIN_FILE="palmos"
	;;
amiga*)
	CFLAGS="$CFLAGS -DAMIGA -D__AMIGA__ -Uamiga"
	;;
esac


if test "${with_win32}" = "yes"; then
	CFLAGS="${CFLAGS} -DELIDE_CODE"
fi

dnl ----------------------------------------------
dnl Checks for header files.
dnl ----------------------------------------------

AC_HEADER_STDC
AC_CHECK_HEADERS(string.h getopt.h dirent.h time.h)


dnl ----------------------------------------------
dnl Check graphics support.
dnl ----------------------------------------------

AC_CHECKING([for graphics support])
CONSOLE_DRIVER="none"

if test "${ac_cv_header_Ph_h}" = "yes"; then
	CONSOLE_DRIVER="photon"
	LIBS="${LIBS} -lph -lphrender"
elif test "${with_win32}" = "yes"; then
	CONSOLE_DRIVER="win32"
	AC_CHECK_LIB(cygwin, usleep)
	LIBS="-lgdi32 ${LIBS} -lcomdlg32"
elif test "${with_sdl}" = "yes"; then
	AC_CHECK_HEADERS(SDL SDL/SDL.h)
	if test "${ac_cv_header_SDL_h}" = "yes"; then
		CONSOLE_DRIVER="sdl"
		CFLAGS="`sdl-config --cflags` ${CFLAGS}"
		if test "${ac_cv_prog_cc_cross}" = "yes"; then
			LIBS="-lSDL ${LIBS}"
		else
			LIBS="`sdl-config --libs` ${LIBS}"
		fi
	fi
	if test "${ac_cv_header_SDL_SDL_h}" = "yes"; then
		CONSOLE_DRIVER="sdl"
		CFLAGS="`sdl-config --cflags` ${CFLAGS} -ISDL"
		if test "${ac_cv_prog_cc_cross}" = "yes"; then
			LIBS="-lSDL ${LIBS}"
		else
			LIBS="`sdl-config --libs` ${LIBS}"
		fi
	fi
	with_x="no"
elif test "${with_svgalib}" = "yes"; then
	AC_CHECK_HEADERS(vga.h)
	if test "${ac_cv_header_vga_h}" = "yes"; then
		CONSOLE_DRIVER="svgalib"
		LIBS="-lvga ${LIBS}"
	fi
elif test "${with_amiga}" = "yes"; then
	CONSOLE_DRIVER="amiga"
	AC_CHECK_LIB(amiga, usleep)
	LIBS="-m68020 -noixemul -ldebug -lm ${LIBS}"
else
	AC_PATH_X
	if test ! -z "${ac_cv_have_x}"; then
		CONSOLE_DRIVER="x11"
		AC_PATH_XTRA
		AC_CHECK_HEADERS(X11/extensions/XShm.h)
dnl		AC_CHECK_HEADERS(X11/extensions/xf86dga.h \
dnl			X11/extensions/xf86vmode.h)

		if test "${ac_cv_header_X11_extensions_XShm_h}" = "yes"; then
			CFLAGS="${CFLAGS} -DMITSHM"
			X_LIBS="${X_LIBS} -lXext -lX11"
		else
			X_LIBS="${X_LIBS} -lX11"
		fi

dnl		if test "${ac_cv_header_X11_extensions_xf86dga_h}" = "yes" -a \
dnl			"${ac_cv_header_X11_extensions_xf86vmode_h}" = "yes";
dnl		then
dnl			AC_CHECK_LIB(Xxf86dga,XF86DGAGetVideo,
dnl			AC_CHECK_LIB(Xxf86vm,XF86VidModeSwitchToMode,
dnl			CFLAGS="${CFLAGS} -DXF86DGA"))
dnl		fi
	fi
fi


dnl ----------------------------------------------
dnl Check sound support
dnl ----------------------------------------------

AC_CHECKING([for sound support])
SOUND_DRIVER="dummy"
if test "${with_sdl}" = "yes"; then
	SOUND_DRIVER="sdl"
else case ${target_os} in
linux*)
	case ${target_cpu} in
	sparc)
		SOUND_DRIVER="bsd"
		;;
	i*86|powerpc|alpha*)
		AC_CHECK_HEADERS(sys/soundcard.h)
		AC_CHECK_HEADERS(sys/asoundlib.h)
		if test "${ac_cv_header_sys_soundcard_h}" = "yes" -a \
		    "${with_alsa}" != "yes"; then
			SOUND_DRIVER="oss"
		elif test "${ac_cv_header_sys_asoundlib_h}" = "yes"; then
			SOUND_DRIVER="alsa"
			AC_CHECK_LIB(asound, snd_pcm_open)
		fi
		;;
	esac
	;;
freebsd*)
	SOUND_DRIVER="freebsd"
	LIBS="${LIBS} -pthread"
	;;
nto*)
	AC_CHECK_HEADERS(sys/asoundlib.h)
	SOUND_DRIVER="qnx"
	;;
hpux*)
	AC_CHECK_HEADERS(sys/audio.h)
	if test "${ac_cv_header_sys_audio_h}" = "yes"; then
		SOUND_DRIVER="hpux"
	fi
	;;
solaris*)
	AC_CHECK_HEADERS(sys/audioio.h)
	if test "${ac_cv_header_sys_audioio_h}" = "yes"; then
		SOUND_DRIVER="solaris"
	fi
	;;
netbsd*|openbsd*)
	AC_CHECK_HEADERS(sys/audioio.h sys/audio.h)
	if test "${ac_cv_header_sys_audioio_h}" = "yes"; then
		SOUND_DRIVER="bsd"
	fi
	if test "${ac_cv_header_sys_audio_h}" = "yes"; then
		SOUND_DRIVER="bsd"
	fi
	;;
irix*)
	AC_CHECK_HEADERS(dmedia/audio.h)
	if test "${ac_cv_header_dmedia_audio_h}" = "yes"; then
		SOUND_DRIVER="sgi"
		LIBS="${LIBS} -laudio"
	fi
	;;
cygwin*)
	LDFLAGS="${LDFLAGS} -Wl,--subsystem,windows,-e,_mainCRTStartup"
	LIBS="-lwinmm ${LIBS}"
	SOUND_DRIVER="win32"
	;;
amiga*)
	SOUND_DRIVER="amiga"
	;;
*)
	SOUND_DRIVER="dummy"
	;;
esac
fi

case "${SOUND_DRIVER}" in
dummy|win32|sdl|freebsd|qnx|amiga)
	;;
*)
	AC_CHECK_LIB(pthread, pthread_create,[
		CFLAGS="${CFLAGS} -D_REENTRANT"
		LIBS="-lpthread ${LIBS}"
	], [
		SOUND_DRIVER="dummy"
	])
	;;
esac

case "${target_os}" in
nto*)
	if test "${ac_cv_header_sys_asoundlib_h}" = "yes"; then
		SOUND_DRIVER="alsa"
		AC_CHECK_LIB(asound, snd_pcm_open)
	fi
	;;
esac

dnl ----------------------------------------------
dnl Check for file globbing support
dnl ----------------------------------------------

AC_CHECKING([for file globbing support])
FILEIO_DRIVER="unix"
AC_CHECK_FUNC(_dos_findfirst,[FILEIO_DRIVER="msdos"])
AC_CHECK_FUNC(_findfirst,[FILEIO_DRIVER="win32"])
dnl AC_CHECK_FUNC(glob,[FILEIO_DRIVER="unix"])


dnl ----------------------------------------------
dnl Checks for typedefs, structures, and compiler
dnl characteristics.
dnl ----------------------------------------------

AC_C_CONST
AC_C_INLINE
AC_HEADER_TIME

if test "${ac_cv_c_inline}" != "no"; then
	CFLAGS="${CFLAGS} -DINLINE=${ac_cv_c_inline}";		\
fi


dnl ----------------------------------------------
dnl Checks for library functions.
dnl ----------------------------------------------

AC_PROG_GCC_TRADITIONAL
AC_FUNC_MEMCMP
dnl AC_TYPE_SIGNAL
AC_FUNC_VPRINTF
AC_CHECK_FUNCS(vsnprintf gettimeofday getopt_long)

AC_MSG_RESULT([----------------------------------------------------------])
AC_MSG_RESULT([using console driver: ${CONSOLE_DRIVER}])
AC_MSG_RESULT([using sound driver: ${SOUND_DRIVER}])
AC_MSG_RESULT([using file globbing method: ${FILEIO_DRIVER}])
AC_MSG_RESULT([cflags: ${CFLAGS}])
AC_MSG_RESULT([libs: ${LIBS}])
AC_MSG_RESULT([----------------------------------------------------------])

AC_OUTPUT(Rules)

