#!/usr/bin/make -f

# parallel building
parallel=$(shell echo $$DEB_BUILD_OPTIONS | grep parallel > /dev/null && echo -j$$(test -f /proc/cpuinfo && cat /proc/cpuinfo | (grep ^processor || echo 1) | wc -l))

# Hardening flags
CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS:=$(shell dpkg-buildflags --get CFLAGS) $(CPPFLAGS)
CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS) $(CPPFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

%:
	dh $@

override_dh_auto_configure:
	mkdir -p build-debian
	cd build-debian && cmake \
		-DWL_INSTALL_PREFIX=/usr \
		-DWL_INSTALL_DATADIR=share/games/widelands \
		-DWL_INSTALL_BINDIR=games \
		-DWL_INSTALL_LOCALEDIR=/usr/share/games/widelands/locale \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_BUILD_TYPE=Release \
		../ && cat src/config.h

override_dh_auto_build:
	cd build-debian && make VERBOSE=1 ${parallel}

override_dh_auto_install:
	cd build-debian && make install DESTDIR=`pwd`/../debian/tmp
	mkdir -p  debian/tmp/usr/share/applications/
	cp debian/widelands.desktop debian/tmp/usr/share/applications/
	# Take care of the fonts (use system-wide ones)
	rm debian/tmp/usr/share/games/widelands/fonts/FreeSans.ttf
	rm debian/tmp/usr/share/games/widelands/fonts/FreeSerif.ttf
	rm debian/tmp/usr/share/games/widelands/fonts/Widelands/LICENSE
	cd debian/tmp/usr/share/games/widelands/fonts/; ln -s /usr/share/fonts/truetype/freefont/FreeSans.ttf 
	cd debian/tmp/usr/share/games/widelands/fonts/; ln -s /usr/share/fonts/truetype/freefont/FreeSerif.ttf
	# Take care of the logo
	for size in 16 32 48 64 128 ; do \
	  mkdir -p debian/widelands/usr/share/icons/hicolor/$${size}x$${size}/apps ; \
	  cp debian/tmp/usr/share/games/widelands/pics/wl-ico-$${size}.png \
	     debian/widelands/usr/share/icons/hicolor/$${size}x$${size}/apps/widelands.png; \
	done
	convert debian/widelands/usr/share/icons/hicolor/32x32/apps/widelands.png \
	        debian/widelands/usr/share/icons/hicolor/32x32/apps/widelands.xpm

override_dh_install:
	dh_install --list-missing

override_dh_strip:
	dh_strip --dbg-package=widelands-dbg
