#!/usr/bin/make -f

export DH_VERBOSE=1

include /usr/share/dpkg/pkg-info.mk
include /usr/share/dpkg/architecture.mk

export HOME=$(CURDIR)/debian/fakehome
export CASSANDRA_USE_JDK11=true

ANT = /usr/bin/ant
# Cassandra chokes when there's a tilde in the version, eg:
# java.lang.IllegalArgumentException: Invalid version value: 5.0~rc1
VERSION = $(shell echo $(DEB_VERSION_UPSTREAM) | cut -d'~' -f1)

%:
	dh $@ --with python3

execute_after_dh_clean:
	$(ANT) realclean
	rm -f build-stamp build.properties
	rm -rf bin/java
	find -name "*.pyc" -exec rm '{}' ';'
	find -name "*py.class" -exec rm '{}' ';'
	$(RM) -r pylib/build
	rm -rf $(HOME)

execute_before_dh_auto_configure:
	printf "version=%s" $(VERSION) > build.properties

override_dh_auto_build:
	mkdir -p $(HOME)
	dh_auto_build -- -Duser.home=$(HOME)
	cd pylib && python3 setup.py install --no-compile --install-layout deb \
		--root $(CURDIR)/debian/cassandra
	sed -i "s/ARCH/$(DEB_HOST_ARCH)/" debian/cassandra.service
	dh_auto_build --sourcedirectory=pylib --buildsystem=pybuild

execute_after_dh_link:
	dh_link -pcassandra usr/share/cassandra/apache-cassandra-$(VERSION).jar \
		usr/share/cassandra/apache-cassandra.jar

execute_after_dh_auto_install-arch:
	dh_auto_install --destdir=debian/cassandra-libs/ --sourcedirectory=pylib --buildsystem=pybuild

execute_after_dh_install-indep:
	cd debian/cassandra/etc/sysctl.d && mv cassandra-sysctl.conf cassandra.conf
	# Don't use the bundled cassandra driver, depend on python3-cassandra instead
	rm -v debian/cassandra/usr/share/cassandra/lib/cassandra-driver-internal-only-*.zip

execute_after_dh_install-arch:
ifeq (amd64, $(DEB_HOST_ARCH))
	rm debian/cassandra-libs/usr/lib/cassandra-libs/lib/sigar-bin/libsigar-x86-linux.so
else
	rm debian/cassandra-libs/usr/lib/cassandra-libs/lib/sigar-bin/libsigar-amd64-linux.so
endif
