#!/bin/sh

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

command_line="$*"

SRCROOT=`dirname $0`

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

unset BUILD_STYLE
unset EXEEXT

unset target_pkg_config build_pkg_config
unset target_sdl_config build_sdl_config
unset target_gtk_config
unset _have_video_driver

all_opt="strsep gtk2 gtkgl sdl sdlgl build_sdl build_sdl_image curses gtk1 cli cocoa alsa oss pulse sunaudio coreaudio jack nullaudio linux_joystick sndfile gp32 nds mingw mpu_rate trace fast_vdg fast_sound logging"
unset $all_opt

_curses=no
_jack=no

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]
  --infodir=DIR           info documentation [DATADIR/info]
  --docdir=DIR            documentation root [DATADIR/doc/xroar]
  --pdfdir=DIR            pdf documentation [DOCDIR]

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-gtk2        don't test for GTK+-2
  --disable-gtkgl       don't test for GtkGLExt
  --disable-sdl         don't test for SDL
  --disable-sdlgl       don't test for SDL OpenGL support
  --enable-curses       build Curses user-interface
  --disable-gtk1        don't test for GTK+-1
  --disable-cocoa       don't test for Mac OS X Cocoa
  --disable-alsa        don't test for ALSA audio
  --disable-oss         don't test for OSS audio
  --disable-pulse       don't test for PulseAudio
  --disable-sun         don't test for Sun audio
  --disable-coreaudio   don't test for Mac OS X Core Audio
  --enable-jack         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
  --disable-trace       disable 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
  --with-makeinfo=PATH     set path to makeinfo
  --with-texi2pdf=PATH     set path to texi2pdf
  --with-install=PATH      set path to install

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

exec 3>config.log
config_log=3

echo "# $command_line" >&$config_log

# defaults from environment
test -n "$CC" && TARGET_CC="$CC"
test -n "$CXX" && TARGET_CXX="$CXX"
test -n "$OBJC" && TARGET_OBJC="$OBJC"
test -n "$CFLAGS" && TARGET_CFLAGS="$CFLAGS"
test -n "$CXXFLAGS" && TARGET_CXXFLAGS="$CXXFLAGS"
test -n "$OBJCFLAGS" && TARGET_OBJCFLAGS="$OBJCFLAGS"
test -n "$CPPFLAGS" && TARGET_CPPFLAGS="$CPPFLAGS"
test -n "$LDFLAGS" && TARGET_LDFLAGS="$LDFLAGS"
test -n "$LDLIBS" && TARGET_LDLIBS="$LDLIBS"
CC=
CXX=
OBJC=
CFLAGS=
OBJCFLAGS=
CPPFLAGS=
LDFLAGS=
LDLIBS=

TMPC=conftemp.c
TMPCXX=conftemp.cc
TMPM=conftemp.m
TMPO=conftemp.o
TMPX=conftemp

while test -n "$1"; do
	opt="$1"
	case "$opt" in
	--) shift; break; ;;
	macosx)
		TARGET_CC="gcc-4.0 -arch ppc -arch i386 -g"
		TARGET_CXX="g++-4.0 -arch ppc -arch i386 -g"
		TARGET_OBJC="gcc-4.0 -arch ppc -arch i386 -g"
		TARGET_CFLAGS="-mmacosx-version-min=10.4 -DMAC_OS_X_VERSION_MIN_REQUIRED=1040"
		TARGET_LDFLAGS="-Wl,-headerpad_max_install_names -Wl,-syslibroot,/Developer/SDKs/MacOSX10.4u.sdk"
		;;
	CC=*) TARGET_CC=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	CXX=*) TARGET_CXX=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	OBJC=*) TARGET_OBJC=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	CFLAGS=*) TARGET_CFLAGS=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	CXXFLAGS=*) TARGET_CXXFLAGS=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	OBJCFLAGS=*) TARGET_OBJCFLAGS=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	CPPFLAGS=*) TARGET_CPPFLAGS=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	LDFLAGS=*) TARGET_LDFLAGS=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	BUILD_CC=*) BUILD_CC=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	BUILD_CXX=*) BUILD_CXX=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	BUILD_OBJC=*) BUILD_OBJC=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	BUILD_CFLAGS=*) BUILD_CFLAGS=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	BUILD_CXXFLAGS=*) BUILD_CXXFLAGS=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	BUILD_OBJCFLAGS=*) BUILD_OBJCFLAGS=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	BUILD_CPPFLAGS=*) BUILD_CPPFLAGS=`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/^[^=]*=//'`; ;;
	--infodir=*) infodir=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--mandir=*) mandir=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--docdir=*) docdir=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--pdfdir=*) pdfdir=`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/^[^=]*=//'`; ;;
	--with-makeinfo=*) MAKEINFO=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--with-texi2pdf=*) TEXI2PDF=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--with-install=*) INSTALL=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	-h|--help)
		helptext
		exit 0
		;;
	*) ;;
	esac
	shift
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/xroar"
test -z "$localstatedir" && localstatedir="$exec_prefix/var"
test -z "$libdir" && libdir="$exec_prefix/lib"
test -z "$includedir" && includedir="$prefix/include"
test -z "$infodir" && infodir="$datadir/info"
test -z "$mandir" && mandir="$prefix/man"
test -z "$docdir" && docdir="$datadir/doc/xroar"
test -z "$pdfdir" && pdfdir="$docdir"

########

if test -z "$BUILD_CC"; then
	if ( gcc -v ) >/dev/null 2>&1; then
		BUILD_CC=gcc
	elif ( clang --version ) >/dev/null 2>&1; then
		BUILD_CC=clang
	elif ( cc ) >/dev/null 2>&1; then
		BUILD_CC=cc
	else
		BUILD_CC=false
	fi
fi

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

if test -z "$BUILD_CXX"; then
	if ( g++ -v ) >/dev/null 2>&1; then
		BUILD_CXX=g++
	elif ( c++ ) >/dev/null 2>&1; then
		BUILD_CXX=c++
	else
		BUILD_CXX=false
	fi
fi

if test -z "$TARGET_CXX"; then
	if ( "$target-g++" -v ) > /dev/null 2>&1; then
		TARGET_CXX="$target-g++"
	else
		TARGET_CXX="$BUILD_CXX"
	fi
fi

__target_cxx_machine="$__target_cc_machine"
if test -z "$__target_cxx_machine"; then
	if __tmp=`$TARGET_CXX -dumpmachine 2>/dev/null`; then
		__target_cxx_machine=`$TARGET_CXX -dumpmachine`
	fi
fi

test -z "$BUILD_OBJC" && BUILD_OBJC="$BUILD_CC"
test -z "$TARGET_OBJC" && TARGET_OBJC="$TARGET_CC"

__target_objc_machine="$__target_cc_machine"
if test -z "$__target_objc_machine"; then
	if __tmp=`$TARGET_OBJC -dumpmachine 2>/dev/null`; then
		__target_objc_machine=`$TARGET_OBJC -dumpmachine`
	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

########

__build_cc_machine=
if __tmp=`$BUILD_CC -dumpmachine 2>/dev/null`; then
	__build_cc_machine="$__tmp"
	echo "Build system type is $__build_cc_machine" >&$config_log
	echo "Build system type is $__build_cc_machine"
fi

__target_cc_machine=
if __tmp=`$TARGET_CC -dumpmachine 2>/dev/null`; then
	__target_cc_machine="$__tmp"
	echo "Target system type is $__target_cc_machine" >&$config_log
	echo "Target system type is $__target_cc_machine"
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 $TMPO -o $TMPX $LDFLAGS $2" >&$config_log
		if $CC $CFLAGS $1 $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"
	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"
	cc_check "$@"
	return $?
}

cxx_check () {
	ret=1
	echo "$CXX $CFLAGS $CXXFLAGS $1 -c -o $TMPO $TMPCXX" >&$config_log
	if $CXX $CFLAGS $CXXFLAGS $1 -c -o $TMPO $TMPCXX >&$config_log 2>&1; then
		if test -n "$cxx_build_only"; then
			rm -f $TMPO
			return 0
		fi
		echo "$CXX $CFLAGS $CXXFLAGS $1 $TMPO -o $TMPX $LDFLAGS $2" >&$config_log
		if $CXX $CFLAGS $CXXFLAGS $1 $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_cxx_check () {
	CXX="$TARGET_CXX"
	CXXFLAGS="$TARGET_CFLAGS $TARGET_CXXFLAGS"
	LDFLAGS="$TARGET_LDFLAGS"
	cxx_check "$@"
	return $?
}

target_cxx_check_compile () {
	cxx_build_only=1
	target_cxx_check "$@"
	ret=$?
	unset cxx_build_only
	return $ret
}

objc_check () {
	ret=1
	echo "$OBJC $CFLAGS $OBJCFLAGS $1 -c -o $TMPO $TMPM" >&$config_log
	if $OBJC $CFLAGS $OBJCFLAGS $1 -c -o $TMPO $TMPM >&$config_log 2>&1; then
		if test -n "$objc_build_only"; then
			rm -f $TMPO
			return 0
		fi
		echo "$OBJC $CFLAGS $OBJCFLAGS $1 $TMPO -o $TMPX $LDFLAGS $2" >&$config_log
		if $OBJC $CFLAGS $OBJCFLAGS $1 $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_objc_check () {
	OBJC="$TARGET_OBJC"
	OBJCFLAGS="$TARGET_CFLAGS $TARGET_OBJCFLAGS"
	LDFLAGS="$TARGET_LDFLAGS"
	objc_check "$@"
	return $?
}

target_objc_check_compile () {
	objc_build_only=1
	target_objc_check "$@"
	ret=$?
	unset objc_build_only
	return $ret
}

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

########

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

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

unset TARGET_CXX_OPT
test -z "$TARGET_CXXFLAGS" && TARGET_CXXFLAGS="$CXXFLAGS"
if test -z "$TARGET_CXXFLAGS"; then
	echocheck C++ optimisation options
	cat > $TMPCXX <<EOF
int main(int argc, char **argv) { return 0; }
EOF
	__opt="-O3"
	case "$__target_cxx_machine" in
	powerpc-apple-*)
		__opt="-O3 -mcpu=7450 -mdynamic-no-pic"
		;;
	*)
		;;
	esac
	target_cxx_check "$__opt" "" && TARGET_CXX_OPT="$__opt"
	echo "$TARGET_CXX_OPT"
fi

unset TARGET_OBJC_OPT
test -z "$TARGET_OBJCFLAGS" && TARGET_OBJCFLAGS="$OBJCFLAGS"
if test -z "$TARGET_OBJCFLAGS"; then
	echocheck Objective C optimisation options
	cat > $TMPM <<EOF
int main(int argc, char **argv) { return 0; }
EOF
	__opt="-O3"
	case "$__target_objc_machine" in
	powerpc-apple-*)
		__opt="-O3 -mcpu=7450 -mdynamic-no-pic"
		;;
	*)
		;;
	esac
	target_objc_check "$__opt" "" && TARGET_OBJC_OPT="$__opt"
	echo "$TARGET_OBJC_OPT"
fi

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

########

if test -z "$target_pkg_config"; 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 -z "$build_pkg_config"; 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_CC="$TARGET_CC -pipe"
fi
rm -f $TMPC

if test "$TARGET_CC" = "$TARGET_CXX -pipe"; then
	TARGET_CXX="$TARGET_CC";
else
	echocheck $TARGET_CXX accepting -pipe
	_cxx_pipe=no
	cat > $TMPCXX <<EOF
int main(int argc, char **argv) { return 0; }
EOF
	target_cxx_check_compile "-pipe" "" && _cxx_pipe=yes
	echo "$_cxx_pipe"
	if test "$_cxx_pipe" = "yes"; then
		TARGET_CXX="$TARGET_CXX -pipe"
	fi
	rm -f $TMPM
fi

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

########

# Check for specific architectures being selected

if test "$_gp32" = "yes"; then
	_nds=no
fi

if test "$_nds" = "yes"; then
	_gp32=no
fi

########

# 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" \!= "no"; then
	echocheck GP32
	gp32_cflags="-march=armv4t -mtune=arm920t -fomit-frame-pointer -ffast-math"
	gp32_ldflags="-specs=gp32_gpsdk.specs -lgpmem -lgpstdlib -lgpos -lgpstdio -lgpgraphic"
	_gp32=no
	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 "$gp32_cflags" "$gp32_ldflags" && _gp32=yes
	rm -f $TMPC
	echo "$_gp32"
fi
if test "$_gp32" = "yes"; then
	BUILD_STYLE=gp32
	_gp32_def="#define HAVE_GP32"
	_have_video_driver=yes
	_fast_vdg=yes
	_gtk2=no
	_gtkgl=no
	_sdl=no
	_sdlgl=no
	_curses=no
	_gtk1=no
	_cli=no
	_cocoa=no
	_alsa=no
	_oss=no
	_pulse=no
	_sunaudio=no
	_coreaudio=no
	_jack=no
	_nullaudio=no
	_linux_joystick=no
	_mingw=no
	_nds=no
	_trace=no
	test -z "$_mpu_rate" && _mpu_rate=no
	test -z "$_fast_sound" && _fast_sound=yes
	test -z "$_logging" && _logging=no
else
	_gp32_def="#undef HAVE_GP32"
fi

########

if test "$_nds" \!= "no"; then
	echocheck NDS
	nds_cflags="-march=armv5te -mtune=arm946e-s -fomit-frame-pointer -ffast-math -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
	BUILD_STYLE=nds
	_nds_def="#define HAVE_NDS"
	_have_video_driver=yes
	_fast_vdg=yes
	_gtk2=no
	_gtkgl=no
	_sdl=no
	_sdlgl=no
	_curses=no
	_gtk1=no
	_cli=no
	_cocoa=no
	_alsa=no
	_oss=no
	_pulse=no
	_sunaudio=no
	_coreaudio=no
	_jack=no
	_nullaudio=no
	_linux_joystick=no
	_mingw=no
	_trace=no
	test -z "$_mpu_rate" && _mpu_rate=no
	test -z "$_fast_sound" && _fast_sound=yes
	test -z "$_logging" && _logging=no
else
	_nds_def="#undef HAVE_NDS"
fi

########

if test "$_strsep" \!= "no"; 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 "$_gtk2" \!= "no"; 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"
	test -z "$_gtk1" && _gtk1=no
else
	_gtk2_def="#undef HAVE_GTK2"
	test -z "$_gtkgl" && _gtkgl=no
fi

########

if test "$_gtkgl" \!= "no"; then
	echocheck GtkGLExt
	test -z "$gtkgl_cflags" && gtkgl_cflags=`$target_pkg_config --cflags gtkglext-1.0 2> /dev/null`
	test -z "$gtkgl_ldflags" && gtkgl_ldflags=`$target_pkg_config --libs gtkglext-1.0 2> /dev/null`
	cat > $TMPC <<EOF
#include <gtk/gtkgl.h>
int main(int argc, char **argv) { gtk_gl_init(&argc, &argv); return 0; }
EOF
	_gtkgl=no
	target_cc_check "$gtkgl_cflags" "$gtkgl_ldflags" && _gtkgl=yes
	rm -f $TMPC
	echo "$_gtkgl"
fi
if test "$_gtkgl" = "yes"; then
	_gtkgl_def="#define HAVE_GTKGL"
	_have_video_driver=yes
else
	_gtkgl_def="#undef HAVE_GTKGL"
fi

########

if test -z "$target_sdl_config"; 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 -z "$build_sdl_config"; 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" \!= "no"; 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"
	_have_video_driver=yes
else
	_sdl_def="#undef HAVE_SDL"
	_sdlgl=no
fi

if test "$_build_sdl" \!= "no"; 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 "$_build_sdl_image" \!= "no"; then
	echocheck SDL_image on build host
	test -z "$build_sdl_image_ldflags" && build_sdl_image_ldflags="-lSDL_image"
	cat > $TMPC <<EOF
#include <SDL.h>
#include <SDL_image.h>
int main(int argc, char **argv) { SDL_Init(0); IMG_Load(""); return 0; }
EOF
	_build_sdl_image=no
	build_cc_check "$build_sdl_cflags" "$build_sdl_ldflags $build_sdl_image_ldflags" && _build_sdl_image=yes
	rm -f $TMPC
	echo "$_build_sdl_image"
fi

########

if test "$_sdlgl" \!= "no"; 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"
else
	_sdlgl_def="#undef HAVE_SDLGL"
fi

########

if test "$_curses" \!= "no"; then
	echocheck Curses
	curses_ldflags="-lcurses"
	cat > $TMPC <<EOF
#include <curses.h>
int main(int argc, char **argv) { initscr(); endwin(); return 0; }
EOF
	_curses=no
	target_cc_check "" "$curses_ldflags" && _curses=yes
	rm -f $TMPC
	echo "$_curses"
fi
if test "$_curses" = "yes"; then
	_curses_def="#define HAVE_CURSES"
	_have_video_driver=yes
	test -z "$_cli" && _cli=yes
else
	_curses_def="#undef HAVE_CURSES"
fi

########

if test -z "$target_gtk_config"; 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 "$_gtk1" \!= "no"; 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"
else
	_gtk1_def="#undef HAVE_GTK1"
fi

########

if test "$_cocoa" \!= "no"; then
	echocheck Mac OS X Cocoa
	cocoa_ldflags="-framework Cocoa"
	cat > $TMPM <<EOF
#import <Cocoa/Cocoa.h>
int main(int argc, char **argv) { return 0; }
EOF
	_cocoa=no
	target_objc_check "" "$cocoa_ldflags" && _cocoa=yes
	rm -f $TMPM
	echo "$_cocoa"
fi
if test "$_cocoa" = "yes"; then
	_cocoa_def="#define HAVE_COCOA"
	sdl_ldflags=`echo $sdl_ldflags|sed 's/[ \t]*-lSDLmain[ \t]*/ /g'`
else
	_cocoa_def="#undef HAVE_COCOA"
fi

########

if test "$_alsa" \!= "no"; then
	echocheck ALSA
	test -z "$alsa_ldflags" && alsa_ldflags="-lasound"
	cat > $TMPC <<EOF
#include <alsa/asoundlib.h>
int main(int argc, char **argv) { snd_pcm_t *pcm_handle; snd_pcm_open(&pcm_handle, "default", SND_PCM_STREAM_PLAYBACK, 0); return 0; }
EOF
	_alsa=no
	target_cc_check "" "$alsa_ldflags" && _alsa=yes
	rm -f $TMPC
	echo "$_alsa"
fi
if test "$_alsa" = "yes"; then
	_alsa_def="#define HAVE_ALSA_AUDIO"
else
	_alsa_def="#undef HAVE_ALSA_AUDIO"
fi

########

if test "$_oss" \!= "no"; 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 "$_pulse" \!= "no"; then
	echocheck PulseAudio
	test -z "$pulse_cflags" && pulse_cflags=`$target_pkg_config --cflags libpulse-simple 2> /dev/null`
	test -z "$pulse_ldflags" && pulse_ldflags=`$target_pkg_config --libs libpulse-simple 2> /dev/null`
	cat > $TMPC <<EOF
#include <pulse/simple.h>
int main(int argc, char **argv) { pa_simple *p = pa_simple_new(0,0,0,0,0,0,0,0,0); return 0; }
EOF
	_pulse=no
	target_cc_check "$pulse_cflags" "$pulse_ldflags" && _pulse=yes
	rm -f $TMPC
	echo "$_pulse"
fi
if test "$_pulse" = "yes"; then
	_pulse_def="#define HAVE_PULSE_AUDIO"
else
	_pulse_def="#undef HAVE_PULSE_AUDIO"
fi

########

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

########

if test "$_coreaudio" \!= "no"; then
	echocheck Mac OS X Core Audio
	coreaudio_ldflags="-framework CoreAudio"
	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 "" "$coreaudio_ldflags" && _coreaudio=yes
	rm -f $TMPC
	echo "$_coreaudio"
fi
if test "$_coreaudio" = "yes"; then
	_coreaudio_def="#define HAVE_MACOSX_AUDIO"
else
	_coreaudio_def="#undef HAVE_MACOSX_AUDIO"
fi

########

if test "$_jack" \!= "no"; 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"
else
	_jack_def="#undef HAVE_JACK_AUDIO"
fi

########

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

########

if test "$_linux_joystick" \!= "no"; then
	echocheck Linux joysticks
	cat > $TMPC <<EOF
#include <sys/ioctl.h>
#include <linux/joystick.h>
int main(int argc, char **argv) { ioctl(0, JSIOCGAXES, 0); return 0; }
EOF
	_linux_joystick=no
	target_cc_check "" "" && _linux_joystick=yes
	rm -f $TMPC
	echo "$_linux_joystick"
fi
if test "$_linux_joystick" = "yes"; then
	_linux_joystick_def="#define HAVE_LINUX_JOYSTICK"
else
	_linux_joystick_def="#undef HAVE_LINUX_JOYSTICK"
fi

########

if test "$_sndfile" \!= "no"; 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"
else
	_sndfile_def="#undef HAVE_SNDFILE"
fi

########

if test "$_mingw" \!= "no"; then
	echocheck MinGW
	mingw_ldflags="-lmingw32 -lwinmm -mwindows"
	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 "" "$mingw_ldflags" && _mingw=yes
	rm -f $TMPC
	echo "$_mingw"
fi
if test "$_mingw" = "yes"; then
	EXEEXT=.exe
	_mingw_def="#define WINDOWS32"
	_cli=no
else
	_mingw_def="#undef WINDOWS32"
fi

########

echocheck "makeinfo >= 4.13"
test -z "$MAKEINFO" && MAKEINFO="makeinfo"
makeinfo_version=`$MAKEINFO --version 2>/dev/null | head -1 | sed 's/^.* \([1-9][0-9\.]*\)$/\1/;s/\.\([0-9][0-9]*\)/.0000\1/;s/\.[0-9]*\(.....\)$/\1/;'`
test -z "$makeinfo_version" && makeinfo_version=0
if test "$makeinfo_version" -ge "400013"; then
	_makeinfo=yes
else
	unset MAKEINFO
	_makeinfo=no
fi
echo $_makeinfo

echocheck texi2pdf
test -z "$TEXI2PDF" && TEXI2PDF="texi2pdf"
if ( $TEXI2PDF --version ) > /dev/null 2>&1; then
	_texi2pdf=yes
else
	unset TEXI2PDF
	_texi2pdf=no
fi
echo $_texi2pdf

echocheck install
test -z "$INSTALL" && INSTALL="install"
if ( $INSTALL --version ) > /dev/null 2>&1; then
	_install=yes
else
	unset INSTALL
	_install=no
fi
echo $_install

########

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

########

test -z "$_mpu_rate" && _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

########

test -z "$_trace" && _trace=yes
if test "$_trace" = "yes"; then
	echo "Trace mode included"
	_trace_def="#define TRACE"
else
	echo "Trace mode not included"
	_trace_def="#undef TRACE"
fi

########

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

########

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

########

test -z "$_logging" && _logging=yes
if test "$_logging" = "yes"; then
	_logging_def="#define LOGGING"
else
	_logging_def="#undef LOGGING"
fi

############################################################################
# Write config.mak and config.h

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

SRCROOT = $SRCROOT

TARGET_ARCH = $__target_cc_machine
BUILD_STYLE = $BUILD_STYLE
EXEEXT = $EXEEXT
AS = $TARGET_AS
CC = $TARGET_CC
CXX = $TARGET_CXX
OBJC = $TARGET_OBJC
OBJCOPY = $TARGET_OBJCOPY

CFLAGS = $TARGET_C_OPT $TARGET_CFLAGS
CXXFLAGS = $TARGET_CXX_OPT $TARGET_CFLAGS $TARGET_CXXFLAGS
OBJCFLAGS = $TARGET_OBJC_OPT $TARGET_CFLAGS $TARGET_OBJCFLAGS
CPPFLAGS = $TARGET_CPPFLAGS
LDFLAGS = $TARGET_LDFLAGS

BUILD_CC = $BUILD_CC
BUILD_CXX = $BUILD_CXX
BUILD_OBJC = $BUILD_OBJC
BUILD_CFLAGS = $BUILD_CFLAGS
BUILD_CXXFLAGS = $BUILD_CXXFLAGS
BUILD_OBJCFLAGS = $BUILD_OBJCFLAGS

MAKEINFO = $MAKEINFO
TEXI2PDF = $TEXI2PDF
INSTALL = $INSTALL

EOF

cat > config.h <<EOF
#ifndef XROAR_CONFIG_H_
#define XROAR_CONFIG_H_

EOF

for opt in $all_opt; do
	eval "isset=\"\$_$opt\""
	if test "$isset" = "yes"; then
		echo "opt_$opt = yes" >> config.mak
		eval "test -n \"\$${opt}_cflags\" && echo \"opt_${opt}_CFLAGS = \$${opt}_cflags\"" >> config.mak
		eval "test -n \"\$${opt}_cxxflags\" && echo \"opt_${opt}_CXXFLAGS = \$${opt}_cxxflags\"" >> config.mak
		eval "test -n \"\$${opt}_objcflags\" && echo \"opt_${opt}_OBJCFLAGS = \$${opt}_objcflags\"" >> config.mak
		eval "test -n \"\$${opt}_ldflags\" && echo \"opt_${opt}_LDFLAGS = \$${opt}_ldflags\"" >> config.mak
	fi
	eval "test -n \"\$_${opt}_def\" && echo \"\$_${opt}_def\"" >> config.h
done

cat >> config.h <<EOF

#endif  /* def XROAR_CONFIG_H_ */
EOF

case "$SRCROOT" in
/*) RELSRCROOT="$SRCROOT"; ;;
*) RELSRCROOT="../$SRCROOT"; ;;
esac

if test \! -f Makefile; then
	echo "Symlinking Makefile"
	ln -s "$SRCROOT"/Makefile Makefile
	mkdir -p gp32
	ln -s "$RELSRCROOT"/gp32/makefile_gp32.mk gp32/makefile_gp32.mk
	mkdir -p nds
	ln -s "$RELSRCROOT"/nds/makefile_nds.mk nds/makefile_nds.mk
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
