#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

%:
	dh $@

override_dh_clean:
	rm -rf schemas
	rm -f locale/*/*/*.mo
	dh_clean

override_dh_auto_build:
	mkdir schemas
	glib-compile-schemas --strict --targetdir=./schemas/ .
	for po in locale/*/*/*.po; do \
		msgfmt -cv -o $${po%.po}.mo $$po; \
        done
	dh_auto_build

override_dh_install:
	# Installing translations
	for mo in $$(cd locale; ls */*/*.mo); do \
        	lang=$$(dirname $$mo); \
		mkdir -p debian/gnome-shell-extension-autohidetopbar/usr/share/locale/$$lang; \
		cp locale/$$mo debian/gnome-shell-extension-autohidetopbar/usr/share/locale/$$lang/; \
	done
	dh_install

# For get-orig-source
PKD   = $(abspath $(dir $(MAKEFILE_LIST)))
PKG   = $(word 2,$(shell dpkg-parsechangelog -l$(PKD)/changelog | grep ^Source))
UVER  = $(shell dpkg-parsechangelog -l$(PKD)/changelog | perl -ne 'print $$1 if m{^Version:\s+(?:\d+:)?(\d.*)(?:\-\d+.*)};')
DTYPE = 
VER  ?= $(subst $(DTYPE),,$(UVER))

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


UURL = git://github.com/mlutfy/hidetopbar.git
UDATE = $(shell date --rfc-3339=seconds --date='TZ="UTC" \
	$(shell echo $(VER) | perl -ne 'print "$$1-$$2-$$3" if m/\+(?:git|svn|hg)(\d{4})(\d{2})(\d{2})/')')

$(PKG)_$(VER)$(DTYPE).orig.tar.xz: 
	#$(info I: UDATE=$(UDATE))
	$(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 v$(VER) || git checkout $$(git log -n1 --format=%h --before="$(UDATE)") \
	&& ( 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..." \
	&& for F in $$(git ls-tree -r --name-only HEAD); do touch --no-dereference -d "$$(git log -1 --format="%ai" -- $$F)" "$$F"; done \
	&& echo "# Cleaning-up..." \
	&& $(RM) -r -v \
		notneededdir/unwantedfile1 \
		notneededdir/unwantedfiles.* \
	&& $(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)"
