#! /usr/bin/make -f

# Debian package information
package		= fdflush
docdir		= /usr/share/doc/$(package)
tmpdir		= $(shell pwd)/debian/tmp

# C compiler information
CC				= gcc
CFLAGS			= -O2
LDFLAGS			= -s

all build:
	make CC="$(CC)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"
	touch stamp-build

clean:
	make $(MFLAGS) distclean
	rm -f stamp-build debian/files debian/substvars
	rm -rf debian/tmp

binary: binary-indep binary-arch

binary-indep:

binary-arch: checkroot
	-rm -rf debian/tmp debian/{files,substvars}
	test -f stamp-build || make $(MFLAGS) -f debian/rules build

# Install executable
	install -d -o root -g root -m 755 $(tmpdir)/bin
	install -p -o root -g root -m 755 fdflush $(tmpdir)/bin/
	strip --remove-section=.comment --remove-section=.note \
		--strip-unneeded $(tmpdir)/bin/fdflush

# Install manpages
	install -d -o root -g root -m 755 $(tmpdir)/usr/share/man/man8
	install -p -o root -g root -m 644 fdflush.8 $(tmpdir)/usr/share/man/man8/
	gzip -9 $(tmpdir)/usr/share/man/man8/*

# Install other documentation
	install -d -o root -g root -m 755 $(tmpdir)$(docdir)
	install -p -o root -g root -m 644 debian/changelog $(tmpdir)$(docdir)
	gzip -9 $(tmpdir)$(docdir)/*
	install -p -o root -g root -m 644 debian/copyright $(tmpdir)$(docdir)

# Install Debian-specific stuff
	install -d -o root -g root -m 755 $(tmpdir)/DEBIAN
	install -p -o root -g root -m 755 debian/preinst $(tmpdir)/DEBIAN
	install -p -o root -g root -m 755 debian/postinst $(tmpdir)/DEBIAN
	install -p -o root -g root -m 755 debian/prerm $(tmpdir)/DEBIAN

# Build the package
	dpkg-shlibdeps debian/tmp/bin/fdflush
	dpkg-gencontrol -isp
	dpkg --build debian/tmp ..

checkroot:
    test root = "`whoami`"

