#!/usr/bin/make -f

# Uncomment to enable verbose mode.
#export DH_VERBOSE=1

include /usr/share/dpatch/dpatch.make

ANT = /usr/bin/ant
VERSION = $(shell dpkg-parsechangelog | sed -ne 's/^Version: \(.*\)/\1/p')

test:
	dh_testdir
	$(ANT) test

clean: unpatch
	dh_testdir
	dh_testroot
	$(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
	dh_clean

build: build-stamp
build-stamp: patch-stamp
	dh_testdir
	printf "version=%s" $(VERSION) > build.properties

	$(ANT) jar
	cd pylib && python setup.py install --no-compile --install-layout deb \
		--root $(CURDIR)/debian/cassandra

	touch build-stamp

install: build
	dh_testdir
	dh_testroot
	dh_installdirs
	dh_install
	dh_pysupport

	# Copy in the jar and symlink to something stable
	dh_install build/apache-cassandra-$(VERSION).jar \
		usr/share/cassandra
	dh_install build/apache-cassandra-thrift-$(VERSION).jar \
		usr/share/cassandra

	# Copy stress jars
	dh_install build/tools/lib/stress.jar \
		usr/share/cassandra
	dh_install tools/lib/*.jar \
		usr/share/cassandra

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

	cd debian/cassandra/etc/sysctl.d && mv cassandra-sysctl.conf cassandra.conf

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installinit -u'start 50 2 3 4 5 . stop 50 0 1 6 .'
	dh_installdocs README.asc CHANGES.txt NEWS.txt
	dh_bash-completion
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-dependent files here.
binary-arch: build install
# We have nothing to do by default.

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install
