#!/bin/sh

prefix=/usr/local
sound=1
unix_style=1
universal=1
force=0
debug=0

if [ `uname -s` = Darwin ]
then
	unix_style=0
fi

display_help () {
	echo "Usage: $0 [OPTION]"
	echo "Options:"
	echo "  --prefix=PREFIX	Install Leges Motus program and data at PREFIX [default: $prefix]"
	echo "  --disable-sound		Disable sound support in this build"
	echo "  --debug			Enable a debug build"
	echo "  --unix-style		(Mac OS X only) Build without frameworks or .app bundle"
	echo "  --disable-universal	(Mac OS X only) Do not build universal binary"
	echo "  --force		Force configure to continue even if required libraries aren't found"
	echo "  --help		Display this help"
}

format_boolean () {
	if [ $1 -ne 0 ]
	then
		echo "Yes"
	else
		echo "No"
	fi
}

find_sdl_include () {
	for part in `sdl-config --cflags`
	do
		if echo "$part" | grep "^-I" >/dev/null
		then
			if [ -f `echo "$part" | cut -c3-`/$1 ]
			then
				return 0
			fi
		fi
	done
	return 1
}

die () {
	if [ $force -eq 0 ]
	then
		exit 1
	fi
}

for arg
do
	if [ "$arg" = "--help" ]
	then
		display_help
		exit 0
	elif [ "`echo "$arg" | cut -f1 -d=`" = "--prefix" ]
	then
		prefix="`echo "$arg" | cut -f2 -d=`"
	elif [ "$arg" = "--debug" ]
	then
		debug=1
	elif [ "$arg" = "--unix-style" ]
	then
		unix_style=1
	elif [ "$arg" = "--disable-sound" ]
	then
		sound=0
	elif [ "$arg" = "--disable-universal" ]
	then
		universal=0
	elif [ "$arg" = "--force" ]
	then
		force=1
	else
		echo "$arg: Unknown argument" >&2
		display_help
		exit 2
	fi
done

echo -n "Checking for g++... "
if which g++ >/dev/null 2>/dev/null
then
	echo "Found"
else
	echo "Not found"
	echo
	echo "g++ (the C++ compiler from the GNU Compiler Connection (gcc)) is required to build Leges Motus."
	echo "Please install the appropriate g++ package for your distro."
	echo "It will typically be categorized as a development package."
	die
fi

if [ $unix_style -eq 1 ]
then
	echo -n "Checking for SDL... "
	if sdl-config --version >/dev/null 2>/dev/null
	then
		echo "Found"
	else
		echo "Not found"
		echo
		echo "SDL is required to build and run Leges Motus."
		echo "You can obtain SDL from http://www.libsdl.org/"
		echo "or install the appropriate package for your distro."
		echo "If you install a distro package, please make sure you also install the -devel package."
		die
	fi
	echo -n "Checking for SDL_image... "
	if find_sdl_include SDL_image.h
	then
		echo "Found"
	else
		echo "Not found"
		echo
		echo "SDL_image is required to build and run Leges Motus."
		echo "You can obtain SDL_image from http://www.libsdl.org/projects/SDL_image/"
		echo "or install the appropriate package for your distro."
		echo "If you install a distro package, please make sure you also install the -devel package."
		die
	fi
	echo -n "Checking for SDL_ttf... "
	if find_sdl_include SDL_ttf.h
	then
		echo "Found"
	else
		echo "Not found"
		echo
		echo "SDL_ttf is required to build and run Leges Motus."
		echo "You can obtain SDL_ttf from http://www.libsdl.org/projects/SDL_ttf/"
		echo "or install the appropriate package for your distro."
		echo "If you install a distro package, please make sure you also install the -devel package."
		die
	fi
	echo -n "Checking for SDL_mixer... "
	if find_sdl_include SDL_mixer.h
	then
		echo "Found"
	else
		echo "Not found"
		echo
		echo "SDL_mixer is required to build and run Leges Motus."
		echo "You can obtain SDL_mixer from http://www.libsdl.org/projects/SDL_mixer/"
		echo "or install the appropriate package for your distro."
		echo "If you install a distro package, please make sure you also install the -devel package."
		echo
		echo "If you don't feel like installing SDL_mixer, you can compile without sound support by passing the --disable-sound option to configure."
		die
	fi
fi
echo
echo
echo "Prefix:           $prefix"
echo "Sound:            `format_boolean $sound`"
echo "Debug:            `format_boolean $debug`"

if [ `uname -s` = Darwin ]
then
	echo "Unix-style build: `format_boolean $unix_style`"
	echo "Universal build:  `format_boolean $universal`"
fi

echo

if [ $unix_style -eq 0 ]
then
	echo "Type 'make' to build and then 'make install' to install into the Applications directory."
else
	echo "Type 'make' to build and then 'make install' to install into $prefix."
fi

echo '#' > config.mak
echo '# This file was generated by running configure' >> config.mak
echo '#' >> config.mak
echo >> config.mak

echo "PREFIX = $prefix" >> config.mak
echo 'BINDIR = $(PREFIX)/bin' >> config.mak
if [ $unix_style -ne 0 ]
then
	echo 'MANDIR = $(PREFIX)/man' >> config.mak
	echo 'DATADIR = $(PREFIX)/share/games/legesmotus' >> config.mak
	echo "UNIXSTYLE = 1" >> config.mak
else
	echo 'MANDIR = $(PREFIX)/share/man' >> config.mak
	echo 'DATADIR = /Applications/Leges\ Motus.app/Contents/Resources/data' >> config.mak
	echo "UNIXSTYLE =" >> config.mak
fi
if [ $sound -ne 0 ]
then
	echo "NOSOUND =" >> config.mak
else
	echo "NOSOUND = 1" >> config.mak
fi
if [ $universal -ne 0 ]
then
	echo "UNIVERSAL = 1" >> config.mak
else
	echo "UNIVERSAL =" >> config.mak
fi
if [ $debug -ne 0 ]
then
	echo "DEBUG = 1" >> config.mak
else
	echo "DEBUG =" >> config.mak
fi
