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

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

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\d+|\+dfsg\d+))}')

%:
	dh $@ --with dkms

override_dh_dkms:
	dh_dkms -V $(VER)

override_dh_install:
	dh_install ceph keys libceph linux rbd debian/Makefile    /usr/src/ceph-$(VER)

## http://wiki.debian.org/onlyjob/get-orig-source
DTYPE =
UURL  = https://github.com/ceph/ceph-client.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})/')')
.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: UDATE=$(UDATE))
	$(if $(wildcard $(PKG)-$(VER)),$(error $(PKG)-$(VER) exist, aborting..))
	@echo "# Downloading..."
	#uscan --noconf --verbose --rename --destdir=$(CURDIR) --check-dirname-level=0 --force-download --download-version $(VER) $(PKD)
	( git clone file:///home/$${USER}/dev/upstream/_github/ceph-client/ --single-branch --branch for-linus --depth 128 $(PKG)-$(VER) \
          || git clone $(UURL) --single-branch --branch for-linus --depth 128 $(PKG)-$(VER) \
        ) || $(RM) -r $(PKG)-$(VER)
	cd $(PKG)-$(VER) \
        && git checkout $$(git log --branches=for-linus -n1 --format=%h --before="$(UDATE)") \
        && [ -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 times (4 threads)..." \
        && ( count=0; for F in $$(git ls-tree -r --name-only HEAD); do \
             ( [ -e "$$F" ] && touch --no-dereference -d "$$(git log -1 --format="%ai" -- "$$F")" "$$F" ) & \
           [ $$(((count+=1) % 4)) -eq 0 ] && wait || [ 1 -eq 1 ]; done ) \
        && echo "# Cleaning-up..." \
        && tar -caf ../$(PKG)_$(VER)$(DTYPE).orig-tmp.tar.xz \
            drivers/block/rbd.c \
            drivers/block/rbd_types.h \
            drivers/block/Makefile \
            drivers/block/Kconfig \
            fs/ceph \
            include/linux/ceph \
            include/linux/crush \
            include/keys/ceph-type.h \
            net/ceph \
            ChangeLog \
        && $(RM) -r .gitignore .mailmap .git * \
        && tar -xf ../$(PKG)_$(VER)$(DTYPE).orig-tmp.tar.xz \
        && $(RM) -v ../$(PKG)_$(VER)$(DTYPE).orig-tmp.tar.xz \
        && echo "# Correcting layout..." \
        && mkdir linux \
        && mv -v include/linux/ceph ./linux/ \
        && mv -v include/linux/crush ./linux/ \
        && mkdir keys \
        && mv -v include/keys/ceph-type.h ./keys/ \
        && mv -v fs/ceph ./ \
        && mkdir rbd \
        && mv -v drivers/block/* ./rbd/ \
        && mkdir libceph \
        && mv -v net/ceph/* ./libceph/ \
        && $(RM) -rv drivers include fs net
	@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)"
