#!/bin/sh

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

cli="$0"

SRCROOT=`dirname $0`
SRCROOT=`cd "$SRCROOT" && /bin/pwd`

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 _have_video_driver
unset _have_audio_driver

all_opt="
endian_h
endian
strsep
zlib
glib2
gtk2
opengl
gtkgl
sdl
sdlgl
build_sdl
build_sdl_image
cli
cocoa
alsa
oss
pulse
sunaudio
coreaudio
jack
nullaudio
linux_joystick
sndfile
mingw
pthreads
gdb_target
trace
fast_sound
logging
"
for opt in $all_opt; do
	eval "unset _$opt"
done

_jack=no
unset _cross
unset __keep_cc_output

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]
  --host=HOST       cross-compile to build programs to run on HOST [BUILD]
  --host-le         specify that HOST is little-endian
  --host-be         specify that HOST is big-endian

Optional Features:
  --disable-glib2       don't test for GLib 2
  --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
  --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-sunaudio    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-trace       disable trace mode
  --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-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

# 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"
test -n "$AR" && TARGET_AR="$AR"
test -n "$RANLIB" && TARGET_RANLIB="$RANLIB"
CC=
CXX=
OBJC=
CFLAGS=
OBJCFLAGS=
CPPFLAGS=
LDFLAGS=
LDLIBS=
AR=
RANLIB=

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

while test -n "$1"; do
	opt="$1"
	case "$opt" in
	*=*)
		key=`echo "$opt"|sed 's/^\([^=]*\)=.*$/\1/'`
		value=`echo "$opt"|sed 's/^[^=]*=//;s/"/\\\\"/g'`
		cli="$cli $key=\"$value\""
		;;
	*)
		cli="$cli $opt";
		;;
	esac
	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=*) _cross=yes; target=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--host-le) _endian=little; ;;
	--host-be) _endian=big; ;;
	--target=*) _cross=yes; target=`echo "$opt"|sed 's/^[^=]*=//'`; ;;
	--target-le) _endian=little; ;;
	--target-be) _endian=big; ;;
	--with-[a-z-]*=*)
		tmpk=with_`echo "$opt"|sed 's/^--with-//;s/=.*//;s/-/_/g'`
		tmpv=`echo "$opt"|sed 's/^[^=]*=//'`
		eval $tmpk="\$tmpv"
		;;
	--enable-[a-z-]*=*)
		tmpk=with_`echo "$opt"|sed 's/^--enable-//;s/=.*//;s/-/_/g'`
		tmpv=`echo "$opt"|sed 's/^[^=]*=//'`
		eval $tmpk="\$tmpv"
		;;
	--with-[a-z-]*)
		tmpk=_`echo "$opt"|sed 's/^--with-//;s/-/_/g'`
		eval $tmpk="yes"
		;;
	--enable-[a-z-]*)
		tmpk=_`echo "$opt"|sed 's/^--enable-//;s/-/_/g'`
		eval $tmpk="yes"
		;;
	--without-[a-z-]*)
		tmpk=_`echo "$opt"|sed 's/^--without-//;s/-/_/g'`
		eval $tmpk="no"
		;;
	--disable-[a-z-]*)
		tmpk=_`echo "$opt"|sed 's/^--disable-//;s/-/_/g'`
		eval $tmpk="no"
		;;
	-h|--help)
		helptext
		exit 0
		;;
	*) ;;
	esac
	shift
done

test -n "$with_sdl_config" && target_sdl_config="$with_sdl_config"
test -n "$with_pkg_config" && target_pkg_config="$with_pkg_config"
test -n "$with_makeinfo" && MAKEINFO="$with_makeinfo"
test -n "$with_texi2pdf" && TEXI2PDF="$with_texi2pdf"
test -n "$with_install" && INSTALL="$with_install"

if test -n "$with_sdl"; then
	sdl_includes="$with_sdl/include"
	sdl_libs="$with_sdl/lib"
fi

if test -n "$with_gtk2"; then
	gtk2_includes="$with_gtk2/include"
	gtk2_libs="$with_gtk2/lib"
fi

echo "# $cli" >&$config_log

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 false; then  # skip
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
fi  # end skip

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_AR" && BUILD_AR="ar"
if test -z "$TARGET_AR"; then
	if type "$target-ar" >/dev/null 2>&1; then
		TARGET_AR="$target-ar"
	else
		TARGET_AR="$BUILD_AR"
	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

test -z "$BUILD_RANLIB" && type ranlib >/dev/null 2>&1 && BUILD_RANLIB="ranlib"
test -z "$BUILD_RANLIB" && BUILD_RANLIB="true"
if test -z "$TARGET_RANLIB"; then
	if type "$target-ranlib" >/dev/null 2>&1; then
		TARGET_RANLIB="$target-ranlib"
	else
		TARGET_RANLIB="$BUILD_RANLIB"
	fi
fi

test -z "$BUILD_WINDRES" && BUILD_WINDRES="windres"
if test -z "$TARGET_WINDRES"; then
	if ( "$target-windres" --version ) > /dev/null 2>&1; then
		TARGET_WINDRES="$target-windres"
	else
		TARGET_WINDRES="$BUILD_WINDRES"
	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
	test -z "$__keep_cc_output" && 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"
}

########

# Figure out how to get C99 mode in target compiler

__c99=no
echocheck C99
cat > $TMPC <<EOF
#if __STDC_VERSION__ < 199901L
#error No C99
#endif
// Throw a few C99 features around...
static struct { int a; float b[]; } t = { .a = 1 };
int main(int argc, char **argv) {
	char tmp[argc]; (void)tmp;
	for (int i = 0; i < 10; i++) ;
	return 0;
}
EOF
__std=""
target_cc_check "" "" && __c99=yes
if test "$__c99" = "no"; then
	test -z "$__std" && target_cc_check "-std=c99" "" && __std=" -std=c99"
	test -n "$__std" && __c99="$__std"
fi
rm -f $TMPC
echo "$__c99"
if test "$__c99" \!= "no"; then
	TARGET_CC="$TARGET_CC$__std"
	BUILD_CC="$BUILD_CC$__std"
fi

########

# 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

if false; then  # skip
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
fi  # end skip

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"

########

# Prefer endian.h if found.

if test -z "$_endian_h" -a -z "$_endian"; then
	echocheck endian.h
	cat > $TMPC <<EOF
#include <stdlib.h>
#include <endian.h>
#undef VALUE
#if __BYTE_ORDER == __BIG_ENDIAN
#define VALUE (EXIT_SUCCESS)
#elif __BYTE_ORDER == __LITTLE_ENDIAN
#define VALUE (EXIT_SUCCESS)
#endif
int main(int argc, char **argv) {
	return VALUE;
}
EOF
	_endian_h=no
	target_cc_check "" "" && _endian_h=yes
	rm -f $TMPC
	echo "$_endian_h"
else
	_endian_h=no
fi
if test "$_endian_h" = "yes"; then
	_endian_h_def="#define HAVE_ENDIAN_H"
else
	_endian_h_def="#undef HAVE_ENDIAN_H"
fi

# Test endianness if endian.h doesn't exist and not cross-compiling.  If cross
# compiling, assume endianness based on arch.

if test "$_endian_h" \!= "yes" -a -z "$_endian" -a -z "$_cross"; then
	echocheck Endianness
	# I only care about 16-bit values at the moment.  There's a whole host
	# of potential wrongness for larger types...
	cat > $TMPC <<EOF
#include <stdint.h>
#include <stdlib.h>
int main(int argc, char **argv) {
	uint16_t i16 = 0;
	*(((uint8_t *)&i16)+0) = 0x12;
	*(((uint8_t *)&i16)+1) = 0x34;
	if (i16 == 0x1234) return EXIT_SUCCESS;
	return EXIT_FAILURE;
}
EOF
	__keep_cc_output=1
	if target_cc_check "" ""; then
		_endian=big
		$TMPX || _endian=little
	fi
	unset __keep_cc_output
	rm -f $TMPC $TMPO $TMPX
	if test -z "$_endian"; then
		# Unfortunately, most architectures are wrong-endian
		case "$__target_cc_machine" in
		m68k-*) _endian=big; ;;
		microblaze-*) _endian=big; ;;
		powerpc-*) _endian=big; ;;
		s390-*) _endian=big; ;;
		sparc-*) _endian=big; ;;
		*) _endian=little; ;;
		esac
	fi
	echo "$_endian"
fi
if test "$_endian" = "big"; then
	_endian_def="#define HAVE_BIG_ENDIAN"
else
	_endian_def="#undef HAVE_BIG_ENDIAN"
fi

########

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 $TMPCXX
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

########

if test "$_strsep" \!= "no"; then
	echocheck "strsep()"
	cat > $TMPC <<EOF
#define _BSD_SOURCE
#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 "$_zlib" \!= "no"; then
	echocheck zlib
	test -z "$zlib_cflags" && zlib_cflags=`$target_pkg_config --cflags zlib 2> /dev/null`
	test -z "$zlib_ldflags" && zlib_ldflags=`$target_pkg_config --libs zlib 2> /dev/null`
	test -z "$zlib_ldflags" && zlib_ldflags="-lz"
	cat > $TMPC <<EOF
#include <stdlib.h>
#include <zlib.h>
int main(int argc, char **argv) { (void)crc32(0L, NULL, 0); (void)gzopen("", ""); return 0; }
EOF
	_zlib=no
	target_cc_check "$zlib_cflags" "$zlib_ldflags" && _zlib=yes
	rm -f $TMPC
	echo "$_zlib"
fi
if test "$_zlib" = "yes"; then
	_zlib_def="#define HAVE_ZLIB"
else
	_zlib_def="#undef HAVE_ZLIB"
fi

########

if test "$_glib2" \!= "no"; then
	echocheck GLib2
	test -z "$glib2_cflags" && glib2_cflags=`$target_pkg_config --cflags glib-2.0 2> /dev/null`
	test -z "$glib2_ldflags" && glib2_ldflags=`$target_pkg_config --libs glib-2.0 2> /dev/null`
	cat > $TMPC <<EOF
#include <stdlib.h>
#include <glib.h>
int main(int argc, char **argv) { (void)g_main_loop_new(NULL, FALSE); return 0; }
EOF
	_glib2=no
	target_cc_check "$glib2_cflags" "$glib2_ldflags" && _glib2=yes
	rm -f $TMPC
	echo "$_glib2"
fi
if test "$_glib2" = "yes"; then
	_glib2_def="#define HAVE_GLIB2"
else
	_glib2_def="#undef HAVE_GLIB2"
	test -z "$_gtk2" && _gtk2=no
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"
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
	_opengl=yes
else
	_gtkgl_def="#undef HAVE_GTKGL"
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 "$_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>
#include <SDL_syswm.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 "$_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"
	_have_audio_driver=yes
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"
	_have_audio_driver=yes
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"
	_have_audio_driver=yes
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"
	_have_audio_driver=yes
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 <stdlib.h>
#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"
	_have_audio_driver=yes
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"
	_have_audio_driver=yes
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 GtkGLExt, Linux joysticks and some non-null sound module are all
# available, we can disable SDL.

if test "$_sdl" \!= "yes" -a "$_gtkgl" = "yes" -a "$_linux_joystick" = "yes" -a "$_have_audio_driver" = "yes"; then
	_sdl=no
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 "$_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>
#include <SDL_syswm.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
	sdl_objcflags="$sdl_cflags"
else
	_sdl_def="#undef HAVE_SDL"
	_sdlgl=no
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"
	_opengl=yes
else
	_sdlgl_def="#undef HAVE_SDLGL"
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_cflags="-D__USE_MINGW_ANSI_STDIO=1"
	mingw_ldflags="-lmingw32 -lwinmm -mwindows -lws2_32"
	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_cflags" "$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

########

if test "$_pthreads" \!= "no"; then
	echocheck POSIX threads
	cat > $TMPC <<EOF
#include <pthread.h>
#include <stdlib.h>
int main(int argc, char **argv) { pthread_create(NULL, NULL, NULL, NULL); return 0; }
EOF
	_pthreads=no
	if test "$_mingw" = "yes"; then
		pthreads_cflags=""
		pthreads_ldflags="-Wl,-Bstatic -lpthread -Wl,-Bdynamic"
		target_cc_check "$pthreads_cflags" "$pthreads_ldflags" && _pthreads=yes
	fi
	if test "$_pthreads" = "no"; then
		pthreads_ldflags="-pthread"
		target_cc_check "$pthreads_cflags" "$pthreads_ldflags" && _pthreads=yes
	fi
	rm -f $TMPC
	echo "$_pthreads"
fi
if test "$_pthreads" = "yes"; then
	_pthreads_def="#define HAVE_PTHREADS"
else
	_pthreads_def="#undef HAVE_PTHREADS"
fi

########

if test "$_gdb_target" \!= "no"; then
	test "$_pthreads" = "yes" && _gdb_target=yes
fi
if test "$_gdb_target" = "yes"; then
	_gdb_target_def="#define WANT_GDB_TARGET"
else
	_gdb_target_def="#undef WANT_GDB_TARGET"
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 "$_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_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
AR = $TARGET_AR
RANLIB = $TARGET_RANLIB
WINDRES = $TARGET_WINDRES

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_AR = $BUILD_AR
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

# Ensure build directories exist
mkdir -p doc portalib src tools
mkdir -p portalib/pl_glib
mkdir -p src/alsa src/gtk2 src/jack src/linux src/macosx src/oss src/pulseaudio
mkdir -p src/sdl src/sunos src/windows32

if test \! -f Makefile; then
	echo "Symlinking Makefile"
	ln -s "$SRCROOT"/Makefile Makefile
	ln -s "$SRCROOT"/src/Makefile src/Makefile
	ln -s "$SRCROOT"/doc/Makefile doc/Makefile
	ln -s "$SRCROOT"/portalib/Makefile portalib/Makefile
	ln -s "$SRCROOT"/tools/Makefile tools/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
