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

export DEB_BUILD_MAINT_OPTIONS=hardening=+all
export LC_ALL=C.UTF-8
export TZ=UTC

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 $(CURDIR)/debian/q3arch make ${DEB_HOST_GNU_CPU} ${DEB_HOST_GNU_SYSTEM}) \
	$(shell $(CURDIR)/debian/q3arch make-build ${DEB_BUILD_GNU_CPU} ${DEB_BUILD_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)) -Wl,-z,defs' \
	NOTSHLIBLDFLAGS='$(filter -fPIE -pie,$(LDFLAGS))' \
	PKG_CONFIG=${DEB_HOST_GNU_TYPE}-pkg-config \
	$(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
	dh_auto_build --sourcedirectory=SP -- \
		$(options) \
		$(sp_options) \
		release
	dh_auto_build --sourcedirectory=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_clean:
	dh_clean -XSP/code/game/g_func_decs.h.orig -XSP/code/game/g_funcs.h.orig

override_dh_auto_clean:
	dh_auto_clean --sourcedirectory=SP -- $(options) $(sp_options)
	dh_auto_clean --sourcedirectory=MP -- $(options) $(mp_options)
	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
