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

export DH_VERBOSE=1

TOP_DIR=$(shell pwd)
BUILD_DIR=$(TOP_DIR)/build
DEBIAN_VERSION ?= $(shell dpkg-parsechangelog --show-field=Version)
UPSTREAM_VERSION ?= $(shell echo $(DEBIAN_VERSION) | cut -d "-" -f 1)
GIT_HASH ?= $(shell echo $(DEBIAN_VERSION) | cut -d "+" -f 2)
PKG = $(shell dpkg-parsechangelog --show-field=Source)
GIT_URL_UPSTREAM = https://github.com/GomSpace/libcsp
TMP_ORIG_SOURCE = $(TOP_DIR)/$(PKG)_git

%:
	dh $@

override_dh_auto_configure:
	$(TOP_DIR)/waf \
		-o $(BUILD_DIR) \
		-t $(TOP_DIR) \
		--with-os=posix \
		--enable-if-can --with-driver-can=socketcan \
		--enable-if-zmqhub \
		--enable-bindings \
		--prefix=debian/libcsp1/usr \
		configure

override_dh_auto_build:
	$(TOP_DIR)/waf \
		-o $(BUILD_DIR) \
		-t $(TOP_DIR) \
		build

override_dh_auto_install:
	$(TOP_DIR)/waf \
		-o $(BUILD_DIR) \
		-t $(TOP_DIR) \
		install
	mv debian/libcsp1/usr/lib/libcsp.so debian/libcsp1/usr/lib/libcsp.so.1
	mkdir -p debian/libcsp-source/usr/src
	tar --xz -c -f debian/libcsp-source/usr/src/libcsp-source.tar.xz \
		--transform 's@^@libcsp/@' \
		bindings \
		CHANGELOG \
		CONTRIBUTORS \
		COPYING \
		debian \
		doc \
		examples \
		include \
		INSTALL.md \
		README.md \
		src \
		utils \
		waf \
		waflib \
		wscript

override_dh_auto_clean:
	rm -rf $(BUILD_DIR) .lock*
	find waflib -name "*.pyc" -exec rm {} +

get-orig-source:
	git clone $(GIT_URL_UPSTREAM) $(TMP_ORIG_SOURCE)
	cd $(TMP_ORIG_SOURCE) ; git checkout $(GIT_HASH);
	cd $(TMP_ORIG_SOURCE) ; ./waf --help > /dev/null
	mv $(TMP_ORIG_SOURCE)/.waf-*/* $(TMP_ORIG_SOURCE)
	sed -i '/^#==>$$/,$$d' $(TMP_ORIG_SOURCE)/waf
	rmdir $(TMP_ORIG_SOURCE)/.waf-*
	find $(TMP_ORIG_SOURCE) -name "*.pyc" -exec rm {} +
	tar --exclude=.git -C $(TMP_ORIG_SOURCE) --xz -c -f ../$(PKG)_$(UPSTREAM_VERSION).orig.tar.xz .
	rm -rf $(TMP_ORIG_SOURCE)
