#!/usr/bin/make -f
# debian/rules for wesnoth
# copyright 2007-2011 by Gerfried Fuchs <rhonda@debian.org>
# Licenced under WTFPLv2

# 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

# 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)

export CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS)
export CFLAGS   := $(shell dpkg-buildflags --get CFLAGS) $(CPPFLAGS)
export CXXFLAGS := $(shell dpkg-buildflags --get CXXFLAGS) $(CPPFLAGS)
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 dpkg-parsechangelog | grep "^Version:" | cut -d" " -f2 | cut -d. -f1)
STRIP_MAJOR   = $(shell echo $(MAJOR_VERSION) | rev | cut -d: -f1 | rev)
MINOR_VERSION = $(shell dpkg-parsechangelog | grep "^Version:" | cut -d" " -f2 | cut -d. -f2 | cut -d- -f1 | sed -e 's/[^0-9]//g')
ISDEV_VERSION = $(shell echo $$(( $(MINOR_VERSION) % 2 )) )
UPSTR_VERSION =  $(shell dpkg-parsechangelog | grep "^Version:" | cut -d" " -f2 | rev | cut -d- -f2 | rev)

## 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/" -DBINDIR="/usr/games" -DDATAROOTDIR="/usr/share/games" -DDATADIRNAME="wesnoth/$(BRANCH_VERSION)" -DDESKTOPDIR="/usr/share/applications" -DDOCDIR="/usr/share/doc/wesnoth-$(BRANCH_VERSION)-data" -DLOCALEDIR="locale" -DMANDIR="/usr/share/man" -DUSE_ANA_NETWORK=OFF -DCMAKE_VERBOSE_MAKEFILE=on -DENABLE_OMP=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:
	mkdir -p build
	cd build && \
		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

	cd $(CURDIR)/debian/tmp/usr/share/games/wesnoth/$(BRANCH_VERSION)/data/tools \
		&& chmod +x extractbindings unit_tree/TeamColorizer \
		wesnoth/wescamp.py wesnoth/wmldata.py wesnoth/wmlparser.py \
		wmlindent wmlflip wmllint wmlscope wesnoth_addon_manager \
		wmlvalidator hexometer/hexometer wmlxgettext imgcheck

	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
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_lintian
	dh_pysupport
	dh_installinit -u 'stop 20 0 1 2 3 4 5 6 .'
	dh_installman
	dh_strip --dbg-package=wesnoth-$(BRANCH_VERSION)-dbg
	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 droid/DroidSansJapanese.ttf droid/DroidSansFallbackFull.ttf; \
		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
	mv debian/wesnoth-$(BRANCH_VERSION)-data/usr/share/icons/hicolor/64x64/apps/wesnoth_editor-icon.png \
		debian/wesnoth-$(BRANCH_VERSION)-data/usr/share/icons/hicolor/64x64/apps/wesnoth-$(BRANCH_VERSION)_editor-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)-nolog.6.gz; \
		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)-nolog.6.gz
	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 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/applications/wesnoth_editor.desktop \
		debian/wesnoth-$(BRANCH_VERSION)-core/usr/share/applications/wesnoth-$(BRANCH_VERSION)_editor.desktop
	# add branch name to desktop entries
	sed -i -e "/^Name/ s/$$/ ($(BRANCH_VERSION))/" \
		debian/wesnoth-$(BRANCH_VERSION)-core/usr/share/applications/*.desktop


	# /usr/share/doc symlinks
	for i in wesnoth-$(BRANCH_VERSION)-core wesnoth-$(BRANCH_VERSION)-dbg; 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=-s binary-common


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