#!/usr/bin/make -f

CFLAGS = -Wall -g
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

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS += -O0
else
CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
INSTALL_PROGRAM += -s
endif

package=xmaddressbook

build:
	$(checkdir)

	xmkmf	
	make CDEBUGFLAGS="$(CFLAGS)"

	touch build

clean:
	$(checkdir)
	-rm -f build
	-make clean
	-rm -f `find . -name "*~"`
	-rm -rf debian/tmp debian/files* core debian/substvars

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_DIR) debian/tmp

	make install DESTDIR=$(CURDIR)/debian/tmp BINDIR=/usr/bin
	cd debian/tmp && $(INSTALL_DIR) `cat ../dirs` DEBIAN

	$(INSTALL_FILE) README TODO debian/tmp/usr/share/doc/xmaddressbook
	$(INSTALL_FILE) debian/copyright debian/tmp/usr/share/doc/xmaddressbook/copyright

	$(INSTALL_FILE) debian/changelog debian/tmp/usr/share/doc/xmaddressbook/changelog.Debian

	$(INSTALL_DIR) debian/tmp/usr/share/man/man1/
	$(INSTALL_FILE) xmaddressbook.1 debian/tmp/usr/share/man/man1/xmaddressbook.1

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	strip -R.note -R.comment debian/tmp/usr/bin/xmaddressbook
endif
	gzip -9f debian/tmp/usr/share/man/man1/xmaddressbook.1 debian/tmp/usr/share/doc/xmaddressbook/changelog.Debian debian/tmp/usr/share/doc/xmaddressbook/README debian/tmp/usr/share/doc/xmaddressbook/TODO

	$(INSTALL_SCRIPT) debian/postinst debian/postrm debian/tmp/DEBIAN
	$(INSTALL_FILE) debian/conffiles debian/tmp/DEBIAN/conffiles
	$(INSTALL_FILE) debian/menu debian/tmp/usr/lib/menu/xmaddressbook

	dpkg-shlibdeps -Tdebian/substvars -dDepends debian/tmp/usr/bin/xmaddressbook

	dpkg-gencontrol -isp
	chown -R root.root debian/tmp
	chmod -R u+rw,go=rX debian/tmp
	dpkg --build debian/tmp ..

define checkdir
	test -f debian/rules
endef

# Below here is fairly generic really

binary:		binary-indep binary-arch

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

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