#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

# TARGET_DIST is one of stable, sarge, etch, terra, unstable, fetch
# from the changelog
TARGET_DIST = $(shell head -1 debian/changelog |awk '{print $$3}'|tr -d ';')

PART_TABLES = \
	Main-Server+Thin-Client-Server.table \
	Main-Server+Workstation.table \
	Main-Server.table \
	Workstation+Thin-Client-Server.table \
	Workstation.table \
	Standalone.table \
	Barebone.table

DEFAULTS = \
	common networked main-server workstation thin-client-server \
	standalone lessdisks ltsp-chroot

INSTALL      = install -m0755
INSTALL_DATA = install -m0644

PACKAGES=$(shell dh_listpackages)

INSTALLUDEB=debian-edu-install-udeb
PROFILEUDEB=debian-edu-profile-udeb
VERSION=$(shell dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2)
ARCH=all
INSTALLUDEBFILENAME=$(INSTALLUDEB)_$(VERSION)_$(ARCH).udeb
REPORTBUGUDEBFILENAME=$(REPORTBUGUDEB)_$(VERSION)_$(ARCH).udeb
PROFILEUDEBFILENAME=$(PROFILEUDEB)_$(VERSION)_$(ARCH).udeb

build:
	$(MAKE)

clean:
	debconf-updatepo
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	$(MAKE) clean
	dh_clean

install-common: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

install-debian-edu-install: install-common
	$(MAKE) install DESTDIR=$(CURDIR)/debian/debian-edu-install
	cp debian/debian-edu-install.lintian-override $(CURDIR)/debian/debian-edu-install/usr/share/lintian/overrides/debian-edu-install
	$(INSTALL) -d $(CURDIR)/debian/debian-edu-install/lib/debian-edu-install/
	$(INSTALL) debian/debian-edu-install.firstboot $(CURDIR)/debian/debian-edu-install/usr/lib/debian-edu-install/firstboot

.PHONY: install-debian-edu-profile-udeb
install-debian-edu-profile-udeb: install-common
	$(INSTALL) debian-edu-aptkey-setup $(CURDIR)/debian/debian-edu-profile-udeb/usr/lib/base-installer.d/99debian-edu-aptkey-setup
	$(INSTALL) debian-edu-preseed $(CURDIR)/debian/debian-edu-profile-udeb/usr/lib/post-base-installer.d/10debian-edu-preseed
	$(INSTALL) debian-edu-add_local_apt $(CURDIR)/debian/debian-edu-profile-udeb/usr/lib/post-base-installer.d/90debian-edu-add_local_apt
	$(INSTALL) debian/debian-edu-profile-udeb.prebaseconfig $(CURDIR)/debian/debian-edu-profile-udeb/usr/lib/finish-install.d/19debian-edu-profile-udeb
	$(INSTALL) autopartkit/20debian-edu $(CURDIR)/debian/debian-edu-profile-udeb/usr/lib/autopartkit.d/20debian-edu
	$(INSTALL) apt-setup/generators/70debian-edu-install $(CURDIR)/debian/debian-edu-profile-udeb/usr/lib/apt-setup/generators/70debian-edu-install
	for profile in $(DEFAULTS) ; do \
	    $(INSTALL_DATA) base-config/defaults.$$profile $(CURDIR)/debian/debian-edu-profile-udeb/usr/lib/debian-edu-install ; \
	done
	$(INSTALL_DATA) lib/partman/recipes/* $(CURDIR)/debian/debian-edu-profile-udeb/lib/partman/recipes
	$(INSTALL_DATA) lib/partman/recipes-powerpc-powermac_newworld/* $(CURDIR)/debian/debian-edu-profile-udeb/lib/partman/recipes-powerpc-powermac_newworld
	$(INSTALL_DATA) lib/partman/recipes-powerpc-prep/* $(CURDIR)/debian/debian-edu-profile-udeb/lib/partman/recipes-powerpc-prep
	$(INSTALL_DATA) lib/partman/commit.d/* $(CURDIR)/debian/debian-edu-profile-udeb/lib/partman/commit.d
	for table in $(PART_TABLES) ; do \
	    $(INSTALL_DATA) autopartkit/$$table \
	    $(CURDIR)/debian/debian-edu-profile-udeb/etc/autopartkit/$$table ; \
	done
	$(INSTALL) debian-edu-profile $(CURDIR)/debian/debian-edu-profile-udeb/usr/bin
	touch $(CURDIR)/debian/debian-edu-profile-udeb/etc/uselvm

# Build architecture-independent files here.
binary-indep: install debian-edu-install debian-edu-install-udeb debian-edu-profile-udeb

debian-edu-install-udeb: # install-debian-edu-install-udeb
	dh_testdir -p$@
	dh_testroot -p$@
	dh_installdebconf -p$@ -n
	dh_strip -p$@
	dh_compress -p$@
	dh_fixperms -p$@
	cp debian/debian-edu-install-udeb.isinstallable debian/debian-edu-install-udeb/DEBIAN/isinstallable
	chmod +x debian/debian-edu-install-udeb/DEBIAN/isinstallable
	dh_installdeb -p$@
	dh_gencontrol -p$@ -- -n$(INSTALLUDEBFILENAME)
ifeq ($(TARGET_DIST),terra)
	sed -i -e 's/^Installer-Menu-Item: \(.*\)00$$/Installer-Menu-Item: \1/' \
		debian/debian-edu-install-udeb/DEBIAN/control
endif
	dh_builddeb -p$@ --filename=$(INSTALLUDEBFILENAME)

.PHONY: debian-edu-profile-udeb
debian-edu-profile-udeb: install-debian-edu-profile-udeb
	dh_testdir -p$@
	dh_testroot -p$@
	dh_installdebconf -p$@ -n
	dh_strip -p$@
	dh_compress -p$@
	dh_fixperms -p$@
	dh_installdeb -p$@
	# Make sure the autopartkit.d templates are normal files.  udpkg do
	# not understand conffiles at the moment.
	rm debian/$(PROFILEUDEB)/DEBIAN/conffiles
	dh_gencontrol -p$@ -- -n$(PROFILEUDEBFILENAME)
ifeq ($(TARGET_DIST),terra)
	sed -i -e 's/^Installer-Menu-Item: \(.*\)00$$/Installer-Menu-Item: \1/' \
		debian/debian-edu-profile-udeb/DEBIAN/control
endif
	dh_builddeb -p$@ --filename=$(PROFILEUDEBFILENAME)

debian-edu-install: install-debian-edu-install
	dh_testdir -p$@
	dh_testroot -p$@
	dh_installdebconf -p$@
	dh_installdocs -p$@
	dh_installexamples -p$@
	dh_installmenu -p$@
	dh_installcron -p$@
	dh_installman -p$@
	dh_installinfo -p$@
	dh_installchangelogs -p$@
	dh_link -p$@
	dh_strip -p$@
	dh_compress -p$@
	dh_fixperms -p$@
	dh_installdeb -p$@
	dh_gencontrol -p$@
	dh_md5sums -p$@
	dh_builddeb -p$@

# Build architecture-dependent files here.
binary-arch: build install

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