#!/bin/bash
#simply run the various commands required to bring the source tree 
#into a state where the end user can simply do ./configure && make

SVN="/usr/bin/svn"
SVNURL="svn://maxr.org"
SVNSRC="$SVNURL/MAX/trunk"
SVNDATA="$SVNURL/MAX/game"
BUILDDIR="maxr-svn"
AUTORECONF="/usr/bin/autoreconf"
ME="maxr-svn-setup.sh"
VERSION="0.5"

echo "Hi $USER. I'm $ME $VERSION
I've been written to make live easier for fans of Mechanized Assault
and eXploration Reloaded that want to play and test maxr from SVN.

I'm a collection of hacks to:
-download maxr from SVN (latest developer version)
-set up and prepare the build environment
-run default configure and make 

For this purpose I need:
-support for compiling c++ code
-installed header/devel packets of dependencies like SDL
-svn (subversion) in $SVN
-autoreconf (autotools) in $AUTORECONF
-connection to the net to access $SVNSRC

Continue (y/n)? "

read word 
if [ ! "$word" = "y" ]; then
	echo "Bye."
	exit 0
fi

if [ -d $BUILDDIR ]; then
	echo "Warn: $BUILDDIR exists. Trying tp upgrade your checkout"
else
	echo "Creating $BUILDDIR"
	mkdir $BUILDDIR
	if [ ! -d $BUILDDIR ]; then
		echo "Ooops"
		exit -1
	fi
fi

if [ ! -x $SVN ]; then
        echo "Warn: Could not find svn which is part of subversion"
else
	if [ ! -d $BUILDDIR/src ]; then
		echo "Creating $BUILDDIR/src"
		mkdir $BUILDDIR/src
	fi
	
	cd $BUILDDIR/src/
	
	if [ -d ".svn" ]; then
		echo "Upgrading source tree"
		$SVN up
	else
		echo "Checking out source tree"
		$SVN co $SVNSRC ./
	fi

	cd ../..
	
	if [ ! -d $BUILDDIR/data ]; then
		echo "Creating $BUILDDIR/data"
		mkdir $BUILDDIR/data
	fi
	
	cd $BUILDDIR/data
	
	if [ -d ".svn" ]; then
		echo "Upgrading data tree"
		$SVN up
	else
		echo "Optional: The data tree hasn't been checked out before."
		echo "Optional: This is no problem at all but checking out the"
		echo "Optional: data tree takes a lot of time. The tree is not"
		echo "Optional: needed if you only want to compile the project. "
		echo "Optional: You do need the tree however for installing!"
		echo ""
		echo "Checkout data tree (y/n)? "
		read word
		if [ "$word" = "y" ]; then
			echo "Checking out data tree"
			$SVN co $SVNDATA ./
		else
			echo "Skipped checkout of data tree. Rerun this script to load data later!"
		fi
	fi
	
	cd ../

	if [ ! -d "./src/mk/linux" ]; then
		echo "Could not set up project files"
		exit -1
	fi

	echo "Setting up project files"
	cp -v ./src/mk/linux/configure.ac ./
	cp -v ./src/mk/linux/Makefile.am ./
	cp -v ./src/mk/linux/buildinfo.sh ./
	cp -v ./src/mk/linux/src/Makefile.am ./src
fi


echo "Generating configure files... may take a while."
if [ ! -x $AUTORECONF ]; then
	echo "Could not find autoreconf which is part of autoconf"
	exit -1
fi
autoreconf --install 
echo "
If everything went well you should now be able to enter $BUILDDIR
and run ./configure and make install. 

Hint: Use e.g. \"make DESTDIR=\$RPM_BUILD_ROOT install\" or
something like that to prepare a package for distributing.

If you do own an original Mechanized Assault & Exploration CD
(c) 1996 Interplay or if you downloaded the original game from
cdosabandonware you can make use of the tool resinstaller and 
extract original gfx and sounds automagically from existing max.res

Run configure now (y/n)?"

read word 
if [ "$word" = "y" ]; then
	./configure 
fi
echo ""
echo "Run make now (y/n)? "
read word
if [ "$word" = "y" ]; then
	make
	echo "If everything went well you should now be able to execute"
	echo "\"make install\" as root in $BUILDDIR and execute \"maxr\" as user to start maxr".
fi
echo "Bye."
exit 0
