#!/usr/bin/make -f

PACKAGE	= $(shell perl -e 'print <> =~ /^(.*) \(.*\)/' debian/changelog)

build: build-stamp
build-stamp:
	test -e debian/control
	$(MAKE)
	touch build-stamp

clean:
	test -e debian/control
	test root = "`whoami`" || (echo need root priviledges; exit 1)
	rm -f build-stamp install-stamp
	make clean
	rm -rf debian/substvars debian/files debian/tmp

install: install-stamp
install-stamp: build-stamp
	test -e debian/control
	test root = "`whoami`" || (echo need root priviledges; exit 1)
	rm -f debian/substvars
	$(MAKE) install DESTDIR=`pwd`/debian/tmp
	touch install-stamp

binary-indep: build install
	test -e debian/control
	test root = "`whoami`" || (echo need root priviledges; exit 1)
	rm -rf debian/substvars debian/tmp
	make install DESTDIR="`pwd`/debian/tmp"
	install -d --mode=0755 \
		"debian/tmp/var/state/kernellab" \
		"debian/tmp/var/state/kernellab/kernels" \
		"debian/tmp/var/state/kernellab/alancox" \
		"debian/tmp/var/state/kernellab/modules" \
		"debian/tmp/var/state/kernellab/images" \
		"debian/tmp/var/state/kernellab/configs"
	install -d --mode=0755 "debian/tmp/usr/share/doc/$(PACKAGE)"
	install --mode=0644 debian/copyright \
		README TODO HACKING \
		"debian/tmp/usr/share/doc/$(PACKAGE)"
	install --mode=0644 debian/README \
		"debian/tmp/usr/share/doc/$(PACKAGE)/README.Debian"
	gzip -9 "debian/tmp/usr/share/doc/$(PACKAGE)/README" \
		"debian/tmp/usr/share/doc/$(PACKAGE)/TODO" \
		"debian/tmp/usr/share/doc/$(PACKAGE)/README.Debian"
	gzip -9 debian/tmp/usr/share/man/man1/*
	install --mode=0644 debian/changelog \
		"debian/tmp/usr/share/doc/$(PACKAGE)/changelog"
	gzip -9 debian/tmp/usr/share/doc/"$(PACKAGE)"/changelog
	install -d --mode=0755 debian/tmp/DEBIAN
	echo /etc/kernellab.conf >debian/tmp/DEBIAN/conffiles
	install -m 755 debian/postinst debian/prerm debian/tmp/DEBIAN
	dpkg-gencontrol -isp
	dpkg --build debian/tmp ..

binary-arch: build install

source diff:                                                                  
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

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