
buildpkgs_opts_signed = -sa -pgpg -sgpg -D -rfakeroot -k"$$DEBEMAIL"
buildpkgs_opts_unsigned = -us -uc -rfakeroot

lintian_opts = --lintian-opts -iI --color always

changelogversion := $(shell head -1 debian/changelog | sed -e "s/.*(//" -e "s/).*//" -e "s/.*://")
version := $(shell echo $(changelogversion) | sed -e "s/-.*//")
debversion := $(shell echo $(changelogversion) | sed -e "s/.*-//")

ghc_version := $(shell ghc --numeric-version)

orig_tarball = Tarballs/$(debname)_$(version).orig.tar.gz


# Unsigned is the default.
all : Stamp/unsigned

signed : Stamp/signed

source : Stamp/source

debian : Stamp/debian

clean :
	rm -f $(debname)_$(version).orig.tar.gz $(debname)_$(version)-$(debversion).debian.tar.gz
	rm -f Stamp/* *.build *.deb *.dsc *.changes *.diff.gz
	rm -rf $(debname)-$(version)

uscan : debian/watch
	uscan --report-status --watchfile debian/watch

#-------------------------------------------------------------------------------
# Rules.

Stamp/signed : Stamp/debian
	(cd $(debname)-$(version) && debuild  $(buildpkgs_opts_signed) $(lintian_opts))
	touch  $@

Stamp/unsigned : Stamp/debian
	(cd $(debname)-$(version) && debuild  $(buildpkgs_opts_unsigned) $(lintian_opts))
	touch  $@

Stamp/debian : Stamp/source
	rm -rf $(debname)-$(version)/debian
	cp -r debian $(debname)-$(version)/
	rm -rf $(debname)-$(version)/debian/_darcs
	touch  $@

Stamp/source : Stamp/tarball
	rm -rf $(origname)-$(version) $(debname)-$(version)
	tar zxf $(debname)_$(version).orig.tar.gz
	@if test $(origname)-$(version) != $(debname)-$(version) ; then \
		mv $(origname)-$(version) $(debname)-$(version) ; \
		fi
	touch  $@

Stamp/tarball : Stamp/download Tarballs/md5sums Tarballs/sha256sums
	md5sum --check Tarballs/md5sums
	sha256sum --check Tarballs/sha256sums
	chmod a+r Tarballs/$(debname)_$(version).orig.tar.gz
	rm -f $(debname)_$(version).orig.tar.gz
	ln -s $(orig_tarball) $(debname)_$(version).orig.tar.gz
	touch  $@

Tarballs/md5sums : Stamp/download
	@if test ! -f $@ || test `grep -c $(debname)_$(version) $@` -ne 1 ; then \
		md5sum Tarballs/*$(version)*.gz > $@ ; \
		fi
	touch $@

Tarballs/sha256sums : Stamp/download
	@if test ! -f $@ || test `grep -c $(debname)_$(version) $@` -ne 1 ; then \
		sha256sum Tarballs/*$(version)*.gz > $@ ; \
		fi
	touch $@

Stamp/download : debian/watch Stamp/version
	@if test `grep -c $(origname) debian/watch` -lt 1 ; then \
		echo "Bad 'debian/watch' file." ; exit 1 ; \
		fi
	@if test ! -f Tarballs/$(debname)_$(version).orig.tar.gz ; then \
		uscan --download-version $(version) --force-download \
			--destdir Tarballs/ --rename --watchfile debian/watch ; \
		fi
	touch $@

Stamp/version : debian/changelog debian/copyright debian/rules
	@echo "Changelog version :" $(changelogversion)
	@echo "Upstream version  :" $(version)
	@echo "Package version   :" $(debversion)
	@ if test ! -d Stamp ; then mkdir Stamp ; fi
	@ if test ! -d Tarballs ; then mkdir Tarballs ; fi
	touch $@

