# This file is sourced by build.sh

# Include build functions used here
. build/unix/config_functions
. build/unix/menu_functions
. build/unix/ansi


uqm_requirements()
{
	# Some requirements:
	have_build_tools_language C || exit 1
	have_build_tools_language CXX || exit 1
	have_build_tool LINK || exit 1
	case "$HOST_SYSTEM" in
		MINGW32*|cegcc)
			have_build_tool WINDRES || exit 1
			;;
		Darwin)
			have_build_tools_language OBJC || exit 1
			have_build_tool REZ || exit 1
			;;
	esac


	# Define WORDS_BIGENDIAN on bigendian machines
	check_endianness


	# Libraries always used
	have_library SDL 1.2.8
	have_library SDL2
	have_library SDL 1.2.8 || have_library SDL2 || exit 1
	use_library libpng || exit 1

	case "$HOST_SYSTEM" in
		WINSCW|ARMV5|GCCE)
			# Symbian does not use dynamically generated config.h
			return
			;;
	esac

	# Add defines for HAVE_READDIR_R, HAVE_SETENV, HAVE_STRUPR,
	# HAVE_STRCASECMP, and HAVE_STRICMP
	define_have_symbol readdir_r
	define_have_symbol setenv
	define_have_symbol strupr
	define_have_symbol strcasecmp
	define_have_symbol stricmp

	# Check to see whether math functions are available for free.
	define_have_symbol acos

	# Require either strcasecmp or stricmp.
	if not have_symbol strcasecmp && not have_symbol stricmp; then
		echo "Fatal: Your system defines neither strcasecmp() nor stricmp()."
		exit 1
	fi

	# If we don't have math for free, add -lm to LDFLAGS.
	if not have_symbol acos; then
		LDFLAGS="$LDFLAGS -lm"
	fi
	
	# Add defines for HAVE_ISWGRAPH, HAVE_WCHAR_T, and HAVE_WINT_T
	define_have_symbol iswgraph
	define_have_type wchar_t
	define_have_type wint_t

	# Add defines for HAVE_GETOPT_LONG and HAVE_REGEX_H
	define_have_symbol getopt_long
	define_have_header regex.h

	# If we have the regex header, see if we need to link it specially
	case "$HOST_SYSTEM" in
		MINGW32*)
			LDFLAGS="$LDFLAGS -lregex"
			;;
	esac

	# Add define for HAVE__BOOL
	define_have_type _Bool

	# Add an environment variable for MACRO_WIN32 and MACRO___MINGW32__
	define_have_macro WIN32
	define_have_macro __MINGW32__

	if [ -n "$MACRO___MINGW32__" ]; then
		USE_WINSOCK=1
	fi
}

uqm_prepare_config()
{
	# Describe the menu:
	MENU_main_ITEMS="debug graphics sound mikmod ovcodec netplay joystick \
			ioformat accel threadlib"
	case "$HOST_SYSTEM" in
		Darwin|WINSCW|ARMV5|GCCE)
			# Installation directory not modifiable
			;;
		MINGW32*|cegcc)
			# No install procedure available for MinGW
			;;
		*)
			MENU_main_ITEMS="$MENU_main_ITEMS install_path"
			;;
	esac
	MENU_main_TITLE="Main menu"
	MENU_main_ITEM_debug_TYPE=CHOICE
	MENU_main_ITEM_graphics_TYPE=CHOICE
	MENU_main_ITEM_sound_TYPE=CHOICE
	MENU_main_ITEM_mikmod_TYPE=CHOICE
	MENU_main_ITEM_ovcodec_TYPE=CHOICE
	MENU_main_ITEM_netplay_TYPE=CHOICE
	MENU_main_ITEM_joystick_TYPE=CHOICE
	MENU_main_ITEM_ioformat_TYPE=CHOICE
	MENU_main_ITEM_accel_TYPE=CHOICE
	MENU_main_ITEM_threadlib_TYPE=CHOICE
	MENU_main_ITEM_install_path_TYPE=MENU

	CHOICE_debug_OPTIONS="nodebug debug strictdebug"
	CHOICE_debug_TITLE="Type of build"
	CHOICE_debug_OPTION_nodebug_TITLE="Optimised release build"
	CHOICE_debug_OPTION_nodebug_ACTION='nodebug_action'
	nodebug_action() {
		case "$HOST_SYSTEM" in
			WINSCW)
				CCOMMONFLAGS="$CCOMMONFLAGS -O2 -d NDEBUG"
				;;						
			ARMV5)
				CCOMMONFLAGS="$CCOMMONFLAGS -O3 -Otime -DNDEBUG"
				;;
			GCCE)
				CCOMMONFLAGS="$CCOMMONFLAGS -O3 -DNDEBUG"
				;;				
			*)
				CCOMMONFLAGS="$CCOMMONFLAGS -O3 -DNDEBUG"
				;;	
		esac	
		DEBUG=0
	}
	CHOICE_debug_OPTION_debug_TITLE="Debugging build"
	CHOICE_debug_OPTION_debug_ACTION='debug_action'
	debug_action() {
		case "$HOST_SYSTEM" in
			WINSCW)
				CCOMMONFLAGS="$CCOMMONFLAGS -g -O0 -W all -d DEBUG -d _DEBUG"
				;;
			ARMV5|GCCE)
				CCOMMONFLAGS="$CCOMMONFLAGS -g -O0 -DDEBUG -D_DEBUG"
				;;
			*)
				CCOMMONFLAGS="$CCOMMONFLAGS -g -O0 -W -Wall -DDEBUG"
				LDFLAGS="$LDFLAGS -O0"
				;;	
		esac
		DEBUG=1
	}
	CHOICE_debug_OPTION_strictdebug_TITLE="Debug + strict compile checks"
	CHOICE_debug_OPTION_strictdebug_ACTION='strictdebug_action'
	strictdebug_action() {
		case "$HOST_SYSTEM" in
			WINSCW)
				CCOMMONFLAGS="$CCOMMONFLAGS -g -O0 -W all -d DEBUG -d _DEBUG"
				;;
			ARMV5|GCCE)
				CCOMMONFLAGS="$CCOMMONFLAGS -g -O0 -DDEBUG -D_DEBUG"
				;;
			*)
				CCOMMONFLAGS="$CCOMMONFLAGS -g -O0 -DDEBUG -W -Wall"
#				CCOMMONFLAGS="$CCOMMONFLAGS -O1"
						# This is needed for -Wunitialized with gcc 3.4
				CCOMMONFLAGS="$CCOMMONFLAGS -Wcast-qual -Wmissing-declarations \
						-Wwrite-strings -Wimplicit -Wreturn-type -Wformat \
						-Wswitch -Wcomment -Wchar-subscripts \
						-Wparentheses -Wcast-align -Wuninitialized"
				CFLAGS="$CFLAGS -Wbad-function-cast -Wmissing-prototypes \
						-Wstrict-prototypes"
						# CFLAGS is for flags not valid in C++.
				CFLAGS="$CFLAGS -Wdeclaration-after-statement"
						# Until we abandon MSVC 6
#				CCOMMONFLAGS="$CCOMMONFLAGS -Waggregate-return"
						# It's not unreasonable to return structs at times.
#				CCOMMONFLAGS="$CCOMMONFLAGS "-Wpointer-arith"
						# Some standard header won't even compile with this on
#				CCOMMONFLAGS="$CCOMMONFLAGS -Wshadow"
						# This gives absurd conflicts with standard files,
						# like from 'y0 and y1'
#				CCOMMONFLAGS="$CCOMMONFLAGS -Werror"
						# We shouldn't do this until we actually nail them
						# all in the original code.  Then we can enforce them
						# on ourselves.
#				CCOMMONFLAGS="$CCOMMONFLAGS -pedantic-errors -ansi -trigraphs"  # ANSI
#				CCOMMONFLAGS="$CCOMMONFLAGS -Wnested-externs"
						# We know they're in the code, and though we'd like to
						# get rid of them, they're not bugs.
#				CCOMMONFLAGS="$CCOMMONFLAGS -Winline"
						# This gives too many warnings which we can do nothing
						# about, obscuring legitimate warnings.
				CFLAGS=`echo $CFLAGS`
				CXXFLAGS=`echo $CXXFLAGS`
				CCOMMONFLAGS=`echo $CCOMMONFLAGS`
						# Remove all the unnecessary spaces from the flags vars
						# for more readable messages.
				LDFLAGS="$LDFLAGS -O0"
				;;
		esac
		DEBUG=1
	}	
	case "$HOST_SYSTEM" in
		ARMV5|WINSCW|GCCE)
			CHOICE_debug_DEFAULT=nodebug
			;;
		*)
			CHOICE_debug_DEFAULT=debug
			;;
	esac
	

	CHOICE_graphics_OPTIONS="pure opengl sdl2"
	CHOICE_graphics_TITLE="Graphics Engine"
	CHOICE_graphics_OPTION_pure_TITLE="SDL1 without OpenGL graphics support"
	CHOICE_graphics_OPTION_pure_ACTION='graphics_pure_action'
	CHOICE_graphics_OPTION_pure_PRECOND='have_library SDL 1.2.8'
	graphics_pure_action() {
		CFLAGS="$CFLAGS -DGFXMODULE_SDL -DSDL_DIR=SDL"
		CCOMMONFLAGS="$CCOMMONFLAGS -DGFXMODULE_SDL"
		GFXMODULE=sdl
		HAVE_OPENGL=0
		use_library SDL 1.2.8
	}
	CHOICE_graphics_OPTION_opengl_TITLE="SDL1 with OpenGL graphics support"
	CHOICE_graphics_OPTION_opengl_ACTION='graphics_opengl_action'
	CHOICE_graphics_OPTION_opengl_PRECOND="have_library SDL 1.2.8 && have_library opengl"
	graphics_opengl_action() {
		CFLAGS="$CFLAGS -DGFXMODULE_SDL -DHAVE_OPENGL -DSDL_DIR=SDL"
		GFXMODULE=sdl
		HAVE_OPENGL=1
		use_library SDL 1.2.8 && use_library opengl
	}
	CHOICE_graphics_OPTION_sdl2_TITLE="SDL2 with modern graphics support"
	CHOICE_graphics_OPTION_sdl2_ACTION='graphics_sdl2_action'
	CHOICE_graphics_OPTION_sdl2_PRECOND='have_library SDL2'
	graphics_sdl2_action() {
		CFLAGS="$CFLAGS -DGFXMODULE_SDL -DSDL_DIR=SDL2"
		GFXMODULE=sdl
		HAVE_OPENGL=0
		use_library SDL2
	}
	if have_library SDL2; then
		CHOICE_graphics_DEFAULT=sdl2
	elif have_library opengl; then
		CHOICE_graphics_DEFAULT=opengl
	else
		CHOICE_graphics_DEFAULT=pure
	fi

	CHOICE_sound_OPTIONS="mixsdl openal"
	CHOICE_sound_TITLE="Sound backend"
	CHOICE_sound_OPTION_mixsdl_TITLE="Use MixSDL for sound (internal)"
	CHOICE_sound_OPTION_mixsdl_ACTION=sound_mixsdl_action
	sound_mixsdl_action() {
		SOUNDMODULE=mixsdl
	}
	CHOICE_sound_OPTION_openal_TITLE="Include OpenAL support (experimental)"
	CHOICE_sound_OPTION_openal_PRECOND="have_library openal"
	CHOICE_sound_OPTION_openal_ACTION=sound_openal_action
	sound_openal_action() {
		CCOMMONFLAGS="$CCOMMONFLAGS -DHAVE_OPENAL"
		SOUNDMODULE=openal
		use_library openal
	}
	CHOICE_sound_DEFAULT=mixsdl

	CHOICE_ovcodec_OPTIONS="standard tremor none"
	CHOICE_ovcodec_TITLE="Ogg Vorbis codec"
	CHOICE_ovcodec_OPTION_standard_TITLE="Xiph libogg + libvorbis"
	CHOICE_ovcodec_OPTION_standard_PRECOND="have_library vorbisfile"
	CHOICE_ovcodec_OPTION_standard_ACTION=ovcodec_standard_action
	ovcodec_standard_action() {
		use_library vorbisfile
		OGGVORBIS=vorbisfile
	}
	CHOICE_ovcodec_OPTION_tremor_TITLE="Tremor (avoids floating point math)"
	CHOICE_ovcodec_OPTION_tremor_PRECOND="have_library tremor"
	CHOICE_ovcodec_OPTION_tremor_ACTION=ovcodec_tremor_action
	ovcodec_tremor_action() {
		CCOMMONFLAGS="$CCOMMONFLAGS -DOVCODEC_TREMOR"
		OGGVORBIS=tremor
		use_library tremor
	}
	CHOICE_ovcodec_OPTION_none_TITLE="No Ogg Vorbis support"
	CHOICE_ovcodec_OPTION_none_ACTION=ovcodec_none_action
	ovcodec_none_action() {
		CCOMMONFLAGS="$CCOMMONFLAGS -DOVCODEC_NONE"
		OGGVORBIS=none
	}
	CHOICE_ovcodec_DEFAULT=standard

	CHOICE_mikmod_OPTIONS="internal external"
	CHOICE_mikmod_TITLE="Tracker music support"
	CHOICE_mikmod_OPTION_internal_TITLE="Included libmikmod"
	CHOICE_mikmod_OPTION_internal_ACTION=mikmod_internal_action
	mikmod_internal_action() {
		CCOMMONFLAGS="$CCOMMONFLAGS -DUSE_INTERNAL_MIKMOD"
		USE_INTERNAL_MIKMOD=1
	}
	CHOICE_mikmod_OPTION_external_TITLE="System libmikmod"
	CHOICE_mikmod_OPTION_external_PRECOND="have_library libmikmod"
	CHOICE_mikmod_OPTION_external_ACTION=mikmod_external_action
	mikmod_external_action() {
		USE_INTERNAL_MIKMOD=""
		use_library libmikmod
	}
	CHOICE_mikmod_DEFAULT=internal

	CHOICE_joystick_OPTIONS="enabled disabled"
	CHOICE_joystick_TITLE="Joystick support"
	CHOICE_joystick_OPTION_enabled_TITLE="enabled"
	#CHOICE_joystick_OPTION_enabled_PRECOND="have_symbol SDL_Joystick"
			# TODO: Check whether SDL has joystick support.
	CHOICE_joystick_OPTION_enabled_ACTION=joystick_enabled_action
	joystick_enabled_action() {
		CCOMMONFLAGS="$CCOMMONFLAGS -DHAVE_JOYSTICK"
	}
	CHOICE_joystick_OPTION_disabled_TITLE="disabled"
	case "$HOST_SYSTEM" in
		ARMV5|WINSCW|GCCE)
			CHOICE_joystick_DEFAULT=disabled
			;;
		*)
			CHOICE_joystick_DEFAULT=enabled
			;;
	esac	
	

	CHOICE_netplay_OPTIONS="none full ipv4"
	CHOICE_netplay_TITLE="Network Supermelee support"
	CHOICE_netplay_OPTION_none_TITLE="disabled"
	CHOICE_netplay_OPTION_none_ACTION=netplay_none_action
	netplay_none_action() {
		NETPLAY=""
	}
	CHOICE_netplay_OPTION_full_TITLE="IPv4 and IPv6"
	CHOICE_netplay_OPTION_full_PRECOND="have_library netlibs"
	CHOICE_netplay_OPTION_full_ACTION=netplay_full_action
	netplay_full_action() {
		CCOMMONFLAGS="$CCOMMONFLAGS -DNETPLAY=NETPLAY_FULL"
		if [ -n "$MACRO_WIN32" ]; then
			LDFLAGS="$LDFLAGS -lws2_32"
		fi
		NETPLAY="FULL"
		use_library netlibs
	}
	CHOICE_netplay_OPTION_ipv4_TITLE="IPv4; no IPv6"
	CHOICE_netplay_OPTION_ipv4_PRECOND="have_library netlibs"
	CHOICE_netplay_OPTION_ipv4_ACTION=netplay_ipv4_action
	netplay_ipv4_action() {
		CCOMMONFLAGS="$CCOMMONFLAGS -DNETPLAY=NETPLAY_IPV4"
		NETPLAY="IPV4"
		use_library netlibs
	}
	CHOICE_netplay_DEFAULT=full

	CHOICE_ioformat_OPTIONS="stdio stdio_zip"
	CHOICE_ioformat_TITLE="Supported file i/o methods"
	CHOICE_ioformat_OPTION_stdio_TITLE="Only direct file i/o"
	CHOICE_ioformat_OPTION_stdio_zip_TITLE="Direct & .zip file i/o"
	CHOICE_ioformat_OPTION_stdio_zip_PRECOND="have_library zlib"
	CHOICE_ioformat_OPTION_stdio_zip_ACTION="ioformat_stdio_zip_action"
	ioformat_stdio_zip_action() {
		CCOMMONFLAGS="$CCOMMONFLAGS -DHAVE_ZIP=1"
		USE_ZIP_IO=1
		use_library zlib
	}
	CHOICE_ioformat_DEFAULT=stdio_zip

	CHOICE_accel_OPTIONS="asm plainc"
	CHOICE_accel_TITLE="Graphics/Sound optimizations"
	CHOICE_accel_OPTION_asm_TITLE="Platform acceleration (asm, etc.)"
	CHOICE_accel_OPTION_asm_ACTION="accel_asm_action"
	accel_asm_action() {
		CCOMMONFLAGS="$CCOMMONFLAGS -DUSE_PLATFORM_ACCEL"
		USE_PLATFORM_ACCEL=1
	}
	CHOICE_accel_OPTION_plainc_TITLE="Only plain C code"
	CHOICE_accel_OPTION_plainc_ACTION="accel_plainc_action"
	accel_plainc_action() {
		USE_PLATFORM_ACCEL=0
	}
	CHOICE_accel_DEFAULT=asm

	CHOICE_threadlib_OPTIONS="sdl pthread"
	CHOICE_threadlib_TITLE="Thread library"
	CHOICE_threadlib_OPTION_sdl_TITLE="SDL-controlled thread library"
	CHOICE_threadlib_OPTION_sdl_ACTION="threadlib_sdl_action"
	threadlib_sdl_action() {
		CCOMMONFLAGS="$CCOMMONFLAGS -DTHREADLIB_SDL"
		THREADLIB="SDL"
	}
	CHOICE_threadlib_OPTION_pthread_TITLE="Pthread thread library"
	CHOICE_threadlib_OPTION_pthread_PRECOND="have_library pthread"
	CHOICE_threadlib_OPTION_pthread_ACTION="threadlib_pthread_action"
	threadlib_pthread_action() {
		CCOMMONFLAGS="$CCOMMONFLAGS -DTHREADLIB_PTHREAD"
		THREADLIB="PTHREAD"
		use_library pthread
	}
	CHOICE_threadlib_DEFAULT=sdl

	MENU_install_path_ITEMS="install_prefix install_bindir install_libdir \
			install_sharedir"
	MENU_install_path_TITLE="Installation paths"
	MENU_install_path_ITEM_install_prefix_TYPE=INPUT
	MENU_install_path_ITEM_install_bindir_TYPE=INPUT
	MENU_install_path_ITEM_install_libdir_TYPE=INPUT
	MENU_install_path_ITEM_install_sharedir_TYPE=INPUT

	INPUT_install_prefix_DEFAULT="/usr/local/games"
	INPUT_install_prefix_TITLE="Installation prefix"
	INPUT_install_prefix_VALIDATOR=validate_path
	INPUT_install_prefix_ACTION='eval INSTALL_PREFIX=$MENU_install_prefix_VALUE'

	INPUT_install_bindir_DEFAULT='$prefix/bin'
	INPUT_install_bindir_TITLE="Location for binaries"
	INPUT_install_bindir_VALIDATOR=validate_path

	INPUT_install_libdir_DEFAULT='$prefix/lib'
	INPUT_install_libdir_TITLE="Location for non-sharable data"
	INPUT_install_libdir_VALIDATOR=validate_path

	INPUT_install_sharedir_DEFAULT='$prefix/share'
	INPUT_install_sharedir_TITLE="Location for sharable data"
	INPUT_install_sharedir_VALIDATOR=validate_path
}

uqm_do_config()
{
	# Show the menu and let people set things
	do_menu MENU main "$BUILD_WORK/config.state"
	echo "Configuration complete."
}

uqm_process_config() {
	menu_process MENU main

	# Set INSTALL_LIBDIR, INSTALL_BINDIR, and INSTALL_SHAREDIR to the specified
	# values, replacing '$prefix' to the prefix set.
	local prefix
	prefix="$INPUT_install_prefix_VALUE"
	eval INSTALL_BINDIR="${INPUT_install_bindir_VALUE%/}/"
	eval INSTALL_LIBDIR="${INPUT_install_libdir_VALUE%/}/"
	eval INSTALL_SHAREDIR="${INPUT_install_sharedir_VALUE%/}/"

	# Set the content dir
	CONTENTDIR="${INSTALL_SHAREDIR}uqm/content"

	CCOMMONFLAGS="$CCOMMONFLAGS -I\"$BUILD_WORK\""

	# Set C++ only flags
	# These allow use of C++ without the standard library
	CXXFLAGS="$CXXFLAGS -fno-rtti -fno-exceptions -nostdinc++"

	# At this point, all the compiler flags must be set.
	CFLAGS="$CFLAGS $CCOMMONFLAGS"
	CXXFLAGS="$CXXFLAGS $CCOMMONFLAGS"
	CCOMMONFLAGS=""

	# Export the HAVE_ symbols to config_unix.h, using config_unix.h.in
	# as template (or config_win.h/config_win.h.in).
	SUBSTITUTE_VARS="$HAVE_SYMBOLS CONTENTDIR"
	case "$HOST_SYSTEM" in
		MINGW32*|CYGWIN*)
			SUBSTITUTE_FILES="config_win.h"
			;;
		*)
			SUBSTITUTE_FILES="config_unix.h"
			;;
	esac
	substitute_vars SUBSTITUTE_VARS SUBSTITUTE_FILES src "$BUILD_WORK"

	# Make build.vars from build.vars.in, substituting variables.
	SUBSTITUTE_VARS="BUILD_SYSTEM HOST_SYSTEM CFLAGS CXXFLAGS LDFLAGS LINK \
			PREPROC_C MKDEP_C COMPILE_C \
			PREPROC_CXX MKDEP_CXX COMPILE_CXX \
			PREPROC_OBJC MKDEP_OBJC COMPILE_OBJC \
			MAKE ECHON SED DEBUG JOYSTICK NETPLAY \
			OGGVORBIS SOUNDMODULE USE_INTERNAL_MIKMOD \
			GFXMODULE HAVE_OPENGL \
			HAVE_GETOPT_LONG HAVE_REGEX_H_FLAG \
			USE_ZIP_IO USE_PLATFORM_ACCEL THREADLIB USE_WINSOCK \
			INSTALL_LIBDIR INSTALL_BINDIR INSTALL_SHAREDIR \
			REZ WINDRES $HAVE_SYMBOLS"
	SUBSTITUTE_FILES="build.vars"
	substitute_vars SUBSTITUTE_VARS SUBSTITUTE_FILES . "$BUILD_WORK"

	# Make 'uqm' shell script from uqm-wrapper.in, substituting variables.
	SUBSTITUTE_VARS="INSTALL_LIBDIR INSTALL_BINDIR INSTALL_SHAREDIR uqm_NAME"
	SUBSTITUTE_FILES="uqm-wrapper"
	substitute_vars SUBSTITUTE_VARS SUBSTITUTE_FILES build/unix "$BUILD_WORK"
}

uqm_load_config()
{
	do_menu_load MENU main "$BUILD_WORK/config.state"
}

uqm_save_config()
{
	do_menu_save MENU main "$BUILD_WORK/config.state"
}

uqm_pre_build() {
	:  # Nothing to do
}

uqm_post_build() {
	local TARGET_FILE
	local RFORK
	eval TARGET_FILE="\$BUILD_WORK/\${${BUILD_PROJECT}_NAME}"
	RFORK="src/res/darwin/${BUILD_PROJECT}.r"

	test -f "$TARGET_FILE" || return
	
	# If run from the command-line on OSX, the Window Manager
	# will refuse to recognize the window unless the program has
	# a resource fork; so we give it a small one here.  When
	# run from inside an application package, this step is useless
	# (the cp command in the install step implicitly strips off
	# the resource fork, in fact)
	case "$HOST_SYSTEM" in
		Darwin)
			$REZ "$RFORK" -o "$TARGET_FILE"
			;;
		ARMV5)
			cp "$TARGET_FILE" "$BUILD_EPOCROOT/epoc32/release/armv5/urel/"
			cd src/symbian
			cmd \\/C bldmake bldfiles
			cmd \\/C abld build armv5 urel
			cd ../..
			;;
		WINSCW)
			cp "$TARGET_FILE" "$BUILD_EPOCROOT/epoc32/release/winscw/udeb/"
			cd src/symbian
			cmd \\/C bldmake bldfiles
			cmd \\/C abld build winscw udeb
			cd ../..
			;;			
		GCCE)
			cp "$TARGET_FILE" "$BUILD_EPOCROOT/epoc32/release/armv5/urel/"
			cd src/symbian
			cmd \\/C bldmake bldfiles
			cmd \\/C abld build gcce urel
			cd ../..
			;;			
	esac
}

uqm_pre_install() {
	:  # Nothing to do
}

uqm_install() {
	case "$HOST_SYSTEM" in
		Darwin)
			uqm_install_osx
			;;
		MINGW32*)
			echo "No installation procedure available for MinGW."
			echo "Read the manual for more information."
			;;
		cegcc*)
			echo "No installation procedure available for Windows CE."
			echo "Read the manual for more information."
			;;
		WINSCW)
			uqm_install_winscw
			;;
		ARMV5)
			uqm_install_armv5
			;;			
		GCCE)
			uqm_install_gcce
			;;						
		*)
			generic_install
			;;
	esac
}

uqm_post_install() {
	:  # Nothing to do
}

uqm_install_osx() {
	local VERSION HEADERS HEADER HEADER_FILE FRAMEWORK

	VERSION=`head -1 content/version`

	INSTROOT="$BUILD_WORK/The Ur-Quan Masters.app/Contents"

	# Make directory structure
	echo "Creating directory structure..." >&2
	mkdirhier "$INSTROOT/MacOS" 0755
	mkdirhier "$INSTROOT/Frameworks" 0755
	mkdirhier "$INSTROOT/Resources/content/addons" 0755
	mkdirhier "$INSTROOT/Resources/content/packages" 0755
	
	# Install misc. resources, icons, etc.
	echo "Installing miscellaneous resources..." >&2
	$SED "s/@@VERSION@@/$VERSION/g" src/res/darwin/Info.plist > \
			"$INSTROOT/Info.plist"
	cp src/res/darwin/PkgInfo "$INSTROOT"
	cp "src/res/darwin/The Ur-Quan Masters.icns" "$INSTROOT/Resources"

	# Find Frameworks and copy them into the application.
	echo "Copying dependancy Frameworks..." >&2
	HEADERS="Ogg/Ogg.h SDL/SDL.h SDL_image/SDL_image.h Vorbis/vorbisfile.h"
	if [ "$uqm_SOUNDMODULE" = openal ]; then
		HEADERS="$HEADERS OpenAL/al.h"
	fi
	for HEADER in $HEADERS; do
		HEADER_FILE=`basename $HEADER`
		eval FRAMEWORK=`echo '' | \
				$PREPROC_C -D__MACOSX__ -include $HEADER - | \
				awk '(/'$HEADER_FILE'/ && $2 == 1) { print $3; exit }' | \
				$SED 's/.Headers.*$/"/'`
		cp -a "$FRAMEWORK" "$INSTROOT/Frameworks"
	done

	# Install game content (it should probably make a zipfile)
	echo "Creating base content package..." >&2
	cp content/version "$INSTROOT/Resources/content/"
	(cd content && \
			find . -type f -not -path '*/CVS*' -not -path '*/.svn*' -not -path '*/addons*' -print | \
			$SED 's/^..//' | zip -X -q -n .ogg -8 -@ uqm-${VERSION}-prv-content.uqm)
	mv content/uqm-$VERSION-prv-content.uqm "$INSTROOT/Resources/content/packages"

	echo "Creating voice content package..." >&2
	(cd content/addons && \
			find 3dovoice -type f -not -path '*/CVS*' -not -path '*/.svn*' -print | \
			zip -X -q -n .ogg -8 -@ ../uqm-${VERSION}-prv-voice.uqm)
	mv content/uqm-$VERSION-prv-voice.uqm "$INSTROOT/Resources/content/addons"

	echo "Creating 3do music content package..." >&2
	(cd content/addons && \
			find 3domusic -type f -not -path '*/CVS*' -not -path '*/.svn*' -print | \
			zip -X -q -n .ogg -8 -@ ../uqm-${VERSION}-prv-3domusic.uqm)
	mv content/uqm-$VERSION-prv-3domusic.uqm "$INSTROOT/Resources/content/addons"

	# Install game binary (and rename it)
	echo "Installing executable..." >&2
	cp $uqm_NAME "$INSTROOT/MacOS/The Ur-Quan Masters"
}

uqm_install_winscw() {
	local PRIVATE_DIR
	
	PRIVATE_DIR="$BUILD_EPOCROOT/epoc32/winscw/c/private/A000A0C3"
			
	uqm_create_symbian_content_package
			
	echo "Creating directory structure to $PRIVATE_DIR ..."
	mkdir "$PRIVATE_DIR"
	mkdir "$PRIVATE_DIR/content"
	mkdir "$PRIVATE_DIR/content/packages"
	mkdir "$PRIVATE_DIR/userdata"
	
	echo "Copying data to $PRIVATE_DIR ..."
	cp content/version "$PRIVATE_DIR/content"
	cp content.uqm "$PRIVATE_DIR/content/packages"
	cp src/symbian/uqm.cfg "$PRIVATE_DIR/userdata"
}

uqm_install_armv5() {	
	uqm_create_symbian_content_package
	
	cd src/symbian
	cmd \\/C makekeys -cert -expdays 9999 -password asdfgh -len 2048 -dname "CN=UQM OR=Ur-Quan Masters CO=FI" uqm.key uqm.cer
	cmd \\/C makesis uqm-armv5.pkg uqm.sis
	cmd \\/C signsis -v uqm.sis uqm.sisx uqm.cer uqm.key asdfgh
	mv uqm.sisx ../..
	cd ../..
}

uqm_install_gcce() {	
	uqm_create_symbian_content_package
	
	cd src/symbian
	cmd \\/C makekeys -cert -expdays 9999 -password asdfgh -len 2048 -dname "CN=UQM OR=Ur-Quan Masters CO=FI" uqm.key uqm.cer
	cmd \\/C makesis uqm-gcce.pkg uqm.sis
	cmd \\/C signsis -v uqm.sis uqm.sisx uqm.cer uqm.key asdfgh
	mv uqm.sisx ../..
	cd ../..
}

uqm_create_symbian_content_package() {
	if [ -e "content.uqm" ]; then
		echo "Content package already exists, skipping"
		return
	fi
	
	local ANIFILE ANIFILES DNAME ESC_DNAME FONTDIR FONTDIRS FONTFILES

	echo "Building temporary content directory..."
	cd content
	find . -type f -not -path '*/CVS*' -not -path '*/.svn*' -not -path './addons*' -not -name "version" -not -name '*.png' -not -name '*.ani' -not -name '*.sml' -not -name '*.med' -not -name '*.mid' -not -name '*.big' >../content.lst
	tar cf ../tmp1.tar -T ../content.lst
	rm ../content.lst
	mkdir ../tmpcontent
	cd ../tmpcontent
	tar xf ../tmp1.tar
	rm ../tmp1.tar
	cd ../content	
	
	echo "Packing ani files..."
	ANIFILES=`find . -regex ".*\(sml\|med\|mid\|big\|ani\)$"`
	for ANIFILE in $ANIFILES; do
		DNAME=`dirname $ANIFILE`/
		ESC_DNAME=`echo $DNAME|sed "s/\//\\\\\\\\\//g"`
		mkdirhier ../tmpcontent/$DNAME 0755
		zip -q -j -0 ../tmpcontent/$ANIFILE $ANIFILE `cat $ANIFILE|cut -d " " -f 1|sed s/^/$ESC_DNAME/`
	done	
	
	echo "Packing font files..."
	FONTDIRS=`find . -name '*.fon'`
	for FONTDIR in $FONTDIRS; do
		FONTFILES=`find $FONTDIR -name '*.png'|sort -t. +2.4 -n`
		zip -q -j -0 ../tmpcontent/$FONTDIR $FONTFILES
	done		
	
	cd ../tmpcontent
	echo "Building content package..."
	zip -q -r ../content.uqm .
	
	cd ..
	echo "Removing temporary content directory..."
	rm -rf tmpcontent
}

uqm_clean() {
	case "$HOST_SYSTEM" in
		MINGW32*|CYGWIN*)
			rm -f "$BUILD_WORK/config_win.h"
			;;
		ARMV5|GCCE)
			local TARGET_FILE	
			eval TARGET_FILE="\$BUILD_WORK/\${${BUILD_PROJECT}_NAME}"
			
			rm -f "$BUILD_WORK/config_unix.h"
			rm -f "$BUILD_EPOCROOT/epoc32/release/armv5/urel/$TARGET_FILE"
			rm -f $TARGET_FILE uqm.sisx content.uqm
			
			cd src/symbian			
			cmd \\/C abld reallyclean
			cmd \\/C bldmake clean
			rm -f uqm.sis uqm.key uqm.cer
			cd ../..
			;;			
		WINSCW)
			local TARGET_FILE	
			eval TARGET_FILE="\$BUILD_WORK/\${${BUILD_PROJECT}_NAME}"
		
			rm -f "$BUILD_WORK/config_unix.h"
			rm -f "$BUILD_EPOCROOT/epoc32/release/winscw/udeb/$TARGET_FILE"
			rm -f $TARGET_FILE content.uqm
			
			cd src/symbian			
			cmd \\/C abld reallyclean
			cmd \\/C bldmake clean
			cd ../..
			;;						
		*)
			rm -f "$BUILD_WORK/config_unix.h"
			;;
	esac
}


