#!/usr/bin/make -f

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

%:
	dh $@

override_dh_auto_configure:
	set -e; for mf in `find . -name Makefile`; do \
	  [ -f $$mf-orig ] || mv -f $$mf $$mf-orig; \
	done
	./configure --prefix=/usr \
	  --libdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
	  --with-libcurl

override_dh_auto_build:
	$(MAKE) install DESTDIR=`pwd`/build \
	  CC="$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)" \
	  RPATHFLAGS= STRIP=:
	$(MAKE) -C doc wpg.info DESTDIR=`pwd`/build \
	  INSTALLINFO=:

override_dh_auto_test:
# Some tests will fail without network access.
# Ignoring failure for now.
	-$(MAKE) check DESTDIR=`pwd`/build

override_dh_auto_install:
	rm -rf debian/tmp
	mv build debian/tmp
	chown root:root -R debian/tmp

override_dh_install:
	find debian/tmp \! -type d
	dh_install
	dh_install -pwfdb \
	  -X/wfdb-config \
	  -X/wave \
	  -X/wave-remote \
	  -X/wavescript \
	  -X/url_view \
	  'usr/bin/*'
# WAG includes man pages for a number of programs that are not
# actually part of this package.  Do not include these, to prevent
# conflicts with future packages.
	dh_install -pwfdb \
	  -X/wfdb-config.1 \
	  -X/wave.1 \
	  -X/dfa.1 \
	  -X/ecgpuwave.1 \
	  -X/edr.1 \
	  -X/imageplt.1 \
	  -X/lwcat.1 \
	  -X/pNNx.1 \
	  -X/plt.1 \
	  -X/pltf.1 \
	  -X/pnnlist.1 \
	  'usr/share/man/man1/*'

override_dh_auto_clean:
	rm -f contents.tmp contents.pkg
	[ ! -f Makefile ] || $(MAKE) clean
	set -e; for of in `find . -name Makefile-orig`; do \
	  mf=$${tf%-orig}; mv -f $$of $$mf; \
	done
