#!/usr/bin/make -f

# Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# 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 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

%:
	dh $@

export DH_VERBOSE=1
export CFLAGS=
export CXXFLAGS=

override_dh_auto_configure:
	@echo "RULES.$@"
	cmake . \
        -DWITH_BOOST=/usr/global/share \
		-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_SUPPORTFILESDIR=share/mysql \
		-DSYSCONFDIR=/etc/mysql \
		-DMYSQL_UNIX_ADDR=/var/run/mysqld/mysqld.sock \
		-DWITH_SSL=bundled \
		-DWITH_INNODB_MEMCACHED=1 \
		-DWITH_MECAB=system \
		-DCOMPILATION_COMMENT="MySQL Community 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.$@"
	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
	# 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 -g root -o root -m 0755 -d debian/tmp/usr/lib/mysql/plugin/debug
	for file in debian/extra/*-plugin; do NEW=`echo $$file | cut -d- -f1`; mv $$file $$NEW.so; done
	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 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
	
	# add directory for legal docs
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/mysql-server
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/mysql-community-server
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/mysql-client
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/mysql-community-client
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/mysql-common
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/libmysqlclient21
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/libmysqlclient-dev
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/libmysqld-dev
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/mysql-testsuite
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/mysql-community-test
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/mysql-community-source
	# add COPYING file to each package
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/mysql-server/COPYING
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/mysql-community-server/COPYING
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/mysql-client/COPYING
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/mysql-community-client/COPYING
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/mysql-common/COPYING
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/libmysqlclient21/COPYING
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/libmysqlclient-dev/COPYING
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/libmysqld-dev/COPYING
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/mysql-testsuite/COPYING
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/mysql-community-test/COPYING
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/mysql-community-source/COPYING
	# add README file to each package
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/mysql-server/README
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/mysql-community-server/README
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/mysql-client/README
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/mysql-community-client/README
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/mysql-common/README
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/libmysqlclient21/README
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/libmysqlclient-dev/README
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/libmysqld-dev/README
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/mysql-testsuite/README
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/mysql-community-test/README
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/mysql-community-source/README
	touch $@

override_dh_installinit:
	@echo "RULES.$@"
	dh_apparmor -pmysql-community-server --profile-name=usr.sbin.mysqld
	
	dh_installinit --name=mysql -- defaults 19 21
	
	touch $@

override_dh_install:
	dh_install --list-missing
		-Xusr/bin/i_mysql_client_test
		-Xusr/share/mysql/mysql.server
		-Xusr/share/mysql/COPYING
		-Xusr/share/mysql/README
		-Xusr/share/mysql/docs/mysql.info
		-Xusr/share/man/man1/mysqlman.1
