#!/usr/bin/make -f
#export DH_VERBOSE=1

export LC_ALL=C.UTF-8
export TZ=UTC

include /usr/share/dpkg/default.mk

PRODUCT_DATE := $(shell date --utc --date="`dpkg-parsechangelog -SDate`" "+%b %_d %Y")

# the Q3 engine's build system doesn't respect CPPFLAGS, so put them in CFLAGS
CFLAGS += $(CPPFLAGS) -fsigned-char

common_options = \
	V=1 \
	BUILD_STANDALONE=1 \
	USE_CODEC_VORBIS=1 \
	USE_CURL=1 \
	USE_CURL_DLOPEN=0 \
	USE_OPENAL=1 \
	USE_OPENAL_DLOPEN=0 \
	USE_VOIP=1 \
	USE_INTERNAL_SPEEX=0 \
	USE_INTERNAL_ZLIB=0 \
	USE_LOCAL_HEADERS=0 \
	$(shell /usr/share/ioquake3/q3arch make ${DEB_HOST_GNU_CPU} ${DEB_HOST_GNU_SYSTEM}) \
	SDL_CFLAGS= \
	BUILD_CLIENT=0 \
	BUILD_SERVER=0 \
	BUILD_GAME_SO=1 \
	CROSS_COMPILING=1 \
	VERSION=$(DEB_VERSION)/$(DEB_VENDOR) \
	PRODUCT_DATE='$(PRODUCT_DATE)' \
	$(NULL)

TARGET = release

ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
# near enough
TARGET = debug
endif

GAME_OPTIONS = \
	BR=build/normal \
	BD='$$(BR)' \
	$(NULL)

GAME_081_OPTIONS = \
	BR=build/0.8.1-compat \
	BD='$$(BR)' \
	OPENARENA_081_COMPATIBLE=1 \
	$(NULL)

%:
	dh $@ --parallel

override_dh_auto_build:
	dh_auto_build -- $(common_options) $(GAME_OPTIONS) $(TARGET)
	dh_auto_build -- $(common_options) $(GAME_081_OPTIONS) $(TARGET)
	sed \
		-e 's!@IOQ3BINARY@!ioquake3!' \
		-e 's!@IOQ3SELF@!openarena!' \
		-e 's!@IOQ3ROLE@!client!' \
		-e 's!@FS_BASEPATH@!/usr/lib/openarena!' \
		< debian/scripts/openarena.in > debian/scripts/openarena
	sed \
		-e 's!@IOQ3BINARY@!ioq3ded!' \
		-e 's!@IOQ3SELF@!openarena-server!' \
		-e 's!@IOQ3ROLE@!server!' \
		-e 's!@FS_BASEPATH@!/usr/lib/openarena-server!' \
		< debian/scripts/openarena.in > debian/scripts/openarena-server
	chmod +x debian/scripts/openarena
	chmod +x debian/scripts/openarena-server

override_dh_clean:
	dh_clean --exclude README-0.8.8~

override_dh_auto_clean:
	rm -f build-stamp
	rm -rf build
	rm -f debian/scripts/openarena
	rm -f debian/scripts/openarena-server

override_dh_auto_install:
	install -d debian/tmp/usr/games
	install -d debian/tmp/usr/lib/openarena/baseoa/pak0/
	install -d debian/tmp/usr/lib/openarena/baseoa/pak6-patch085/
	install -d debian/tmp/usr/lib/openarena/baseoa/pak6-patch088/
	install -d debian/tmp/usr/lib/openarena/missionpack/
	install -d debian/tmp/usr/lib/openarena-server/baseoa/
	install -d debian/tmp/usr/lib/openarena-server/missionpack/
	# The client currently needs two builds:
	# * one compatible with OA 0.8.1 pure servers
	# * one compatible with OA 0.8.5 and 0.8.8 pure servers
	# On impure servers, we default to the latest.
	install -m644 build/0.8.1-compat/baseq3/*.so \
		debian/tmp/usr/lib/openarena/baseoa/pak0/
	install -m644 build/normal/baseq3/*.so \
		debian/tmp/usr/lib/openarena/baseoa/pak6-patch088
	( cd debian/tmp/usr/lib/openarena/baseoa/pak6-patch085 && \
		ln -s ../pak6-patch088/*.so . )
	( cd debian/tmp/usr/lib/openarena/baseoa && \
		ln -s pak6-patch088/*.so . )
	# The "mission pack" doesn't currently have incompatible versions so
	# just use the latest
	install -m644 build/normal/missionpack/*.so\
		debian/tmp/usr/lib/openarena/missionpack/
	# The server only needs the latest version, since it's the server
	# that determines which version clients are expected to be
	# compatible with
	install -m644 build/normal/baseq3/qagame*.so \
		debian/tmp/usr/lib/openarena-server/baseoa/
	install -m644 build/normal/missionpack/qagame*.so\
		debian/tmp/usr/lib/openarena-server/missionpack/

override_dh_strip:
	dh_strip --dbg-package=openarena-dbg

.PHONY: get-orig-source
get-orig-source:
	test ! -e get-orig-source
	mkdir get-orig-source
	if test -e ../oa-0.8.8.tar.bz2; then \
		cp ../oa-0.8.8.tar.bz2 get-orig-source/; \
	else \
		wget -O get-orig-source/oa-0.8.8.tar.bz2 \
		http://files.poulsander.com/~poul19/public_files/oa/dev088/oa-0.8.8.tar.bz2; \
	fi
	test "`sha256sum -b get-orig-source/oa-0.8.8.tar.bz2`" = \
	"47fed4ea740278aa970538fc15819a4f6b8631eda380431ba09eb1b31ecbe5ad *get-orig-source/oa-0.8.8.tar.bz2"
	tar -xvj -C get-orig-source -f get-orig-source/oa-0.8.8.tar.bz2
	rm -rf get-orig-source/oa-0.8.8/tools/lcc
	rm -rf get-orig-source/oa-0.8.8/windows_scripts/*.exe
	mv get-orig-source/oa-0.8.8 get-orig-source/openarena_0.8.8.orig
	tar -zcvf openarena_0.8.8.orig.tar.gz \
		-C get-orig-source openarena_0.8.8.orig
	rm -rf get-orig-source
