#! /usr/bin/make -f

ARCH=$(shell dpkg-architecture -qDEB_BUILD_ARCH)
VERSION=$(shell LC_ALL=C dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2)
DATE=$(shell echo $(VERSION) | cut -d '.' -f 1)
SUITE=$(shell LC_ALL=C dpkg-parsechangelog | grep ^Distribution: | cut -d ' ' -f 2 | cut -d - -f 1)
ifeq (${SUITE},UNRELEASED)
USE_UDEBS_FROM=trusty
TRANSSTATUS=
BOOTMENU_BEEP=n
else
USE_UDEBS_FROM=$(SUITE)
USE_PROPOSED_UPDATES=0
TRANSSTATUS=translation-status
BOOTMENU_BEEP=y
endif

ARCHIVEDIR=debian/tmp/installer-$(ARCH)
DESTDIR=$(ARCHIVEDIR)/$(VERSION)
IMAGEDIR=$(DESTDIR)/images
TARNAME=debian-installer-images_$(VERSION)_$(ARCH).tar.gz

export NO_PNG_PKG_MANGLE=1

clean:
	dh_testdir
	dh_testroot
	dh_clean
	$(MAKE) -C build reallyclean
	$(MAKE) -C doc/devel/partman clean
	$(MAKE) -C doc/devel/internals clean

# Must run as root, so is not run as part of regular build.
build-images:
	$(MAKE) -C build all_build stats release \
		USE_UDEBS_FROM=$(USE_UDEBS_FROM) BUILD_DATE=$(DATE) \
		USE_PROPOSED_UPDATES=$(USE_PROPOSED_UPDATES) \
		TRANSSTATUS=$(TRANSSTATUS) BOOTMENU_BEEP=$(BOOTMENU_BEEP)
	
build: build-stamp
build-stamp:
	rm -f $@
	$(MAKE) -C doc/devel/internals
	$(MAKE) -C doc/devel/partman
	touch $@

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	
	debian/rules build-images

	install -d $(IMAGEDIR)
	cp -a build/dest/* $(IMAGEDIR)
	ln -s $(VERSION) $(ARCHIVEDIR)/current

binary-arch: install  
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs doc/* -X Makefile -X build.sh -X .xsl \
		-X internals.xml -X partman-doc.sgml
	dh_compress
	dh_fixperms

	# Only depend on the packages, not versions, at least for now:
	echo -n "udeb:Depends=" >> debian/debian-installer-udebs.substvars
	while read p v a; do \
		echo -n "$$p, " >> debian/debian-installer-udebs.substvars; \
	done < build/dest/udeb.list
	echo "" >> debian/debian-installer-udebs.substvars
	dh_gencontrol
	dh_md5sums
	dh_builddeb
	cd debian/tmp && tar czvf ../../../$(TARNAME) .
	dpkg-distaddfile $(TARNAME) raw-installer -

binary-indep:

binary: binary-indep binary-arch 
.PHONY: build build-images clean binary-indep binary-arch binary install 
