#!/bin/sh

if [ -z $CXX ]
then
	CXX=g++
fi

if [ -z $CC ]
then
	CC=gcc
fi

if [ -z $SDL_CONFIG ]
then
	SDL_CONFIG=sdl-config
fi

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

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 "  --bindir=DIR             Install executable binaries at DIR [default: PREFIX/bin]"
	echo "  --mandir=DIR             Install manual pages at DIR [default: PREFIX/man]"
	echo "  --datadir=DIR            Install data files at DIR [default: PREFIX/share/games/legesmotus]"
	echo "  --sharedir=DIR           Install shared files at DIR [default: PREFIX/share]"
	echo "  --disable-sound          Disable sound support in this build"
	echo "  --disable-upgrade-check  Disable upgrade checking 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"
	echo
	echo "Influential environment variables:"
	echo "  CC		C/Objective-C compiler command"
	echo "  CFLAGS	C/Objective-C compiler flags"
	echo "  CXX		C++ compiler command"
	echo "  CXXFLAGS	C++ compiler flags"
	echo "  LDFLAGS	linker flags"
	echo "  SDL_CONFIG	sdl-config command"
}

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
}

# Check args

for arg
do
	case $arg in
	--help)
		display_help
		exit 0
		;;
	--prefix=*)
		prefix="`echo "$arg" | cut -f2 -d=`"
		;;
	--bindir=*)
		bindir="`echo "$arg" | cut -f2 -d=`"
		;;
	--mandir=*)
		mandir="`echo "$arg" | cut -f2 -d=`"
		;;
	--datadir=*)
		datadir="`echo "$arg" | cut -f2 -d=`"
		;;
	--sharedir=*)
		sharedir="`echo "$arg" | cut -f2 -d=`"
		;;
	--debug)
		debug=1
		;;
	--unix-style)
		unix_style=1
		;;
	--disable-sound)
		sound=0
		;;
	--disable-upgrade-check)
		upgrade_nag=0
		;;
	--disable-universal)
		universal=0
		;;
	--force)
		force=1
		;;
	*)
		echo "$arg: Unknown argument" >&2
		display_help
		exit 2
		;;
	esac
done

# Set defaults

if [ -z $bindir ]
then
	bindir="$prefix/bin"
fi

if [ $unix_style -eq 1 ]
then
	if [ -z $mandir ]
	then
		mandir="$prefix/man"
	fi

	if [ -z $datadir ]
	then
		datadir="$prefix/share/games/legesmotus"
	fi

	if [ -z $sharedir ]
	then
		sharedir="$prefix/share"
	fi
else
	if [ -z $mandir ]
	then
		mandir="$prefix/share/man"
	fi

	if [ -z $datadir ]
	then
		datadir="/Applications/Leges Motus.app/Contents/Resources/data"
	fi

	if [ -z $sharedir ]
	then
		sharedir="$prefix/share"
	fi
fi


printf '%s' "Checking for g++... "
if which $CXX >/dev/null 2>/dev/null && $CXX --version | grep 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
	printf '%s' "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
	printf '%s' "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
	printf '%s' "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
	if [ $sound -ne 0 ]
	then
		printf '%s' "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
fi
echo
echo
echo "Prefix:             $prefix"
echo "Binaries directory: $bindir"
echo "Manuals directory:  $mandir"
echo "Data directory:     $datadir"
echo "Share directory:    $sharedir"
echo "Sound:              `format_boolean $sound`"
echo "Upgrade check:      `format_boolean $upgrade_nag`"
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 = \"${bindir}\"" >> config.mak
echo "MANDIR = \"${mandir}\"" >> config.mak
echo "DATADIR = \"${datadir}\"" >> config.mak
echo "SHAREDIR = \"${sharedir}\"" >> config.mak

if [ $sound -ne 0 ]
then
	echo "NOSOUND =" >> config.mak
else
	echo "NOSOUND = 1" >> config.mak
fi
if [ $upgrade_nag -ne 0 ]
then
	echo "NO_UPGRADE_NAG =" >> config.mak
else
	echo "NO_UPGRADE_NAG = 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
if [ $unix_style -ne 0 ]
then
	echo "UNIXSTYLE = 1" >> config.mak
else
	echo "UNIXSTYLE =" >> config.mak
fi

echo "CC = $CC" >> config.mak
echo "CXX = $CXX" >> config.mak
echo "SDL_CONIFG = $SDL_CONFIG" >> config.mak
echo "CFLAGS += $CFLAGS" >> config.mak
echo "CXXFLAGS += $CXXFLAGS" >> config.mak
echo "LDFLAGS += $LDFLAGS" >> config.mak
