#!/usr/bin/make -f
# Copyright 1998 Rob Browning <rlb@defaultvalue.org>
# This file is covered under the terms of the Gnu Public License.

SHELL=/bin/bash

# the architecture of the package
arch      := $(shell dpkg --print-architecture)

# The Debian target name of the package
target    := ${arch}-debian-linux-gnu

export CFLAGS := -DDEBIAN -O2 -g
export LDFLAGS := -g

debian/stamp-build:
	${checkdir}
	make
	touch debian/stamp-build

build: debian/stamp-build

clean:
	${checkdir}
	rm -f build debian/stamp-* debian/substvars
	-make distclean
	find -name '*~' | xargs --no-run-if-empty rm -f
	-rm -rf debian/tmp debian/files*

binary-indep:	checkroot build
	${checkdir}
        # There are no architecture-independent files to be uploaded
        # generated by this package.  If there were any they would be
        # made here.

binary-arch:	checkroot build
	${checkdir}
	rm -rf debian/tmp

	install -d debian/tmp

        # binaries
	install -d debian/tmp/usr/bin
	cp -a bin/* debian/tmp/usr/bin/
        ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	  strip --strip-unneeded debian/tmp/usr/bin/*
        endif

        # manpages
	install -d debian/tmp/usr/share/man/man1
	cp -a man/* debian/tmp/usr/share/man/man1
	find debian/tmp/usr/share/man -type f | xargs gzip -9v
	(cd debian/tmp/usr/share/man && \
	 for file in `find -type l`; \
	 do \
	   ln -sf lockfile-progs.1.gz $${file}.gz; \
	   rm -f $${file}; \
	 done)

	install -d debian/tmp/usr/share/doc/lockfile-progs
	cp debian/changelog debian/tmp/usr/share/doc/lockfile-progs/

	find debian/tmp/usr/share/doc/lockfile-progs -type f | xargs gzip -9v
	cp debian/copyright debian/tmp/usr/share/doc/lockfile-progs

        # Mangle permissions to conform.
	chown -R root.root debian/tmp
	find debian/tmp -type d | xargs chmod 755
	find debian/tmp -not -type d -a -not -type l | xargs chmod 644
	chmod 755 debian/tmp/usr/bin/*
	chown :mail debian/tmp/usr/bin/mail-lock
	chmod g+s debian/tmp/usr/bin/mail-lock

        # control scripts	
	install -d debian/tmp/DEBIAN
	cp debian/prerm debian/tmp/DEBIAN
	chmod 755 debian/tmp/DEBIAN/prerm

        # Now really get busy.
	dpkg-shlibdeps `find debian/tmp/usr/bin/ -type f`

	dpkg-gencontrol -isp -plockfile-progs
	dpkg --build debian/tmp ..

define checkdir
	test -f debian/rules
	test -f lockfile-progs.c
endef

# Below here is fairly generic really

binary:		binary-indep binary-arch

source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

checkroot:
	${checkdir}
	test root = "`whoami`"

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