#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

file_ver    = regina.ver # Versions file
VER_DOT     = $(shell grep VER_DOT $(file_ver) | sed 's/^VER_DOT=//' )
VER_SOMAJOR = $(shell grep VER_SOMAJOR $(file_ver) | sed 's/^VER_SOMAJOR=//' )

HISTORY     = debian/HISTORY
CONFIGS     = config.guess config.sub
CONFIGTGZ   = config.tar.gz

rexxpkg     = regina-rexx

include /usr/share/dpkg/architecture.mk
export DEB_HOST_ARCH DEB_BUILD_ARCH DEB_HOST_MULTIARCH

ifeq ($(DEB_BUILD_ARCH), ia64)
	DEB_CFLAGS_MAINT_STRIP=""
endif

# Get default build flags for maximum hardening (Needs dpkg-dev >= 1.16.1.1)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

include /usr/share/dpkg/buildtools.mk

export CC    # Needed for passing cross compiler.

confflags=--prefix=/usr \
	--build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE) \
	--with-addon-dir=/usr/lib/$(DEB_HOST_MULTIARCH)/$(rexxpkg)/$(VER_DOT) \
	--libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH)

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Preserve old config files and replace them by recent ones.
	[ -e "$(CONFIGTGZ)" ] || tar -cvzf $(CONFIGTGZ) $(addprefix common/,$(CONFIGS))
	for i in $(CONFIGS); do cp -f /usr/share/misc/$$i common/$$i; done
	# Add here commands to configure the package.
	./configure $(confflags)
	touch configure-stamp

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: configure-stamp
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE) CFLAGS="$(CFLAGS) $(CPPFLAGS)" LDFLAGS="$(LDFLAGS)"

        # Bundle all READMEs into a single HISTORY file
	cat $(shell echo  README.0* README.2* README.3* \
		| tr -s ' ' '\n' | sort -r ) > $(HISTORY)

	cp rxqueue.1 rxqueue-regina-rexx.1

	touch build-stamp

clean:
	dh_testdir

	# Add here commands to clean up after the build process.
	rm -f build-stamp configure-stamp
	if [ -e "$(CONFIGTGZ)" ]; then \
	tar -xvzf $(CONFIGTGZ) && rm $(CONFIGTGZ); fi
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f $(HISTORY) rxqueue-regina-rexx.1

	dh_clean

install: build
	dh_testdir
	dh_prep
	dh_installdirs

	# Add here commands to install the package into debian/tmp.
	$(MAKE) install-lib DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) install-dev DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) install-rexx DESTDIR=$(CURDIR)/debian/tmp

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

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs -A $(HISTORY) HACKERS.txt
	dh_install
	dh_installman
	dh_installexamples
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_shlibdeps
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure patch \
	unpatch
