#!/usr/bin/make -f


DEBIAN_PATH  := $(abspath $(dir $(firstword $(MAKEFILE_LIST))))
SRC_VERSION  := $(shell dpkg-parsechangelog -l$(DEBIAN_PATH)/changelog | \
	awk '/^Version:/ {sub(/[-+][^-]*/, "", $$2); print $$2}')

major := $(firstword $(subst ., ,$(SRC_VERSION)))
minor := $(lastword $(subst ., ,$(SRC_VERSION)))

url := git://github.com/illumos/illumos-gate.git

VER := $(major).$(minor)

year := $(shell expr 2010 + $(major))
month := $(minor)

illumos-gate:
	git clone $(url) illumos-gate || rm -rf illumos-gate

checkout: illumos-gate
	cd illumos-gate && git checkout `git rev-list -n 1 --before="$(year)-$(month)-01" master`
	touch $@

illumos-gate-$(VER).tar.xz: checkout
	tar -c -f - --exclude-vcs illumos-gate | xz -9 > $@

ChangeLog: checkout
	cd illumos-gate && git log --date-order --after=1/10/2011 --before="$(year)-$(month)-01" --stat > ../$@

build-arch build-indep build:

debian/control: debian/control.in debian/rules
	sed 's,@VER@,$(VER),g' $< > $@

binary-arch binary-indep binary: binary-stamp
binary-stamp: install-stamp debian/control
	dh_testroot
	dh_testdir
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb
	touch $@


install: install-stamp
install-stamp: illumos-gate-$(VER).tar.xz ChangeLog
	dh_testroot
	dh_testdir
	dh_installdirs usr/src/illumos-gate
	dh_install illumos-gate-$(VER).tar.xz usr/src/illumos-gate/
	dh_installchangelogs ChangeLog
	touch $@

clean: debian/control
	dh_clean
	rm -f ChangeLog
	rm -f checkout
	rm -f illumos-gate-$(VER).tar.xz
	rm -rf illumos-gate
