#!/usr/bin/make -f
PKD   = $(abspath $(dir $(MAKEFILE_LIST)))
PKG   = $(shell dpkg-parsechangelog -l$(PKD)/changelog -SSource)
VER  = $(shell dpkg-parsechangelog -l$(PKD)/changelog -SVersion | cut -d- -f1)

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- -DCMAKE_INSTALL_PREFIX=/usr/ -DCDOGS_DATA_DIR=./

override_dh_install:
	dh_install
	rm -f debian/cdogs-sdl-data/usr/share/games/cdogs/doc/COPYING.*
	rm -f debian/cdogs-sdl-data/usr/share/games/cdogs/doc/LICENSE.*
	rm -f debian/cdogs-sdl-data/usr/share/games/cdogs/doc/license.rlutil.txt
	rm -f debian/cdogs-sdl-data/usr/share/games/cdogs/graphics/LICENSE.md
	rm -f debian/cdogs-sdl-data/usr/share/games/cdogs/missions/sauer2/LICENSE.txt
	rm -f debian/cdogs-sdl-data/usr/share/games/cdogs/sounds/footsteps/license.txt
	[ ! -f debian/cdogs-sdl-data/usr/share/games/cdogs/graphics/make_bullet_spritesheet.sh ] || \
	    chmod +x debian/cdogs-sdl-data/usr/share/games/cdogs/graphics/make_bullet_spritesheet.sh

## http://wiki.debian.org/onlyjob/get-orig-source
.PHONY: get-orig-source
get-orig-source: $(PKG)_$(VER).orig.tar.xz $(info I: $(PKG)_$(VER))
	@

## checkout from git (add "git" to Build-Depends)
UURL = git://github.com/cxong/cdogs-sdl
$(PKG)_$(VER).orig.tar.xz:
	$(if $(wildcard $(PKG)-$(VER)),$(error $(PKG)-$(VER) exist, aborting..))
	@echo "# Downloading..."
	git clone $(UURL) $(PKG)-$(VER) \
	|| $(RM) -r $(PKG)-$(VER)
	cd $(PKG)-$(VER) \
	&& git checkout $(VER) \
	&& git submodule init \
	&& git submodule update --init --recursive \
	&& git submodule update --recursive \
	&& [ -s ChangeLog ] || ( echo "# Generating ChangeLog..." \
	   ; git log --pretty="format:%ad  %aN  <%aE>%n%n%x09* %s%n" --date=short > ChangeLog \
	   ; touch -d "$$(git log -1 --format='%ci')" ChangeLog) \
	&& echo "# Setting times..." \
	&& git ls-tree -r --name-only HEAD | while read F ; do touch --no-dereference -d "$$(git log -1 --format="%ai" -- "$$F")" "$$F"; done \
	&& $(RM) -r .git .git*
	@echo "# Packing..."
	find -L "$(PKG)-$(VER)" -xdev -type f -print | sort \
	| XZ_OPT="-6v" tar -caf "$(PKG)_$(VER)$(DTYPE).orig.tar.xz" -T- --owner=root --group=root --mode=a+rX \
	&& $(RM) -r "$(PKG)-$(VER)"

override_dh_strip:
	dh_strip --no-automatic-dbgsym
	
