#! /usr/bin/make -f
#
#

# The name of the package (for example, `emacs').
p = qpopper
# The version of the package (for example, `19.28').
v = 2.2
# The Debian revision of the package (for example, `2').
d = 1
# The urgency of the release (for the changes file)
urg = medium
# Distributions this has to go in.
dist = "unstable"

# The architecture of the build
a = $(shell dpkg --print-architecture)

build:
# Builds the binary package.
	make -f make.linux
	touch stamp-build

clean:
# Undoes the effect of `make -f debian/rules build'.
	@-if [ -d debian-tmp ]; then \
		echo "Enter the root password to clean up the debian-tmp dir";\
		su -c "rm -rf debian-tmp";\
	fi
	make -f make.linux clean
	find . -name '*.bak' -o -name '*~' | xargs rm -f --
	rm -f loadmap popper.linux
	rm -f stamp-build


binary:
# Makes a binary package.
	test -f stamp-build || make -f debian/rules build
	@-[ "`whoami`" != root ] && \
		echo "Enter the root password to build the .deb file"
	@su -c "exec make -f debian/rules binary-root"

binary-root:
# Makes a binary package.
	rm -rf debian-tmp
	install -d -g root -m 755 debian-tmp
	chmod g-s debian-tmp
	install -d -g root -m 755 debian-tmp/usr/doc/$p
	install -d -g root -m 755 debian-tmp/usr/sbin
	install -d -g root -m 755 debian-tmp/usr/bin
	install -d -g root -m 755 debian-tmp/var/spool/popbull
	install -d -g root -m 755 debian-tmp/usr/man/man8
	install -d -g root -m 755 debian-tmp/DEBIAN
	install -s -g root -m 755 popper.linux debian-tmp/usr/sbin/in.qpopper
	install -g root -m 644 popper.8 debian-tmp/usr/man/man8/qpopper.8
	install -s -o mail -g root -m 4755 popauth debian-tmp/usr/bin
	install -g root -m 644 popauth.8 debian-tmp/usr/man/man8/popauth.8
	echo ".so man8/qpopper.8" > debian-tmp/usr/man/man8/in.qpopper.8
	install -g root -m 644 debian/qpopper.doc debian-tmp/usr/doc/$(p)
	sed -e 's/=V/$(v)/' -e 's/=D/$(d)/' -e 's/=A/$(a)/' debian/control \
	  > debian-tmp/DEBIAN/control
	chmod 644 debian-tmp/DEBIAN/control
	install -g root -m 755 debian/postinst debian-tmp/DEBIAN/postinst
	install -g root -m 755 debian/postrm debian-tmp/DEBIAN/postrm
	dpkg --build debian-tmp
	mv debian-tmp.deb ../$(p)_$(v)-$(d)_$(a).deb
	rm -rf debian-tmp

source:
# Makes a source package.
	-test -f stamp-build && make -f debian/rules clean
	( cd .. && tar cf - $(p)_$(v) | gzip -9f > $(p)_$(v)-$(d).tar.gz )

diff:
# Makes a context diff.
	-test -f stamp-build && make -f debian/rules clean
	-test -d ../$(p)_$(v).orig \
	  || ( echo "Original source package is not available." ; false )
	( cd .. && diff -ruN $(p)_$(v).orig $(p)_$(v) | gzip -9f \
	  > $(p)_$(v)-$(d).diff.gz )

changes:
# Makes the dchanges file
	test -f ../$(p)_$(v)-$(d)_$(a).deb || \
		(echo ".deb file not available"; false)
	test -f ../$(p)_$(v)-$(d).diff.gz || \
		(echo ".diff.gz file not available"; false)
	test -f ../$(p)_$(v)-$(d).tar.gz || \
		(echo ".tar.gz file not available"; false)
	(cd .. && dchanges -n urg=$(urg) dist=$(dist) \
		ce=$(p)_$(v)/debian/changes \
		$(p)_$(v)-$(d)_$(a).deb \
		$(p)_$(v)-$(d).tar.gz \
		$(p)_$(v)-$(d).diff.gz)

dist: binary source diff changes
# Prepares the package for distribution.

orig:
# Prepares the original package from the current
# Debian revision source package and context diff.
	( cd .. \
	  && mkdir $(p).orig \
	  && cd $(p).orig \
	  && tar xzf ../$(p)_$(v)-$(d).tar.gz \
	  && cd $(p)_$(v) \
	  && ( zcat ../../$(p)_$(v)-$(d).diff.gz \
	    | patch -sER -p1 ) \
	  && find . -name "*.orig" -exec rm -f {} \; \
	  && cd .. \
	  && mv $(p)_$(v) ../$(p)_$(v).orig \
	  && cd .. \
	  && rmdir $(p).orig )
	touch stamp-orig

