#!/usr/bin/make -f

DEBVERS := $(shell dpkg-parsechangelog | sed -n -e 's/^Version: //p')
VERSION := $(shell echo '$(DEBVERS)' | sed -e 's/^[[:digit:]]*://' -e 's/[~-].*//')

build:
	mkdir -p debian/build
	cd debian/build && cmake -D USER_BUILD_VERSION=${DEBVERS} -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=../tmp/usr ../.. -DBUILD_SHARED_LIBS=OFF
	cd debian/build && $(MAKE)

clean:
	rm -rf debian/files debian/substvars debian/tmp debian/build

binary: binary-arch binary-indep

binary-indep:

binary-arch:
	install -d debian/tmp/DEBIAN
	install -d debian/tmp/usr/bin
	install -d debian/tmp/usr/share/doc/aseba
	install -d debian/tmp/usr/share/doc/aseba/example-code
	install -d debian/tmp/usr/share/doc/aseba/example-scenarios
	cp changelog.md debian/tmp/usr/share/doc/aseba/changelog
	cp debian/changelog debian/tmp/usr/share/doc/aseba/changelog.debian
	cp debian/copyright debian/tmp/usr/share/doc/aseba
	cp aseba/targets/playground/examples/*.png debian/tmp/usr/share/doc/aseba/example-scenarios
	cp aseba/targets/playground/examples/*.playground debian/tmp/usr/share/doc/aseba/example-scenarios
	cp aseba/targets/playground/examples/thymio-default-behaviours.aesl debian/tmp/usr/share/doc/aseba/example-scenarios
	cp aseba/targets/playground/examples/*.aesl debian/tmp/usr/share/doc/aseba/example-code
	cp aseba/targets/challenge/examples/challenge-goto-energy.aesl debian/tmp/usr/share/doc/aseba/example-code
	strip --strip-unneeded -R .note -R .comment  debian/build/*/*.a
	strip --strip-unneeded -R .note -R .comment  debian/build/*/*/*.a
	cd debian/build && $(MAKE) install
	cp debian/README.Debian debian/tmp/usr/share/doc/aseba/
	dpkg-shlibdeps debian/tmp/usr/bin/*
	chown -R root.root debian/tmp
	chmod -R o-s,go=u,go-ws debian/tmp
	dpkg-gencontrol
	dpkg --build debian/tmp ..
