#!/usr/bin/make -f

BUILDDIR = debian/wmwork
DEBDIR   = $(BUILDDIR)/DEBIAN
DOCDIR   = $(BUILDDIR)/usr/share/doc/wmwork
testdir  = test -f src/wmwork.c && test -f debian/rules
testroot = test x`whoami` = xroot

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	CONFFLAGS += --build $(DEB_HOST_GNU_TYPE)
else
	CONFFLAGS += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

CPPFLAGS = -Wall $(shell dpkg-buildflags --get CPPFLAGS)
export CPPFLAGS

CFLAGS = -Wall -g $(shell dpkg-buildflags --get CFLAGS)
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
export CFLAGS

LDFLAGS = -Wall $(shell dpkg-buildflags --get LDFLAGS)
export LDFLAGS

clean:
	$(testdir)
	rm -f build debian/files debian/substvars
	[ ! -f src/Makefile ] || $(MAKE) -C src distclean
	rm -rf debian/wmwork

build: build-arch

build-indep:

build-arch:
	$(testdir)
	cd src && ./configure $(CONFFLAGS) --prefix=/usr --mandir=/usr/share/man
	$(MAKE) -C src
	touch build

binary: binary-arch

binary-indep:

binary-arch: build
	$(testdir)
	$(testroot)
	rm -rf debian/wmwork

	$(MAKE) -C src install DESTDIR=$(CURDIR)/debian/wmwork
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	strip -R .comment -R .note $(BUILDDIR)/usr/bin/wmwork
endif
	gzip -9 $(BUILDDIR)/usr/share/man/man1/wmwork.1
	install -D -o root -g root -m 0644 debian/menu $(BUILDDIR)/usr/share/menu/wmwork

	install -D -o root -g root -m 0644 CHANGES $(DOCDIR)/changelog
	install -D -o root -g root -m 0644 debian/changelog $(DOCDIR)/changelog.Debian
	install    -o root -g root -m 0644 README debian/copyright $(DOCDIR)
	install -D -o root -g root -m 0644 debian/worklog $(DOCDIR)/examples/worklog
	gzip -9 $(DOCDIR)/changelog*

	install -d -o root -g root -m 0755 $(DEBDIR)
	install    -o root -g root -m 0644 debian/control $(DEBDIR)
	install    -o root -g root -m 0755 debian/postinst debian/postrm $(DEBDIR)
	cd $(BUILDDIR) && find usr -type f -print0 | xargs -0 md5sum > DEBIAN/md5sums
	chmod 0644 $(DEBDIR)/md5sums

	dpkg-shlibdeps $(BUILDDIR)/usr/bin/wmwork
	dpkg-gencontrol -isp -pwmwork -P$(BUILDDIR)
	dpkg --build $(BUILDDIR) ..

.PHONY: clean binary binary-indep binary-arch
