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

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

# Flags valid in both CFLAGS and CXXFLAGS
compiler_flags := \
	-Wall \
	-Wno-unused-parameter \
	-Wno-missing-field-initializers \
	-fno-strict-aliasing \
	-Wno-strict-aliasing \
	-Wno-unused-but-set-variable \
	$(NULL)
DEB_CFLAGS_MAINT_APPEND := \
	$(compiler_flags) \
	$(NULL)
DEB_CXXFLAGS_MAINT_APPEND := \
	$(compiler_flags) \
	$(NULL)
# OpenJK assumes that absence of -D_DEBUG implies -DNDEBUG
DEB_CPPFLAGS_MAINT_APPEND := \
	-DNDEBUG \
	$(NULL)
DEB_LDFLAGS_MAINT_APPEND := \
	-lm \
	$(NULL)

include /usr/share/dpkg/default.mk

# OpenJK upstream consider their support for Jedi Knight II: Jedi Outcast
# to be very much experimental, so don't build it for unstable or Ubuntu
# by default. Override with DEB_BUILD_OPTIONS=[no]outcast
ifneq (,$(filter nooutcast,$(DEB_BUILD_OPTIONS)))
$(info Disabling JK2 because DEB_BUILD_OPTIONS=nooutcast)
with_jk2 = OFF
else
ifneq (,$(filter outcast,$(DEB_BUILD_OPTIONS)))
$(info Enabling JK2 because DEB_BUILD_OPTIONS=outcast)
with_jk2 = ON
else
ifeq (,$(filter-out experimental UNRELEASED,$(DEB_DISTRIBUTION)))
$(info Enabling JK2 because this is an experimental or UNRELEASED build)
with_jk2 = ON
else
$(info Disabling JK2 by default)
with_jk2 = OFF
endif
endif
endif

dh_options =
ifneq ($(with_jk2),ON)
dh_options += -Nopenjk-outcast
endif

# for convenience, use a builddir that is excluded by upstream's .gitignore
%:
	dh $@ --builddir=obj --with=systemd --parallel $(dh_options)

override_dh_auto_configure:
	dh_auto_configure -- \
		-DBuildJK2SPEngine=$(with_jk2) \
		-DBuildJK2SPRdVanilla=$(with_jk2) \
		-DBuildJK2SPGame=$(with_jk2) \
		-DBuildTests=ON \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr/lib/openjk \
		-DCMAKE_VERBOSE_MAKEFILE=TRUE \
		$(NULL)

override_dh_auto_build: debian/icons
	dh_auto_build

override_dh_auto_install: debian/icons
	dh_auto_install
	cd debian/tmp/usr/lib/openjk/JediAcademy && ln -s openjk_sp.* openjk_sp
	cd debian/tmp/usr/lib/openjk/JediAcademy && ln -s openjk.* openjk
	cd debian/tmp/usr/lib/openjk/JediAcademy && ln -s openjkded.* openjkded
	install -d debian/tmp/usr/lib/openjk/JediAcademy/base
	rm -fv debian/tmp/usr/lib/openjk/JediAcademy/base/*.so
	cd debian/tmp/usr/lib/openjk/JediAcademy/base && ln -nfsv ../OpenJK/*.so .
	sed \
		-e 's!@IOQ3BINARY@!openjk_sp!' \
		-e 's!@IOQ3SELF@!openjk_sp!' \
		-e 's!@LIBDIR@!openjk/JediAcademy!' \
		-e 's!@DATA@!jedi-academy-data!' \
		-e 's!@IOQ3GAME@!Jedi Academy!' \
		-e 's!@IOQ3ROLE@!single-player!' \
		< debian/scripts/jedi.in > debian/scripts/openjk_sp
	sed \
		-e 's!@IOQ3BINARY@!openjk!' \
		-e 's!@IOQ3SELF@!openjk!' \
		-e 's!@LIBDIR@!openjk/JediAcademy!' \
		-e 's!@DATA@!jedi-academy-data!' \
		-e 's!@IOQ3GAME@!Jedi Academy!' \
		-e 's!@IOQ3ROLE@!multiplayer!' \
		< debian/scripts/jedi.in > debian/scripts/openjk
	sed \
		-e 's!@IOQ3BINARY@!openjkded!' \
		-e 's!@IOQ3SELF@!openjkded!' \
		-e 's!@LIBDIR@!openjk/JediAcademy!' \
		-e 's!@DATA@!jedi-academy-data!' \
		-e 's!@IOQ3GAME@!Jedi Academy!' \
		-e 's!@IOQ3ROLE@!server!' \
		< debian/scripts/jedi.in > debian/scripts/openjkded
ifeq ($(with_jk2),ON)
	cd debian/tmp/usr/lib/openjk/JediOutcast && ln -s openjo_sp.* openjo_sp
	install -d debian/tmp/usr/lib/openjk/JediOutcast/OpenJK
	cd debian/tmp/usr/lib/openjk/JediOutcast/OpenJK && ln -s ../../JediAcademy/OpenJK/*.so .
	install -d debian/tmp/usr/lib/openjk/JediOutcast/base
	rm -fv debian/tmp/usr/lib/openjk/JediOutcast/base/*.so
	cd debian/tmp/usr/lib/openjk/JediOutcast/base && ln -s ../OpenJK/jospgame*.so .
	cd debian/tmp/usr/lib/openjk/JediOutcast/base && ln -s ../../JediAcademy/OpenJK/*.so .
	sed \
		-e 's!@IOQ3BINARY@!openjo_sp!' \
		-e 's!@IOQ3SELF@!openjo_sp!' \
		-e 's!@LIBDIR@!openjk/JediOutcast!' \
		-e 's!@DATA@!jedi-outcast-data!' \
		-e 's!@IOQ3GAME@!Jedi Knight II: Jedi Outcast!' \
		-e 's!@IOQ3ROLE@!single-player!' \
		< debian/scripts/jedi.in > debian/scripts/openjo_sp
endif
	chmod 0755 debian/scripts/openj*

ifeq ($(DEB_HOST_ARCH_OS),linux)
override_dh_install:
	dh_install
	dh_apparmor -popenjk-common --profile-name=usr.games.openjk
	dh_apparmor -popenjk-academy-server --profile-name=usr.games.openjkded
endif

override_dh_installchangelogs:
	dh_installchangelogs CHANGELOG.md

debian/icons: debian/rules
	install -d debian/icons/512 debian/icons/128 \
		debian/icons/64 debian/icons/32 debian/icons/16
	install -m644 shared/icons/OpenJK_Icon_128.png debian/icons/128/openjk.png
	install -m644 shared/icons/OpenJK_Icon_64.png debian/icons/64/openjk.png
	install -m644 shared/icons/OpenJK_Icon_32.png debian/icons/32/openjk.png
	install -m644 shared/icons/OpenJK_Icon_16.png debian/icons/16/openjk.png
	convert -resize 512x512 shared/icons/OpenJK_Icon_1024.png \
		debian/icons/512/openjk.png

# no libraries here, only plugins
override_dh_makeshlibs:
	:

override_dh_strip:
	dh_strip --dbgsym-migration='openjk-dbg (<< 0~20160320+dfsg1-1~)'

override_dh_clean:
	rm -fr debian/icons
	dh_clean

# =====================================================================

ORIG_REPO ?= https://github.com/JACoders/OpenJK
ORIG_REL =
# Empty if it's a release
ORIG_REV = f6d4a7f338b6ba53c684cba1c076713b88a9ae3f
# 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 = 20160320
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.2".
	ORIG_VER := ${ORIG_REL}${ORIG_SUFFIX}
	ORIG_EXPORT := ${ORIG_REL}
else
ifeq (${ORIG_REL},)
	ORIG_VER := 0~${ORIG_DATE}${ORIG_SUFFIX}
	ORIG_EXPORT := ${ORIG_REV}
else
	ORIG_VER := ${ORIG_REL}+${ORIG_DATE}${ORIG_SUFFIX}
	ORIG_EXPORT := ${ORIG_REV}
endif
endif

get-orig-source:
	git clone --bare --branch master ${ORIG_REPO} openjk-${ORIG_VER}.git
	GIT_DIR=openjk-${ORIG_VER}.git git archive \
		--format=tar \
		--prefix=openjk-${ORIG_VER}.orig/ \
		${ORIG_EXPORT} | tar -xvf-
	rm -fr openjk-${ORIG_VER}.orig/code*/client/OpenAL
	rm -fr openjk-${ORIG_VER}.orig/code*/client/eax
	rm -fr openjk-${ORIG_VER}.orig/code*/*.dll
	rm -fr openjk-${ORIG_VER}.orig/lib/*.lib
	rm -fr openjk-${ORIG_VER}.orig/lib/SDL2
	rm -fr openjk-${ORIG_VER}.orig/lib/jpeg-*
	rm -fr openjk-${ORIG_VER}.orig/lib/libpng
	rm -fr openjk-${ORIG_VER}.orig/lib/zlib
	rm -fr openjk-${ORIG_VER}.orig/tools
	tar --xz -cvf openjk_${ORIG_VER}.orig.tar.xz openjk-${ORIG_VER}.orig
	rm -rf openjk-${ORIG_VER}.orig
	rm -rf openjk-${ORIG_VER}.git

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