#!/usr/bin/make -f

.PHONY: build

VERSION:=$(shell cat VERSION)
export VERSION

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

build:
	dh $@

override_dh_auto_configure:

override_dh_prep:
	dh_prep -Xdebian/tmp

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

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

override_dh_auto_build: dirs
	V=1 LDCONFIG=: WIRINGPI_SUDO= WIRINGPI_SUID=0 \
	    DESTDIR=`pwd`/debian/tmp/usr \
	    PREFIX= WIRINGPI_SUDO= \
	    bash -xe ./build

override_dh_auto_install:
	dh_install
	set -ex; for l in libwiringPi libwiringPiDev; do \
		ln -sf $$l.so.$${VERSION} \
			debian/libwiringpi$$soname/usr/lib/$$l.so.$$soname; \
		ln -sf $$l.so.$${VERSION} \
			debian/libwiringpi-dev/usr/lib/$$l.so; \
	done

%:
	dh $@
