#!/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/^.*-//' )

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_J := -j$(shell if [ -f /proc/cpuinfo ] ; then grep -c processor.* /proc/cpuinfo ; else echo 1 ; fi)
ifeq (${MAKE_J}, -j0)
  MAKE_J := -j1
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
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_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
ifneq ($(ARCH_OS),hurd)
	if [ ! -d /proc/self ]; then echo "/proc IS NEEDED" 1>&2; exit 1; fi 
endif
	( 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"} \
	    	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"} \
	    cmake -DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		$(FORCE_FPIC) \
		-DMYSQL_UNIX_ADDR=/var/run/mysqld/mysqld.sock \
	       	-DMYSQL_USER=mysql \
		-DWITH_LIBWRAP=ON \
		-DWITH_READLINE=OFF \
		-DWITH_LIBEDIT=OFF \
		$(USE_STATIC_MYSQLD) \
		$(USE_LINUX_NATIVE_AIO) \
		-DWITH_SSL=yes \
	    -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

MAKE_PIC = cd $(builddir) && $(MAKE) $(MAKE_J)

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

build-pic-stamp:
	@echo "RULES.$@"
	$(MAKE_PIC) -C scripts
	$(MAKE_PIC) -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:
	@echo "RULES.$@"
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	# 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/
	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-common: We now provide our own config file.
	install -d $(TMP)/etc/mysql
	install -m 0644 debian/additions/my.cnf $(TMP)/etc/mysql/my.cnf
	# 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-5.5-testsuite
	mkdir -p $(TMP)/usr/lib/mysql-testsuite
	cp -rp $(TMP)/usr/share/mysql-test/* $(TMP)/usr/lib/mysql-testsuite
	# mysql-server
	#install -m 0755 $(BUILDDIR)/scripts/mysqld_safe $(TMP)/usr/bin/mysqld_safe
	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 -D -m 0644 $(EXPORTED_SOURCE_TARBALL) $(TMP)/usr/src/mysql/`basename $(EXPORTED_SOURCE_TARBALL)`
	install -D -m 0644 $(EXPORTED_SOURCE_TARBALL) $(TMP)/usr/src/mysql/`basename $(EXPORTED_SOURCE_TARBALL)`

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

# This looks very dodgy to 'periapt'.
override_dh_makeshlibs-arch:
	dh_makeshlibs -plibmysqlclient18 -V'libmysqlclient18 (>= 5.5.13-1)'

# Transitional stage only.
# Allows us to test new rules without touching anything else
override_dh_install:
	dh_movefiles

binary:	binary-indep binary-arch

get-orig-source:
	@wget -nv -T10 -t3 \
	  -O /tmp/mysql-$(DEB_UPSTREAM_VERSION).tar.gz \
	  http://ftp.gwdg.de/pub/misc/mysql/Downloads/MySQL-$(DEB_UPSTREAM_VERSION_MAJOR_MINOR)/mysql-$(DEB_UPSTREAM_VERSION).tar.gz
	@tar xfz /tmp/mysql-$(DEB_UPSTREAM_VERSION).tar.gz -C /tmp
	@rm -rf /tmp/mysql-$(DEB_UPSTREAM_VERSION)/Docs
	@rm -rf /tmp/mysql-$(DEB_UPSTREAM_VERSION)/debian
	@mv /tmp/mysql-$(DEB_UPSTREAM_VERSION) /tmp/$(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION).orig
	@cd /tmp ; tar czf $(DEB_SOURCE_PACKAGE)_$(DEB_UPSTREAM_VERSION).orig.tar.gz $(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION).orig
	@rm -f /tmp/mysql-$(DEB_UPSTREAM_VERSION).tar.gz
	@rm -rf /tmp/$(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION).orig

%:
	dh $@ --parallel

