#!/usr/bin/make -f

export DH_VERBOSE=1

# enable Debian Hardening
# see: https://wiki.debian.org/Hardening
export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-pie
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

ARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH)
ARCH_OS := $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS)
BUILDDIR := builddir
builddir = $(BUILDDIR)
DEBVERSION := $(shell dpkg-parsechangelog | awk '/^Version: / { print $$2 }' | sed 's/^.*-//' )
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_BUILD_GNU_SYSTEM ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_SYSTEM)
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)
DISTRIBUTION := $(shell lsb_release -i -s)
EXPORTED_SOURCE_TARBALL := debian/mysql-source-5.7.tar.gz
TMP := $(CURDIR)/debian/tmp/
NUMJOBS=$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
TESTJOBS=$(NUMJOBS)
# Some tests will fail on platforms that are not tested by upstream
ifneq (,$(filter $(ARCH), amd64 i386))
    TESTSUITE_FAIL_CMD:=exit 1
else
    TESTSUITE_FAIL_CMD:=true
endif

ifeq (,$(NUMJOBS))
	NUMJOBS=1
endif

MAKEFLAGS += -j $(NUMJOBS)

# The below system setting constrains how many servers can be run in parallel
# A server with default config needs a bit over 5300 slots, so we assume 6000 to be sure
AIOMAX := $(shell cat /proc/sys/fs/aio-max-nr)
AIOPER := 6000
PARMAX := $(shell echo $$(( $(AIOMAX)/$(AIOPER))))

ifeq ($(shell test $(TESTJOBS) -gt $(PARMAX); echo $$?),0)
	TESTJOBS:=$(shell echo $(PARMAX) )
endif

export MYSQL_BUILD_CC=$(DEB_HOST_GNU_TYPE)-gcc
export MYSQL_BUILD_CXX=$(DEB_HOST_GNU_TYPE)-g++

# The full upstream test suite takes a very long time to run, so we run
# only the main test suite unless fulltest is specified.
ifeq ($(findstring fulltest,$(DEB_BUILD_OPTIONS)),)
    MTR_SUITE=--suite=main
endif


override_dh_auto_clean:
	@echo "RULES.$@"
	dh_testdir 
	dh_testroot
	[ ! -d mysql-test/var ] || rm -rf mysql-test/var
	rm -rf $(BUILDDIR)
	debconf-updatepo
	rm -f $(EXPORTED_SOURCE_TARBALL)

override_dh_prep: 

override_dh_auto_configure: configure-stamp


configure-stamp:
	@echo "RULES.$@"
	dh_testdir
	( test -d $(builddir) || mkdir $(builddir) ) && cd $(builddir) && \
	sh -c  'PATH=$${MYSQL_BUILD_PATH:-"/bin:/usr/bin"} \
		CC=$${MYSQL_BUILD_CC:-gcc} \
		CXX=$${MYSQL_BUILD_CXX:-g++} \
		cmake -DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		-DMYSQL_UNIX_ADDR=/var/run/mysqld/mysqld.sock \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DBUILD_CONFIG=mysql_release \
		-DWITH_LIBWRAP=ON \
		-DWITH_ZLIB=system \
		-DWITH_LZ4=system \
		-DWITH_EDITLINE=system \
		-DWITH_LIBEVENT=system \
		-DWITH_BOOST=../boost \
		-DCOMPILATION_COMMENT="($(DISTRIBUTION))" \
		-DMYSQL_SERVER_SUFFIX="-$(DEBVERSION)" \
		-DINSTALL_LAYOUT=DEB \
		-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 \
		-DWITH_EMBEDDED_SERVER=ON \
		-DWITH_ARCHIVE_STORAGE_ENGINE=ON \
		-DWITH_BLACKHOLE_STORAGE_ENGINE=ON \
		-DWITH_FEDERATED_STORAGE_ENGINE=ON \
		-DWITH_INNODB_MEMCACHED=1 \
		-DWITH_EXTRA_CHARSETS=all ..'
	touch $@

override_dh_auto_build: build-stamp

# $(EXPORTED_SOURCE_TARBALL) must be built before the builds commence, since
# the builds change some of the files picked up by this tar invocation, and
# this can race against tar with an error like "tar:
# ./builddir-pic/strings/CMakeFiles/strings.dir: file changed as we read it".
# So define the rule for building it here, and have the build rules depend on
# it.
$(EXPORTED_SOURCE_TARBALL):
	GZIP=-n tar -zcf $(EXPORTED_SOURCE_TARBALL) \
	    --exclude=debian . \
	    --transform="s,^\./,mysql-5.7/,"

build-stamp: $(EXPORTED_SOURCE_TARBALL)
	@echo "RULES.$@"
	cd $(builddir) && $(MAKE)
	touch $@

override_dh_auto_test:
	@echo "RULES.$@"
ifeq ($(findstring nocheck,$(DEB_BUILD_OPTIONS)),)
	cp -r mysql-test/* $(builddir)/mysql-test/
	cp -r sql/share/* $(builddir)/sql/share/
	cp -r scripts/*sql $(builddir)/scripts/
ifeq ($(ARCH_OS),hurd)
	# Tests not fully working under Hurd, see
	# http://bugs.mysql.com/bug.php?id=64685. Run unit tests only.
	cd $(builddir) && $(MAKE) test || $(TESTSUITE_FAIL_CMD) ;
else
	touch $(builddir)/mysql-test/skiplist
	# Tests that are known to be unstable on all platforms are skipped
	# http://bugs.mysql.com/bug.php?id=83340
	echo "main.xa_prepared_binlog_off	: unstable test" >> $(builddir)/mysql-test/skiplist

	# Skip replication tests since they are timing sensitive and may
	# result in false positives.
	cd $(builddir)/mysql-test && ./mtr --report-unstable-tests --parallel=$(TESTJOBS) --skip-rpl $(MTR_SUITE) --force --skip-test-list=./skiplist || $(TESTSUITE_FAIL_CMD) ;
endif
endif

override_dh_auto_install: auto_install-stamp

auto_install-stamp:
	@echo "RULES.$@"
	dh_testdir
	dh_testroot
	# make install (trailing slash needed for innobase)
	cd $(builddir) && $(MAKE) install DESTDIR=$(TMP)/
	# After installing, remove rpath to make lintian happy.
	set +e; \
	find ./debian/tmp/ -type f -print0 \
		| xargs -0 --no-run-if-empty chrpath -k 2>/dev/null \
		| fgrep RPATH= \
		| cut -d: -f 1 \
		| xargs --no-run-if-empty chrpath -d; \
	set -e
	install -d -m 0755 -o root -g root $(TMP)/usr/lib/mysql
	mkdir -p $(TMP)/usr/share/doc/mysql-server-5.7
	nm -n $(BUILDDIR)/sql/mysqld |gzip -n -9 > $(TMP)/usr/share/doc/mysql-server-5.7/mysqld.sym.gz
	# This seems to be the only safe place to fix permissions issues
        # not handled by dh_fixperms.
	install -m 0755 debian/additions/echo_stderr $(TMP)/usr/share/mysql/
	mkdir -p $(TMP)/etc/mysql
	install -m 0755 debian/additions/debian-start $(TMP)/etc/mysql/
	install -m 0644 debian/additions/mysql_embedded.1 $(TMP)/usr/share/man/man1
	install -m 0644 debian/additions/debian_create_root_user.sql $(TMP)/usr/share/mysql/
	# These files are incorrectly marked as executable. Upstream bug: http://bugs.mysql.com/bug.php?id=80571
	chmod 0644 $(TMP)/usr/lib/mysql-test/t/mysqld_safe.sh
	chmod 0644 $(TMP)/usr/lib/mysql-test/suite/innodb/t/alter_rename_existing.test
	chmod 0644 $(TMP)/usr/lib/mysql-test/suite/innodb/r/alter_rename_existing.result
	chmod 0644 $(TMP)/usr/share/mysql/mysql-log-rotate
	# install AppArmor profile
	install -D -m 644 debian/additions/apparmor-profile $(TMP)/etc/apparmor.d/usr.sbin.mysqld
	touch $@


override_dh_installlogrotate-arch:
	dh_installlogrotate --name mysql-server

override_dh_install:
	dh_install --list-missing		\
		-Xusr/share/man			\
		-Xusr/share/mysql/mysql.server
	dh_apparmor -pmysql-server-5.7 --profile-name=usr.sbin.mysqld

override_dh_installchangelogs:
	dh_installchangelogs Docs/ChangeLog

override_dh_systemd_enable:
	dh_systemd_enable --name=mysql

# Start mysql in runlevel 19 before 20 where apache, proftpd etc gets
# started which might depend on a running database server.
override_dh_installinit-arch:
	dh_installinit --name=mysql -- defaults 19 21
	dh_systemd_start --restart-after-upgrade

override_dh_installcron-arch:
	dh_installcron --name mysql-server

get-orig-source:
	uscan --force-download --verbose

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

