#!/usr/bin/make -f

# Copyright (c) 2015, 2022, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

ifneq (,$(findstring SYSTEMD,SYSTEMD))
CMAKE_SYSTEMD_OPTIONS := -DWITH_SYSTEMD=1 -DSYSTEMD_SERVICE_NAME="mysql"
endif

%:
	dh $@

export DH_VERBOSE=1
export CFLAGS=
export CXXFLAGS=

override_dh_auto_configure:
	@echo "RULES.$@"
	cmake . \
		-DBUILD_CONFIG=mysql_release \
		$(CMAKE_SYSTEMD_OPTIONS) \
		-DWITH_WSREP=1 \
		-DDOWNLOAD_BOOST=1 -DWITH_BOOST=/tmp \
		-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_SUPPORTFILESDIR=share/mysql \
		-DSYSCONFDIR=/etc/mysql \
		-DMYSQL_UNIX_ADDR=/var/run/mysqld/mysqld.sock \
		-DWITH_SSL=system \
		-DWITH_INNODB_MEMCACHED=1 \
		-DWITH_MECAB=system \
		-DWITH_NUMA=ON \
		-DCOMPILATION_COMMENT="MySQL Wsrep Server (GPL)" \
		-DINSTALL_LAYOUT=DEB \
		-DDEB_PRODUCT=community \
		
	touch $@

override_dh_auto_build:
	@echo "RULES.$@"
	$(MAKE) -j8 VERBOSE=1
	touch $@

override_dh_auto_test:
	@echo "RULES.$@"
	export CTEST_OUTPUT_ON_FAILURE=1
	cd release && \
	make test || true
	touch $@



override_dh_auto_install:

	@echo "RULES.$@"
	# complete install first
	$(MAKE) install DESTDIR=debian/tmp
	# remove all redundant files
	rm debian/tmp/usr/lib/mysql-test/cmake_install.cmake
	rm debian/tmp/usr/lib/mysql-test/CTestTestfile.cmake
	rm debian/tmp/usr/lib/mysql-test/Makefile
	# add missing man pages
	# install -g root -o root -m 0644 debian/extra/mysql_embedded.1 debian/tmp/usr/share/man/man1
	# add MySQL Server debug binary and library to package
	# install -g root -o root -m 0755 debian/extra/server-binary debian/tmp/usr/sbin/mysqld-debug
	# install -g root -o root -m 0755 debian/extra/embedded-server debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libmysqld-debug.a
	# add debug plugin libraries to package
	install -m 0644 support-files/wsrep.cnf debian/tmp/usr/share/mysql
	# SysV init stuff, or Systemd files ? On Debian 8, we need both!
ifneq (,$(findstring SYSV,SYSTEMD))
	install -D -m 0755 support-files/mysql.server debian/tmp/etc/init.d/mysql
endif
ifneq (,$(findstring SYSTEMD,SYSTEMD))
	install -m 0755 scripts/mysqld_bootstrap debian/tmp/usr/bin/
endif
	# This seems to be the only safe place to fix permissions issues
	# not handled by dh_fixperms.
	# install -m 0755 debian/additions/echo_stderr debian/tmp/usr/share/mysql/
	mkdir -p debian/tmp/etc/mysql
	install -m 0755 debian/extra/debian-start debian/tmp/etc/mysql/
	install -m 0755 debian/extra/debian-start.inc.sh debian/tmp/usr/share/mysql/
	install -g root -o root -m 0755 -d debian/tmp/usr/lib/mysql/plugin/debug
	#install -g root -o root -m 0755 debian/extra/*.so debian/tmp/usr/lib/mysql/plugin/debug
	#install -g root -o root -m 0755 debian/extra/daemon_example.ini debian/tmp/usr/lib/mysql/plugin/debug
	# 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 debian/tmp/etc/apparmor.d/usr.sbin.mysqld
	# add systemd script
	install -m 0755 debian/extra/mysql-systemd-start debian/tmp/usr/share/mysql/

	touch $@


override_dh_installlogrotate-arch:
	dh_installlogrotate --name mysql-server

override_dh_installinit:
	@echo "RULES.$@"
	dh_apparmor -pmysql-wsrep-server-5.7 --profile-name=usr.sbin.mysqld
	dh_systemd_enable --name=mysql --no-enable
	dh_installinit --no-start --name=mysql -- defaults 19 21
	dh_systemd_start --restart-after-upgrade
	touch $@

override_dh_install:
	dh_install --fail-missing \
		-Xusr/bin/i_mysql_client_test \
		-Xusr/share/mysql/mysql.server \
		-Xusr/share/man/man1/mysql.server.1 \
		-Xusr/share/mysql/binary-configure \
		-Xusr/share/mysql/LICENSE-test \
		-Xusr/share/mysql/README-test \
		-Xusr/share/mysql/docs/mysql.info \
		-Xusr/share/man/man1/mysqlman.1 \
		\
		-X.h.pp
