#!/usr/bin/make -f

export DH_VERBOSE=1
export DEB_BUILD_HARDENING=1

TMP:=$(CURDIR)/debian/tmp/

ARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH)
ARCH_OS := $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_SYSTEM ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_SYSTEM)
DEBVERSION := $(shell dpkg-parsechangelog | awk '/^Version: / { print $$2 }' | sed 's/^.*-//' )

ifneq (,$(filter $(ARCH), i386 kfreebsd-i386 hurd-i386))
    TAOCRYPT_OPT="-DTAOCRYPT_DISABLE_X86ASM"
endif

# Disable TokuDB due to failing test suite
# TokuDB is disabled in Trusty and can very well 
# be disabled in Utopic too
TOKUDB_OPT:=-DWITHOUT_TOKUDB=true

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

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)
RELEASE := $(shell lsb_release -r -s)

# Parallel build support as adviced
# at https://www.debian.org/doc/debian-policy/ch-source.html#s-debianrules-options
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    # Placeholder code to implement max cpu count value checking
    # if [$(NUMJOBS) > $(shell if [ -f /proc/cpuinfo ] ; then grep -c processor.* /proc/cpuinfo ; else echo 1 ; fi)]
    #  then NUMJOBS = 0
    # fi
    MAKEFLAGS += -j $(NUMJOBS)
endif

# Add support for verbose builds
ifneq (,$(filter verbose,$(DEB_BUILD_OPTIONS)))
    MAKEFLAGS += VERBOSE=1
endif

MAKE_TEST_TARGET:=test-force
ifneq ($(findstring fulltest,$(DEB_BUILD_OPTIONS)),)
# make test-bt is the testsuite run by the MySQL build team
# before a release, but it is long
    MAKE_TEST_TARGET:=test-bt
endif

USE_ASSEMBLER:=--enable-assembler

ifneq (,$(filter $(ARCH), amd64 kfreebsd-amd64 i386 kfreebsd-i386 ia64 s390 s390x))
    TESTSUITE_FAIL_CMD:=exit 1
else
    TESTSUITE_FAIL_CMD:=true
endif

SHARED_LIBS_DIR := $(TMP)/usr/lib/$(DEB_HOST_MULTIARCH)/

BUILDDIR := builddir
builddir = $(BUILDDIR)

# This causes seg11 crashes if LDAP is used for groups in /etc/nsswitch.conf
# so it is disabled by default although, according to MySQL, it brings >10%
# performance gain if enabled. See #299382.
ifeq ($(STATIC_MYSQLD), 1)
    USE_STATIC_MYSQLD:=--with-mysqld-ldflags=-all-static
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 # Update po-files when clean runs before each build

override_dh_prep:
        # Don't clean /tmp/ away, it is needed by all binary packages

override_dh_auto_configure:
	@echo "RULES.$@"
	dh_testdir
	
	# Versioned symbols are only available on Linux.
	# Remove symbols file on kFreeBSD builds so that
	# dpkg-gensymbols will not fail the build.
ifneq (,$(filter $(ARCH), kfreebsd-i386 kfreebsd-amd64))
	rm debian/libmariadbclient18.symbols
endif
	
	# Rename shared libraries SONAME and filenames
	# libmysqlclient18.so > libmariadbclient18.so etc
	sed -i 's/mysqlclient/mariadbclient/g' client/CMakeLists.txt tests/CMakeLists.txt scripts/CMakeLists.txt
	sed -i 's/mysqlclient/mariadbclient/g' libmysql/CMakeLists.txt libmysql/libmysql_versions.ld.in
	sed -i 's/mysqlclient/mariadbclient/g' man/mysql_config.1
	sed -i 's/mysqlclient/mariadbclient/g' scripts/make_win_bin_dist scripts/mysql_config.sh scripts/mysql_config.pl.in
	# Patch 44_scripts__mysql_config__libs also contains 'mysqlclient' but is compatible with this
	# Stuff in storage/ndb/* is not compiled, no need to sed it.
	# Neither does RPM packaging related files or comments in other code need sedding.
	
	( test -d $(builddir) || mkdir $(builddir) ) && cd $(builddir) && \
	sh -c  'PATH=$${MYSQL_BUILD_PATH:-"/usr/local/bin:/usr/bin:/bin"} \
	    	CC=$${MYSQL_BUILD_CC:-gcc} \
		CFLAGS=$${MYSQL_BUILD_CFLAGS:-"-O2 -DBIG_JOINS=1 -fno-strict-aliasing ${TAOCRYPT_OPT}"} \
	    	CXX=$${MYSQL_BUILD_CXX:-g++} \
	    	CXXFLAGS=$${MYSQL_BUILD_CXXFLAGS:-"-O3 -DBIG_JOINS=1 -felide-constructors -fno-exceptions -fno-rtti -fno-strict-aliasing ${TAOCRYPT_OPT}"} \
	    cmake -DCMAKE_INSTALL_PREFIX=/usr \
	    $(USE_STATIC_MYSQLD) \
	    -$(MAKEFLAGS) \
	    $(TOKUDB_OPT) \
	    -DCOMPILATION_COMMENT="($(DISTRIBUTION))" \
	    -DMYSQL_SERVER_SUFFIX="-$(DEBVERSION)" \
	    -DSYSTEM_TYPE="debian-$(DEB_BUILD_GNU_SYSTEM)" \
	    -DBUILD_CONFIG=mysql_release \
	    -DINSTALL_LIBDIR=lib/$(DEB_HOST_MULTIARCH) \
	    -DINSTALL_PLUGINDIR=lib/mysql/plugin \
		-DDEB=1 ..'
	touch $@

# This is needed, otherwise 'make test' will run before binaries have been built
override_dh_auto_build:
	@echo "RULES.$@"
	cd $(builddir) && $(MAKE)
	touch $@

override_dh_auto_test:
	@echo "RULES.$@"
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	cd $(builddir) && $(MAKE) $(MAKE_TEST_TARGET) || $(TESTSUITE_FAIL_CMD) ;
endif

override_dh_auto_install:
	@echo "RULES.$@"
	dh_testdir
	dh_testroot
	
	# If TokuDB plugin was built
	# add it to the server install list.
	[ ! -f $(BUILDDIR)/usr/lib/mysql/plugin/ha_tokudb.so ] || echo 'usr/lib/mysql/plugin/ha_tokudb.so\netc/mysql/conf.d/tokudb.cnf\nusr/bin/tokuftdump\nusr/share/doc/mariadb-server-5.5/README-TOKUDB\nusr/share/doc/mariadb-server-5.5/README.md' >> debian/mariadb-server-5.5.install
	
	mkdir -p $(TMP)/etc/mysql/conf.d/
	cp debian/additions/mysqld_safe_syslog.cnf $(TMP)/etc/mysql/conf.d/
	
	# 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
	
	# libmysqlclient-dev: forgotten header file since 3.23.25?
	cp $(BUILDDIR)/include/my_config.h $(TMP)/usr/include/mysql/
	cp include/my_dir.h $(TMP)/usr/include/mysql/
	
	# mariadb-common: MariaDB-specific config stuff.
	install -d $(TMP)/etc/mysql/conf.d
	install -m 0644 debian/additions/mariadb.cnf $(TMP)/etc/mysql/conf.d/mariadb.cnf
	
	# mariadb-server
	rm -vf $(TMP)/usr/share/mysql/mi_test_all* \
	       $(TMP)/usr/share/mysql/mysql-log-rotate \
	       $(TMP)/usr/share/mysql/mysql.server \
	       $(TMP)/usr/share/mysql/binary-configure
	nm -n $(BUILDDIR)/sql/mysqld |gzip -9 > $(TMP)/usr/share/doc/mariadb-server-5.5/mysqld.sym.gz
	# Below didn't work, use more explicit command above
	# nm -n $(BUILDDIR)/sql/mysqld |gzip -9 > debian/mysqld.sym.gz
	mkdir -p $(TMP)/etc/mysql/conf.d/
	install -m 0755 debian/additions/echo_stderr $(TMP)/usr/share/mysql/
	install -m 0755 debian/additions/debian-start $(TMP)/etc/mysql/
	install -m 0755 debian/additions/debian-start.inc.sh $(TMP)/usr/share/mysql/
	mkdir -p $(TMP)/usr/share/doc/mariadb-server-5.5
	install -m 0644 $(builddir)/Docs/INFO_SRC $(TMP)/usr/share/doc/mariadb-server-5.5/INFO_SRC
	install -m 0644 $(builddir)/Docs/INFO_BIN $(TMP)/usr/share/doc/mariadb-server-5.5/INFO_BIN
	
	# install AppArmor profile
	install -D -m 644 debian/apparmor-profile $(TMP)/etc/apparmor.d/usr.sbin.mysqld
	
	# install Apport hook
	install -D -m 644 debian/mariadb-server-5.5.py $(TMP)/usr/share/apport/package-hooks/source_mariadb-5.5.py
	
	# mariadb-test
	mv $(TMP)/usr/mysql-test $(TMP)/usr/share/mysql
	touch $@

override_dh_installlogrotate-arch:
	dh_installlogrotate --name mysql-server

# 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

override_dh_installcron-arch:
	dh_installcron --name mysql-server

%:
	dh $@ --parallel

# vim: ts=8
