#!/usr/bin/make -f

PACKAGE := carpaltunnel
DEBTMP := debian/tmp.$(PACKAGE)
PYTHONBIN := /usr/bin/python

build:
	test -e debian/control
	$(PYTHONBIN) setup.py build

clean:
	test -e debian/control
	test root = "`whoami`" || (echo need root priviledges; exit 1)
	-$(PYTHONBIN) setup.py clean --all
	find . -type f -name '*.pyc' -print0 \
	| xargs -0 --no-run-if-empty rm --
	rm -f MANIFEST
	rm -rf debian/substvars debian/files $(DEBTMP)

install:
	test -e debian/control
	test root = "`whoami`" || (echo need root priviledges; exit 1)
	rm -rf debian/substvars $(DEBTMP)
	install -d $(DEBTMP)
	chmod 0755 $(DEBTMP) # make sure no sgid dir is playing tricks
	$(PYTHONBIN) setup.py install --prefix=$(DEBTMP)/usr
	install -d -m0755 "$(DEBTMP)/usr/share/man/man8"
	xmlto man -o "$(DEBTMP)/usr/share/man/man8" doc/carpaltunnel.8.xml
	gzip -9f "$(DEBTMP)/usr/share/man/man8"/*

	xmlto xhtml-nochunks -o "$(DEBTMP)/usr/share/doc/carpaltunnel" doc/tutorial.xml
	lynx -dump -nolist \
		"$(DEBTMP)/usr/share/doc/carpaltunnel/tutorial.html" \
		>"$(DEBTMP)/usr/share/doc/carpaltunnel/tutorial.txt"
	chmod -R u+rw,go-w+rX $(DEBTMP)

binary-arch: build install

binary-indep: build install
	test -e debian/control
	test root = "`whoami`" || (echo need root priviledges; exit 1)

	install -d --mode=0755 "$(DEBTMP)/usr/share/doc/$(PACKAGE)"
	install --mode=0644 debian/copyright \
		"$(DEBTMP)/usr/share/doc/$(PACKAGE)"
	install --mode=0644 debian/changelog \
		"$(DEBTMP)/usr/share/doc/$(PACKAGE)/changelog"
	gzip -9 "$(DEBTMP)/usr/share/doc/$(PACKAGE)/changelog"
	install -d --mode=0755 $(DEBTMP)/DEBIAN
	dpkg-gencontrol -isp -P"$(DEBTMP)"
	dpkg --build $(DEBTMP) ..

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