# Contributor: J.W. Birdsong <jwbirdsongATgmailDOTcom>
# Updated by Arand Nash <ienorand@gmail.com>
# Based on the AUR package for version 1.0 by Massimiliano Torromeo <massimiliano.torromeo@gmail.com>
pkgname=redeclipse-svn
# svn automagic seems to override the $pkgver anyways..
pkgver=1.01SVN
pkgrel=1
pkgdesc="A single-player and multi-player first-person ego-shooter, built as a total conversion of Cube Engine 2, "
arch=('i686' 'x86_64')
url="http://redeclipse.net"
license=('custom' 'ZLIB')
depends=('sdl_image'  'libgl' 'sdl_mixer')
makedepends=('subversion'  'mesa')
provides=(redeclipse)
conflicts=(redeclipse)

_svntrunk=https://redeclipse.svn.sourceforge.net/svnroot/redeclipse
_svnmod=redeclipse

build() {
	cd "$srcdir"

	# update/checkout svn
	if [ -d "$_svnmod"/.svn ]; then
		msg "Updating existing SVN checkout"
		(cd "$_svnmod" && svn up -r "$pkgver")
	else
		msg "New checkout from SVN"
		svn co "$_svntrunk" --config-dir ./ -r "$pkgver" "$_svnmod"
	fi

	msg "SVN checkout done (or server timeout)"
	msg "SVN export to clean build directory"

	# export to get rid of .svn metadata
	rm -rf "$srcdir/$pkgname"
	svn export "$srcdir/$_svnmod" "$srcdir/$pkgname"

	msg "Starting make..."

	# build in exported directory
	cd "$srcdir/$pkgname/src"
	make
}

package() {
	cd "$srcdir/$pkgname"

	# create directories
	install -d "$pkgdir"/usr/{share/"$pkgname",}/bin
	# this isn't really necessary since we use a custom script anyways
	sed -i "s|RE_DATA=.|RE_DATA=/usr/share/$pkgname|" "redeclipse.sh"

	# install binaries
	install -m755 src/re{server,client} "$pkgdir/usr/share/$pkgname/bin/"
	# install data
	cp -rf  data/ "$pkgdir/usr/share/$pkgname"

	# create wrapper scripts for client and server
	cat <<EOF > "$pkgdir/usr/bin/$pkgname"
#!/bin/sh
RE_OPTIONS="-r"
cd "/usr/share/$pkgname"
exec bin/reclient ${RE_OPTIONS} ${1+"$@"}
# ${1+"$@"} is a portability hack, keyword "wrapper script"
EOF

	cat <<EOF > "$pkgdir/usr/bin/$pkgname-server"
#!/bin/sh
RE_OPTIONS=""
cd "/usr/share/$pkgname"
exec bin/reserver ${RE_OPTIONS} ${1+"$@"}
# ${1+"$@"} is a portability hack, keyword "wrapper script"
EOF

	chmod 755 "$pkgdir/usr/bin/$pkgname"
	chmod 755 "$pkgdir/usr/bin/$pkgname-server"

	# install desktop file, icon, license
	install -Dm644 src/install/nix/redeclipse.desktop  "$pkgdir/usr/share/applications/$pkgname.desktop"
	install -Dm644 src/redeclipse.ico  "$pkgdir/usr/share/pixmaps/$pkgname.png"
	install -Dm644 license.txt "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}

