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

SHELL=/bin/bash

pwd       := $(shell pwd)
version   := $(shell dpkg-parsechangelog | grep Version)
version   := $(shell echo ${version} | perl -pe 's/Version:\s+//o')
upstream  := $(shell echo ${version} | perl -pe 's/-.*//o')
major_rev := $(shell echo ${upstream} | perl -pe 's/\.[^.]*$$//o')
minor_rev := $(shell echo ${upstream} | perl -pe 's/^[^.]*\.//o')
deb_rev   := $(shell echo ${version} | perl -pe 's/.*-//o')

# 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

check_vars:
	@echo "pwd: ${pwd}"
	@echo "version: ${version}"
	@echo "upstream: ${upstream}"
	@echo "major_rev: ${major_rev}"
	@echo "minor_rev: ${minor_rev}"
	@echo "deb_rev: ${deb_rev}"
.PHONY: check_vars


# Not needed yet...
#debian/% : debian/%.in debian/changelog
#	perl -p -e "s/\@MAJOR_VERSION\@/${major_rev}/go;" \
#          -e "s/\@MINOR_VERSION\@/${minor_rev}/go;" \
#          -e "s/\@FULL_VERSION\@/${upstream}/go;" \
#          -e "s/\@DEBIAN_REVISION\@/${deb_rev}/go;" \
#			> $@ < $<

#debian/stamp-setup: debian/control debian/copyright debian/menu
#	find debian/bin -type f | xargs chmod 775
#	touch debian/stamp-setup

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
	strip `find debian/tmp/usr/bin/ -type f`

  # manpages
	install -d debian/tmp/usr/man/man1
	cp -a man/* debian/tmp/usr/man/man1
	find debian/tmp/usr/man -type f | xargs gzip -9v
	(cd debian/tmp/usr/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/doc/lockfile-progs
	cp debian/changelog debian/tmp/usr/doc/lockfile-progs/

	find debian/tmp/usr/doc/lockfile-progs -type f | xargs gzip -9v
	cp debian/copyright debian/tmp/usr/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
  #(cd debian && cp preinst postinst prerm postrm tmp/DEBIAN)
  #(cd debian/tmp/DEBIAN && chmod 755 preinst postinst prerm postrm)

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

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

define checkdir
	test -f debian/rules
	test -f lockfile-progs.1
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

# Local variables:
# mode: makefile
# tab-width: 2
# End:
