#!/usr/bin/make -f
# common debian/rules for minimalist packages

DIRS := $(shell ls -d etc lib usr var 2> /dev/null)
MD5DIRS := $(DIRS)
ifeq (,$(filter usr,$(MD5DIRS)))
	MD5DIRS += usr
endif
PACKAGE := $(shell grep ^Package: debian/control | cut -d ' ' -f 2)
SCRIPTS := $(shell ls debian/postinst debian/postrm debian/prerm 2> /dev/null)

build:

clean:
	[ -e debian/control ]
	cd debian && rm -fr files tmp

binary-indep:
	[ -e debian/control ]
	rm -fr debian/tmp
	mkdir -p debian/tmp/DEBIAN
ifneq (,$(DIRS))
	cp -a $(DIRS) debian/tmp
endif
ifneq (,$(SCRIPTS))
	cp -a $(SCRIPTS) debian/tmp/DEBIAN
endif
	mkdir -p debian/tmp/usr/share/doc/$(PACKAGE)
	cp -a debian/changelog debian/copyright \
		debian/tmp/usr/share/doc/$(PACKAGE)
	gzip -9n debian/tmp/usr/share/doc/$(PACKAGE)/changelog
	dpkg-gencontrol
ifneq (,$(filter etc,$(DIRS)))
	cd debian/tmp && find etc -type f -printf "/%p\n" | LANG= sort \
		> DEBIAN/conffiles
	[ -s debian/tmp/DEBIAN/conffiles ] || rm debian/tmp/DEBIAN/conffiles
endif
	cd debian/tmp && find $(MD5DIRS) -type f -exec md5sum '{}' \; \
		| LANG= sort -k 2 > DEBIAN/md5sums
	dpkg-deb -b debian/tmp ..

binary-arch:

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