#!/bin/sh

# This is not a GNU Autoconf configure script.  It is manually maintained.

SRCROOT=`dirname $0`

test -z "$prefix" && prefix=/usr/local

target_pkg_config=auto
build_pkg_config=auto
target_sdl_config=auto
build_sdl_config=auto
target_gtk_config=auto
build_gtk_config=auto

_c_nested_funcs=auto
_strsep=auto
_sdl=auto
_sdlgl=auto
_build_sdl=auto
_gtk2=auto
_gtk1=auto
_cli=yes
_carbon=auto
_oss=auto
_sun=auto
_coreaudio=auto
_jack=auto
_nullaudio=yes
_sndfile=auto
_mingw=auto
_gp32=auto
_nds=auto
_mpu_rate=auto
_trace=no
_fast_vdg=auto
_fast_sound=auto

_have_video_driver=

case `echo -n` in
-n) _echo_n=""; _echo_c='\c'; ;;	# SysV echo
*) _echo_n="-n"; _echo_c=""; ;;		# BSD echo
esac

helptext () {
	cat <<EOF
Usage: $0 [OPTION]...

Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [$prefix]
  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                          [PREFIX]
  --bindir=DIR            user executables [EPREFIX/bin]
  --datadir=DIR           read-only architecture-independent data [PREFIX/share]
  --sysconfdir=DIR        read-only single-machine data [PREFIX/etc]
  --mandir=DIR            man documentation [PREFIX/man]

System types:
  --build=BUILD     configure for building on BUILD [guessed]
  --target=TARGET   cross-compile to build programs to run on TARGET [BUILD]

Optional Features:
  --disable-sdl         don't test for SDL
  --disable-sdlgl       don't test for SDL OpenGL support
  --disable-gtk2        don't test for GTK+-2
  --disable-gtk1        don't test for GTK+-1
  --disable-carbon      don't test for Mac OS X Carbon
  --disable-oss         don't test for OSS audio
  --disable-sun         don't test for Sun audio
  --disable-coreaudio   don't test for Mac OS X Core Audio
  --disable-jack        don't test for JACK audio
  --disable-nullaudio   don't include null audio driver
  --disable-mingw       don't test for MinGW
  --disable-gp32        don't test for GP32
  --disable-nds         don't test for Nintendo DS
  --enable-trace        enable trace mode
  --disable-mpu-rate    disable setting of emulated MPU rate
  --enable-fast-vdg     enable faster but less accurate VDG code
  --enable-fast-sound   enable faster but less accurate sound code

  --with-sdl-config=PATH   set path to sdl-config script
  --with-pkg-config=PATH   set path to pkg-config script
  --with-gtk-config=PATH   set path to gtk-config script

  -h, --help            display this help and exit
EOF
}

exec 3>config.log
config_log=3

EXEEXT=
TARGET_CRT0=
BUILD_CRT0=
CRT0=
TMPC=conftemp.c
TMPO=conftemp.o
TMPX=conftemp

for opt do
	case "$opt" in
	CFLAGS=*) TARGET_CFLAGS=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	LDFLAGS=*) TARGET_LDFLAGS=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	BUILD_CFLAGS=*) BUILD_CFLAGS=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	BUILD_LDFLAGS=*) BUILD_LDFLAGS=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--prefix=*) prefix=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--exec-prefix=*) exec_prefix=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--bindir=*) bindir=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--sbindir=*) sbindir=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--libdir=*) libdir=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--datadir=*) datadir=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--sysconfdir=*) sysconfdir=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--includedir=*) includedir=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--mandir=*) mandir=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--build=*) build=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--host=*) target=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--target=*) target=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--enable-[a-z-]*) eval _`echo "$opt"|sed 's/^--enable-//;s/-/_/g'`=yes; ;;
	--disable-[a-z-]*) eval _`echo "$opt"|sed 's/^--disable-//;s/-/_/g'`=no; ;;
	--with-sdl-config=*) target_sdl_config=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--with-sdl=*)
		_tmp=`echo "$opt"|sed 's/^[^=]*=//'`
		sdl_includes="$_tmp/include"
		sdl_libs="$_tmp/lib"
		;;
	--with-gtk-config=*) gtk_config=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--with-gtk2=*)
		_tmp=`echo "$opt"|sed 's/^[^=]*=//'`
		gtk2_includes="$_tmp/include"
		gtk2_libs="$_tmp/lib"
		;;
	--with-gtk1=*)
		_tmp=`echo "$opt"|sed 's/^[^=]*=//'`
		gtk1_includes="$_tmp/include"
		gtk1_libs="$_tmp/lib"
		;;
	--with-pkg-config=*) target_pkg_config=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	-h|--help)
		helptext
		exit 0
		;;
	*)
	esac
done

test -z "$exec_prefix" && exec_prefix=$prefix
test -z "$bindir" && bindir=$exec_prefix/bin
test -z "$sbindir" && sbindir=$exec_prefix/sbin
test -z "$libexecdir" && libexecdir=$exec_prefix/libexec
test -z "$datadir" && datadir=$prefix/share
test -z "$sysconfdir" && sysconfdir=$prefix/etc
test -z "$localstatedir" && localstatedir=$exec_prefix/var
test -z "$libdir" && libdir=$exec_prefix/lib
test -z "$includedir" && includedir=$prefix/include
test -z "$mandir" && mandir=$prefix/man

test -z "$BUILD_CC" && BUILD_CC=gcc
if test -z "$TARGET_CC"; then
	if ( $target-gcc -v ) > /dev/null 2>&1; then
		TARGET_CC=$target-gcc
	else
		TARGET_CC=$BUILD_CC
	fi
fi

test -z "$BUILD_AS" && BUILD_AS=as
if test -z "$TARGET_AS"; then
	if ( $target-as --version ) > /dev/null 2>&1; then
		TARGET_AS=$target-as
	else
		TARGET_AS=$BUILD_AS
	fi
fi

test -z "$BUILD_OBJCOPY" && BUILD_OBJCOPY=objcopy
if test -z "$TARGET_OBJCOPY"; then
	if ( $target-objcopy --version ) > /dev/null 2>&1; then
		TARGET_OBJCOPY=$target-objcopy
	else
		TARGET_OBJCOPY=$BUILD_OBJCOPY
	fi
fi

########

cc_check () {
	ret=1
	echo "$CC $CFLAGS $1 -c -o $TMPO $TMPC" >&$config_log
	if $CC $CFLAGS $1 -c -o $TMPO $TMPC >&$config_log 2>&1; then
		if test -n "$cc_build_only"; then
			rm -f $TMPO
			return 0
		fi
		echo "$CC $CFLAGS $1 $CRT0 $TMPO -o $TMPX $LDFLAGS $2" >&$config_log
		if $CC $CFLAGS $1 $CRT0 $TMPO -o $TMPX $LDFLAGS $2 >&$config_log 2>&1; then
			echo "# Success" >&$config_log
			ret=0
		else
			echo "# Link failed: exit code = $?" >&$config_log
		fi
	else
		echo "# Compile failed: exit code = $?" >&$config_log
	fi
	rm -f $TMPO $TMPX
	return $ret
}

target_cc_check () {
	CC="$TARGET_CC"
	CFLAGS="$TARGET_CFLAGS"
	LDFLAGS="$TARGET_LDFLAGS"
	CRT0="$TARGET_CRT0"
	cc_check "$@"
	return $?
}

target_cc_check_compile () {
	cc_build_only=1
	target_cc_check "$@"
	ret=$?
	unset cc_build_only
	return $ret
}

build_cc_check () {
	CC="$BUILD_CC"
	CFLAGS="$BUILD_CFLAGS"
	LDFLAGS="$BUILD_LDFLAGS"
	CRT0="$BUILD_CRT0"
	cc_check "$@"
	return $?
}

echocheck () {
	echo "### Checking for $@" >&$config_log
	echo $_echo_n "Checking for $@ ... $_echo_c"
}

########

__build_cc_version=`$BUILD_CC -dumpmachine`
echo "Build system type is $__build_cc_version" >&$config_log
echo "Build system type is $__build_cc_version"
__target_cc_version=`$TARGET_CC -dumpmachine`
echo "Target system type is $__target_cc_version" >&$config_log
echo "Target system type is $__target_cc_version"

# Try and determine best optimisation if user has not specified any flags.

test -z "$TARGET_CFLAGS" && TARGET_CFLAGS="$CFLAGS"
if test -z "$TARGET_CFLAGS"; then
	echocheck optimisation options
	__opt="-O3"
	cat > $TMPC <<EOF
int main(int argc, char **argv) { return 0; }
EOF
	case "$__target_cc_version" in
	powerpc-apple-*)
		__tmp="-fast -mcpu=7450 -mdynamic-no-pic"
		target_cc_check "$__tmp" "" && __opt="$__tmp"
		;;
	*)
		;;
	esac
	echo "$__opt"
	TARGET_CFLAGS="$__opt"
fi

test -z "$TARGET_LDFLAGS" && TARGET_LDFLAGS="$LDFLAGS"

########

if test "$target_pkg_config" = "auto"; then
	if ( $target-pkg-config --version ) > /dev/null 2>&1; then
		target_pkg_config="$target-pkg-config"
	elif ( pkg-config --version ) > /dev/null 2>&1; then
		target_pkg_config="pkg-config"
	else
		target_pkg_config="false"
	fi
fi
if test "$build_pkg_config" = "auto"; then
	if ( pkg-config --version ) > /dev/null 2>&1; then
		build_pkg_config="pkg-config"
	else
		build_pkg_config="false"
	fi
fi

########

echocheck $TARGET_CC accepting -pipe
_c_pipe=no
cat > $TMPC <<EOF
int main(int argc, char **argv) { return 0; }
EOF
target_cc_check_compile "-pipe" "" && _c_pipe=yes
echo "$_c_pipe"
if test "$_c_pipe" = "yes"; then
	_TARGET_CFLAGS="$_TARGET_CFLAGS -pipe"
fi
rm -f $TMPC

########

# Rather involved tests to see if we can build against the GP32 SDK.
# Omits testing for the gpsound library, as I don't use that.
if test "$_gp32" = "auto"; then
	echocheck GP32
	_gp32=no
	TARGET_CRT0=gp32/crt0.o
	mkdir -p gp32
	echo "$TARGET_AS -o $TARGET_CRT0 $SRCROOT/gp32/crt0.s" >&$config_log
	if $TARGET_AS -o $TARGET_CRT0 $SRCROOT/gp32/crt0.s >&$config_log 2>&1; then
		cat > $TMPC <<EOF
#include <gpdef.h>
#include <gpstdlib.h>
#include <initval_port.h>
#ifdef USE_GP_MEM
#include <gpmem.h>
#endif
#include <gpgraphic.h>
unsigned int HEAPSTART;
unsigned int HEAPEND;
int Main(int argc, char *argv) { return 0; }
int GpPredefinedStackGet(H_THREAD th) { return 0; }
void GpMain(void *arg) { GPDRAWSURFACE g; GpGraphicModeSet(GPC_DFLAG_8BPP, NULL); GpLcdSurfaceGet(&g, 0); }
void GpProcSound(void) { }
EOF
		target_cc_check "-mcpu=arm9tdmi -funroll-loops" "-nostartfiles -T $SRCROOT/gp32/lnkscript -lgpmem -lgpstdlib -lgpos -lgpstdio -lgpgraphic" && _gp32=yes
		rm -f $TMPC
		rm -f $TARGET_CRT0
	fi
	echo "$_gp32"
fi
if test "$_gp32" = "yes"; then
	EXEEXT=.elf
	_TARGET_CFLAGS="$_TARGET_CFLAGS -mcpu=arm9tdmi -funroll-loops"
	_TARGET_LDFLAGS="$_TARGET_LDFLAGS -nostartfiles -T $SRCROOT/gp32/lnkscript -lgpmem -lgpstdlib -lgpos -lgpstdio -lgpgraphic"
	_gp32_def="#define HAVE_GP32"
	_have_video_driver=yes
	_fast_vdg=yes
	_strsep=yes
	_sdl=no
	_sdlgl=no
	_gtk2=no
	_gtk1=no
	_cli=no
	_carbon=no
	_oss=no
	_sun=no
	_coreaudio=no
	_jack=no
	_nullaudio=no
	_mingw=no
	_nds=no
	test "$_mpu_rate" = "auto" && _mpu_rate=no
	test "$_fast_sound" = "auto" && _fast_sound=yes
else
	_gp32_def="#undef HAVE_GP32"
	TARGET_CRT0=
fi

########

if test "$_nds" = "auto"; then
	echocheck NDS
	nds_cflags="-march=armv5te -mtune=arm946e-s -fomit-frame-pointer -ffast-math -mthumb -mthumb-interwork -DARM9"
	nds_ldflags="-specs=ds_arm9.specs -lfat -lnds9"
	cat > $TMPC <<EOF
#include <nds.h>
int main(int argc, char **argv) { powerON(POWER_LCD | POWER_2D_B); return 0; }
EOF
	_nds=no
	target_cc_check "$nds_cflags" "$nds_ldflags" && _nds=yes
	rm -f $TMPC
	echo "$_nds"
fi
if test "$_nds" = "yes"; then
	EXEEXT=.tmp
	_TARGET_CFLAGS="$_TARGET_CFLAGS $nds_cflags"
	_TARGET_LDFLAGS="$_TARGET_LDFLAGS $nds_ldflags"
	_nds_def="#define HAVE_NDS"
	_have_video_driver=yes
	_fast_vdg=yes
	_strsep=yes
	_sdl=no
	_sdlgl=no
	_gtk2=no
	_gtk1=no
	_cli=no
	_carbon=no
	_oss=no
	_sun=no
	_coreaudio=no
	_jack=no
	_nullaudio=no
	_mingw=no
	test "$_mpu_rate" = "auto" && _mpu_rate=no
	test "$_fast_sound" = "auto" && _fast_sound=yes
else
	_nds_def="#undef HAVE_NDS"
fi

########

if test "$_strsep" = "auto"; then
	echocheck "strsep()"
	cat > $TMPC <<EOF
#include <string.h>
int main(int argc, char **argv) { (void)strsep(0,0); }
EOF
	_strsep=no
	target_cc_check "" "" && _strsep=yes
	rm -f $TMPC
	echo "$_strsep"
fi
if test "$_strsep" = "yes"; then
	_strsep_def="#undef NEED_STRSEP"
else
	_strsep_def="#define NEED_STRSEP"
fi

########

if test "$_c_nested_funcs" = "yes" -o "$_c_nested_funcs" = "auto"; then
	echocheck $TARGET_CC allowing nested functions
	cat > $TMPC <<EOF
int main(int argc, char **argv) { int a; int next(void) { return a; } return 0; }
EOF
	_c_nested_funcs=no
	target_cc_check_compile "" "" && _c_nested_funcs=yes
	echo "$_c_nested_funcs"
fi
if test "$_c_nested_funcs" = "no"; then
	echocheck $TARGET_CC accepting -fnested-functions
	target_cc_check "-fnested-functions" "" && _c_nested_funcs=yes
	echo "$_c_nested_funcs"
	if test "$_c_nested_funcs" = "yes"; then
		_TARGET_CFLAGS="$_TARGET_CFLAGS -fnested-functions"
	fi
fi
rm -f $TMPC
if test "$_c_nested_funcs" = "yes"; then
	_c_nested_funcs_def="#define HAVE_C_NESTED_FUNCTIONS"
else
	_c_nested_funcs_def="#undef HAVE_C_NESTED_FUNCTIONS"
fi

########

if test "$target_sdl_config" = "auto"; then
	if ( $target-sdl-config --version ) > /dev/null 2>&1; then
		target_sdl_config="$target-sdl-config"
	elif ( sdl-config --version ) > /dev/null 2>&1; then
		target_sdl_config="sdl-config"
	else
		target_sdl_config="false"
	fi
fi
if test "$build_sdl_config" = "auto"; then
	if ( sdl-config --version ) > /dev/null 2>&1; then
		build_sdl_config="sdl-config"
	else
		build_sdl_config="false"
	fi
fi

if test "$_sdl" = "yes" -o "$_sdl" = "auto"; then
	echocheck SDL
	test -n "$sdl_includes" && sdl_cflags="-I$sdl_includes"
	test -z "$sdl_cflags" && sdl_cflags=`$target_sdl_config --cflags 2> /dev/null`
	test -z "$sdl_cflags" && sdl_cflags=`$target_pkg_config --cflags sdl 2> /dev/null`
	test -n "$sdl_libs" && sdl_ldflags="-L$sdl_libs -lSDL"
	test -z "$sdl_ldflags" && sdl_ldflags=`$target_sdl_config --libs 2> /dev/null`
	test -z "$sdl_ldflags" && sdl_ldflags=`$target_pkg_config --libs sdl 2> /dev/null`
	cat > $TMPC <<EOF
#include <SDL.h>
int main(int argc, char **argv) { SDL_Init(0); return 0; }
EOF
	_sdl=no
	target_cc_check "$sdl_cflags" "$sdl_ldflags" && _sdl=yes
	# Save this temp .c file for build host test
	rm -f $TMPC
	echo "$_sdl"
fi
if test "$_sdl" = "yes"; then
	_sdl_def="#define HAVE_SDL"
	_TARGET_CFLAGS="$_TARGET_CFLAGS $sdl_cflags"
	_TARGET_LDFLAGS="$_TARGET_LDFLAGS $sdl_ldflags"
	_have_video_driver=yes
else
	_sdl_def="#undef HAVE_SDL"
fi

if test "$_build_sdl" = "yes" -o "$_build_sdl" = "auto"; then
	echocheck SDL on build host
	test -z "$build_sdl_cflags" && build_sdl_cflags=`$build_sdl_config --cflags 2> /dev/null`
	test -z "$build_sdl_cflags" && build_sdl_cflags=`$build_pkg_config --cflags sdl 2> /dev/null`
	test -z "$build_sdl_ldflags" && build_sdl_ldflags=`$build_sdl_config --libs 2> /dev/null`
	test -z "$build_sdl_ldflags" && build_sdl_ldflags=`$build_pkg_config --libs sdl 2> /dev/null`
	cat > $TMPC <<EOF
#include <SDL.h>
int main(int argc, char **argv) { SDL_Init(0); return 0; }
EOF
	_build_sdl=no
	build_cc_check "$build_sdl_cflags" "$build_sdl_ldflags" && _build_sdl=yes
	rm -f $TMPC
	echo "$_build_sdl"
fi

########

if test "$_sdlgl" = "auto" -a "$_sdl" = "yes"; then
	echocheck SDL OpenGL support
	cat > $TMPC <<EOF
#include <SDL.h>
#include <SDL_opengl.h>
int main(int argc, char **argv) { GLuint t; SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); glGenTextures(1, &t); return 0; }
EOF
	_sdlgl=no
	sdlgl_ldflags="-lGL"
	target_cc_check "$sdl_cflags" "$sdl_ldflags $sdlgl_ldflags" && _sdlgl=yes
	if test "$_sdlgl" = "no"; then
		sdlgl_ldflags="-framework OpenGL"
		target_cc_check "$sdl_cflags" "$sdl_ldflags $sdlgl_ldflags" && _sdlgl=yes
	fi
	if test "$_sdlgl" = "no"; then
		sdlgl_ldflags="-lopengl32"
		target_cc_check "$sdl_cflags" "$sdl_ldflags $sdlgl_ldflags" && _sdlgl=yes
	fi
	echo "$_sdlgl"
fi
if test "$_sdlgl" = "yes"; then
	_sdlgl_def="#define HAVE_SDLGL"
	_TARGET_LDFLAGS="$_TARGET_LDFLAGS $sdlgl_ldflags"
else
	_sdlgl_def="#undef HAVE_SDLGL"
fi

########

if test "$_gtk2" = "yes" -o "$_gtk2" = "auto"; then
	echocheck GTK+-2
	test -z "$gtk2_cflags" && gtk2_cflags=`$target_pkg_config --cflags gtk+-2.0 2> /dev/null`
	test -z "$gtk2_ldflags" && gtk2_ldflags=`$target_pkg_config --libs gtk+-2.0 2> /dev/null`
	cat > $TMPC <<EOF
#include <gtk/gtk.h>
int main(int argc, char **argv) { gtk_init(&argc, &argv); return 0; }
EOF
	_gtk2=no
	target_cc_check "$gtk2_cflags" "$gtk2_ldflags" && _gtk2=yes
	rm -f $TMPC
	echo "$_gtk2"
fi
if test "$_gtk2" = "yes"; then
	_gtk2_def="#define HAVE_GTK2"
	_TARGET_CFLAGS="$_TARGET_CFLAGS $gtk2_cflags"
	_TARGET_LDFLAGS="$_TARGET_LDFLAGS $gtk2_ldflags"
	test "$_gtk1" = "auto" && _gtk1=no
else
	_gtk2_def="#undef HAVE_GTK2"
fi

########

if test "$target_gtk_config" = "auto"; then
	if ( $target-gtk-config --version ) > /dev/null 2>&1; then
		target_gtk_config="$target-gtk-config"
	elif ( gtk-config --version ) > /dev/null 2>&1; then
		target_gtk_config="gtk-config"
	else
		target_gtk_config="false"
	fi
fi
if test "$build_gtk_config" = "auto"; then
	if ( gtk-config --version ) > /dev/null 2>&1; then
		build_gtk_config="gtk-config"
	else
		build_gtk_config="false"
	fi
fi

if test "$_gtk1" = "yes" -o "$_gtk1" = "auto"; then
	echocheck GTK+-1
	test -z "$gtk1_cflags" && gtk1_cflags=`$target_gtk_config --cflags 2> /dev/null`
	test -z "$gtk1_cflags" && gtk1_cflags=`$target_pkg_config --cflags gtk+ 2> /dev/null`
	test -z "$gtk1_ldflags" && gtk1_ldflags=`$target_gtk_config --libs 2> /dev/null`
	test -z "$gtk1_ldflags" && gtk1_ldflags=`$target_pkg_config --libs gtk+ 2> /dev/null`
	cat > $TMPC <<EOF
#include <gtk/gtk.h>
int main(int argc, char **argv) { gtk_init(&argc, &argv); return 0; }
EOF
	_gtk1=no
	target_cc_check "$gtk1_cflags" "$gtk1_ldflags" && _gtk1=yes
	rm -f $TMPC
	echo "$_gtk1"
fi
if test "$_gtk1" = "yes"; then
	_gtk1_def="#define HAVE_GTK1"
	_TARGET_CFLAGS="$_TARGET_CFLAGS $gtk1_cflags"
	_TARGET_LDFLAGS="$_TARGET_LDFLAGS $gtk1_ldflags"
else
	_gtk1_def="#undef HAVE_GTK1"
fi

########

if test "$_carbon" = "auto"; then
	echocheck Mac OS X Carbon
	cat > $TMPC <<EOF
#include <Carbon/Carbon.h>
int main(int argc, char **argv) { NavDialogRef n; NavDialogRun(n); return 0; }
EOF
	_carbon=no
	target_cc_check "" "-framework Carbon" && _carbon=yes
	rm -f $TMPC
	echo "$_carbon"
fi
if test "$_carbon" = "yes"; then
	_carbon_def="#define HAVE_CARBON"
	_TARGET_LDFLAGS="$_TARGET_LDFLAGS -framework Carbon"
else
	_carbon_def="#undef HAVE_CARBON"
fi

########

if test "$_oss" = "auto"; then
	echocheck OSS
	cat > $TMPC <<EOF
#include <sys/soundcard.h>
int main(int argc, char **argv) { int t = AFMT_S8; t = SNDCTL_DSP_GETFMTS; return 0; }
EOF
	_oss=no
	target_cc_check "" "" && _oss=yes
	rm -f $TMPC
	echo "$_oss"
fi
if test "$_oss" = "yes"; then
	_oss_def="#define HAVE_OSS_AUDIO"
else
	_oss_def="#undef HAVE_OSS_AUDIO"
fi

########

if test "$_sun" = "auto"; then
	echocheck Sun audio
	cat > $TMPC <<EOF
#include <sys/audioio.h>
#include <sys/stropts.h>
int main(int argc, char **argv) { audio_info_t d; int t = AUDIO_ENCODING_LINEAR; t = I_FLUSH; return 0; }
EOF
	_sun=no
	target_cc_check "" "" && _sun=yes
	rm -f $TMPC
	echo "$_sun"
fi
if test "$_sun" = "yes"; then
	_sun_def="#define HAVE_SUN_AUDIO"
else
	_sun_def="#undef HAVE_SUN_AUDIO"
fi

########

if test "$_coreaudio" = "auto"; then
	echocheck Mac OS X Core Audio
	cat > $TMPC <<EOF
#include <CoreAudio/AudioHardware.h>
int main(int argc, char **argv) { AudioDeviceID d; AudioDeviceSetProperty(d, NULL, 0, false, 0, 0, NULL); return 0; }
EOF
	_coreaudio=no
	target_cc_check "" "-framework CoreAudio" && _coreaudio=yes
	rm -f $TMPC
	echo "$_coreaudio"
fi
if test "$_coreaudio" = "yes"; then
	_coreaudio_def="#define HAVE_MACOSX_AUDIO"
	_TARGET_LDFLAGS="$_TARGET_LDFLAGS -framework CoreAudio"
else
	_coreaudio_def="#undef HAVE_MACOSX_AUDIO"
fi

########

if test "$_jack" = "auto"; then
	echocheck Jack audio
	test -z "$jack_cflags" && jack_cflags=`$target_pkg_config --cflags jack 2> /dev/null`
	test -z "$jack_ldflags" && jack_ldflags=`$target_pkg_config --libs jack 2> /dev/null`
	cat > $TMPC <<EOF
#include <jack/jack.h>
int main(int argc, char **argv) { jack_client_t *c; jack_activate(c); return 0; }
EOF
	_jack=no
	target_cc_check "$jack_cflags" "$jack_ldflags" && _jack=yes
	rm -f $TMPC
	echo "$_jack"
fi
if test "$_jack" = "yes"; then
	_jack_def="#define HAVE_JACK_AUDIO"
	_TARGET_CFLAGS="$_TARGET_CFLAGS $jack_cflags"
	_TARGET_LDFLAGS="$_TARGET_LDFLAGS $jack_ldflags"
else
	_jack_def="#undef HAVE_JACK_AUDIO"
fi

########

if test "$_nullaudio" = "yes"; then
	_nullaudio_def="#define HAVE_NULL_AUDIO"
else
	_nullaudio_def="#undef HAVE_NULL_AUDIO"
fi

########

if test "$_sndfile" = "auto"; then
	echocheck libsndfile
	test -z "$sndfile_cflags" && sndfile_cflags=`$target_pkg_config --cflags sndfile 2> /dev/null`
	test -z "$sndfile_ldflags" && sndfile_ldflags=`$target_pkg_config --libs sndfile 2> /dev/null`
	cat > $TMPC <<EOF
#include <sndfile.h>
int main(int argc, char **argv) { SNDFILE *f; SF_INFO i; f = sf_open("", SFM_READ, &i); return 0; }
EOF
	_sndfile=no
	target_cc_check "$sndfile_cflags" "$sndfile_ldflags" && _sndfile=yes
	rm -f $TMPC
	echo "$_sndfile"
fi
if test "$_sndfile" = "yes"; then
	_sndfile_def="#define HAVE_SNDFILE"
	_TARGET_CFLAGS="$_TARGET_CFLAGS $sndfile_cflags"
	_TARGET_LDFLAGS="$_TARGET_LDFLAGS $sndfile_ldflags"
else
	_sndfile_def="#undef HAVE_SNDFILE"
fi

########

if test "$_mingw" = "auto"; then
	echocheck MinGW
	cat > $TMPC <<EOF
#include <windows.h>
#include <commdlg.h>
int main(int argc, char **argv) { OPENFILENAME o; o.Flags = OFN_PATHMUSTEXIST; GetSaveFileName(&o); return 0; }
EOF
	_mingw=no
	target_cc_check "" "-lmingw32 -mwindows" && _mingw=yes
	rm -f $TMPC
	echo "$_mingw"
fi
if test "$_mingw" = "yes"; then
	EXEEXT=.exe
	_TARGET_LDFLAGS="$_TARGET_LDFLAGS -lmingw32 -mwindows"
	_mingw_def="#define WINDOWS32"
	_cli=no
else
	_mingw_def="#undef WINDOWS32"
fi

########

if test "$_cli" = "yes"; then
	_cli_def="#define HAVE_CLI"
else
	_cli_def="#undef HAVE_CLI"
fi

########

test "$_mpu_rate" = "auto" && _mpu_rate=yes
if test "$_mpu_rate" = "yes"; then
	_mpu_rate_def="#define VARIABLE_MPU_RATE"
else
	_mpu_rate_def="#undef VARIABLE_MPU_RATE"
fi

########

if test "$_trace" = "yes"; then
	echo "Trace mode enabled"
	_trace_def="#define TRACE"
else
	echo "Trace mode disabled"
	_trace_def="#undef TRACE"
fi

########

test "$_fast_vdg" = "auto" && _fast_vdg=no
if test "$_fast_vdg" = "yes"; then
	echo "Fast video enabled"
	_fast_vdg_def="#define FAST_VDG"
else
	echo "Fast video disabled"
	_fast_vdg_def="#undef FAST_VDG"
fi

########

test "$_fast_sound" = "auto" && _fast_sound=no
if test "$_fast_sound" = "yes"; then
	echo "Fast audio enabled"
	_fast_sound_def="#define FAST_SOUND"
else
	echo "Fast audio disabled"
	_fast_sound_def="#undef FAST_SOUND"
fi

############################################################################
# Write config.mak

echo "Creating config.mak"
cat > config.mak <<EOF
prefix = $prefix
exec_prefix = $exec_prefix
bindir = $bindir
sbindir = $sbindir
libdir = $libdir
datadir = $datadir
sysconfdir = $sysconfdir
includedir = $includedir
mandir = $mandir

SRCROOT = $SRCROOT

EXEEXT = $EXEEXT
CRT0 = $TARGET_CRT0
AS = $TARGET_AS
CC = $TARGET_CC
OBJCOPY = $TARGET_OBJCOPY
CFLAGS = $TARGET_CFLAGS$_TARGET_CFLAGS
LDFLAGS = $TARGET_LDFLAGS$_TARGET_LDFLAGS

BUILD_CC = $BUILD_CC
BUILD_CFLAGS = $BUILD_CFLAGS
BUILD_SDL_CFLAGS = $build_sdl_cflags
BUILD_SDL_LDFLAGS = $build_sdl_ldflags

CONFIG_C_NESTED_FUNCS = $_c_nested_funcs
CONFIG_SDL = $_sdl
CONFIG_SDLGL = $_sdlgl
CONFIG_BUILD_SDL = $_build_sdl
CONFIG_GTK2 = $_gtk2
CONFIG_GTK1 = $_gtk1
CONFIG_CLI = $_cli
CONFIG_CARBON = $_carbon
CONFIG_OSS = $_oss
CONFIG_SUN = $_sun
CONFIG_COREAUDIO = $_coreaudio
CONFIG_JACK = $_jack
CONFIG_NULLAUDIO = $_nullaudio
CONFIG_SNDFILE = $_sndfile
CONFIG_GP32 = $_gp32
CONFIG_NDS = $_nds
CONFIG_MINGW = $_mingw
CONFIG_TRACE = $_trace
CONFIG_FAST_VDG = $_fast_vdg
EOF

############################################################################
# Write config.h

echo "Creating config.h"
cat > config.h <<EOF
#ifndef __CONFIG_H__
#define __CONFIG_H__

/* Various libc features? */
$_strsep_def

/* Nested functions allowed? */
$_c_nested_funcs_def

/* SDL? */
$_sdl_def

/* SDL OpenGL support? */
$_sdlgl_def

/* GTK+-2? */
$_gtk2_def

/* GTK+-1? */
$_gtk1_def

/* CLI? */
$_cli_def

/* Mac OS X Carbon? */
$_carbon_def

/* OSS audio? */
$_oss_def

/* Sun audio? */
$_sun_def

/* Mac OS X Core Audio? */
$_coreaudio_def

/* Jack audio? */
$_jack_def

/* Null audio? */
$_nullaudio_def

/* libsndfile? */
$_sndfile_def

/* GP32? */
$_gp32_def

/* NDS? */
$_nds_def

/* MinGW (Windows32)? */
$_mingw_def

/* MPU rate adjustable? */
$_mpu_rate_def

/* Build trace mode? */
$_trace_def

/* Fast VDG code? */
$_fast_vdg_def

/* Fast audio code? */
$_fast_sound_def

#endif  /* def __CONFIG_H__ */
EOF

if test \! -f Makefile; then
	echo "Symlinking Makefile"
	ln -s "$SRCROOT"/Makefile Makefile
fi

exec 3<&-

############################################################################
# Some final checks to inform user about important things

if test -z "$_have_video_driver"; then
	cat <<EOF

Erk!  No feature able of providing video output has been configured.
There's probably no point in building until you sort this out.
EOF
	exit 1
fi
