#!/usr/bin/make -f
# -*- makefile -*-

DESTDIR=debian/tmp
VERSION=$(shell sed -r s/-rc[0-9]+$$// version)
SOVERSION=$(firstword $(subst ., ,$(VERSION)))
G++VERSION=$(shell g++ -dumpversion)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

%:
	dh $@

override_dh_auto_build:
	dh_auto_build
	scons platform=linux-gcc
	python $(CURDIR)/doxybuild.py --doxygen=/usr/bin/doxygen

override_dh_auto_test:
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	: # test results are ignored. according to the documentation, not all tests pass
	-scons check platform=linux-gcc
endif

override_dh_auto_clean:
	dh_auto_clean
	scons -c platform=linux-gcc
	$(RM) test/data/test_*_*.actual test/data/test_*_*.actual-rewrite \
		test/data/test_*_*.process-output test/data/test_*_*.rewrite
	find . -iname *.pyc -d | xargs $(RM)
	find . -iname .sconsign.dbm.dblite | xargs $(RM)
	$(RM) -r dist/*
	$(RM) -r doc/doxyfile

override_dh_auto_install:
	dh_auto_install
	install -vd $(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)
	install -vd $(DESTDIR)/usr/lib/pkgconfig
	install -vd $(DESTDIR)/usr/include/json
	install -v libs/linux-gcc-$(G++VERSION)/libjson_linux-gcc-$(G++VERSION)_libmt.so \
		$(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/libjsoncpp.so.$(VERSION)
	ln -sf libjsoncpp.so.$(VERSION) \
		$(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/libjsoncpp.so.$(SOVERSION)
	ln -sf libjsoncpp.so.$(SOVERSION) \
		$(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/libjsoncpp.so
	install -v libs/linux-gcc-$(G++VERSION)/libjson_linux-gcc-$(G++VERSION)_libmt.a \
		$(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/libjsoncpp.a
	install -v -m 0644 debian/pkgconfig/* $(DESTDIR)/usr/lib/pkgconfig
	install -v include/json/* $(DESTDIR)/usr/include/json

	install -d $(DESTDIR)/usr/share/doc/jsoncpp-api-html
	install dist/doxygen/jsoncpp-api-html-$(VERSION)*/* \
		$(DESTDIR)/usr/share/doc/jsoncpp-api-html
