#! /usr/bin/make -f
#
# To make the binary distribution package, the ``Debianized'' source package
# and the context diff to the original package, type `./debian.rules dist'.
# Make sure that `debian.rules' is executable before the final distribution
# is made.
#
# Invoke each target with `./debian.rules <target>'.  All targets should be
# invoked with the package root as the current directory.
#
# The `binary' target must be run as root, as it needs to install files with
# specific ownerships.  The `diff' target assumes that you have the original
# source package available, unpacked, in ../$(p)-$(v).orig, or that you have
# the previous revision of the ``Debianized'' source package and context diff
# in the parent directory.

CC = gcc
CFLAGS = -O2 -D_POSIX_SOURCE -DDOT_IS_LAST
LDFLAGS = -s

# The name of the package (for example, `emacs').
p = bc
# The version of the package (for example, `19.28').
v = 1.03
# The Debian revision of the package (for example, `2').
d = 8

build:
# Builds the binary package.
	-test ! -f stamp-configure && touch stamp-configure && \
		./configure --prefix=$(PREFIX) 
	make CC="$(CC)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"
	touch stamp-build

clean:
# Undoes the effect of `make -f debian.rules build'.
	rm -f stamp-build
	rm -f stamp-configure
	rm -rf debian-tmp
	rm -f bc
	rm -f bc.c
	rm -f Only in bc-1.03.orig: bc.c.dist
	rm -f config.cache
	rm -f config.h
	rm -f config.log
	rm -f dc
	rm -f scan.c
	rm -f y.tab.h
	make clean

binary-bc:
	test -f stamp-build || make -f debian.rules build
	rm -rf debian-tmp
	install -d -g root -m 755 -o root debian-tmp
	chmod g-s debian-tmp
	install -d -g root -m 755 -o root debian-tmp/DEBIAN
	install -d -g root -m 755 -o root debian-tmp/usr/bin
	install -g root -m 755 -o root bc debian-tmp/usr/bin/bc
	install -d -g root -m 755 -o root debian-tmp/usr/man/man1
	install -g root -m 644 -o root bc.1 debian-tmp/usr/man/man1/bc.1
	install -d -g root -m 755 -o root debian-tmp/usr/doc/copyright
	install -g root -m 644 -o root debian.COPYRIGHT \
	  debian-tmp/usr/doc/copyright/bc
	install -d -g root -m 755 -o root debian-tmp/usr/doc/examples
	cp -a Examples debian-tmp/usr/doc/examples/bc
	chmod -R g-sw debian-tmp/usr/doc/examples/bc
	chown -R root.root debian-tmp/usr/doc/examples/bc
	sed -e 's/VERSION:.*=V/VERSION: $(v)/' \
            -e 's/PACKAGE_REVISION:.*=D/PACKAGE_REVISION: $(d)/' \
            <debian.control.bc >debian-tmp/DEBIAN/control
	chmod 644 debian-tmp/DEBIAN/control
	dpkg --build debian-tmp
	mv debian-tmp.deb ../bc-$(v)-$(d).deb

binary-dc:
	test -f stamp-build || make -f debian.rules build
	rm -rf debian-tmp
	install -d -g root -m 755 -o root debian-tmp
	chmod g-s debian-tmp
	install -d -g root -m 755 -o root debian-tmp/DEBIAN
	install -d -g root -m 755 -o root debian-tmp/usr/bin
	install -g root -m 755 -o root dc debian-tmp/usr/bin/dc
	install -d -g root -m 755 -o root debian-tmp/usr/info
	install -g root -m 644 -o root dc.info debian-tmp/usr/info/dc.info
	install -d -g root -m 755 -o root debian-tmp/usr/man/man1
	install -g root -m 644 -o root dc.1 debian-tmp/usr/man/man1/dc.1
	install -d -g root -m 755 -o root debian-tmp/usr/doc/copyright
	install -g root -m 644 -o root debian.COPYRIGHT \
	  debian-tmp/usr/doc/copyright/dc
	rm -f debian-tmp/usr/info/dc.info*.gz
	gzip -9f debian-tmp/usr/info/dc.info*
	install -d -g root -m 755 -o root debian-tmp/usr/doc/dc
	install -g root -m 644 -o root \
		dc.texinfo debian-tmp/usr/doc/dc/dc.texinfo
	install -g root -m 644 -o root debian.dc.doc.Makefile \
		debian-tmp/usr/doc/dc/Makefile
	gzip -9f debian-tmp/usr/doc/dc/*
	sed -e 's/VERSION:.*=V/VERSION: $(v)/' \
            -e 's/PACKAGE_REVISION:.*=D/PACKAGE_REVISION: $(d)/' \
            <debian.control.dc >debian-tmp/DEBIAN/control
	chmod 644 debian-tmp/DEBIAN/control
	install -g root -m 755 -o root debian.postinst.dc \
	  debian-tmp/DEBIAN/postinst
	install -g root -m 755 -o root debian.postrm.dc \
	  debian-tmp/DEBIAN/postrm
	dpkg --build debian-tmp
	mv debian-tmp.deb ../dc-$(v)-$(d).deb

binary: binary-bc binary-dc
# Makes a binary package.

source: clean
# 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: clean
# Makes a context diff.
	-test -f stamp-build && make -f debian.rules clean
	-test -d ../$(p)-$(v).orig -o -f ../$(p)-$(v)-`expr $(d) - 1`.diff.gz \
	  || ( echo "Original source package is not available." ; false )
	-test -d ../$(p)-$(v).orig || make -f debian.rules orig
	( cd .. && diff -cNr $(p)-$(v).orig $(p)-$(v) | gzip -9f \
	  > $(p)-$(v)-$(d).diff.gz )
	-test -f stamp-orig \
	  && rm -rf ../$(p)-$(v).orig && rm -f stamp-orig

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

orig:
# Prepares the original package from the previous
# Debian revision source package and context diff.
	( cd .. \
	  && mkdir $(p).orig \
	  && cd $(p).orig \
	  && tar xzf ../$(p)-$(v)-`expr $(d) - 1`.tar.gz \
	  && ( zcat ../../$(p)-$(v)-`expr $(d) - 1`.diff.gz \
	    | grep ^diff | cut -d' ' -f4 | xargs touch ) \
	  && cd $(p)-$(v) \
	  && ( zcat ../../$(p)-$(v)-`expr $(d) - 1`.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

