#!/usr/bin/make -f

%:
	dh $@

export DH_VERBOSE=1
export CFLAGS=-Wno-error=free-nonheap-object -Wno-error=implicit-function-declaration
export CXXFLAGS=-Wno-error=free-nonheap-object -Wno-error=implicit-function-declaration
export CPPFLAGS=-Wno-error=free-nonheap-object -Wno-error=implicit-function-declaration

PACKAGE=percona-server
PS_VERSION_EXTRA = '1'
REVISION = '67b170e6'
COMPILATION_COMMENT_RELEASE = "Percona Server (GPL), Release $(PS_VERSION_EXTRA), Revision $(REVISION)"
COMPILATION_COMMENT_DEBUG = "Percona Server - Debug (GPL), Release $(PS_VERSION_EXTRA), Revision $(REVISION)"
TOKUDB_BACKUP_VERSION = "9.7.1-1"

V8PWD=/mnt/jenkins/workspace/ps9.0-RELEASE/test/v8
TMP=$(CURDIR)/debian/tmp/
TMPD=$(CURDIR)/debian/tmp-debug/
prefix=/usr
rpath='$$ORIGIN/../private'

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)

TOKUDB_OPTS_DEFAULT ?= -DWITH_VALGRIND=OFF -DUSE_VALGRIND=OFF -DDEBUG_EXTNAME=OFF -DBUILD_TESTING=OFF -DUSE_GTAGS=OFF -DUSE_CTAGS=OFF -DUSE_ETAGS=OFF -DUSE_CSCOPE=OFF -DTOKUDB_BACKUP_PLUGIN_VERSION=$(TOKUDB_BACKUP_VERSION)
TOKUDB_OPTS_RELEASE ?= $(TOKUDB_OPTS_DEFAULT) -DTOKU_DEBUG_PARANOID=OFF
TOKUDB_OPTS_DEBUG ?= $(TOKUDB_OPTS_DEFAULT) -DTOKU_DEBUG_PARANOID=ON

EXPORTED_SOURCE_TARBALL = debian/percona-server-source.tar.gz

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

# Disable LTO only for focal and bullseye
ifeq ($(DISTRELEASE),focal)
	LTO_FLAG = -DWITH_LTO=OFF
else ifeq ($(DISTRELEASE),bullseye)
	LTO_FLAG = -DWITH_LTO=OFF
else
	LTO_FLAG = -DWITH_LTO=ON
endif

# Use bundled zlib on distros with zlib < 1.2.13
ifeq ($(DISTRELEASE),focal)
	ZLIB_OPTION = -DWITH_ZLIB=bundled
else ifeq ($(DISTRELEASE),jammy)
	ZLIB_OPTION = -DWITH_ZLIB=bundled
else ifeq ($(DISTRELEASE),bullseye)
	ZLIB_OPTION = -DWITH_ZLIB=bundled
else ifeq ($(DISTRELEASE),buster)
	ZLIB_OPTION = -DWITH_ZLIB=bundled
else
	ZLIB_OPTION = -DWITH_ZLIB=system
endif

# Use bundled lz4 on distros with lz4 < 1.9.3
ifeq ($(DISTRELEASE),focal)
	LZ4_OPTION = -DWITH_LZ4=bundled
else ifeq ($(DISTRELEASE),buster)
	LZ4_OPTION = -DWITH_LZ4=bundled
else
	LZ4_OPTION = -DWITH_LZ4=system
endif

MYSQL_SRC = $(shell pwd)

# Profile-Guided Optimization: disabled by default
# To enable: export DEB_PGO=1 before dpkg-buildpackage
ifdef DEB_PGO
PGO_GENERATE = -DFPROFILE_GENERATE=1
PGO_USE = -DFPROFILE_USE=1
endif

builddir = builddir
builddebug = debug

# Common cmake flags shared across debug, release, and PGO builds.
# Each build adds only its unique flags (build type, plugin dir, compilation
# comment, TOKUDB opts) and references this list.
CMAKE_COMMON_FLAGS = \
		-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_SBINDIR=sbin \
		-DINSTALL_SCRIPTDIR=bin \
		-DINSTALL_SUPPORTFILESDIR=share/mysql \
		-DSYSCONFDIR=/etc/mysql \
		-DMYSQL_UNIX_ADDR=/var/run/mysqld/mysqld.sock \
		-DSYSTEM_TYPE="debian-linux-gnu" \
		-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=ON \
		-DROCKSDB_DISABLE_AVX2=1 \
		-DROCKSDB_DISABLE_MARCH_NATIVE=1 \
		-DWITH_PACKAGE_FLAGS=OFF \
		-DWITH_SYSTEM_LIBS=ON \
		-DWITH_PROTOBUF=bundled \
		-DWITH_RAPIDJSON=bundled \
		-DWITH_ICU=bundled \
		-DWITH_ZSTD=system \
		$(ZLIB_OPTION) \
		$(LZ4_OPTION) \
		-DWITH_EDITLINE=bundled \
		-DWITH_LIBEVENT=bundled \
		-DWITH_FIDO=bundled \
		-DWITH_ENCRYPTION_UDF=ON \
		-DWITH_NUMA=ON \
		-DWITH_LDAP=system \
		-DWITH_PERCONA_TELEMETRY=ON \
		-DWITH_JS_LANG=ON -DV8_INCLUDE_DIR=$(V8PWD)/include -DV8_LIB_DIR=$(V8PWD)/out.gn/static/obj \
		$(LTO_FLAG) \
		-DWITH_EXTRA_CHARSETS=all

override_dh_auto_configure:
	@echo "RULES.$@"

ifeq ($(SKIP_DEBUG_BINARY),)
	( test -d $(builddebug) || mkdir $(builddebug) ) && cd $(builddebug) && \
	    cmake -DCMAKE_BUILD_TYPE=Debug \
		-DINSTALL_PLUGINDIR=lib/mysql/plugin/debug \
		-DCOMPILATION_COMMENT=$(COMPILATION_COMMENT_DEBUG) \
		$(CMAKE_COMMON_FLAGS) \
		$(TOKUDB_OPTS_DEBUG) ..
endif

	( test -d $(builddir) || mkdir $(builddir) ) && cd $(builddir) && \
	    cmake $(PGO_GENERATE) \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DINSTALL_PLUGINDIR=lib/mysql/plugin \
		-DCOMPILATION_COMMENT=$(COMPILATION_COMMENT_RELEASE) \
		$(CMAKE_COMMON_FLAGS) \
		$(TOKUDB_OPTS_RELEASE) ..

	touch $@

override_dh_auto_build:
	@echo "RULES.$@"
	
	[ -f $(EXPORTED_SOURCE_TARBALL) ] || tar -zcf $(EXPORTED_SOURCE_TARBALL) \
	--exclude=debian . \
	--transform="s,^\./,percona-server/,"

ifeq ($(SKIP_DEBUG_BINARY),)
	# limited to 4 due to WITH_LTO=ON
	cd $(builddebug) && $(MAKE) -j4 VERBOSE=1
endif

	cd $(builddir) && $(MAKE) -j$(NCPU) VERBOSE=1

# PGO rebuild: only runs when DEB_PGO=1 is set
ifdef DEB_PGO
	# PGO: Run MTR load to generate profile data, then rebuild
	cd $(builddir) && $(MAKE) run-profile-suite && rm -r $$(readlink mysql-test/var)
	rm -rf $(builddir)
	mkdir $(builddir) && cd $(builddir) && \
	    cmake $(PGO_USE) \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DINSTALL_PLUGINDIR=lib/mysql/plugin \
		-DCOMPILATION_COMMENT=$(COMPILATION_COMMENT_RELEASE) \
		$(CMAKE_COMMON_FLAGS) \
		$(TOKUDB_OPTS_RELEASE) ..
	cd $(builddir) && $(MAKE) -j$(NCPU) VERBOSE=1
endif

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)/)

	# add Percona Server 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
	install -m 0755 debian/extra/mysql-systemd-start $(TMP)/usr/share/mysql/
	install -m 0755 debian/extra/mysqlrouter-systemd-start $(TMP)usr/share/mysqlrouter/

	# 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/
	# add apparmor profile
#	install -g root -o root -m 0644 -D debian/extra/apparmor-profile $(TMP)/etc/apparmor.d/usr.sbin.mysqld
#	install -g root -o root -m 0644 -D debian/extra/apparmor-profile-router $(TMP)/etc/apparmor.d/usr.bin.mysqlrouter


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

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

override_dh_strip:
	dh_strip \
		-Xlibprotobuf-lite -Xlibprotobuf. \
		-Xlibabsl_ \
		-Xlibicu \
		-Xlibmysqlclient.a -Xlibperconaserverclient.a

override_dh_shlibdeps:
	dh_shlibdeps -Lpercona-server-server -l/usr/lib/mysql/private --dpkg-shlibdeps-params=--ignore-missing-info

override_dh_missing:
	dh_missing --list-missing

override_dh_install:
	dh_install
	# Install mysqld_pre_systemd if built (requires libsystemd-dev at cmake time)
	if [ -f debian/tmp/usr/bin/mysqld_pre_systemd ]; then \
		install -D -m 0755 debian/tmp/usr/bin/mysqld_pre_systemd debian/percona-server-server/usr/bin/mysqld_pre_systemd; \
	fi
	cp js/LICENSE* debian/percona-server-js/usr/share/doc/percona-server-js/

override_dh_installinit:
	@echo "RULES.$@"
	dh_apparmor -ppercona-server-server --profile-name=usr.sbin.mysqld
	dh_apparmor -ppercona-mysql-router --profile-name=usr.bin.mysqlrouter
	dh_installsystemd --name=mysql
	dh_installsystemd --name=mysql@ --no-enable --no-start
	dh_installsystemd --name=mysqlrouter
