#! /usr/bin/make -f

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

d = ./debian/tmp
python = $(d)/python
perl = $(d)/perl

build: checkdir

clean: checkdir
	$(RM) -r debian/{files,substvars,tmp}
	find . -name \*~ -print0 | xargs -0 $(RM)

binary: binary-arch binary-indep

binary-arch:

binary-indep: checkdir build checkroot

	$(RM) -r debian/{files,substvars,tmp}

	install -d $(python)/DEBIAN
	install -d $(python)/usr/lib/site-python/dpkg
#	install -d $(python)/usr/lib/python/dpkg
	install -d $(python)/usr/share/doc/dpkg-python

	install -c debian/changelog $(python)/usr/share/doc/dpkg-python/changelog.Debian
	gzip -9vf $(python)/usr/share/doc/dpkg-python/*
	install -c debian/copyright $(python)/usr/share/doc/dpkg-python/copyright

#	cp python/dpkg/*.py $(python)/usr/lib/site-python/dpkg/
	cp python/dpkg/*.py $(python)/usr/lib/site-python/dpkg

	install -c debian/dpkg-python.postinst $(python)/DEBIAN/postinst
	install -c debian/dpkg-python.prerm $(python)/DEBIAN/prerm

	install -d $(perl)/DEBIAN
	install -d $(perl)/usr/lib/perl5/Dpkg
	install -d $(perl)/usr/share/doc/dpkg-perl

	install -c debian/changelog $(perl)/usr/share/doc/dpkg-perl/changelog.Debian
	gzip -9vf $(perl)/usr/share/doc/dpkg-perl/*
	install -c debian/copyright $(perl)/usr/share/doc/dpkg-perl/copyright

	cp -rp perl5/Dpkg/* $(perl)/usr/lib/perl5/Dpkg/
	find $(perl)/usr/lib/perl5 -type f \! -name \*.pm -print0 | xargs -0 $(RM)

	chown -R root.root $(d)
	find $(d) -type f -print0 | xargs -0 chmod 644
	find $(d) -type d -print0 | xargs -0 chmod 755

	chmod a+x $(d)/*/DEBIAN/{postinst,prerm}

	dpkg-gencontrol -pdpkg-python -P$(python) -isp
	dpkg --build $(python) ..

	dpkg-gencontrol -pdpkg-perl -P$(perl) -isp
	dpkg --build $(perl) ..

checkdir:
	test -f perl5/Dpkg/Archive/FTP.pm

checkroot:
	test "`whoami`" = "root"
