#!/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 $@ --parallel -Dsource

# Build in dh_auto_install target because Makefile
# decides to rebuild if flags change (DESTDIR)
override_dh_auto_build:

# Requires X
override_dh_auto_test:

override_dh_auto_install:
	# this is very special. LDFLAGS must not be set or there is a segfault when generating the tiles.
	bash -c "unset LDFLAGS && cd source && DESTDIR=../debian/stone-soup make TILES=y prefix=/usr DATADIR=/usr/share/games/stone-soup SAVEDIR='~/.config/stone-soup' install"
	#dh_auto_install -- TILES=y prefix=/usr DATADIR=/usr/share/games/stone-soup SAVEDIR='~/.config/stone-soup' install
	# We need the executable in a different directory.
	mv debian/stone-soup/usr/games/crawl debian/stone-soup/usr/lib/games/stone-soup/crawl

	cd debian/stone-soup && rm usr/share/games/stone-soup/docs/license/libpng-LICENSE.txt \
	                           usr/share/games/stone-soup/docs/license/lualicense.txt \
	                           usr/share/games/stone-soup/docs/license/pcre_license.txt && \
	                        chmod -x usr/share/games/stone-soup/dat/tiles/title_psiweapon_kiku.png

override_dh_install:
	dh_install --list-missing

override_dh_installchangelogs:
	dh_installchangelogs docs/changelog.txt

## 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 = https://github.com/crawl/crawl.git
$(PKG)_$(VER).orig.tar.xz: $(info I: $(PKG)_$(VER))
	$(if $(wildcard $(PKG)-$(VER)),$(error $(PKG)-$(VER) exist, aborting..))
	@echo "# Downloading..."
	git clone --recursive $(UURL) $(PKG)-$(VER) \
	|| $(RM) -r $(PKG)-$(VER)
	cd $(PKG)-$(VER) \
	&& git checkout $(VER) \
	&& [ -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 version..." \
	&& mv crawl-ref $(PKG)-$(VER) && mv ChangeLog $(PKG)-$(VER) && git describe --tags > $(PKG)-$(VER)/source/util/release_ver \
	&& echo "# Cleaning-up..." \
	&& $(RM) -r .git .git* $(PKG)-$(VER)/.git*
	@echo "# Packing..."
	cd $(PKG)-$(VER) && find -L "$(PKG)-$(VER)" -xdev -type f -print | LC_ALL=C sort \
	| XZ_OPT="-6v" tar -caf "../$(PKG)_$(VER).orig.tar.xz" -T- --owner=root --group=root --mode=a+rX \
	&& cd .. && $(RM) -r "$(PKG)-$(VER)"
