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

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

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

include /usr/share/dpkg/default.mk

%:
	dh $@

options := \
	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 debian/q3arch make ${DEB_HOST_GNU_CPU} ${DEB_HOST_GNU_SYSTEM}) \
	BUILD_GAME_QVM=0 \
	COPYDIR=$(CURDIR)/debian/tmp/usr/lib/rtcw \
	NO_STRIP=1 \
	OPTIMIZE='' \
	VERSION=$(DEB_VERSION)/$(DEB_VENDOR) \
	CFLAGS='$(filter-out -fPIE -pie,$(CFLAGS)) $(CPPFLAGS)' \
	NOTSHLIBCFLAGS='$(filter -fPIE -pie,$(CFLAGS))' \
	LDFLAGS='$(filter-out -fPIE -pie,$(LDFLAGS))' \
	NOTSHLIBLDFLAGS='$(filter -fPIE -pie,$(LDFLAGS))' \
	$(NULL)

sp_options := \
	BR=$(CURDIR)/debian/build/sp \
	$(NULL)

mp_options := \
	BR=$(CURDIR)/debian/build/mp \
	$(NULL)

ifeq ($(DEB_HOST_ARCH),armhf)
options += ARM_VER_CHECK=armv7l
else
options += ARM_VER_CHECK=something_else
endif

override_dh_auto_build:
	install -d debian/build/mp debian/build/sp
	make -C SP -- \
		$(options) \
		$(sp_options) \
		release
	make -C MP -- \
		$(options) \
		$(mp_options) \
		release

override_dh_auto_install:
	make -C SP -- \
		$(options) \
		$(sp_options) \
		copyfiles
	cd debian/tmp/usr/lib/rtcw && ln -s iowolfsp.* iowolfsp
	make -C MP -- \
		$(options) \
		$(mp_options) \
		copyfiles
	cd debian/tmp/usr/lib/rtcw && ln -s iowolfmp.* iowolfmp
	cd debian/tmp/usr/lib/rtcw && ln -s iowolfded.* iowolfded
	install -d debian/tmp/usr/games
	sed \
		-e 's!@IOQ3BINARY@!iowolfsp!' \
		-e 's!@IOQ3SELF@!wolfsp!' \
		-e 's!@IOQ3ROLE@!single-player!' \
		< debian/scripts/rtcw.in > debian/tmp/usr/games/wolfsp
	sed \
		-e 's!@IOQ3BINARY@!iowolfmp!' \
		-e 's!@IOQ3SELF@!wolfmp!' \
		-e 's!@IOQ3ROLE@!multiplayer!' \
		< debian/scripts/rtcw.in > debian/tmp/usr/games/wolfmp
	sed \
		-e 's!@IOQ3BINARY@!iowolfded!' \
		-e 's!@IOQ3SELF@!wolfded!' \
		-e 's!@IOQ3ROLE@!multiplayer server!' \
		< debian/scripts/rtcw.in > debian/tmp/usr/games/wolfded
	chmod +x debian/tmp/usr/games/*

ifeq ($(DEB_HOST_ARCH_OS),linux)
override_dh_install-arch:
	dh_install -a
	dh_apparmor -prtcw --profile-name=usr.lib.rtcw
	dh_apparmor -prtcw-server --profile-name=usr.lib.rtcw.iowolfded
endif

override_dh_strip:
	dh_strip --ddeb-migration='rtcw-dbg (<< 1.42d+dfsg1-4~)'

override_dh_clean:
	dh_clean -XSP/code/game/g_func_decs.h.orig -XSP/code/game/g_funcs.h.orig

override_dh_auto_clean:
	make -C SP -- $(options) $(sp_options) clean
	make -C MP -- $(options) $(mp_options) clean
	find debian/build || :
	rm -fr debian/build

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

# Not done automatically to avoid a build-dependency on inkscape
maintainer-update-icons: \
		debian/32/wolfsp.png \
		debian/48/wolfsp.png \
		debian/32/wolfmp.png \
		debian/48/wolfmp.png \
		$(NULL)
	inkscape -C -w 32 -h 32 --export-png=debian/32/wolfsp.png debian/wolfsp.svg
	inkscape -C -w 48 -h 48 --export-png=debian/48/wolfsp.png debian/wolfsp.svg
	inkscape -C -w 32 -h 32 --export-png=debian/32/wolfmp.png debian/wolfmp.svg
	inkscape -C -w 48 -h 48 --export-png=debian/48/wolfmp.png debian/wolfmp.svg

ORIG_SOURCE = iortcw
ORIG_REPO ?= https://github.com/iortcw/iortcw
ORIG_REL = 1.50a
ORIG_REV = 1.5a
ORIG_DATE =
ORIG_SUFFIX = +dfsg1

ORIG_EXPORT := ${ORIG_REV}

ifeq (${ORIG_DATE},)
	ORIG_VER := ${ORIG_REL}${ORIG_SUFFIX}
else
	ORIG_VER := ${ORIG_REL}+${ORIG_DATE}${ORIG_SUFFIX}
endif

get-orig-source:
	git clone --bare --depth=1 ${ORIG_REPO} ${ORIG_SOURCE}-${ORIG_VER}.git
	GIT_DIR=${ORIG_SOURCE}-${ORIG_VER}.git git fetch ${ORIG_REPO} ${ORIG_EXPORT}
	GIT_DIR=${ORIG_SOURCE}-${ORIG_VER}.git git archive \
		--format=tar \
		--prefix=${ORIG_SOURCE}-${ORIG_VER}.orig/ \
		${ORIG_EXPORT} | tar -xf-
	test -e ${ORIG_SOURCE}-${ORIG_VER}.orig/MP/COPYING.txt || wget -O ${ORIG_SOURCE}-${ORIG_VER}.orig/MP/COPYING.txt https://raw.githubusercontent.com/id-Software/RTCW-MP/master/COPYING.txt
	test -e ${ORIG_SOURCE}-${ORIG_VER}.orig/SP/COPYING.txt || wget -O ${ORIG_SOURCE}-${ORIG_VER}.orig/SP/COPYING.txt https://raw.githubusercontent.com/id-Software/RTCW-SP/master/COPYING.txt
	: # if in the Debian checkout, check that they haven't changed
	if [ -e debian/rtcw.install ]; then \
		e=0; \
		diff -u MP/COPYING.txt ${ORIG_SOURCE}-${ORIG_VER}.orig/MP/COPYING.txt || e=$$?; \
		diff -u SP/COPYING.txt ${ORIG_SOURCE}-${ORIG_VER}.orig/SP/COPYING.txt || e=$$?; \
		exit $$e; \
	fi
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/AL
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/SDL2
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/curl-*
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/freetype-*
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/jpeg-*
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/libcurl-*
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/libogg-*
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/libs
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/libvorbis-*
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/opus-*
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/opusfile-*
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/tools/lcc
	# Preserve ioapi.? and unzip.? (minizip) because they've been modified
	# to use the idTech3 memory-pool implementation, to avoid leaks when
	# loading a level is aborted. Delete the rest of zlib
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/zlib-*/adler32.?
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/zlib-*/crc32.?
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/zlib-*/crypt.?
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/zlib-*/gzguts.?
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/zlib-*/inffast.?
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/zlib-*/inffixed.?
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/zlib-*/inflate.?
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/zlib-*/inftrees.?
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/zlib-*/zconf.?
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/zlib-*/zlib.?
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/code/zlib-*/zutil.?
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig/*/media
	tar --xz -cvf ${ORIG_SOURCE}_${ORIG_VER}.orig.tar.xz ${ORIG_SOURCE}-${ORIG_VER}.orig
	rm -rf ${ORIG_SOURCE}-${ORIG_VER}.orig
	rm -rf ${ORIG_SOURCE}-${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 ${ORIG_SOURCE}_${ORIG_VER}.orig.tar.xz ../build-area/
	@echo "try using:"
	@echo "gbp import-orig ../build-area/${ORIG_SOURCE}_${ORIG_VER}.orig.tar.xz"
