#!/usr/bin/make -f
# debian/rules for wesnoth
# copyright 2007-2018 by Rhonda D'Vine <rhonda@debian.org>
# Licenced under MIT

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# This has to be exported to make some magic below work.
export DH_OPTIONS

# for DEB_VERSION* variables
include /usr/share/dpkg/pkg-info.mk

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_BUILD_ARCH  ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)

# less debug info to avoid running out of address space
ifneq (,$(filter $(DEB_HOST_ARCH), mips mipsel))
    CXXFLAGSDBG = -g1
endif

export CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS)
export CFLAGS   := $(shell dpkg-buildflags --get CFLAGS) $(CPPFLAGS) -std=c11
export CXXFLAGS := $(shell dpkg-buildflags --get CXXFLAGS) $(CPPFLAGS) -std=c++11 $(CXXFLAGSDBG)
export LDFLAGS  := $(shell dpkg-buildflags --get LDFLAGS)

ifneq (,$(filter $(DEB_BUILD_ARCH),alpha))
  LDFLAGS += -Wl,--no-relax
endif
ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
        CFLAGS += -O0
else
        CFLAGS += -O2
endif
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif


# used for package name extension
BRANCH_VERSION = $(shell echo $(DEB_SOURCE) | sed 's/^wesnoth-//')
BRANCH_MAJOR   = $(shell echo $(BRANCH_VERSION) | cut -d. -f1)
BRANCH_MINOR   = $(shell echo $(BRANCH_VERSION) | cut -d. -f2)
BRANCH_ISDEV   = $(shell echo $$(( $(BRANCH_MINOR) % 2 )) )

# calculate the version strings
EPOCH_VERSION = $(shell echo $(DEB_VERSION) | cut -d: -f1)
MAJOR_VERSION = $(shell echo $(DEB_VERSION) | cut -d. -f1)
MINOR_VERSION = $(shell echo $(DEB_VERSION) | cut -d. -f2 | cut -d- -f1 | sed -e 's/[^0-9]//g')
ISDEV_VERSION = $(shell echo $$(( $(MINOR_VERSION) % 2 )) )
UPSTR_VERSION = $(DEB_VERSION_EPOCH_UPSTREAM)

# choose correct MIN_VERSION (for >=) and MAX_VERSION (for <<) depending on
# ISDEV_VERSION and BRANCH_ISDEV
ifeq ($(ISDEV_VERSION), 1)
ifeq ($(BRANCH_ISDEV), 1)
	# e.g. wesnoth-1.17 (1:1.17.0-1)
	# Depends: wesnoth-1.17 (<< 1:1.17.0.~), wesnoth-1.17 (>= 1:1.17.0)
	MIN_VERSION = $(UPSTR_VERSION)
	MAX_VERSION = $(UPSTR_VERSION).~
else
	# e.g. wesnoth-1.18 (1:1.17.26-1)
	# Depends: wesnoth-1.18 (<< 1:1.19), wesnoth-1.18 (>= 1:1.17.26)
	MIN_VERSION = $(UPSTR_VERSION)
	MAX_VERSION = $(MAJOR_VERSION).$(shell echo $$(( $(MINOR_VERSION) + 2 )) )
endif
else
	# e.g. wesnoth-1.18 (1:18.0-1)
	# Depends: wesnoth-1.18 (<< 1:1.19), wesnoth-1.18 (>= 1:1.18)
	MIN_VERSION = $(MAJOR_VERSION).$(MINOR_VERSION)
	MAX_VERSION = $(MAJOR_VERSION).$(shell echo $$(( $(MINOR_VERSION) + 1 )) )
endif

CMAKE_SWITCHES = \
	-DENABLE_SYSTEM_LUA=on \
	-DBINARY_SUFFIX="-$(BRANCH_VERSION)" \
	-DCMAKE_INSTALL_PREFIX="/usr/" \
	-DCMAKE_INSTALL_BINDIR="/usr/games" \
	-DCMAKE_INSTALL_DATAROOTDIR="/usr/share/games" \
	-DDATADIRNAME="wesnoth/$(BRANCH_VERSION)" \
	-DCMAKE_INSTALL_DOCDIR="/usr/share/doc/wesnoth-$(BRANCH_VERSION)-data" \
	-DLOCALEDIR="locale" \
	-DCMAKE_INSTALL_MANDIR="/usr/share/man" \
	-DPREFERENCES_DIR=".config/wesnoth-$(BRANCH_VERSION)" \
	-DFIFO_DIR=/run/wesnothd/$(BRANCH_VERSION) \
	-DCMAKE_VERBOSE_MAKEFILE=on

# Heads up: dh_installsystemd only recognizes and generates maintscript stanzas for unit files under /usr/lib as of debhelper >= 13.11.6
# If backporting to a sufficiently older version of Debian/Ubuntu with an older debhelper, move this back to /lib
SYSTEMD_SERVICE = debian/wesnoth-$(BRANCH_VERSION)-server/usr/lib/systemd/system/wesnoth-$(BRANCH_VERSION)-server.service

%:
	dh $@ --buildsystem=cmake --builddirectory=build

override_dh_auto_configure:
	dh_auto_configure -- $(CMAKE_SWITCHES)

override_dh_auto_test:
	# sets "SDL_AUDIODRIVER=dummy" so the SDL_Mixer test works under sbuild,
	# unlike the regular "tests" target.
	dh_auto_build -- conftests

override_dh_install:
	# helpheader.html is not referenced by anything installed in the binary
	# packages and it causes lintian to complain about embedded jquery and
	# privacy-breach-generic warnings, so avoid installing it.
	dh_install -Xgitignore -Xhelpheader.html -Xhelptrailer.html

execute_after_dh_install-arch:
	# manpage symlinks for old binaries
	for i in debian/wesnoth-$(BRANCH_VERSION)/usr/share/man/*/man6/wesnoth.6; do \
		ln -s wesnoth-$(BRANCH_VERSION).6.gz \
			`dirname $$i`/wesnoth-$(BRANCH_VERSION)-smallgui.6.gz; \
		ln -s wesnoth-$(BRANCH_VERSION).6.gz \
			`dirname $$i`/wesnoth-$(BRANCH_VERSION)_editor.6.gz; \
	done
	ln -s wesnoth-$(BRANCH_VERSION).6.gz \
		debian/wesnoth-$(BRANCH_VERSION)/usr/share/man/man6/wesnoth-$(BRANCH_VERSION)-smallgui.6.gz
	ln -s wesnoth-$(BRANCH_VERSION).6.gz \
		debian/wesnoth-$(BRANCH_VERSION)/usr/share/man/man6/wesnoth-$(BRANCH_VERSION)_editor.6.gz
	
	# move the manpages to their proper name
	for i in debian/wesnoth-$(BRANCH_VERSION)/usr/share/man/man6/wesnoth.6 \
		debian/wesnoth-$(BRANCH_VERSION)/usr/share/man/*/man6/wesnoth.6; do \
		mv $$i $$(echo $$i | sed -e 's/wesnoth\.6/wesnoth-$(BRANCH_VERSION).6/'); \
	done
	for i in debian/wesnoth-$(BRANCH_VERSION)-server/usr/share/man/man6/wesnothd.6 \
		debian/wesnoth-$(BRANCH_VERSION)-server/usr/share/man/*/man6/wesnothd.6; do \
		mv $$i $$(echo $$i | sed -e 's/wesnothd\.6/wesnothd-$(BRANCH_VERSION).6/'); \
	done
	
	# move the desktop and metainfo files to their proper name
	mv debian/wesnoth-$(BRANCH_VERSION)/usr/share/applications/org.wesnoth.Wesnoth.desktop \
		debian/wesnoth-$(BRANCH_VERSION)/usr/share/applications/org.wesnoth.Wesnoth-$(BRANCH_VERSION).desktop
	mv debian/wesnoth-$(BRANCH_VERSION)/usr/share/metainfo/org.wesnoth.Wesnoth.appdata.xml \
		debian/wesnoth-$(BRANCH_VERSION)/usr/share/metainfo/org.wesnoth.Wesnoth-$(BRANCH_VERSION).appdata.xml
	# add branch name to desktop and metainfo entries
	sed -i -e "/^Name/ s/$$/ ($(BRANCH_VERSION))/" \
		debian/wesnoth-$(BRANCH_VERSION)/usr/share/applications/*.desktop
	sed -i -e "s/Wesnoth.desktop/Wesnoth-$(BRANCH_VERSION).desktop/" \
		debian/wesnoth-$(BRANCH_VERSION)/usr/share/metainfo/*.xml
	
	# /usr/share/doc symlinks
	for i in wesnoth-$(BRANCH_VERSION) ; do \
		install -p -d -m755 debian/$$i/usr/share/doc; \
		ln -s wesnoth-$(BRANCH_VERSION)-data debian/$$i/usr/share/doc/$$i; \
	done

execute_after_dh_install-indep:
	# workaround for dh_link (mis)behavior, see BTS#445550
	for i in dejavu/DejaVuSans.ttf dejavu/DejaVuSans-Bold.ttf dejavu/DejaVuSansMono.ttf dejavu/DejaVuSansMono-Bold.ttf dejavu/DejaVuSans-Oblique.ttf droid/DroidSansFallbackFull.ttf lato/Lato-Bold.ttf lato/Lato-Italic.ttf lato/Lato-Medium.ttf lato/Lato-MediumItalic.ttf lato/Lato-Light.ttf lato/Lato-LightItalic.ttf lato/Lato-Regular.ttf lato/Lato-BlackItalic.ttf lato/Lato-Black.ttf lato/Lato-BoldItalic.ttf lato/Lato-Bold.ttf lato/Lato-HairlineItalic.ttf lato/Lato-Hairline.ttf lato/Lato-HeavyItalic.ttf lato/Lato-Heavy.ttf lato/Lato-SemiboldItalic.ttf lato/Lato-Semibold.ttf lato/Lato-ThinItalic.ttf lato/Lato-Thin.ttf adf/OldaniaADFStd-BoldItalic.otf adf/OldaniaADFStd-Bold.otf adf/OldaniaADFStd-Italic.otf adf/OldaniaADFStd-Regular.otf lohit-bengali/Lohit-Bengali.ttf; \
		do ln -sf /usr/share/fonts/truetype/$$i \
	       	debian/wesnoth-$(BRANCH_VERSION)-data/usr/share/games/wesnoth/$(BRANCH_VERSION)/fonts/`basename $$i`; \
	done
	
	# rename icons versioned
	for i in 128x128/apps/wesnoth-icon.png 16x16/apps/wesnoth-icon.png 256x256/apps/wesnoth-icon.png 32x32/apps/wesnoth-icon.png 512x512/apps/wesnoth-icon.png 64x64/apps/wesnoth-icon.png; \
		do mv debian/wesnoth-$(BRANCH_VERSION)-data/usr/share/icons/hicolor/$$i \
		debian/wesnoth-$(BRANCH_VERSION)-data/usr/share/icons/hicolor/`dirname $$i`/wesnoth-$(BRANCH_VERSION)-icon.png; \
	done
	mv debian/wesnoth-$(BRANCH_VERSION)-data/usr/share/icons/HighContrast/scalable/apps/wesnoth-icon.svg \
		debian/wesnoth-$(BRANCH_VERSION)-data/usr/share/icons/HighContrast/scalable/apps/wesnoth-$(BRANCH_VERSION)-icon.svg
	
	# Drop data/tools/wesnoth_addon_manager (standalone client for addon devs to
	# upload/download WML content from addon server, no impact on in-game addon
	# manager) from wesnoth-tools due to embedded code copies.
	# https://lists.debian.org/debian-devel-games/2024/02/msg00006.html
	rm -rf debian/wesnoth-$(BRANCH_VERSION)-tools/usr/share/games/wesnoth/$(BRANCH_VERSION)/data/tools/addon_manager/
	rm -f debian/wesnoth-$(BRANCH_VERSION)-tools/usr/share/games/wesnoth/$(BRANCH_VERSION)/data/tools/wesnoth_addon_manager
	
	# lintian error tag package-contains-eslint-config-file
	rm -f debian/wesnoth-$(BRANCH_VERSION)-tools/usr/share/games/wesnoth/$(BRANCH_VERSION)/utils/wesnoth-map-diff/.eslintrc.js
	
	# /usr/share/doc symlinks
	for i in wesnoth wesnoth-core wesnoth-music; do \
		install -p -d -m755 debian/$$i/usr/share/doc; \
		ln -s wesnoth-$(BRANCH_VERSION)-data debian/$$i/usr/share/doc/$$i; \
	done

override_dh_link:
	# see BTS#445550

override_dh_installchangelogs:
	dh_installchangelogs --no-trim changelog.md

override_dh_installsystemd:
	install -d -m755 $$(dirname $(SYSTEMD_SERVICE))
	sed -E \
		-e 's|@BINARY_SUFFIX@||g' \
		-e 's|@FIFO_DIR@|/run/wesnothd|g' \
		-e 's|@CMAKE_INSTALL_FULL_BINDIR@|/usr/games|g' \
		-e 's|(Description[ \t]*=[ \t]*Wesnoth)|\1 $(BRANCH_VERSION)|' \
		-e 's|(Documentation[ \t]*=[ \t]*man:wesnothd)|\1-$(BRANCH_VERSION)|' \
		-e 's|(ExecStart[ \t]*=.*/wesnothd)|\1-$(BRANCH_VERSION)|' \
		-e 's|(SyslogIdentifier[ \t]*=.*)|\1-$(BRANCH_VERSION)|' \
		packaging/systemd/wesnothd.service.in \
		1>$(SYSTEMD_SERVICE)
	# make sure we don't miss any substitutions, in case of upstream changes
	! grep -E -- '@[A-Z0-9_]+@' $(SYSTEMD_SERVICE)
	grep -q -- 'Description[ \t]*=[ \t]*Wesnoth $(BRANCH_VERSION)' $(SYSTEMD_SERVICE)
	grep -q -- 'Documentation[ \t]*=[ \t]*man:wesnothd-$(BRANCH_VERSION)' $(SYSTEMD_SERVICE)
	grep -q -- 'ExecStart[ \t]*=.*/wesnothd-$(BRANCH_VERSION)' $(SYSTEMD_SERVICE)
	grep -q -- 'SyslogIdentifier[ \t]*=.*-$(BRANCH_VERSION)' $(SYSTEMD_SERVICE)
	dh_installsystemd --no-start --no-enable

override_dh_gencontrol:
	dh_gencontrol -- \
		-Vwesnoth:Min-Version=$(MIN_VERSION) \
		-Vwesnoth:Max-Version=$(MAX_VERSION) \
		-Vwesnoth:Branch-Version=$(BRANCH_VERSION)