#!/usr/bin/make -f

export DH_VERBOSE=1
export DEB_BUILD_HARDENING=1

PACKAGE:=mysql-5.5

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)
DEBVERSION := $(shell dpkg-parsechangelog | awk '/^Version: / { print $$2 }' | sed 's/^.*-//' )

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

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)

EXPORTED_SOURCE_TARBALL := debian/mysql-source-5.5.tar.gz

DISTRIBUTION := $(shell lsb_release -i -s)

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
ifeq ($(ARCH_OS),hurd)
# Tests not fully working under Hurd
# See http://bugs.mysql.com/bug.php?id=64685
    MAKE_TEST_TARGET:=test
endif

USE_ASSEMBLER:=--enable-assembler 

ifneq (,$(filter $(ARCH), amd64 i386))
    TESTSUITE_FAIL_CMD:=exit 1
else
    TESTSUITE_FAIL_CMD:=true
endif

BUILDDIR := builddir
BUILDDIR_PIC := builddir-pic
builddir = $(if $(findstring -pic,$@),$(BUILDDIR_PIC),$(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
	
# See http://blogs.innodb.com/wp/2010/04/innodb-performance-aio-linux/
# Also #659565
ifneq (,$(findstring linux-gnu,$(DEB_HOST_GNU_TYPE)))
    USE_LINUX_NATIVE_AIO:=-DLINUX_NATIVE_AIO=ON
endif

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

override_dh_prep: 

override_dh_auto_configure: configure-stamp configure-pic-stamp

configure-pic-stamp: FORCE_FPIC_CFLAGS=-fPIC
configure-pic-stamp: FORCE_FPIC=-DWITH_PIC=On

configure-stamp configure-pic-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} \
		CFLAGS=$${MYSQL_BUILD_CFLAGS:-"-O2 -DBIG_JOINS=1 ${FORCE_FPIC_CFLAGS} -fno-strict-aliasing ${TAOCRYPT_OPT}"} \
	    	CXX=$${MYSQL_BUILD_CXX:-g++} \
	    	CXXFLAGS=$${MYSQL_BUILD_CXXFLAGS:-"-O3 -DBIG_JOINS=1 -felide-constructors -fno-exceptions -fno-rtti ${FORCE_FPIC_CFLAGS} -fno-strict-aliasing ${TAOCRYPT_OPT}"} \
	    cmake -DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		$(FORCE_FPIC) \
		-DMYSQL_UNIX_ADDR=/var/run/mysqld/mysqld.sock \
	       	-DMYSQL_USER=mysql \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DWITH_LIBWRAP=ON \
		-DWITH_READLINE=OFF \
		-DWITH_LIBEDIT=OFF \
		$(USE_STATIC_MYSQLD) \
		$(USE_LINUX_NATIVE_AIO) \
		-DWITH_SSL=bundled \
		-DWITH_ZLIB=system \
	    -DCOMPILATION_COMMENT="($(DISTRIBUTION))" \
	    -DMYSQL_SERVER_SUFFIX="-$(DEBVERSION)" \
	    -DSYSTEM_TYPE="debian-linux-gnu" \
	    -DINSTALL_LAYOUT=RPM \
	    -DINSTALL_LIBDIR=lib/$(DEB_HOST_MULTIARCH) \
	    -DINSTALL_PLUGINDIR=lib/mysql/plugin \
        -DWITH_EMBEDDED_SERVER=ON \
        -DHAVE_EMBEDDED_PRIVILEGE_CONTROL=ON \
	    -DWITH_ARCHIVE_STORAGE_ENGINE=ON \
	    -DWITH_BLACKHOLE_STORAGE_ENGINE=ON \
	    -DWITH_FEDERATED_STORAGE_ENGINE=ON \
		-DWITH_EXTRA_CHARSETS=all ..'
	touch $@


override_dh_auto_build: build-stamp build-pic-stamp

build-stamp:
	@echo "RULES.$@"
	[ -f $(EXPORTED_SOURCE_TARBALL) ] || tar -zcf $(EXPORTED_SOURCE_TARBALL) \
	--exclude=debian . \
	--transform="s,^\./,mysql-5.5/,"
	cd $(builddir) && $(MAKE)
	touch $@

build-pic-stamp:
	@echo "RULES.$@"
	cd $(builddir) && $(MAKE) -C scripts
	cd $(builddir) && $(MAKE) -C libmysqld
	touch $@

override_dh_auto_test:
	@echo "RULES.$@"
ifeq ($(findstring nocheck,$(DEB_BUILD_OPTIONS)),)
	cp unittest/unit.pl $(builddir)/unittest/
	cp -r mysql-test/* $(builddir)/mysql-test/
	cp -r sql/share/* $(builddir)/sql/share/
	cp -r scripts/*sql $(builddir)/scripts/
	cd $(builddir) && $(MAKE) $(MAKE_TEST_TARGET) || $(TESTSUITE_FAIL_CMD) ; 
endif

override_dh_auto_install: auto_install-stamp

auto_install-stamp:
	@echo "RULES.$@"
	dh_testdir
	dh_testroot
	dh_prep
	# some self written manpages which hopefully
	# gets overwritten sooner or later with upstreams
	mkdir -p $(TMP)/usr/share/man/man1/
	mkdir -p $(TMP)/usr/share/man/man8/
	cp debian/additions/*.1 $(TMP)/usr/share/man/man1/
	mkdir -p $(TMP)/etc/mysql/conf.d/
	touch $(TMP)/etc/mysql/conf.d/.keepme
	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_r is now a symlink to libmysqlclient. But it is
	# created wrong by the cmake build system and points at
	# libmysqlclient.so instead of the corresponding versioned lib.
	for i in `ls $(TMP)/usr/lib/$(DEB_HOST_MULTIARCH)/libmysqlclient.so*` ; do \
		rlib=`basename $$i | sed -e 's/libmysqlclient\./libmysqlclient_r./'` ;\
		ln -sf `basename $$i` $(TMP)/usr/lib/$(DEB_HOST_MULTIARCH)/$$rlib ;\
	done
	# install libmysqld built with -FPIC
	install -d -m 0755 -o root -g root $(TMP)/usr/lib/mysql
	install -m 0644 -o root -g root $(BUILDDIR_PIC)/libmysqld/libmysqld.a $(TMP)/usr/lib/mysql/libmysqld_pic.a
	# mysql_config won't report the -fPIC, so give libmysqld-pic users a way to get their flags
	install -m 0755 -o root -g root $(BUILDDIR_PIC)/scripts/mysql_config $(TMP)/usr/bin/mysql_config_pic
	# mysql-client
	install -m 0755 debian/additions/mysqlreport $(TMP)/usr/bin/
	install -m 0755 debian/additions/innotop/innotop $(TMP)/usr/bin/
	install -m 0644 debian/additions/innotop/innotop.1 $(TMP)/usr/share/man/man1/
	# mysql-server
	mkdir -p $(TMP)/usr/share/doc/mysql-server-5.5/examples
	mv $(TMP)/usr/share/mysql/*cnf 	    $(TMP)/usr/share/doc/mysql-server-5.5/examples/
	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/mysql-server-5.5/mysqld.sym.gz
	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/

	# 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/mysql-server-5.5.py $(TMP)/usr/share/apport/package-hooks/source_mysql-5.5.py

	touch $@

override_dh_install:
	dh_movefiles
	dh_install

override_dh_installchangelogs:
	dh_installchangelogs Docs/ChangeLog

override_dh_installlogrotate-arch:
	dh_installlogrotate --name mysql-server
	if [ "$(DISTRIBUTION)" = "Ubuntu" ]; then \
	  dh_apparmor -pmysql-server-5.5 --profile-name=usr.sbin.mysqld; \
	fi

# 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

override_dh_makeshlibs-arch:
	dh_makeshlibs -plibmysqlclient18 -V'libmysqlclient18 (>= 5.5.24+dfsg-1)'

override_dh_builddeb:
	dh_builddeb -- -Zxz

binary:	binary-indep binary-arch

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

%:
	dh $@ --parallel

