#!/usr/bin/make -f
#export DH_VERBOSE=1
#export DEB_BUILD_MAINT_OPTIONS=hardening=+bindnow
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
PKG := freeorion

%:
	dh $@ --parallel --max-parallel=4 --sourcedirectory=FreeOrion --with python2

override_dh_clean:
	dh_clean debian/*.png debian/*.xpm *.cfg FreeOrion/GG/GG/Config.h
	$(RM) -rv usr

override_dh_auto_configure:
	dh_auto_configure --sourcedirectory=FreeOrion/GG -- \
            -DBUILD_DOCUMENTATION=off \
            -DBUILD_TUTORIALS=off \
            -DBUILD_OGRE_DRIVER=ON \
            -DBUILD_OGRE_OIS_PLUGIN=ON \
            -DLIB_SUFFIX=/freeorion \
            -DCMAKE_SHARED_LINKER_FLAGS="-lltdl -Wl,--as-needed $(LDFLAGS)"
	dh_auto_build     --sourcedirectory=FreeOrion/GG
	dh_auto_install   --sourcedirectory=FreeOrion/GG --destdir=$(CURDIR)
	dh_auto_test      --sourcedirectory=FreeOrion/GG
	dh_auto_clean     --sourcedirectory=FreeOrion/GG
	CMAKE_INCLUDE_PATH=$(CURDIR)/usr/include \
        CMAKE_LIBRARY_PATH=$(CURDIR)/usr/lib/freeorion \
        dh_auto_configure

override_dh_auto_build:
	## generate icons
	convert -monitor FreeOrion/FreeOrion.ico debian/freeorion.png
	mv debian/freeorion-0.png debian/freeorion.png
	convert -monitor -resize 32x32 debian/freeorion.png debian/freeorion.xpm
	dh_auto_build

#workaround for 'dpkg-shlibdeps: error: couldn't find library libGiGi*.so'
override_dh_shlibdeps:
	LD_LIBRARY_PATH="$(CURDIR)/usr/lib/freeorion:$(LD_LIBRARY_PATH)" dh_shlibdeps

override_dh_installchangelogs:
	dh_installchangelogs FreeOrion/changelog.txt

override_dh_install:
	dh_install
	find debian -type d -empty -delete -printf 'removed %p\n'
	$(RM) -v debian/freeorion-data/usr/share/games/freeorion/default/LICENSE* \
                 debian/freeorion-data/usr/share/games/freeorion/default/COPYING
	## use the correct path to the Ogre 1.8 installation
	sed -i 's:PluginFolder=\.:PluginFolder=/usr/lib/$(DEB_HOST_MULTIARCH)/OGRE-1.8.0:' \
            $(CURDIR)/debian/$(PKG)/usr/lib/$(PKG)/ogre_plugins.cfg

override_dh_python2:
	dh_python2 -pfreeorion-data /usr/share/games

override_dh_builddeb:
	dh_builddeb -- -Zxz

## get-orig-source
VER := $(shell dpkg-parsechangelog -l$(dir $(MAKEFILE_LIST))changelog | perl -ne 'print $$1 if m{Version:\s*(\d[\d.]*(?:\+svn[\d]\d*))}')
REV := $(shell echo $(VER) | perl -ne 'print $$1 if m/\+svn(\d+)/')

get-orig-source: $(PKG)_$(VER).orig.tar.xz
	@

$(PKG)_$(VER).orig.tar.xz:
	svn checkout --config-option config:miscellany:use-commit-times=yes -r $(REV) \
            http://freeorion.svn.sourceforge.net/svnroot/freeorion/trunk $(PKG)-$(VER) \
        || $(RM) -r $(PKG)-$(VER)
	@echo "Clean-up..."
	cd $(PKG)-$(VER) \
        && find . -depth -name ".svn" -exec $(RM) -r '{}' \; \
        && $(RM) -r -v \
            FreeOrion/GG/libltdl \
            FreeOrion/loki_setup \
            FreeOrion/linux-static \
            FreeOrion/cmake/FindBullet.cmake \
            FreeOrion/OIS \
            FreeOrion/client/human/OgrePlugins \
            FreeOrion/Installer \
            FreeOrion/msvc2010 \
            FreeOrion/Xcode \
            FreeOrion/default/*.ttf
	@echo "Packing..."
	find -L "$(PKG)-$(VER)" -xdev -type f -print \
        | sort | XZ_OPT="-7v" tar -caf "$(PKG)_$(VER).orig.tar.xz" -T- --owner=root --group=root --mode=a+rX \
        && $(RM) -r "$(PKG)-$(VER)"
