#!/usr/bin/make -f

ifeq ($(findstring parallel=,$(DEB_BUILD_OPTIONS)),)
	export DEB_BUILD_OPTIONS+=parallel=$(shell getconf _NPROCESSORS_ONLN)
endif

export DEB_LDFLAGS_MAINT_APPEND=-lGLU

DEB_HOST_ARCH_OS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)

%:
	dh $@ --buildsystem=cmake --parallel

override_dh_auto_clean:
	$(MAKE) -C lib/irrlicht/source/Irrlicht clean
	
	# Remove embedded libraries from Irrlicht source
	rm -rf lib/irrlicht/source/Irrlicht/jpeglib/
	rm -rf lib/irrlicht/source/Irrlicht/zlib/
	rm -rf lib/irrlicht/source/Irrlicht/libpng/
	rm -rf lib/irrlicht/source/Irrlicht/lzma/
	rm -rf lib/irrlicht/source/Irrlicht/bzip2/
	
	dh_auto_clean

override_dh_auto_configure:
	# Generate sources.cmake
	sh update_file_list.sh
	
	# libbluetooth-dev is only available on linux, hence build with
	# wiimote input device support only on linux
	if [ $(DEB_HOST_ARCH_OS) = linux ]; then \
	dh_auto_configure -- -DSTK_INSTALL_BINARY_DIR="games" \
	                     -DSTK_INSTALL_DATA_DIR="share/games/supertuxkart"; \
	else \
	dh_auto_configure -- -DSTK_INSTALL_BINARY_DIR="games" \
	                     -DSTK_INSTALL_DATA_DIR="share/games/supertuxkart" \
	                     -DUSE_WIIUSE=OFF; \
	fi

override_dh_auto_build:
	# Generate manual page	 
	docbook-to-man debian/supertuxkart.sgml > debian/supertuxkart.6

	# Build embedded irrlicht
	NDEBUG=1 $(MAKE) -C lib/irrlicht/source/Irrlicht staticlib
	
	dh_auto_build

override_dh_install:
	# Licenses are all documented in debian/copyright
	find $(CURDIR)/debian/tmp \( -iname "License.txt" -o -iname \
	"License.Debian.txt" -o -iname "Music_license.rtf" \) -delete
	rm -f $(CURDIR)/debian/tmp/usr/share/games/supertuxkart/data/tracks/overworld/licence2.txt
	
	# cmake installs some unneeded autotools-related files as well
	find $(CURDIR)/debian/tmp \( -iname "Makefile.am" -o -iname \
	"Makefile.in" \) -delete
	
	# Remove unneeded (developer) scripts in data directory
	find $(CURDIR)/debian/tmp \( -iname "run_me.sh" -o -iname \
	"optimize_data.sh" -o -iname "check.sh" \) -delete
	
	# Install xpm icons in /usr/share/pixmaps into /usr/share/icons/hicolor as
	# well. Also install hi-res icon into /usr/share/pixmaps. LP: #937976
	install -m 644 $(CURDIR)/debian/tmp/usr/share/games/supertuxkart/data/gui/karts.png \
	$(CURDIR)/debian/tmp/usr/share/pixmaps/supertuxkart.png
	mkdir -p $(CURDIR)/debian/tmp/usr/share/icons/hicolor/32x32/apps
	install -m 644 $(CURDIR)/debian/tmp/usr/share/pixmaps/supertuxkart_32.png \
	$(CURDIR)/debian/tmp/usr/share/icons/hicolor/32x32/apps/supertuxkart.png
	mkdir -p $(CURDIR)/debian/tmp/usr/share/icons/hicolor/128x128/apps
	install -m 644 $(CURDIR)/debian/tmp/usr/share/pixmaps/supertuxkart_128.png \
	$(CURDIR)/debian/tmp/usr/share/icons/hicolor/128x128/apps/supertuxkart.png
	install -m 644 $(CURDIR)/debian/supertuxkart.xpm \
	$(CURDIR)/debian/tmp/usr/share/icons/hicolor/32x32/apps/supertuxkart.xpm
	
	dh_install

override_dh_strip:
	dh_strip --dbg-package=supertuxkart-dbg
