#!/usr/bin/make -f

# We require some bash features
override SHELL := /bin/bash

include /usr/share/dpkg/default.mk
include /usr/share/dpkg/buildtools.mk

LANGUAGES := en $(patsubst po/%.po,%,$(wildcard po/*.po))

# Files that have version strings substituted in the tarball but never in git
VERSION_SUB_FILES := configure.ac devel.but osx-info.plist puzzles.but version.h

# Commit message for updating them
VERSION_SUB_MSG := "Apply version string substitutions from the tarball"

CFLAGS += -Wno-error=deprecated-declarations

%:
	dh $@ --without autoreconf

# We need to configure and build the executablea twice - once to
# generate the icon files, and a second time using the icon files.

CONFIGURE_ENV := CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS) -DDEBIAN_VERSION=\"\\\"$(DEB_VERSION)\\\"\"" LDFLAGS="$(LDFLAGS)"
CONFIGURE_OPTS := --prefix=/usr --bindir=/usr/games --program-prefix=sgt-

override_dh_auto_configure:
	./mkfiles.pl
	./mkauto.sh
	$(CONFIGURE_ENV) CC="$(CC_FOR_BUILD)" LD="$(LD_FOR_BUILD)" \
	    PKG_CONFIG="$(PKG_CONFIG_FOR_BUILD)" \
	    ./configure $(CONFIGURE_OPTS) --build=$(DEB_BUILD_GNU_TYPE)
	$(MAKE)
	$(MAKE) -C icons gtkicons pngicons
	$(MAKE) clean
	./mkfiles.pl
	./mkauto.sh
	$(CONFIGURE_ENV) CC="$(CC)" LD="$(LD)" PKG_CONFIG="$(PKG_CONFIG)" \
	    ./configure $(CONFIGURE_OPTS) --build=$(DEB_BUILD_GNU_TYPE) \
	    $(if $(filter-out $(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE)),--host=$(DEB_HOST_GNU_TYPE))

override_dh_auto_build:
	$(MAKE)
	$(MAKE) -f Makefile.doc BINPREFIX=sgt-

# Disable running tests; they are quite slow and don't seem likely to catch
# regressions.
override_dh_auto_test:

override_dh_auto_install:
	$(MAKE) DESTDIR=debian/sgt-puzzles install

override_dh_installdocs:
	for lang in $(LANGUAGES); do \
		dh_install html-$$lang/*.html usr/share/sgt-puzzles/help/$$lang \
		|| exit; \
	done
	dh_installdocs

override_dh_installman:
	for lang in $(LANGUAGES); do \
		dh_installman --language=$${lang/en/C} man-$$lang/* || exit; \
	done

override_dh_install:
	dh_install debian/desktop/*.desktop usr/share/applications
	for icon in icons/*-48d24.png; do \
	    base="$$(basename $$icon -48d24.png)" && \
	    install -m644 $$icon debian/sgt-puzzles/usr/share/icons/hicolor/48x48/apps/sgt-$$base.png \
	    || exit; \
	done

override_dh_auto_clean:
	$(MAKE) -C icons clean
	! [ -f Makefile ] || $(MAKE) distclean
	$(MAKE) -f Makefile.doc clean
	dh_clean $(addprefix Makefile.,am clangcl cyg emcc gnustep gtk in nestedvm osx vc wce) aclocal.m4 compile configure depcomp gamedesc.txt install-sh list.c missing

FORCE:
.PHONY: FORCE

../puzzles.tar.gz: FORCE
	wget -N -O $@ https://www.chiark.greenend.org.uk/~sgtatham/puzzles/puzzles.tar.gz

../puzzles.tar.version: ../puzzles.tar.gz
	version="$$(tar -xOzf $< --wildcards '*/version.h' | \
		    sed -n 's/^\#define VER "Version \([^ ]*\)"$$/\1/p')" && \
	test "$${version#*.}" != "$$version" &&				\
	echo "$$version" > $@

update-upstream: ../puzzles.tar.gz ../puzzles.tar.version
	version="$$(cat ../puzzles.tar.version)" &&			\
	upstream_commit="$${version#*.}" &&				\
	git tag -m "Upstream version $$version" "upstream/$$version"	\
		"$$upstream_commit" &&					\
	git deborig --version "$$version" &&				\
	git debrebase new-upstream "$$version" &&			\
	tar -xzf $< --strip-components=1 --wildcards			\
		$(addprefix '*/',$(VERSION_SUB_FILES)) &&		\
	version_sub_commit="$$(git rev-list --grep=$(VERSION_SUB_MSG) -F \
			       $$(git debrebase breakwater)..)" &&	\
	git commit -m $${version_sub_commit:+"fixup! "}$(VERSION_SUB_MSG) \
		$(VERSION_SUB_FILES) &&					\
	{ test -z "$$version_sub_commit" || git debrebase -i --autosquash; }
