#!/usr/bin/make -f
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Uncomment to enable verbose mode.
#export DH_VERBOSE=1

include /usr/share/quilt/quilt.make

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

test:
	dh_testdir
	$(ANT) test

clean: unpatch
	dh_clean build-stamp
	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: $(QUILT_STAMPFN)
	dh_testdir
	printf "version=%s" $(VERSION) > build.properties

	$(ANT) generate-cql-html
	$(ANT) jar
	cd pylib && python3 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

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

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

	# Copy fqltool jars
	dh_install build/tools/lib/fqltool.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 doc/cql3/CQL.css doc/cql3/CQL.html CASSANDRA-14092.txt
	dh_installexamples tools/*.yaml
	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
