#!/usr/bin/make -f

# Turn on VERBOSE output
#export DH_VERBOSE=1

#enable dpatch
include /usr/share/dpatch/dpatch.make

# These are used for cross-compiling and for saving the configure script
# # from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS := $(CFLAGS) -pipe -Wall -g

ifneq ($(DEB_BUILD_ARCH),amd64)
# these are specifically for 2.4 compat
CONFFLAGS += apr_cv_epoll=no apr_cv_tcp_nodelay_with_cork=no
endif

CONFFLAGS += ac_cv_prog_AWK=mawk

# Enable debug builds
ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
CFLAGS += -O0
else
CFLAGS += -O2
endif

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

ifeq (,$(findstring notest,$(DEB_BUILD_OPTIONS)))
	TEST_TARGET = test
else
	TEST_TARGET =
endif

SHELL=/bin/bash

config.status: configure
	dh_testdir
	# Determine whether it's safe to replace ino_t with unsigned long
	rm -f debian/ino_t_test
	gcc -o debian/ino_t_test debian/ino_t_test.c
	debian/ino_t_test
	rm -f debian/ino_t_test

	mkdir -p build-$(DEB_HOST_GNU_TYPE) build-$(DEB_HOST_GNU_TYPE)/docs
	cd build-$(DEB_HOST_GNU_TYPE) && CFLAGS="$(CFLAGS)" $(CONFFLAGS) ../configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --enable-layout=Debian --includedir=\$${prefix}/usr/include/apr-1.0 --with-installbuilddir=\$${prefix}/usr/share/apr-1.0/build  --enable-nonportable-atomics

build: patch-stamp build-stamp

build-stamp: config.status
	dh_testdir

	cd build-$(DEB_HOST_GNU_TYPE) && $(MAKE)
	cd build-$(DEB_HOST_GNU_TYPE) && $(MAKE) dox

	touch $@

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-stamp test-stamp debian/ino_t_test
	rm -rf build-$(DEB_HOST_GNU_TYPE)
	-$(MAKE) distclean

	dh_clean

test: test-stamp

test-stamp: build
	dh_testdir
	cd build-$(DEB_HOST_GNU_TYPE) && $(MAKE) check
	touch $@

install: build $(TEST_TARGET)
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -a

	cd build-$(DEB_HOST_GNU_TYPE) && $(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs CHANGES
	dh_installdocs
	dh_install -a
	dh_installman -plibapr1-dev debian/apr-1-config.1
	dh_link
	dh_strip --dbg-package=libapr1-dbg
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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