#!/usr/bin/make -f

%:
	dh $@

export DH_VERBOSE=1
export CFLAGS=
export CXXFLAGS=

PACKAGE=percona-server-5.7
PS_VERSION_EXTRA = '38'
REVISION = '3692a61'
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)"

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-server-source-5.7.tar.gz

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

MYSQL_SRC = $(shell pwd)

builddir = builddir
builddebug = debug

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 \
		-DWITH_LIBWRAP=ON \
		-DWITH_ZLIB=system \
		-DWITH_SSL=system \
		-DCOMPILATION_COMMENT=$(COMPILATION_COMMENT_DEBUG) \
		-DSYSTEM_TYPE="debian-linux-gnu" \
		-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 \
		-DDOWNLOAD_BOOST=1 \
		-DWITH_BOOST=libboost \
		-DWITHOUT_TOKUDB=ON \
		-DWITH_SCALABILITY_METRICS=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 \
		-DWITH_LIBWRAP=ON \
		-DWITH_ZLIB=system \
		-DWITH_SSL=system \
		-DCOMPILATION_COMMENT=$(COMPILATION_COMMENT_RELEASE) \
		-DSYSTEM_TYPE="debian-linux-gnu" \
		-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 \
		-DDOWNLOAD_BOOST=1 \
		-DWITH_BOOST=libboost \
		-DWITHOUT_TOKUDB=ON \
		-DWITH_SCALABILITY_METRICS=ON \
		-DWITH_EXTRA_CHARSETS=all ..

	touch $@

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

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

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

	touch $@

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 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 source tarball for source package
	install -D -m 0644 $(EXPORTED_SOURCE_TARBALL) $(TMP)/usr/src/percona-server/`basename $(EXPORTED_SOURCE_TARBALL)`

override_dh_strip:
	dh_strip --dbg-package=percona-server-5.7-dbg

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