#!/usr/bin/make -f

%:
	dh $@ --parallel --with systemd

include VERSION

export DH_VERBOSE=1
export CFLAGS=-O3 -g -fno-strict-aliasing -Wno-deprecated-declarations -Wno-error=unused-result -Wno-error=nonnull-compare -Wno-maybe-uninitialized -Wno-sign-compare -Wno-unused-parameter -Wno-discarded-qualifiers -Wno-maybe-uninitialized -Wno-error=misleading-indentation
export CXXFLAGS=-O3 -g -felide-constructors -fno-strict-aliasing -Wno-deprecated-declarations -Wno-error=unused-result -Wno-error=nonnull-compare -Wno-maybe-uninitialized -Wno-virtual-move-assign -Wno-sign-compare -Wno-unused-parameter -Wno-maybe-uninitialized -Wno-error=misleading-indentation

DEB_SOURCE_PACKAGE ?= $(shell dpkg-parsechangelog -SSource)
DEB_VERSION ?= $(shell dpkg-parsechangelog -SVersion)
DEB_NOEPOCH_VERSION ?= $(shell echo $(DEB_VERSION) | cut -d: -f2-)
DEB_UPSTREAM_VERSION ?= $(shell echo $(DEB_NOEPOCH_VERSION) | sed 's/-[^-]*$$//')
DEB_UPSTREAM_VERSION_MAJOR_MINOR := $(shell echo $(DEB_UPSTREAM_VERSION) | sed -r -n 's/^([0-9]+\.[0-9]+).*/\1/p')
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

PACKAGE=$(DEB_SOURCE_PACKAGE)
PS_VERSION_EXTRA = $(shell echo "Percona XtraDB Cluster (GPL), $(DEBVERSION)")

wsrep_version = $(shell echo $(DEB_NOEPOCH_VERSION) | sed -E -n 's/^.*-(.*)-.*/\1/p')
percona_server_version = $(shell echo $(MYSQL_VERSION_EXTRA) | sed -E "s/\-//")
revision = $(shell cat PS_REFRESH_VERSION | cut -c1-7)

compilation_comment_debug = "Percona XtraDB Cluster - Debug (GPL), Release rel$(percona_server_version), Revision $(revision), WSREP version $(wsrep_version)"
compilation_comment_release = "Percona XtraDB Cluster (GPL), Release rel$(percona_server_version), Revision $(revision), WSREP version $(wsrep_version)"

server_suffix = "-$(percona_server_version)"

TMP=$(CURDIR)/debian/tmp/
TMPD=$(CURDIR)/debian/tmp-debug/
prefix=/usr

ARCH = $(shell dpkg-architecture -qDEB_BUILD_ARCH)
ARCH_OS = $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS)

NCPU = $(shell grep -c processor /proc/cpuinfo)
MAKE_JFLAG = -j$(NCPU)

DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

# NOTE: Force use of gcc-6 as 7 is currently incompatible.
USE_THIS_GCC_VERSION = -6

export CC=$(DEB_HOST_GNU_TYPE)-gcc$(USE_THIS_GCC_VERSION)
export CXX=$(DEB_HOST_GNU_TYPE)-g++$(USE_THIS_GCC_VERSION)

EXPORTED_SOURCE_TARBALL = debian/percona-xtradb-cluster-source-5.7.tar.gz

DISTRIBUTION = $(shell lsb_release -i -s)
DISTRELEASE = $(shell lsb_release -c -s)

MYSQL_SRC = $(shell pwd)

builddir = builddir
galeradir = percona-xtradb-cluster-galera
builddebug = debug

override_dh_auto_configure:
ifeq ($(SKIP_DEBUG_BINARY),)
	( test -d $(builddebug) || mkdir $(builddebug) ) && cd $(builddebug) && \
		cmake -DBUILD_CONFIG=mysql_release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DINSTALL_DOCDIR=share/mysql/docs \
		-DINSTALL_DOCREADMEDIR=share/mysql \
		-DINSTALL_INCLUDEDIR=include/mysql \
		-DINSTALL_INFODIR=share/mysql/docs \
		-DINSTALL_LIBDIR=lib/$(DEB_HOST_MULTIARCH) \
		-DINSTALL_MANDIR=share/man \
		-DINSTALL_MYSQLSHAREDIR=share/mysql \
		-DINSTALL_MYSQLTESTDIR=lib/mysql-test \
		-DINSTALL_PLUGINDIR=lib/mysql/plugin/debug \
		-DINSTALL_SBINDIR=sbin \
		-DINSTALL_SCRIPTDIR=bin \
		-DINSTALL_SUPPORTFILESDIR=share/mysql \
		-DSYSCONFDIR=/etc/mysql \
		-DMYSQL_UNIX_ADDR=/var/run/mysqld/mysqld.sock \
		-DCMAKE_BUILD_TYPE=Debug \
		-DWITH_LIBWRAP=ON \
		-DWITH_ZLIB=system \
		-DWITH_SSL=system \
		-DWITH_SCALABILITY_METRICS=ON \
		-DSYSTEM_TYPE="debian-linux-gnu" \
		-DWITH_READLINE=system \
		-DWITHOUT_TOKUDB=ON \
		-DCOMPILATION_COMMENT=$(compilation_comment_debug) \
		-DMYSQL_SERVER_SUFFIX=$(server_suffix) \
		-DINSTALL_LAYOUT=RPM \
		-DWITH_INNODB_DISALLOW_WRITES=ON \
		-DWITH_WSREP=ON \
		-DINSTALL_LIBDIR=lib/$(DEB_HOST_MULTIARCH) \
		-DINSTALL_PLUGINDIR=lib/mysql/plugin/debug \
		-DINSTALL_LAYOUT=DEB \
		-DWITH_EMBEDDED_SERVER=OFF \
		-DWITH_INNODB_MEMCACHED=ON \
		-DWITH_MECAB=system \
		-DWITH_ARCHIVE_STORAGE_ENGINE=ON \
		-DWITH_BLACKHOLE_STORAGE_ENGINE=ON \
		-DWITH_FEDERATED_STORAGE_ENGINE=ON \
		-DWITH_PAM=ON \
		-DWITH_ROCKSDB=0 \
		-DDOWNLOAD_BOOST=0 \
		-DWITH_BOOST=../boost \
		-DWITH_EXTRA_CHARSETS=all ..
endif

	( test -d $(builddir) || mkdir $(builddir) ) && cd $(builddir) && \
		cmake -DBUILD_CONFIG=mysql_release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DINSTALL_DOCDIR=share/mysql/docs \
		-DINSTALL_DOCREADMEDIR=share/mysql \
		-DINSTALL_INCLUDEDIR=include/mysql \
		-DINSTALL_INFODIR=share/mysql/docs \
		-DINSTALL_LIBDIR=lib/$(DEB_HOST_MULTIARCH) \
		-DINSTALL_MANDIR=share/man \
		-DINSTALL_MYSQLSHAREDIR=share/mysql \
		-DINSTALL_MYSQLTESTDIR=lib/mysql-test \
		-DINSTALL_PLUGINDIR=lib/mysql/plugin \
		-DINSTALL_SBINDIR=sbin \
		-DINSTALL_SCRIPTDIR=bin \
		-DINSTALL_SUPPORTFILESDIR=share/mysql \
		-DSYSCONFDIR=/etc/mysql \
		-DMYSQL_UNIX_ADDR=/var/run/mysqld/mysqld.sock \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DWITH_LIBWRAP=ON \
		-DWITH_ZLIB=system \
		-DWITH_SSL=system \
		-DWITH_SCALABILITY_METRICS=ON \
		-DSYSTEM_TYPE="debian-linux-gnu" \
		-DWITH_READLINE=system \
		-DWITHOUT_TOKUDB=ON \
		-DCOMPILATION_COMMENT=$(compilation_comment_release) \
		-DMYSQL_SERVER_SUFFIX=$(server_suffix) \
		-DINSTALL_LAYOUT=RPM \
		-DWITH_INNODB_DISALLOW_WRITES=ON \
		-DWITH_WSREP=ON \
		-DINSTALL_LIBDIR=lib/$(DEB_HOST_MULTIARCH) \
		-DINSTALL_PLUGINDIR=lib/mysql/plugin \
		-DINSTALL_LAYOUT=DEB \
		-DWITH_EMBEDDED_SERVER=OFF \
		-DWITH_INNODB_MEMCACHED=ON \
		-DWITH_MECAB=system \
		-DWITH_ARCHIVE_STORAGE_ENGINE=ON \
		-DWITH_BLACKHOLE_STORAGE_ENGINE=ON \
		-DWITH_FEDERATED_STORAGE_ENGINE=ON \
		-DWITH_PAM=ON \
		-DWITH_ROCKSDB=0 \
		-DDOWNLOAD_BOOST=0 \
		-DWITH_BOOST=../boost \
		-DWITH_EXTRA_CHARSETS=all ..

	cd $(galeradir) &&  scons $(MAKE_JFLAG) revno=$(GALERA_REVNO) psi=1 libgalera_smm.so $(SCONS_ARGS) && scons $(MAKE_JFLAG) revno=$(GALERA_REVNO) garb/garbd $(SCONS_ARGS)

override_dh_auto_build:
	[ -f $(EXPORTED_SOURCE_TARBALL) ] || tar -zcf $(EXPORTED_SOURCE_TARBALL) \
	--exclude=debian . \
	--transform="s,^\./,percona-xtradb-cluster-5.7/,"

ifeq ($(SKIP_DEBUG_BINARY),)
	cd $(builddebug) && $(MAKE) -j$(NCPU)  VERBOSE=1
endif

	cd $(builddir) && $(MAKE) -j$(NCPU) VERBOSE=1
	cd $(galeradir) &&  scons $(MAKE_JFLAG) revno=$(GALERA_REVNO) psi=1 libgalera_smm.so $(SCONS_ARGS) && scons $(MAKE_JFLAG) revno=$(GALERA_REVNO) garb/garbd $(SCONS_ARGS)

override_dh_auto_install:
	# complete install first
	(cd $(builddebug) && $(MAKE) install DESTDIR=$(TMPD)/)
	(cd $(builddir) && $(MAKE) install DESTDIR=$(TMP)/)
	(cd $(galeradir) && cp -r garb $(TMP)/)
	(cd $(galeradir) && cp libgalera_smm.so $(TMP)/)
	(cd $(galeradir) && cp packages/rpm/README $(TMP)/)

	# add systemd script
	mkdir -p $(TMP)/var/lib/mysql-files
	mkdir -p $(TMP)/var/lib/percona-xtradb-cluster
	mkdir -p $(TMP)/var/run/mysql
	install -m 0755 debian/extra/mysql-systemd-start $(TMP)/usr/share/mysql/

	mkdir -p $(TMP)/usr/lib/percona-xtradb-cluster-testsuite
	cp -rp $(TMP)/usr/lib/mysql-test/* $(TMP)/usr/lib/percona-xtradb-cluster-testsuite

	# mysql-server
	mkdir -p $(TMP)/usr/share/doc/percona-xtradb-cluster-5.7/examples
	rm -vf $(TMP)/usr/share/percona-xtradb-cluster/mi_test_all* \
		$(TMP)/usr/share/percona-xtradb-cluster/mysql-log-rotate \
		$(TMP)/usr/share/percona-xtradb-cluster/mysql.server \
		$(TMP)/usr/share/percona-xtradb-cluster/binary-configure
	nm -n $(builddir)/sql/mysqld |gzip -9 > $(TMP)/usr/share/doc/percona-xtradb-cluster-5.7/mysqld.sym.gz

	# install default file for init script timeout params
	install -d $(TMP)/etc/default
	install -m 0644 debian/extra/default-mysql $(TMP)/etc/default/mysql
	install -m 0644 debian/extra/default-mysql.bootstrap $(TMP)/etc/default/mysql.bootstrap
        # Add helper functions for maintainer scripts
	install -m 0644 debian/extra/mysql-helpers debian/tmp/usr/share/mysql/

	# install source tarball for source package
	install -D -m 0644 $(EXPORTED_SOURCE_TARBALL) $(TMP)/usr/src/percona-xtradb-cluster/`basename $(EXPORTED_SOURCE_TARBALL)`

override_dh_strip:
	dh_strip --dbg-package=percona-xtradb-cluster-5.7-dbg

override_dh_systemd_enable:
	dh_systemd_enable --name=mysql
	dh_systemd_enable --name=mysql@

override_dh_installinit:
	dh_installinit --name=mysql -- defaults 19 21
	dh_installinit --name=mysql@ -- defaults 19 21

override_dh_systemd_start:
	dh_systemd_start --restart-after-upgrade

refresh-boost:
	bash debian/bundle-boost.sh $(DEB_UPSTREAM_VERSION)
