# Template file for 'ioquake3-rpi'
pkgname=ioquake3-rpi
version=20130506
revision=2
short_desc="ioquake3 for Raspberry Pi"
maintainer="Juan RP <xtraeme@voidlinux.eu>"
license="GPL-2"
homepage="http://ioquake3.org/"
distfiles="http://ftp.gwdg.de/pub/misc/ftp.idsoftware.com/idstuff/quake3/linux/linuxq3apoint-1.32b-3.x86.run"
skip_extraction="linuxq3apoint-1.32b-3.x86.run"
checksum=c36132c5556b35e01950f1e9c646235033a5130f87ad776ba2bc7becf4f4f186

# XXX only for rpi
create_wrksrc=yes
only_for_archs="armv6l armv6l-musl armv7l armv7l-musl"
hostmakedepends="pkg-config git"
makedepends="SDL-devel rpi-userland-devel"

do_configure() {
	# Fetch ioquake3 for rpi
	git clone git://github.com/raspberrypi/quake3.git $pkgname --depth=1
	
	cd $pkgname
	patch < ${FILESDIR}/ioquake3-build.sh.patch
	sed -e "s,@XBPS_CROSS_TRIPLET@,${XBPS_CROSS_TRIPLET}-,g" \
	    -e "s,/opt/vc,${XBPS_CROSS_BASE}/opt/vc,g" \
	    -e "s,SDL_LIB=.*,SDL_LIB=${XBPS_CROSS_BASE}/usr/lib,g" \
	    -e 's,-lSDL,-lSDL -Wl\,-R/opt/vc/lib,' \
		-i build.sh

	# Extract Patch Files
	cp $XBPS_SRCDISTDIR/${pkgname}-${version}/linuxq3apoint-1.32b-3.x86.run .
	chmod +x linuxq3apoint-1.32b-3.x86.run

	./linuxq3apoint-1.32b-3.x86.run --tar xf
}

do_build() {
	cd $pkgname
	./build.sh
}

do_install() {
	vmkdir opt/${pkgname}/baseq3
	vmkdir opt/${pkgname}/missionpack
	# Copy the executables
	install -m755 ${pkgname}/build/release-linux-arm/*.arm \
		${DESTDIR}/opt/${pkgname}
	install -Dm755 ${pkgname}/build/release-linux-arm/baseq3/*.so \
		${DESTDIR}/opt/${pkgname}/baseq3/

	install -Dm755 ${pkgname}/build/release-linux-arm/missionpack/*.so \
		${DESTDIR}/opt/${pkgname}/missionpack/

	# modify launcher scripts
	cp ${FILESDIR}/quake3.launcher .
	cp ${FILESDIR}/quake3ded.launcher .

	sed -i "s:IOQ3_BINARY:ioquake3.arm:" quake3.launcher
	sed -i "s:IOQ3_BINARY:ioq3ded.arm:" quake3ded.launcher
	sed -i "s:IOQ3_PATH:ioquake3-rpi:" quake3.launcher
	sed -i "s:IOQ3_PATH:ioquake3-rpi:" quake3ded.launcher

	# Install Quake 3 Patch Files
	install -m644 ${pkgname}/baseq3/*.pk3 \
		${DESTDIR}/opt/${pkgname}/baseq3/

	# Install Quake 3 Expansion Pack Patch Files
	install -m644 ${pkgname}/missionpack/*.pk3 \
		${DESTDIR}/opt/${pkgname}/missionpack/

	# Install Launcher (Client)
	vinstall quake3.launcher 755 usr/bin quake3

	# Install Launcher (Server)
	vinstall quake3ded.launcher 755 usr/bin quake3ded

	# Install Desktop File
	vinstall ${FILESDIR}/quake3.desktop 644 usr/share/applications

	# Install Icon File
	vinstall ${pkgname}/misc/quake3.png 644 usr/share/pixmaps
}
