#!/usr/bin/make -f

SHELL=/bin/bash

DESTDIR=$(shell pwd)/debian/tmp

debversion=$(shell head -n1 debian/changelog | cut -d ' ' -f2 | sed 's/[()]//g')
source=$(shell head -n1 debian/changelog | cut -d ' ' -f1)

soname=3
soversion=3.6
changelog=README.36

libpkg=libregina$(soname)
rexxpkg=regina-rexx
destprefix=usr

export DEB_HOST_ARCH  ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
export DEB_BUILD_ARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)

confflags=--prefix=/$(destprefix) --with-addon-dir=/usr/lib/$(source)/$(soversion)/ 

# Enable Regina GCI only on supported platforms.
ifneq ($(filter alpha amd64 i386,$(DEB_HOST_ARCH)), )
  confflags+=--enable-gci
endif

ifeq ($(DEB_BUILD_ARCH), ia64)
  CCOPTS=-g
else
  CCOPTS:=$(shell dpkg-buildflags --get CFLAGS)
endif

CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

build: build-arch build-indep
build-arch: stamp-build
build-indep: stamp-build
stamp-build: stamp-configure
	$(checkdir)
	$(MAKE) DEB_CFLAGS="$(CCOPTS) $(CPPFLAGS)"
	touch stamp-build

configure: stamp-configure
stamp-configure:
	$(checkdir)
	cp -f /usr/share/misc/config.sub common/
	cp -f /usr/share/misc/config.guess common/
	LDFLAGS="$(LDFLAGS)" ./configure $(confflags)
	touch stamp-configure

clean:
	$(checkdir)
	-rm -f stamp-build stamp-configure config.cache config.log config.guess config.sub
	[ ! -f Makefile ] || $(MAKE) distclean
	-chmod 644 debian/md5_sums
	-rm -f `find . \( -name '*~' -o -name '*.bak' \) -print`
	-rm -rf core debian/tmp* debian/files* debian/*substvars debian/shlibs.local
	rm -f common/config.sub common/config.guess

binary-indep: checkroot build
	$(checkdir)

binary-arch: checkroot build
	$(checkdir)
	rm -rf debian/tmp{-lib,-dev,-rexx}
	install -d -m 755 debian/tmp{-lib,-dev,-rexx}/DEBIAN
	install -d -m 755 debian/tmp-lib/$(destprefix)/share/doc/$(libpkg)
	install -d -m 755 debian/tmp-rexx/$(destprefix)/share/doc/$(rexxpkg)
	install -d -m 755 debian/tmp-dev/$(destprefix)/share/doc/$(libpkg)-dev
#
	install -m 755 debian/postinst debian/tmp-lib/DEBIAN/postinst
	install -m 755 debian/postrm debian/tmp-lib/DEBIAN/postrm
	echo "libregina $(soname) $(libpkg) (>= $(debversion))" > debian/shlibs.local
	install -m 644 debian/shlibs.local debian/tmp-lib/DEBIAN/shlibs
#
	$(MAKE) install-lib prefix=debian/tmp-lib/$(destprefix)
	$(MAKE) install-dev prefix=debian/tmp-dev/$(destprefix)
	$(MAKE) install-rexx prefix=debian/tmp-rexx/$(destprefix) \
		rexx_examples=share/doc/$(rexxpkg)/examples
#
	dpkg-gensymbols -p$(libpkg) -Pdebian/tmp-lib -Idebian/$(libpkg).symbols
#
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	strip --strip-unneeded --remove-section=.comment --remove-section=.note \
		debian/tmp-lib/$(destprefix)/lib/libregina.so.$(soversion)
	strip --strip-unneeded --remove-section=.comment --remove-section=.note \
		debian/tmp-lib/$(destprefix)/lib/regina-rexx/$(soversion)/libregutil.so
	strip --strip-debug debian/tmp-dev/$(destprefix)/lib/libregina.a
	strip --strip-all --remove-section=.comment \
		--remove-section=.note debian/tmp-rexx/$(destprefix)/bin/*
endif
#
	cp $(changelog) debian/tmp-lib/$(destprefix)/share/doc/$(libpkg)/changelog
	cp debian/changelog  debian/tmp-lib/$(destprefix)/share/doc/$(libpkg)/changelog.Debian
	gzip -9f debian/tmp-lib/$(destprefix)/share/doc/$(libpkg)/*
	cp debian/copyright debian/tmp-lib/$(destprefix)/share/doc/$(libpkg)
#
	gzip -9f debian/tmp-rexx/$(destprefix)/share/man/man1/*.1
	ln -sf regina.1.gz debian/tmp-rexx/$(destprefix)/share/man/man1/rexx.1.gz
	cp $(changelog) debian/tmp-rexx/$(destprefix)/share/doc/$(rexxpkg)/changelog
	cp debian/changelog  debian/tmp-rexx/$(destprefix)/share/doc/$(rexxpkg)/changelog.Debian
	gzip -9f debian/tmp-rexx/$(destprefix)/share/doc/$(rexxpkg)/changelog*
	cp debian/copyright debian/tmp-rexx/$(destprefix)/share/doc/$(rexxpkg)
#
	gzip -9f debian/tmp-dev/$(destprefix)/share/man/man1/*.1
	cp $(changelog) debian/tmp-dev/$(destprefix)/share/doc/$(libpkg)-dev/changelog
	cp debian/changelog  debian/tmp-dev/$(destprefix)/share/doc/$(libpkg)-dev/changelog.Debian
	gzip -9f debian/tmp-dev/$(destprefix)/share/doc/$(libpkg)-dev/changelog*
	cp debian/copyright debian/tmp-dev/$(destprefix)/share/doc/$(libpkg)-dev
#
	LD_LIBRARY_PATH=/$(destprefix)/lib/libfakeroot:$(DESTDIR)-lib/$(destprefix)/lib \
		dpkg-shlibdeps debian/tmp-lib/$(destprefix)/lib/libregina.so.$(soversion) \
		debian/tmp-lib/$(destprefix)/lib/regina-rexx/$(soversion)/libregutil.so
	dpkg-gencontrol -isp -p$(libpkg) -Pdebian/tmp-lib
	echo "regver:Depends=$(libpkg) (= $(debversion))" > debian/dev-substvars
	dpkg-gencontrol -isp -p$(libpkg)-dev -Tdebian/dev-substvars -Pdebian/tmp-dev
#
	LD_LIBRARY_PATH=/$(destprefix)/lib/libfakeroot:$(DESTDIR)-lib/$(destprefix)/lib \
		dpkg-shlibdeps debian/tmp-rexx/$(destprefix)/bin/rexx \
			       debian/tmp-rexx/$(destprefix)/bin/regina
	dpkg-gencontrol -isp -p$(rexxpkg) -Pdebian/tmp-rexx
#
	chown -R root.root debian/tmp-{lib,dev,rexx}
	chmod -R go=rX,u+w debian/tmp-{lib,dev,rexx}
	chmod 755 debian/md5_sums
	debian/md5_sums tmp-{lib,dev,rexx}
	dpkg --build debian/tmp-lib ..
	dpkg --build debian/tmp-dev ..
	dpkg --build debian/tmp-rexx ..

define checkdir
	test -f debian/rules
endef

binary: binary-indep binary-arch

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

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