#!/usr/bin/make -f

#export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

include /usr/share/dpkg/architecture.mk

export LIBDIR = /usr/lib/$(DEB_HOST_MULTIARCH)/security

%:
	dh $@

override_dh_auto_build-arch:
	dh_auto_build --sourcedirectory=src

override_dh_auto_build-indep:
	dh_auto_build --sourcedirectory=doc

override_dh_auto_install-arch:
	dh_auto_install --sourcedirectory=src

override_dh_auto_install-indep:
	dh_auto_install --sourcedirectory=doc

execute_before_dh_install-indep:
	mv debian/tmp/usr/share/doc/pam_python debian/tmp/usr/share/doc/libpam-python
	dh_install

override_dh_auto_test:
	:

include /usr/share/dpkg/pkg-info.mk

PKD   = $(word 1,$(abspath $(dir $(MAKEFILE_LIST))))
PKG   = $(DEB_SOURCE)
UVER  = $(shell echo $(DEB_VERSION) | cut -d "-" -f1)
DTYPE =
VER  ?= $(subst $(DTYPE),,$(UVER))

UURL = https://github.com/sunweaver/pam-python.git
UREV = $(shell echo $(VER) | cut -d"." -f4)

## http://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))
	@

$(PKG)_$(VER)$(DTYPE).orig.tar.xz: $(info I: GIT Revision=$(UREV))
	$(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 "$(UREV)" \
	   && 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 -rf .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)"
