#!/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) -I/usr/include/wolfssl -DOPENSSL_ALL
export CFLAGS   := $(shell dpkg-buildflags --get CFLAGS) $(CPPFLAGS) -std=c++11 -fopenmp
export CXXFLAGS := $(shell dpkg-buildflags --get CXXFLAGS) $(CPPFLAGS) -std=c++11 -fopenmp  $(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


# calculate the version strings
MAJOR_VERSION = $(shell echo $(DEB_VERSION) | cut -d. -f1)
STRIP_MAJOR   = $(shell echo $(MAJOR_VERSION) | rev | cut -d: -f1 | rev)
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/MAX_VERSION depending on ISDEV_VERSION
ifeq ($(ISDEV_VERSION), 1)
	MIN_VERSION = $(UPSTR_VERSION)
	MAX_VERSION = $(UPSTR_VERSION).~
else
	MIN_VERSION = $(MAJOR_VERSION).$(MINOR_VERSION)
	MAX_VERSION = $(MAJOR_VERSION).$(shell echo $$(( $(MINOR_VERSION) + 1 )) )
endif

## used for package name extension
BRANCH_VERSION = $(STRIP_MAJOR).$(MINOR_VERSION)

CONFIGURE_SWITCHES = --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --disable-strict-compilation --disable-maintainer-mode --disable-rpath --prefix=/usr --mandir=\$${prefix}/share/man --bindir=\$${prefix}/games --with-datadir-name=wesnoth/$(BRANCH_VERSION) --with-localedir=\$${prefix}/share/games/wesnoth/$(BRANCH_VERSION)/locale --with-fifodir=/var/run/wesnothd --datadir=\$${prefix}/share/games --enable-server --with-fribidi --enable-python-install

CMAKE_SWITCHES = -DBINARY_SUFFIX="-$(BRANCH_VERSION)" -DCMAKE_INSTALL_PREFIX="/usr/" -DCMAKE_INSTALL_BINDIR="/usr/games" -DCMAKE_INSTALL_DATAROOTDIR="/usr/share/games" -DDATADIRNAME="wesnoth/$(BRANCH_VERSION)" -DDESKTOPDIR="/usr/share/applications" -DCMAKE_INSTALL_DOCDIR="/usr/share/doc/wesnoth-$(BRANCH_VERSION)-data" -DLOCALEDIR="locale" -DCMAKE_INSTALL_MANDIR="/usr/share/man" -DPREFERENCES_DIR=".config/wesnoth-$(BRANCH_VERSION)" -DUSE_ANA_NETWORK=OFF -DCMAKE_VERBOSE_MAKEFILE=on


clean:
	dh_testdir
	dh_testroot
	rm -f debian/stamp-*
	# Add here commands to clean up after the build process.
	rm -rf build locale # locale doesn't get generated inside build ...
	dh_clean


configure: debian/stamp-configure
debian/stamp-configure:
	dh_auto_configure --builddirectory=build --buildsystem=cmake -- $(CMAKE_SWITCHES)
	# CFLAGS="$(CFLAGS)" LDFLAGS="-Wl,-z,defs" ./configure $(CONFIGURE_SWITCHES)
	touch $@


build: build-arch build-indep
build-arch: debian/stamp-build
build-indep: debian/stamp-build

debian/stamp-build: debian/stamp-configure
	$(MAKE) -C build
	touch $@


install: debian/stamp-install
debian/stamp-install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	$(MAKE) -C build DESTDIR=$(CURDIR)/debian/tmp install

	dh_install -Xgitignore

	touch $@



# Must not depend on anything. This is to be called by
# binary-arch/binary-indep
# in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs changelog.md
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_lintian
	dh_installinit
	dh_installsystemd --no-start --no-enable
	dh_installman
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol -- \
		-Vwesnoth:Min-Version=$(MIN_VERSION) \
		-Vwesnoth:Max-Version=$(MAX_VERSION) \
		-Vwesnoth:Branch-Version=$(BRANCH_VERSION)
	dh_md5sums
	dh_builddeb


# Build architecture independant packages using the common target.
binary-indep: install
	# 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; \
		do ln -s /usr/share/fonts/truetype/$$i \
	       	debian/wesnoth-$(BRANCH_VERSION)-data/usr/share/games/wesnoth/$(BRANCH_VERSION)/fonts/`basename $$i`; \
		done

	# rename icons versioned

	mv debian/wesnoth-$(BRANCH_VERSION)-data/usr/share/icons/hicolor/64x64/apps/wesnoth-icon.png \
		debian/wesnoth-$(BRANCH_VERSION)-data/usr/share/icons/hicolor/64x64/apps/wesnoth-$(BRANCH_VERSION)-icon.png

	# /usr/share/doc symlinks
	for i in wesnoth wesnoth-core wesnoth-music 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

	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: install
	# manpage symlinks for old binaries
	for i in debian/wesnoth-$(BRANCH_VERSION)-core/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)-core/usr/share/man/man6/wesnoth-$(BRANCH_VERSION)-smallgui.6.gz
	ln -s wesnoth-$(BRANCH_VERSION).6.gz \
		debian/wesnoth-$(BRANCH_VERSION)-core/usr/share/man/man6/wesnoth-$(BRANCH_VERSION)_editor.6.gz

	# move the manpages to their proper name
	for i in debian/wesnoth-$(BRANCH_VERSION)-core/usr/share/man/man6/wesnoth.6 \
		debian/wesnoth-$(BRANCH_VERSION)-core/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)-core/usr/share/applications/wesnoth.desktop \
		debian/wesnoth-$(BRANCH_VERSION)-core/usr/share/applications/wesnoth-$(BRANCH_VERSION).desktop
	mv debian/wesnoth-$(BRANCH_VERSION)-core/usr/share/metainfo/wesnoth.appdata.xml \
		debian/wesnoth-$(BRANCH_VERSION)-core/usr/share/metainfo/wesnoth-$(BRANCH_VERSION).appdata.xml
	# add branch name to desktop and metainfo entries
	sed -i -e "/^Name/ s/$$/ ($(BRANCH_VERSION))/" \
		debian/wesnoth-$(BRANCH_VERSION)-core/usr/share/applications/*.desktop
	sed -i -e "s/wesnoth.desktop/wesnoth-$(BRANCH_VERSION).desktop/" \
		debian/wesnoth-$(BRANCH_VERSION)-core/usr/share/metainfo/*.xml


	# /usr/share/doc symlinks
	for i in wesnoth-$(BRANCH_VERSION)-core ; do \
		install -p -d -m755 debian/$$i/usr/share/doc; \
		ln -s wesnoth-$(BRANCH_VERSION)-data debian/$$i/usr/share/doc/$$i; \
		done

	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common


binary: binary-arch binary-indep
.PHONY: clean configure build binary-indep binary-arch binary install patch unpatch
