#!/bin/sh
#
# Configure script for 'rezerwar'
# Copyright (c) Bertrand Janin (tamentis@neopulsar.org)
#
#

REZERWAR_VERSION=0.4.2

# check_devkitpro() - Check if DevKitPro is installed (for Wii)
check_devkitpro() {
	if [ -z "$DEVKITPRO" ]; then
		echo "DevKitPro doesn't seem to be installed (missing DEVKITPRO environment"
		echo "variable). Please install DevKitPro and re-run ./configure"
		exit
	fi
}


# check_sdl() - Check if SDL is installed
check_sdl() {
	echo -n "Checking for SDL... "
	SDLCONFIG=`which sdl-config`

	if [ ! -x "$SDLCONFIG" ]; then
		echo "not installed (missing sdl-config)."
		exit
	fi

	SDLVERSION=`$SDLCONFIG --version`
	MAJOR=`echo $SDLVERSION | sed 's/^\([0-9]\)\..*/\1/'`
	MINOR=`echo $SDLVERSION | sed 's/.*\.\([0-9]\)\..*/\1/'`
	REV=`echo $SDLVERSION   | sed 's/.*\.\([0-9]\{1,2\}\)$/\1/'`

	if [ "$MAJOR" != "1" -o "$MINOR" != "2" ]; then
		echo "found wrong version ($SDLVERSION) =/"
		exit
	fi

	echo "found ($SDLVERSION)"

	return 0
}

# check_sdl_mixer() - Check if SDL_mixer is installed (assumes we have sdl)
check_sdl_mixer() {
	echo -n "Checking for SDL_mixer... "

	cat > test_sdl_mixer.c <<EOF
#include <stdio.h>
#include "SDL_mixer.h"

struct _s { int major; int minor; int patch; } version;
main() {
	SDL_MIXER_VERSION(&version);
	printf("%d.%d.%d\n", version.major, version.minor, version.patch);
	return 0;
}
EOF

	if ! gcc `sdl-config --cflags` test_sdl_mixer.c -o test_sdl_mixer; then
		echo "SDL_mixer not found =/"
		rm -f test_sdl_mixer*
		exit
	fi

	SDLMIXVERSION=`./test_sdl_mixer`
	if [ -z "$SDLMIXVERSION" ]; then
		echo "SDL_mixer found but unable to produce proper output."
		rm -f test_sdl_mixer*
		exit
	fi

	echo "found ($SDLMIXVERSION)"
	rm -f test_sdl_mixer*

	return 0
}


# clean() - Remove config, Makefiles...
clean() {
	[ -r "Makefile" ] && make clean
	rm -f src/config.h Makefile src/Makefile
}


# distclean() - Remove unnecessary media sources, svn dirs, etc.
distclean() {
	clean > /dev/null
	find . -name ".svn" -type d -exec rm -rf {} \;
	find data -name "*.svg" -exec rm -f {} \;
	find data -name "*.xcf" -exec rm -f {} \;
	rm -f data/music/*wav
	rm -f doc/DOWNLOADS
	rm -f data/Makefile data/gfx/Makefile data/music/Makefile
	rm -rf data/gfx/tools
}


# generate_config_h() - Generate the config.h file
generate_config_h() {
	echo "/* Automagically generated by 'configure' */"
	echo

	echo "#define VERSION \"rezerwar $REZERWAR_VERSION\""
	[ -n "$CFG_DATAPATH" ]	&& echo "#define DATAPATH \"$CFG_DATAPATH\""
	[ -n "$CFG_CFGPATH" ]	&& echo "#define CFGPATH \"$CFG_CFGPATH\""
	[ -z "$CFG_NOSOUND" ]	&& echo "#define WITH_SOUND"
}


# generate_src_makefile() - Generate the Makefile for the src/ directory
generate_src_makefile() {
	echo "# Automagically generated by 'configure'"
	echo

	if [ -n "$MK_OVERRIDE" ]; then
		cat $MK_OVERRIDE
		return 0
	fi

	[ -n "$MK_EXTRAOBJS" ] && echo "OBJECTS += $MK_EXTRAOBJS"

	echo "CC?=gcc"
	echo "CFLAGS+=\`$SDLCONFIG --cflags\` -Wall -O2 $MK_EXTRACFLAGS"
	echo "LIBS+=`$SDLCONFIG --libs` -lSDL_mixer"

	cat mkfiles/Makefile.src
}


# generate_makefile() - Generate the main Makefile
generate_makefile() {
	echo "# Automagically generated by 'configure'"
	echo

	[ -n "$TARGET_BIN" ]    && echo "TARGET_BIN=\${DESTDIR}$TARGET_BIN"
	[ -n "$TARGET_DOC" ]    && echo "TARGET_DOC=\${DESTDIR}$TARGET_DOC"
	[ -n "$TARGET_DATA" ]   && echo "TARGET_DATA=\${DESTDIR}$TARGET_DATA"
	[ -n "$MK_EXTRABUILD" ] && echo "all_win32: build $MK_EXTRABUILD"

	cat mkfiles/Makefile.main
}

# get_os() - Get the current platform
get_os() {
	echo -n "Target OS... "
	OS=$(uname)
	if [ -n "$*" ]; then
		OS="$*"
	fi
	echo $OS
}


# help() - Spit out basic help
help() {
	echo "usage: ./configure [-h] [-l] [-d] [os]"
	echo
	echo "    -h   This help screen."
	echo "    -l   List supported OSes"
	echo
	echo "    -c   Clean up"
	echo
	echo "By default, this script will attempt to detect your OS and"
	echo "will guess the most appropriate configuration. If it gets your"
	echo "system wrong, you can force it, check -l to see the list of"
	echo "supported OSes."
	exit
}


# listos() - return the supported compilation systems
listos() {
	echo "Supported platforms (and aliases):"
	echo " - Unix, Linux, FreeBSD"
	echo " - Haiku, BeOS"
	echo " - Wii"
	exit
}


# Command-line handling
case $* in
	-h*|--h*)
		help
		;;
	-l)
		listos
		;;
	-c)
		echo "Cleaning up..."
		clean
		exit
		;;
	-d)
		echo "Cleaning up for distribution..."
		distclean
		exit
		;;
	-dw)
		distclean win32
		exit
		;;
esac


# Main loop


# Operating system picker
get_os $*
case $OS in
Linux|Unix|POSIX|*BSD)
	check_sdl
	check_sdl_mixer
	[ -z "$PREFIX" ]	&& PREFIX="/usr"
	[ -z "$TARGET_BIN" ]	&& TARGET_BIN="$PREFIX/games"
	[ -z "$TARGET_DOC" ]	&& TARGET_DOC="$PREFIX/share/doc/rezerwar"
	[ -z "$TARGET_DATA" ]	&& TARGET_DATA="$PREFIX/share/games/rezerwar"
	CFG_DATAPATH="$TARGET_DATA"
	CFG_CFGPATH=".rezerwar"
	;;
Debug)
	check_sdl
	check_sdl_mixer
	CFG_DATAPATH="../data"
	CFG_CFGPATH=".rezerwar"
	MK_EXTRACFLAGS="-ggdb"
	;;
Haiku|BeOS)
	check_sdl
	check_sdl_mixer
	[ -z "$PREFIX" ]	&& PREFIX="/boot/apps/rezerwar"
	[ -z "$TARGET" ]	&& TARGET="$PREFIX"
	TARGET_BIN="$TARGET"
	TARGET_DOC="$TARGET"
	TARGET_DATA="$TARGET"
	CFG_DATAPATH="$TARGET"
	CFG_CFGPATH=".rezerwar"
	CFG_NOSOUND=1
	MK_EXTRAOBJS="strsep.o"
	;;
Win32|MINGW32*)
	check_sdl
	CFG_DATAPATH="data"
	MK_EXTRAOBJS="strsep.o"
	MK_EXTRABUILD="build_win32"
	;;
Wii)
	check_devkitpro
	CFG_DATAPATH="sd:/apps/rezerwar/data"
	MK_OVERRIDE="mkfiles/Makefile.wii"
	;;
*)
	echo
	echo "Sorry but '$OS' is currently unsupported, if your OS is POSIX compliant you"
	echo "can give 'POSIX' a chance as first parameter to ./configure. Get the list"
	echo "of supported platforms with './configure -l'"
	exit
esac


generate_config_h > src/config.h
generate_src_makefile > src/Makefile
generate_makefile > Makefile


echo
echo "Configured for '$OS', run 'make' (or 'gmake') to compile."

