#!/usr/bin/make -f
# written by Jan Wagner <waja@cyconet.org>

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

# 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 := $(shell dpkg-buildflags --get CFLAGS)
CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS)
LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS)

CFLAGS += -Wall
# Makefile's value is otherwise overriden and modules no longer work without
LDFLAGS += -Wl,--export-dynamic

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
MAKEFLAGS += -j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

config.status: configure
	dh_testdir
	# non-standard Makefile.in, does not use CPPFLAGS and does
	# not use CFLAGS when linking, so work around that:
	./configure --prefix=/usr --mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info --sysconfdir=/etc \
		--localstatedir=/var \
		--build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_BUILD_GNU_TYPE) \
		CFLAGS='$(CPPFLAGS) $(CFLAGS)' LDFLAGS='$(CFLAGS) $(LDFLAGS)'
	# avoid Makefile overriding optimisation flags:
	sed -e '/-Wall -O2/d' -i Makefile

build: build-arch build-indep
build-arch: build-stamp
build-indep:
build-stamp: config.status
	dh_testdir
	# Makefile uses CFLAGS += @CFLAGS@ ..., so empty the on in environment
	# Makefile also overrides the one set by configure, so supply it again...
	CFLAGS="" $(MAKE) LDFLAGS='$(CFLAGS) $(LDFLAGS)'
	touch build-stamp

clean:
	rm -f build-stamp
ifeq ($(shell ls Makefile),Makefile)
	$(MAKE) mrproper
endif
	dh clean

binary-indep:

binary-arch: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Add here commands to install the arch part of the package
	$(MAKE) DESTDIR=debian/arpalert install PREFIX=debian/arpalert/usr
	install -d -m 0755 debian/arpalert/usr/share/doc/arpalert/examples/scripts/
	cp -R scripts/contribs debian/arpalert/usr/share/doc/arpalert/examples/scripts/
	cp -R scripts/test_scripts debian/arpalert/usr/share/doc/arpalert/examples/scripts/test
	# move alert script to /usr/share/arpalert/
	install -d -m 0755 debian/arpalert/usr/share/arpalert/
	mv debian/arpalert/usr/share/doc/arpalert/examples/scripts/contribs/send_alert.pl debian/arpalert/usr/share/arpalert/
	dh binary-arch --until dh_compress
	dh_fixperms --exclude=/var/lib/arpwatch
	dh binary-arch --after dh_fixperms

binary: binary-indep binary-arch

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