#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
#export DH_VERBOSE = 1

# These won't do anything until the Makefiles are fixed
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

VERSION:=$(shell cat VERSION)
export VERSION
MAJOR:=$(shell echo $${VERSION%%.*})
WIRINGPI_SONAME_SUFFIX:=.$(MAJOR)
export WIRINGPI_SONAME_SUFFIX


%:
	dh $@

override_dh_prep:
	dh_prep -Xdebian/tmp

dirs:
	dh_installdirs -A
	mkdir debian/tmp
	set -e; for pkg in `dh_listpackages`; do \
		(cd debian/$$pkg; find -type d) | \
		(cd debian/tmp; xargs mkdir -p) \
	done
	mkdir -p debian/tmp/usr/lib/$(DEB_TARGET_GNU_TYPE)/

override_dh_clean:
	dh_clean
	WIRINGPI_SUDO= sh -xe ./build clean

override_dh_auto_build: dirs
	V=1 LDCONFIG=: WIRINGPI_SUDO= WIRINGPI_SUID=0 \
		DESTDIR=`pwd`/debian/tmp/usr PREFIX= sh -xe ./build
	mv `find debian/tmp/usr/lib -type f` debian/tmp/usr/lib/$(DEB_TARGET_GNU_TYPE)/
	set -ex; for lib in `find debian/tmp/usr/lib/$(DEB_TARGET_GNU_TYPE)/ -type f`; do \
		ln -s $${lib##*/} $${lib%%.$(VERSION)}; \
		ln -s $${lib##*/} $${lib%%.$(VERSION)}.$(MAJOR); \
	done
