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

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

DEB_CPPFLAGS_MAINT_APPEND := \
	-DWITH_OPENARENA \
	$(NULL)

DEB_CFLAGS_MAINT_APPEND := \
	-fsigned-char \
	$(NULL)

include /usr/share/dpkg/default.mk

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

%:
	dh $@ --parallel

ifeq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
TARGET = release
else
TARGET = debug
endif

OPTIONS := \
		BR=build \
		BD=build \
		V=1 \
		USE_CODEC_OPUS=1 \
		USE_CODEC_VORBIS=1 \
		USE_CURL=1 \
		USE_CURL_DLOPEN=0 \
		USE_INTERNAL_LIBS=0 \
		USE_LOCAL_HEADERS=0 \
		USE_OPENAL=1 \
		USE_OPENAL_DLOPEN=0 \
		USE_VOIP=1 \
		$(shell $(CURDIR)/debian/q3arch make ${DEB_HOST_GNU_CPU} ${DEB_HOST_GNU_SYSTEM}) \
		BUILD_CLIENT_SMP=1 \
		DEFAULT_BASEDIR=/usr/lib/ioquake3 \
		BUILD_GAME_SO=1 \
		BUILD_GAME_QVM=0 \
		VERSION=$(DEB_VERSION)/$(DEB_VENDOR) \
		PRODUCT_DATE='$(PRODUCT_DATE)' \
		CFLAGS='$(CFLAGS) $(CPPFLAGS)' \
		FULLBINEXT="" \
		NO_STRIP=1 \
		$(NULL)

override_dh_auto_build:
	dh_auto_build -- $(OPTIONS) $(TARGET)

override_dh_auto_install: DESTDIR = debian/tmp
override_dh_auto_install:
	install -d $(DESTDIR)/usr/lib/ioquake3/baseq3/
	$(MAKE) copyfiles $(OPTIONS) COPYDIR=$(DESTDIR)/usr/lib/ioquake3
	install -m644 README.md build/README
	install -d $(DESTDIR)/usr/lib/quake3/base/baseq3/
	install -d $(DESTDIR)/usr/lib/quake3/demo/demoq3/
	install -d $(DESTDIR)/usr/lib/quake3/ta/baseq3/
	install -d $(DESTDIR)/usr/lib/quake3/ta/missionpack/
	cd $(DESTDIR)/usr/lib/quake3/base/baseq3 && ln -s ../../../ioquake3/baseq3/*.so .
	cd $(DESTDIR)/usr/lib/quake3/demo/demoq3 && ln -s ../../../ioquake3/baseq3/*.so .
	cd $(DESTDIR)/usr/lib/quake3/ta/baseq3 && ln -s ../../../ioquake3/baseq3/*.so .
	cd $(DESTDIR)/usr/lib/quake3/ta/missionpack && ln -s ../../../ioquake3/missionpack/*.so .

override_dh_strip:
	dh_strip --dbg-package=ioquake3-dbg

# ---------------------------------------------------------------------------

ORIG_REPO ?= https://github.com/ioquake/ioq3/
ORIG_REL = 1.36
# Empty if packaging a tagged release
ORIG_REV = 558da2527725fff521bbd3a6d341c606d395d92a
# Use the date of the ORIG_REV, or 20130426.1 if you snapshot twice in a day,
# or empty if ORIG_REV is
ORIG_DATE = 20160122
ORIG_SUFFIX = +dfsg1

ifeq (${ORIG_REV},)
	# I have no idea what tags are going to look like, because there
	# aren't any yet. For now, assume they'll be like "1.37".
	ORIG_VER := ${ORIG_REL}${ORIG_SUFFIX}
	ORIG_EXPORT := ${ORIG_REL}
else
	# I'd use "+gitsomething", but that sorts earlier than
	# "+svn1788", and "++gitsomething" is really ugly. +u sorts later,
	# and can be justified as short for "upstream".
	ORIG_VER := ${ORIG_REL}+u${ORIG_DATE}${ORIG_SUFFIX}
	ORIG_EXPORT := ${ORIG_REV}
endif

get-orig-source:
	git clone --bare --branch master ${ORIG_REPO} ioquake3-${ORIG_VER}.git
	GIT_DIR=ioquake3-${ORIG_VER}.git git archive \
		--format=tar \
		--prefix=ioquake3-${ORIG_VER}.orig/ \
		${ORIG_EXPORT} | tar -xvf-
	rm -rf ioquake3-${ORIG_VER}.orig/code/AL
	rm -rf ioquake3-${ORIG_VER}.orig/code/SDL12
	rm -rf ioquake3-${ORIG_VER}.orig/code/SDL2
	rm -rf ioquake3-${ORIG_VER}.orig/code/tools/lcc
	rm -rf ioquake3-${ORIG_VER}.orig/code/jpeg-8c
	rm -rf ioquake3-${ORIG_VER}.orig/code/libcurl*
	rm -rf ioquake3-${ORIG_VER}.orig/code/libogg-*
	rm -rf ioquake3-${ORIG_VER}.orig/code/libs
	rm -rf ioquake3-${ORIG_VER}.orig/code/libspeex
	rm -rf ioquake3-${ORIG_VER}.orig/code/libvorbis-*
	rm -rf ioquake3-${ORIG_VER}.orig/code/opus-*
	rm -rf ioquake3-${ORIG_VER}.orig/code/opusfile-*
	rm -rf ioquake3-${ORIG_VER}.orig/code/zlib
	tar --xz -cvf ioquake3_${ORIG_VER}.orig.tar.xz ioquake3-${ORIG_VER}.orig
	rm -rf ioquake3-${ORIG_VER}.orig
	rm -rf ioquake3-${ORIG_VER}.git

# to be invoked from a git checkout with upstream github as a remote;
# tarball ends up in ../build-area
maintainer-get-orig-source:
	$(MAKE) -f debian/rules get-orig-source ORIG_REPO=$$(pwd)/.git
	mv -i -v ioquake3_${ORIG_VER}.orig.tar.xz ../build-area/
