#!/usr/bin/make -f
#export DH_VERBOSE=1

DPATH := $(abspath $(dir $(MAKEFILE_LIST)))
DTYPE := +dfsg2
PKG := bullet
VER ?= $(shell dpkg-parsechangelog -l$(DPATH)/changelog | perl -ne 'print $$1 if m{Version:\s*(\d[\d\.]*(?:-r(?:ev)?\d+))}')

VERSION=2.81

%:
	dh $@ --parallel --buildsystem=cmake

override_dh_auto_configure:
	dh_auto_configure -- \
		-DBUILD_SHARED_LIBS=on \
		-DBUILD_EXTRAS=on \
		-DBUILD_DEMOS=off \
		-DINSTALL_LIBS=on \
		-DINSTALL_EXTRA_LIBS=on \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DLIB_SUFFIX=/$(DEB_HOST_MULTIARCH)

override_dh_auto_build-indep:
	doxygen
	$(RM) -v html/*.md5 html/*.map
	# De-duplication (http://wiki.debian.org/dedup.debian.net):
	rdfind -outputname /dev/null -makesymlinks true html
	symlinks -rsc html

override_dh_compress:
	dh_compress --exclude=.pdf

override_dh_strip:
	dh_strip --dbg-package=libbullet$(VERSION)-dbg

override_dh_clean:
	dh_clean
	$(RM) -r html


.PHONY: get-orig-source
## http://wiki.debian.org/onlyjob/get-orig-source
get-orig-source: $(PKG)_$(VER)$(DTYPE).orig.tar.xz
	@

$(PKG)_$(VER)$(DTYPE).orig.tar.xz:
	@echo "# Downloading..."
	uscan --noconf --verbose --rename --destdir=$(CURDIR) --check-dirname-level=0 --force-download --download-version $(VER) $(DPATH)
	$(if $(wildcard $(PKG)-$(VER)),$(error $(PKG)-$(VER) exist, aborting..))
	@echo "# Extracting..."
	mkdir $(PKG)-$(VER) \
	&& tar xf $(PKG)_$(VER).orig.tar.* --directory $(PKG)-$(VER) --strip-components 1 \
	|| $(RM) -r $(PKG)-$(VER)
	@echo "# Clean-up..."
	cd $(PKG)-$(VER) \
	&& $(RM) -r -v \
		build \
		Glut \
		GLUT32.DLL \
		glut64.dll \
		Extras/CUDA \
		Extras/khx2dae \
		Extras/glui \
		Extras/CDTestFramework/License.txt.bak \
		Extras/CDTestFramework/GLUT32.DLL \
		Extras/Serialize/BulletXmlWorldImporter/tiny* \
		Demos/BulletDinoDemo \
		Demos/DX11ClothDemo \
		Demos/ParticlesOpenCL \
		UnitTests/cppunit \
		Bullet_User_Manual.pdf \
		autom4te.cache
	@echo "# Packing..."
	find -L "$(PKG)-$(VER)" -xdev -type f -print | sort \
	| XZ_OPT="-6v" tar -caf "$(PKG)_$(VER)$(DTYPE).orig.tar.xz" -T- --owner=root --group=root --mode=a+rX \
	&& $(RM) -r "$(PKG)-$(VER)"
