#!/usr/bin/make -f

%:
	dh $@

export DH_VERBOSE=1
export CFLAGS=
export CXXFLAGS=
export CPPFLAGS=

PACKAGE=percona-xtradb-cluster
PS_VERSION_EXTRA = $(shell echo "Percona XtraDB Cluster (GPL), $(DEBVERSION)")
OS_VERSION = $(shell lsb_release -rs)

wsrep_version="26.1.4.3"
percona_server_version="29"
revision="d29a325"
rel="1"
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=""
prefix=/usr

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)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

DEB_SOURCE_PACKAGE ?= $(strip $(shell egrep '^Source: ' debian/control | cut -f 2 -d ':'))
DEB_VERSION ?= $(shell dpkg-parsechangelog | egrep '^Version:' | cut -f 2 -d ' ')
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)

EXPORTED_SOURCE_TARBALL = debian/percona-xtradb-cluster-source-8.0.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

rpathdebug = '$$ORIGIN/../../private/debug'
rpath = '$$ORIGIN/../private'

override_dh_auto_configure:
	@echo "RULES.$@"

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 \
		-DCOMPILATION_COMMENT=$(compilation_comment_debug) \
		-DSYSTEM_TYPE="debian-linux-gnu" \
		-DWITHOUT_TOKUDB=ON \
		-DMYSQL_SERVER_SUFFIX=".$(rel)" \
		-DWITH_WSREP=ON \
		-DWITH_LDAP=system \
		-DINSTALL_LAYOUT=DEB \
		-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 \
		-DWITH_UNIT_TESTS=0 \
		-DFORCE_INSOURCE_BUILD=1 \
		-DDOWNLOAD_BOOST=1 \
		-DWITH_BOOST=libboost \
		-DWITH_SYSTEM_LIBS=ON \
                -DWITH_PROTOBUF=bundled \
                -DWITH_RAPIDJSON=bundled \
                -DWITH_ICU=bundled \
                -DWITH_ZSTD=bundled \
                -DWITH_LZ4=bundled \
                -DWITH_ZLIB=bundled \
                -DWITH_EDITLINE=bundled \
                -DWITH_LIBEVENT=bundled \
                -DWITH_FIDO=bundled \
                -DWITH_NUMA=ON \
                -DWITH_PERCONA_TELEMETRY=ON \
		-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 \
		-DMINIMAL_RELWITHDEBINFO=OFF \
		-DCOMPILATION_COMMENT=$(compilation_comment_release) \
		-DSYSTEM_TYPE="debian-linux-gnu" \
		-DWITHOUT_TOKUDB=ON \
		-DMYSQL_SERVER_SUFFIX=".$(rel)" \
		-DWITH_WSREP=ON \
		-DWITH_LDAP=system \
		-DINSTALL_LAYOUT=DEB \
		-DWITH_INNODB_MEMCACHED=ON \
		-DFORCE_INSOURCE_BUILD=1 \
		-DWITH_MECAB=system \
		-DWITH_ARCHIVE_STORAGE_ENGINE=ON \
		-DWITH_BLACKHOLE_STORAGE_ENGINE=ON \
		-DWITH_FEDERATED_STORAGE_ENGINE=ON \
		-DWITH_PAM=ON \
		-DWITH_ROCKSDB=0 \
		-DWITH_UNIT_TESTS=0 \
		-DDOWNLOAD_BOOST=1 \
		-DWITH_BOOST=libboost \
		-DWITH_SYSTEM_LIBS=ON \
                -DWITH_PROTOBUF=bundled \
                -DWITH_RAPIDJSON=bundled \
                -DWITH_ICU=bundled \
                -DWITH_ZSTD=bundled \
                -DWITH_LZ4=bundled \
                -DWITH_ZLIB=bundled \
                -DWITH_EDITLINE=bundled \
                -DWITH_LIBEVENT=bundled \
                -DWITH_FIDO=bundled \
                -DWITH_NUMA=ON \
		-DWITH_PERCONA_TELEMETRY=ON \
		-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)

	touch $@

override_dh_auto_build:
	@echo "RULES.$@"

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

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)

	touch $@

override_dh_builddeb:
	dh_builddeb -- -Zgzip

override_dh_auto_install:
	@echo "RULES.$@"

	# complete install first
	(cd $(builddebug) && $(MAKE) install DESTDIR=$(TMPD)/)
	(cd $(builddir) && $(MAKE) install DESTDIR=$(TMP)/)
	(cd $(galeradir) && cp -r garb $(TMP)/)
	(cd $(builddir)/$(galeradir) && cp libgalera_smm.so $(TMP)/)
	(cd $(galeradir) && cp packages/rpm/README $(TMP)/)
	mkdir -p $(TMP)/usr/bin/pxc_extra/
	(cp -r pxb-2.4 $(TMP)/usr/bin/pxc_extra/)
	(mv $(TMP)/usr/bin/pxc_extra/pxb-2.4/lib/xtrabackup/* $(TMP)/usr/bin/pxc_extra/pxb-2.4/lib/)
	(rm -rf $(TMP)/usr/bin/pxc_extra/pxb-2.4/lib/xtrabackup)
	(cp -r pxb-8.0 $(TMP)/usr/bin/pxc_extra/)
	(mv $(TMP)/usr/bin/pxc_extra/pxb-8.0/lib/xtrabackup/* $(TMP)/usr/bin/pxc_extra/pxb-8.0/lib/)
	(rm -rf $(TMP)/usr/bin/pxc_extra/pxb-8.0/lib/xtrabackup)


	# add PXC configuration file my.cnf to mysql-common package
	#install -g root -o root -m 0644 -D debian/extra/my.cnf $(TMP)/etc/mysql/my.cnf

	# add MySQL Server debug binary and library to package
	#install -g root -o root -m 0755 debian/extra/server-binary $(TMP)/usr/sbin/mysqld-debug

	# add systemd script
	mkdir -p $(TMP)/var/lib/mysql-files
	mkdir -p $(TMP)/var/lib/mysql
	mkdir -p $(TMP)/var/run/mysql
	mkdir -p $(TMP)/lib/systemd/system
	install -m 0755 debian/extra/mysql-systemd-start $(TMP)/usr/share/mysql/
	install -m 0755 debian/extra/mysql-systemd $(TMP)/usr/bin/
	install -m 0644 debian/extra/mysql.service $(TMP)/lib/systemd/system/
	install -m 0644 debian/extra/mysql@.service $(TMP)/lib/systemd/system/

	install -m 0755 $(TMP)/garb/files/garb-systemd $(TMP)/usr/bin/
	install -m 0644 $(TMP)/garb/files/garb.service $(TMP)/lib/systemd/system/garb.service
	sed -i 's/sysconfig\/garb/default\/garb/' $(TMP)/lib/systemd/system/garb.service

	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
	# FIXME install -m 0755 debian/additions/echo_stderr $(TMP)/usr/share/mysql/
	# install -d $(TMP)/etc/mysql
	# install -m 0755 debian/additions/debian-start $(TMP)/etc/mysql/
	# install -m 0755 debian/additions/debian-start.inc.sh $(TMP)/usr/share/mysql/

	install -d $(TMP)/usr/lib/debug/usr/bin
	objcopy --only-keep-debug $(TMP)usr/bin/garbd $(TMP)/usr/lib/debug/usr/bin/garbd
	chmod 644 $(TMP)/usr/lib/debug/usr/bin/garbd
	strip --remove-section=.comment --remove-section=.note $(TMP)usr/bin/garbd
	objcopy --add-gnu-debuglink $(TMP)/usr/lib/debug/usr/bin/garbd $(TMP)usr/bin/garbd

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

	install -m 0644 $(TMP)/garb/files/garb.cnf $(TMP)/etc/default/garb

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

	# install AppArmor profile for PXC
	install -d $(TMP)/etc/apparmor.d
	install -d $(TMP)/etc/apparmor.d/local

override_dh_install:
	dh_install
	mkdir -p debian/percona-xtradb-cluster-server-debug/usr/lib/mysql/private/debug
	cp ./builddir/library_output_directory/libprotobuf-lite.so* debian/percona-xtradb-cluster-server-debug/usr/lib/mysql/private/debug
	patchelf --debug --set-rpath $(rpathdebug) debian/percona-xtradb-cluster-server-debug/usr/lib/mysql/plugin/debug/group_replication.so
	patchelf --debug --set-rpath $(rpath) debian/percona-xtradb-cluster-server/usr/lib/mysql/plugin/authentication_fido.so
	@echo "RULES.$@"

ifeq ($(OS_VERSION), 18.04)
override_dh_strip:
	dh_strip --dbg-package=percona-xtradb-cluster-dbg --exclude=authentication_fido.so --exclude=libprotobuf-lite.so*
else
override_dh_strip:
	dh_strip --dbg-package=percona-xtradb-cluster-dbg
	dh_strip -Xlibprotobuf-lite
endif

override_dh_installinit:
	@echo "RULES.$@"
	if [ "$(DISTRELEASE)" != "precise" -a "$(DISTRELEASE)" != "trusty" -a "$(DISTRELEASE)" != "wheezy" ]; then dh_systemd_enable --name=mysql; dh_systemd_enable --name=garb; fi
	# dh_installinit --name=mysql -- defaults 19 21
	# dh_installinit --name=garbd -- defaults 19 21
	if [ "$(DISTRELEASE)" != "precise" -a "$(DISTRELEASE)" != "trusty" -a "$(DISTRELEASE)" != "wheezy" ]; then dh_systemd_start --restart-after-upgrade; fi
	touch $@
