#!/usr/bin/make -f
# -*- makefile -*-
export DH_VERBOSE=1

TG_BRANCHES := "debian/locations"

include /usr/share/quilt/quilt.make
-include /usr/share/topgit/tg2quilt.mk

# Package name
PACKAGE_NAME = logapp

# Install program
INSTALL = install

INSTALL_FILE    = $(INSTALL) -p    -o root -g root  -m  644
INSTALL_PROGRAM = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_SCRIPT  = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_DIR     = $(INSTALL) -p -d -o root -g root  -m  755


# The installation directory
PACKAGE_DIR = debian/$(PACKAGE_NAME)

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# Set default CFLAGS
CFLAGS ?= -g

build-arch: build

build: build-stamp

build-indep:

build-stamp: patch
	$(checkdir)
	$(MAKE)
	touch $@

clean: checkroot unpatch
	$(checkdir)
	rm -f build-stamp
	[ ! -f Makefile ] || $(MAKE) clean
	rm -rf $(PACKAGE_DIR) debian/files debian/substvars debian/logapplogmake \
		debian/logapplogsvn debian/logapplogcvs debian/logapp.substvars

install: checkroot build
	$(checkdir)
	$(INSTALL_DIR) $(PACKAGE_DIR)/usr/bin
	$(MAKE) DESTDIR=$(CURDIR)/$(PACKAGE_DIR) install

# Build architecture-independent files here.
binary-indep: checkroot build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: checkroot build install
	$(checkdir)
	$(INSTALL_DIR) $(PACKAGE_DIR)/usr/share/doc/$(PACKAGE_NAME)/
	for i in README TODO;do \
		$(INSTALL_FILE) -m 644 $$i $(PACKAGE_DIR)/usr/share/doc/$(PACKAGE_NAME)/; \
	done;
	$(INSTALL_FILE) -m 644 debian/changelog $(PACKAGE_DIR)/usr/share/doc/$(PACKAGE_NAME)/changelog.Debian
	gzip -9v $(PACKAGE_DIR)/usr/share/doc/$(PACKAGE_NAME)/changelog.Debian
	$(INSTALL_FILE) -m 644 debian/copyright $(PACKAGE_DIR)/usr/share/doc/$(PACKAGE_NAME)/copyright
	$(INSTALL_DIR) $(PACKAGE_DIR)/usr/share/man/man1
	$(INSTALL_FILE) -m 644 $(PACKAGE_NAME).1 $(PACKAGE_DIR)/usr/share/man/man1
	gzip -9v $(PACKAGE_DIR)/usr/share/man/*/*
	(cd $(PACKAGE_DIR)/usr/share/man/man1/;for i in logcvs logmake logsvn;do ln -s logapp.1.gz $$i.1.gz; done)
	# Recreate the symbolic links for the executables, to ensure that they are relative to $PWD
	(cd $(PACKAGE_DIR)/usr/bin;for i in logcvs logmake logsvn;do rm -f "$$i"; ln -s logapp $$i; done)

# Strip binaries
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	strip -R.note -R.comment $(PACKAGE_DIR)/usr/bin/*
endif
	dpkg-shlibdeps $(PACKAGE_DIR)/usr/bin/logapp
	$(INSTALL_DIR) $(PACKAGE_DIR)/DEBIAN
	cd $(PACKAGE_DIR) && find * -type f ! -regex '^DEBIAN/.*' -print0 | LC_ALL=C sort -z | xargs -r0 md5sum > DEBIAN/md5sums
	dpkg-gencontrol -p$(PACKAGE_NAME) -P$(PACKAGE_DIR)
	dpkg-deb --build $(PACKAGE_DIR) ..

binary: binary-indep binary-arch

define checkdir
	test -f logapp.h && test -f debian/rules
endef

checkroot:
	$(checkdir)
	test root = "`whoami`"


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