#!/usr/bin/make -f

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

DEB_LDFLAGS_MAINT_APPEND  += -Wl,-z,defs
export DEB_LDFLAGS_MAINT_APPEND
DPKG_EXPORT_BUILDFLAGS = yes
include /usr/share/dpkg/default.mk
export CFLAGS += $(CPPFLAGS)

%:
	dh $@

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

# YQ2_ARCH is meant to be uname -m, except that i?86, amd64 and arm*
# are normalized to i386, x86_64 and arm respectively; the value matters,
# because it appears in saved games. Newer CPU architectures tend to have
# the GNU CPU name be the same as the Linux uname -m, because anything
# else would be pointless complication.
ifeq ($(DEB_HOST_ARCH_CPU),i386)
# Don't use i686 here
YQ2_ARCH=i386
else ifeq ($(DEB_HOST_ARCH_CPU),powerpc)
# Linux and GNU disagree on the canonical name of this architecture
YQ2_ARCH=ppc
else ifeq ($(DEB_HOST_ARCH_CPU),ppc64el)
# Linux and GNU disagree on the canonical name of this architecture
YQ2_ARCH=ppc64le
else
# This is the same as uname -m on most architectures. On ARM it's just
# "arm" which is what we wanted anyway.
YQ2_ARCH=$(DEB_HOST_GNU_CPU)
endif

# Similarly, YQ2_OSTYPE is meant to be uname -s.
ifeq ($(DEB_HOST_ARCH_OS),linux)
YQ2_OSTYPE=Linux
else ifeq ($(DEB_HOST_ARCH_OS),kfreebsd)
YQ2_OSTYPE=GNU/kFreeBSD
else ifeq ($(DEB_HOST_ARCH_OS),hurd)
YQ2_OSTYPE=GNU
else
$(error Set YQ2_OSTYPE to the value of uname -s on $(DEB_HOST_ARCH))
endif

subdirs = subdir-ctf subdir-rogue subdir-xatrix

override_dh_auto_build: $(subdirs)
	dh_auto_build -- \
		VERBOSE=1 \
		WITH_RPATH=no \
		WITH_SYSTEMWIDE=yes \
		YQ2_ARCH=$(YQ2_ARCH) \
		YQ2_OSTYPE=$(YQ2_OSTYPE) \
		${NULL}

.PHONY: $(subdirs)
$(subdirs): subdir-%:
	dh_auto_build --sourcedirectory=$* -- \
		VERBOSE=1 \
		YQ2_ARCH=$(YQ2_ARCH) \
		YQ2_OSTYPE=$(YQ2_OSTYPE) \
		${NULL}

override_dh_install:
	install -d debian/tmp/ctf
	install -d debian/tmp/rogue
	install -d debian/tmp/xatrix
	install -m644 CHANGELOG debian/tmp/NEWS
	install -m644 ctf/CHANGELOG debian/tmp/ctf/NEWS
	install -m644 rogue/CHANGELOG debian/tmp/rogue/NEWS
	install -m644 xatrix/CHANGELOG debian/tmp/xatrix/NEWS
	dh_install

override_dh_installchangelogs:
	dh_installchangelogs -XCHANGELOG

override_dh_installdocs:
	dh_installdocs README.md doc/*.md