#!/usr/bin/make -f

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

IDLDIR=$(shell pkg-config libxul --variable=idldir)
SDKDIR=$(shell pkg-config libxul --variable=sdkdir)
IDL_FILES=$(notdir $(wildcard idl/*.idl))
XPT_FILES=$(addprefix components/, $(IDL_FILES:.idl=.xpt))

%:
	dh $@ --with xul-ext --buildsystem=xul_ext --parallel --max-parallel=4

$(XPT_FILES):
	python $(SDKDIR)/sdk/bin/typelib.py -I $(IDLDIR) -o $@ $(addprefix idl/,$(notdir $(@:.xpt=.idl)))

override_dh_auto_build: $(XPT_FILES)
	dh_auto_build

override_dh_builddeb:
	dh_builddeb -- -Zxz

PKD  = $(abspath $(dir $(MAKEFILE_LIST)))
PKG  = $(word 2,$(shell dpkg-parsechangelog -l$(PKD)/changelog | grep ^Source))
VER ?= $(shell dpkg-parsechangelog -l$(PKD)/changelog | perl -ne 'print $$1 if m{Version:\s*([\d\.+git]+)}')
GDATE = $(shell echo $(VER) | perl -ne 'print "$$1-$$2-$$3" if m/\+git(\d{4})(\d{2})(\d{2})/')
.PHONY: get-orig-source
get-orig-source: $(PKG)_$(VER).orig.tar.xz
	@

$(PKG)_$(VER).orig.tar.xz:
	@echo "# Downloading..."
	#uscan --noconf --verbose --rename --destdir=$(CURDIR) --check-dirname-level=0 --force-download --download-version $(VER) $(PKD)
	git clone git://github.com/JasonBarnabe/stylish.git $(PKG)-$(VER)
	cd $(PKG)-$(VER) \
	&& git reset --hard $$(git rev-list --all -n 1 --before="$(GDATE) +0000") \
	&& git checkout master \
	&& [ -s ChangeLog ] || ( echo "# Generating ChangeLog..." \
	   ; git log --pretty="format:%ad  %aN  <%aE>%n%n%x09* %s%d%n" --date=short > ChangeLog ) \
	&& mv stylish-firefox/* . \
	&& $(RM) -r .git .git* stylish-chrome stylish-firefox \
	&& cd .. && echo "# Packing..." \
	&& tar -caf $(PKG)_$(VER).orig.tar.xz $(PKG)-$(VER) --owner=root --group=root --mode=a+rX --mtime=$(GDATE)
	$(RM) -r $(PKG)-$(VER)
