#!/usr/bin/make -f

export SHELL = /bin/bash

export DH_VERBOSE=1

include debian/defs.mk

changelog_values := $(shell dpkg-parsechangelog \
                        | awk '/^(Version|Source|Distribution):/ {print $$2}')
PKGSOURCE := $(word 1, $(changelog_values))
PKGVERSION := $(word 2, $(changelog_values))
PKGDISTRO := $(word 3, $(changelog_values))
PKGFLAVOR := $(shell echo $(PKGSOURCE) | perl -nle 's/mysql-(.*?)-\d+.*/$$1/ && print ')

ifeq ("x$(MYSQL_FLAVOR)","x")
  export MYSQL_FLAVOR=$(PKGFLAVOR)
endif

ifneq ("x$(MYSQL_FLAVOR)","x")
  MYSQL_BRANDED_BASE_VERSION:=$(MYSQL_FLAVOR)-$(MYSQL_BASE_VERSION)
  MYSQL_BRANDED_PREVIOUS_BASE_VERSION:=$(MYSQL_FLAVOR)-$(MYSQL_PREVIOUS_BASE_VERSION)
else
  ifeq ( "$(MYSQL_BRANDED_BASE_VERSION)","@MYSQL_BRANDED_BASE_VERSION@" )
    MYSQL_BRANDED_BASE_VERSION:=$(MYSQL_BASE_VERSION)
    MYSQL_BRANDED_PREVIOUS_BASE_VERSION:=$(MYSQL_PREVIOUS_BASE_VERSION)
  else
    MYSQL_BRANDED_BASE_VERSION:=$(MYSQL_BRANDED_BASE_VERSION)
    MYSQL_BRANDED_PREVIOUS_BASE_VERSION:=$(MYSQL_BRANDED_PREVIOUS_BASE_VERSION)
  endif 
endif

PACKAGE=mysql-$(MYSQL_BRANDED_BASE_VERSION)

PWD=$(shell pwd)
TMP=$(PWD)/debian/tmp/


ARCH = $(shell dpkg-architecture -qDEB_BUILD_ARCH)

DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

MAKE_J = $(shell if grep -q processor.*3 /proc/cpuinfo; then echo "-j 4"; else echo ""; fi )

ifeq ($(findstring $(ARCH),i386 sparc),$(ARCH))
    USE_ASSEMBLER=--enable-assembler 
endif

ifeq ($(findstring $(ARCH), arm),$(ARCH))
    FOMIT_FRAME_POINTER=
else
    FOMIT_FRAME_POINTER=-fomit-frame-pointer
endif

# trying to raise stability on i386. See #116631
# don't use it on ia64
ifeq ($(findstring $(ARCH),i386),$(ARCH))
  FNO_EXCEPTIONS=-fno-exceptions
endif

# 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

ifneq ("$(MYSQL_BUILD_TEST)","yes")
    MYSQL_BUILD_TEST=no
endif

control-file:
	@echo "## This file is autogenerated you want to edit control.in instead" > debian/control.tmp
	sed -e "s/@MYSQL_BASE_VERSION@/$(MYSQL_BASE_VERSION)/g" \
	    -e "s/@MYSQL_BRANDED_BASE_VERSION@/$(MYSQL_BRANDED_BASE_VERSION)/g" \
	    -e "s/@VERSIONSTRING@/$(VERSIONSTRING)/g" \
	    -e "s/@MYSQL_PREVIOUS_BASE_VERSION@/$(MYSQL_PREVIOUS_BASE_VERSION)/g" \
	    -e "s/@MYSQL_BRANDED_PREVIOUS_BASE_VERSION@/$(MYSQL_BRANDED_PREVIOUS_BASE_VERSION)/g" \
	    -e "s/@MYSQL_SOURCE_BASE_VERSION@/$(MYSQL_SOURCE_BASE_VERSION)/g" \
	    -e "s/@SHARED_LIB_MAJOR_VERSION@/$(SHARED_LIB_MAJOR_VERSION)/g" \
	    -e "s/@NDB_SHARED_LIB_MAJOR_VERSION@/$(NDB_SHARED_LIB_MAJOR_VERSION)/g" \
		debian/control.in >> debian/control.tmp
	[ -e debian/control ] \
	  && cmp -s debian/control debian/control.tmp \
	  && rm -f debian/control.tmp && exit 0; \
	  mv debian/control.tmp debian/control


missing:
	./BUILD/autorun.sh 

configure: configure-stamp
configure-stamp:
	@echo "RULES.configure-stamp"
	dh_testdir

ifneq ($(ARCH_OS),hurd)
	@if [ ! -d /proc/self ]; then echo "/proc IS NEEDED" 1>&2; exit 1; fi 
endif

	@cp debian/control debian/control.sav
	( \
	CC="$(MYSQL_BUILD_CC)"  \
	CXX="$(MYSQL_BUILD_CXX)" \
	BUILD_OPTS="${MYSQL_BUILD_OPTS:-''}"  \
	CFLAGS="${MYSQL_BUILD_CFLAGS:-'-DBIG_JOINS=1 -O2'}" \
	CXXFLAGS="${MYSQL_BUILD_CXXFLAGS:-'-DBIG_JOINS=1 -felide-constructors -fno-rtti -O2'}" \
	./configure \
		--build=${DEB_BUILD_GNU_TYPE} \
		--host=${DEB_HOST_GNU_TYPE} \
		\
		--prefix=/usr \
	        --exec-prefix=/usr \
	        --libexecdir=/usr/sbin \
	        --datadir=/usr/share \
	        --localstatedir=/var/lib/mysql \
	        --includedir=/usr/include \
	        --infodir=/usr/share/info \
	        --mandir=/usr/share/man \
		\
		--with-comment="MySQL Server (custom build)" \
		--with-server-suffix="-custom" \
		\
		--enable-shared \
		--enable-thread-safe-client \
	        $(USE_ASSEMBLER) \
		--enable-local-infile \
		\
                --with-big-tables \
		--with-unix-socket-path=/var/run/mysqld/mysqld.sock \
	       	--with-mysqld-user=mysql \
		$(USE_STATIC_MYSQLD) \
		--without-bench \
		--with-zlib-dir=bundled \
		--with-ssl \
		--with-readline \
		--with-extra-charsets=all \
		--with-innodb \
                --with-blackhole-storage-engine \
                --with-example-storage-engine \
		\
		--with-isam \
		--with-archive-storage-engine \
		--with-csv-storage-engine \
		--with-federated-storage-engine \
		--without-embedded-server \
		--with-ndbcluster \
		--with-ndb-shm \
		--without-ndb-sci \
		--without-ndb-test \
		--with-ndb-docs \
		$(MYSQL_BUILD_OPTS) \
	)

	@mv debian/control.sav debian/control

	touch configure-stamp


build: build-stamp
build-stamp: configure
	dh_testdir

	$(MAKE) $(MAKE_J)

	if [ -f sql/.libs/mysqld ] ; then \
		nm --numeric-sort sql/.libs/mysqld > sql/mysqld.sym ; \
	else \
		nm --numeric-sort sql/mysqld > sql/mysqld.sym ; \
	fi


	if [ "${MYSQL_BUILD_TEST}" != "no" ] ; then \
	  ( cd mysql-test ;\
	    MTR_BUILD_THREAD=auto ; \
	    export MTR_BUILD_THREAD ; \
            perl ./mysql-test-run.pl --force --report-features ; \
            perl ./mysql-test-run.pl --force --ps-protocol ; \
            true ) \
	fi


	touch build-stamp

stamp-control:
	# We have to prepare the various control files

	echo "in stamp-control - BASE_VER==$(MYSQL_BASE_VERSION)"
	# We have some naming inconsistencies here... 
	for f in debian/*.in debian/po/POTFILES.in.in ; do \
            f2=`echo $$f | sed "s,BASE,$(MYSQL_BRANDED_BASE_VERSION),g;\
                                s,PREV,$(MYSQL_BRANDED_PREVIOUS_BASE_VERSION),g;\
                                s,NLIB,$(NDB_SHARED_LIB_MAJOR_VERSION),g;\
                                s,SLIB,$(SHARED_LIB_MAJOR_VERSION),g;s,\.in$$,,"`; \
            if [ $$f2 != debian/control -a $$f2 != debian/Makefile ]; then \
                sed -e "s/@MYSQL_BASE_VERSION@/$(MYSQL_BASE_VERSION)/g" \
	    	    -e "s/@MYSQL_BRANDED_BASE_VERSION@/$(MYSQL_BRANDED_BASE_VERSION)/g" \
		    -e "s/@VERSIONSTRING@/$(VERSIONSTRING)/g" \
                    -e "s/@PRIORITY@/$(PRIORITY)/g" \
		    -e "s/@MYSQL_PREVIOUS_BASE_VERSION@/$(MYSQL_PREVIOUS_BASE_VERSION)/g" \
	    	    -e "s/@MYSQL_BRANDED_PREVIOUS_BASE_VERSION@/$(MYSQL_BRANDED_PREVIOUS_BASE_VERSION)/g" \
		    -e "s/@MYSQL_SOURCE_BASE_VERSION@/$(MYSQL_SOURCE_BASE_VERSION)/g" \
		    -e "s/@SHARED_LIB_MAJOR_VERSION@/$(SHARED_LIB_MAJOR_VERSION)/g" \
		    -e "s/@NDB_SHARED_LIB_MAJOR_VERSION@/$(NDB_SHARED_LIB_MAJOR_VERSION)/g" \
                    -e "s,@SCRIPTDIR@,/$(scriptdir),g" \
                    -e "s,@INFO@,$(findstring info, $(doc_dependencies)),g" \
                  <$$f >$$f2; \
            fi; \
        done
	echo "MYSQL_BUILD_OPTS=$(MYSQL_BUILD_OPTS)" >> debian/defs.mk
	echo "MYSQL_BUILD_CXXFLAGS=$(MYSQL_BUILD_CXXFLAGS)" >> debian/defs.mk
	echo "MYSQL_BUILD_CFLAGS=$(MYSQL_BUILD_CFLAGS)" >> debian/defs.mk
	echo "MYSQL_BUILD_CC=$(MYSQL_BUILD_CC)" >> debian/defs.mk
	echo "MYSQL_BUILD_CXX=$(MYSQL_BUILD_CXX)" >> debian/defs.mk


# depending on two files we expect to be in the root
# to protect from accidentally deleting a whole bunch of 
# files somewhere else.
clean: configure.in Makefile.am control-file

	rm -f debian/mysql-server-$(MYSQL_BRANDED_BASE_VERSION).mysql.init

	# We depend on this later, and delete the results in the mean time
	$(MAKE) -f debian/rules stamp-control
	dh_testdir 
	dh_testroot
	rm -f configure-stamp
	rm -f build-stamp

	cp debian/defs.mk debian/defs.mk.sav
	cp debian/control debian/control.sav
	-make distclean
	mv debian/defs.mk.sav debian/defs.mk
	mv debian/control.sav debian/control

	debconf-updatepo

	for f in debian/*.in; do \
            f2=`echo $$f | sed "s,BASE,$(MYSQL_BRANDED_BASE_VERSION),g;\
                                s,PREV,$(MYSQL_BRANDED_PREVIOUS_BASE_VERSION),g;\
                                s,NLIB,$(NDB_SHARED_LIB_MAJOR_VERSION),g;\
                                s,SLIB,$(SHARED_LIB_MAJOR_VERSION),g;s,\.in$$,,"`; \
            if [ $$f2 != debian/control -a $$f2 != debian/Makefile -a $$f2 != debian/defs.mk ]; then \
		rm -f $$f2; \
            fi; \
        done
	rm -f po/POTFILES.in

	dh_clean -v
	


install:
install: build stamp-control
	@echo "RULES.install"
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Sort of hackish way to make sure we have a init script 
	# for dh_installinit
	cp support-files/mysql.server debian/mysql-server-$(MYSQL_BRANDED_BASE_VERSION).mysql.init

	mkdir -p $(TMP)/usr/lib/mysql/mysqld.sym
	cp sql/mysqld.sym $(TMP)/usr/lib/mysql/mysqld.sym

	# TODO: need real man pages!
	#for f in mysql mysqldump mysqlaccess mysqladmin mysqlshow myisam_ftdump myisamlog myisampack mysql_explain_log mysqld_multi mysqld_safe mysql_fix_privilege_tables mysql_upgrade mysql_tzinfo_to_sql mysql_zap perror replace safe_mysqld ; do touch $(TMP)/usr/share/man/man1/$$f.1 ; done
	#for f in mysqld mysqlmanager ; do touch $(TMP)/usr/share/man/man8/$$f.8 ; done
	# make install (trailing slash needed for innobase)
	$(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: move shared libraries (but not the rest like libheap.a & co)
	mv $(TMP)/usr/lib/mysql/libmysqlclient* $(TMP)/usr/lib
	mv $(TMP)/usr/lib/mysql/libndbclient* $(TMP)/usr/lib
	perl -pi -e 's#/usr/lib/mysql#/usr/lib#' $(TMP)/usr/lib/libmysqlclient.la
	perl -pi -e 's#/usr/lib/mysql#/usr/lib#' $(TMP)/usr/lib/libmysqlclient_r.la
	perl -pi -e 's#/usr/lib/mysql#/usr/lib#' $(TMP)/usr/lib/libndbclient.la

	# Check if our beloved versioned symbols are really there
	if [ "`objdump -T $(TMP)/usr/lib/libmysqlclient.so.$(SHARED_LIB_MAJOR_VERSION).0.0 | grep -c libmysqlclient_$(SHARED_LIB_MAJOR_VERSION)`" -lt 500 ]; then \
	  echo "ERROR: versioned symbols are absent"; \
	  exit 1; \
	fi     
	if [ "`objdump -T $(TMP)/usr/lib/libndbclient.so.$(NDB_SHARED_LIB_MAJOR_VERSION).0.0 | grep -c libndbclient_$(NDB_SHARED_LIB_MAJOR_VERSION)`" -lt 500 ]; then \
          echo "ERROR: versioned symbols are absent from libndbclient"; \
          exit 1; \
        fi

	# libmysqlclient-dev: forgotten header file since 3.23.25?
	cp include/my_config.h $(TMP)/usr/include/mysql/
	cp include/my_dir.h $(TMP)/usr/include/mysql/

	# 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

	pod2man scripts/mysqlhotcopy $(TMP)/usr/share/man/man1/mysqlhotcopy.1

	# mysql-server
	install -m 0755 scripts/mysqld_safe $(TMP)/usr/bin/mysqld_safe
	mkdir -p $(TMP)/usr/share/doc/mysql-server-$(MYSQL_BRANDED_BASE_VERSION)/examples
	mv $(TMP)/usr/share/mysql/*cnf 	    $(TMP)/usr/share/doc/mysql-server-$(MYSQL_BRANDED_BASE_VERSION)/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

	# we can't install *.pl into /usr/bin - so we have to rename it.
	sed "s#filename => 'ndb_size.tmpl#filename => '/usr/share/mysql/ndb_size.tmpl#" < $(TMP)/usr/bin/ndb_size.pl > $(TMP)/usr/bin/ndb_size
	cp -a mysql-test $(TMP)/usr/share/mysql/
	cp -a sql-bench $(TMP)/usr/share/mysql/
	# lintian overrides
	mkdir -p $(TMP)/usr/share/lintian/overrides/
	cp debian/mysql-server-$(MYSQL_BRANDED_BASE_VERSION).lintian-overrides $(TMP)/usr/share/lintian/overrides/mysql-server-$(MYSQL_BRANDED_BASE_VERSION)
	cp debian/mysql-client-$(MYSQL_BRANDED_BASE_VERSION).lintian-overrides $(TMP)/usr/share/lintian/overrides/mysql-client-$(MYSQL_BRANDED_BASE_VERSION)

	# For 4.1 -> 5.0 transition
	d=$(TMP)/usr/share/mysql-common/internal-use-only/; \
	mkdir -p $$d; \
	cp debian/mysql-server-$(MYSQL_BRANDED_BASE_VERSION).mysql.init $$d/_etc_init.d_mysql; \
	cp debian/mysql-server-$(MYSQL_BRANDED_BASE_VERSION).mysql-server.logrotate $$d/_etc_logrotate.d_mysql-server; \

	dh_movefiles

# Build architecture-independent files here.
binary-indep: build install
	@echo "RULES.binary-indep"
	dh_testdir -i
	dh_testroot -i
	dh_installdebconf -i
	dh_installdocs -i
	dh_installexamples -i
	dh_installmenu -i
	dh_installlogrotate -i
	dh_installinit -i
	dh_installcron -i 
	dh_installman -i
	dh_installinfo -i
	dh_installlogcheck -i
	dh_installchangelogs -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_perl -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	@echo "RULES.binary-arch"
	dh_testdir 
	dh_testroot

	dh_installdebconf -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installmenu -a
	dh_installlogrotate -a --name mysql-server
	# Start mysql in runlevel 19 before 20 where apache, proftpd etc gets
	# started which might depend on a running database server.
	dh_installinit -a --name=mysql-storage 	-- defaults 19 21
	dh_installinit -a --name=mysql		-- defaults 19 21
	dh_installinit -a --name=mysql-management 	-- defaults 20
	dh_installcron -a --name mysql-server
	dh_installman -a
	dh_installinfo -a
	dh_installlogcheck -a
	dh_installchangelogs -a
	dh_strip -a
	dh_link -a	# .so muss nach .so.1.2.3 installier werden!
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a
	dh_makeshlibs -plibmysqlclient$(SHARED_LIB_MAJOR_VERSION) -V"libmysqlclient$(SHARED_LIB_MAJOR_VERSION) (>= $(PKGVERSION))"
	dh_makeshlibs -plibndbclient$(NDB_SHARED_LIB_MAJOR_VERSION) -V"libndbclient2 (>= $(PKGVERSION))"
	dh_installdeb -a
	dh_perl -a
	dh_shlibdeps -a -l debian/libmysqlclient$(SHARED_LIB_MAJOR_VERSION)/usr/lib -L libmysqlclient$(SHARED_LIB_MAJOR_VERSION)
	dh_shlibdeps -a -l debian/libndbclient$(NDB_SHARED_LIB_MAJOR_VERSION)/usr/lib -L libndbclient$(NDB_SHARED_LIB_MAJOR_VERSION)
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

binary:	binary-indep binary-arch
.PHONY: clean stamp-control control-file configure build binary binary-indep binary-arch install 

# vim: ts=8
