#!/usr/bin/make -f

#export DH_VERBOSE=1

include /usr/share/dpatch/dpatch.make

CMAKE=cmake
TMP=$(CURDIR)/debian/tmp/

feature_set="community"
major_version=29
minor_version=1
compilation_comment_release="Percona Server (GPL), Release $(major_version).$(minor_version)"
server_suffix="-$(major_version).$(minor_version)"
prefix="/usr"

clean: clean-patched unpatch

clean-patched:
	dh clean
	rm -rf CMakeFiles

build: patch
	dh_testdir
	$(CMAKE) . -DBUILD_CONFIG=mysql_release \
	   -DINSTALL_LAYOUT=DEB \
           -DCMAKE_BUILD_TYPE=RelWithDebInfo \
           -DWITH_EMBEDDED_SERVER=OFF \
           -DMYSQL_UNIX_ADDR="/var/run/mysqld/mysqld.sock" \
           -DFEATURE_SET=$(feature_set) \
           -DCOMPILATION_COMMENT=$(compilation_comment_release) \
           -DMYSQL_SERVER_SUFFIX=$(server_suffix)
	make $(MAKE_JFLAG)
	
	# Build HandlerSocket
	echo "Configuring HandlerSocket"
	cd storage/HandlerSocket-Plugin-for-MySQL && \
	bash -x autogen.sh && \
	CXX=g++ CXXFLAGS="$(CXXFLAGS) -I$(CURDIR)/include" \
		./configure --with-mysql-source=$(CURDIR) \
			--with-mysql-bindir=$(CURDIR)/scripts \
			--with-mysql-plugindir=$(prefix)/lib/mysql/plugin \
			--libdir=$(prefix)/lib \
			--prefix=$(prefix) && \
	make $(MAKE_JFLAG)
	
	# Build UDF
	cd UDF && \
	CXX=g++ CXXFLAGS="$(CXXFLAGS) -I$(CURDIR)/include" \
		./configure --includedir=$(CURDIR)/include \
			--libdir=$(prefix)/lib/mysql/plugin && \
	make $(MAKE_JFLAG)
	
	dh_auto_test

binary:
	echo "Making binary"
	dh --before dh_auto_install binary
	dh_auto_install
	cd storage/HandlerSocket-Plugin-for-MySQL && make DESTDIR=$(TMP) install
	cd UDF && make DESTDIR=$(TMP) install
	dh --after dh_auto_install --before dh_installinit binary
	dh_installinit --name=mysql
	dh --after dh_installinit binary


binary-arch:
	echo "Making binary"
	dh --before dh_auto_install binary-arch
	dh_auto_install
	cd storage/HandlerSocket-Plugin-for-MySQL && make DESTDIR=$(TMP) install
	cd UDF && make DESTDIR=$(TMP) install
	dh --after dh_auto_install --before dh_installinit binary-arch
	dh_installinit --name=mysql
	dh --after dh_installinit binary-arch

binary-indep:
	echo "Making binary"
	dh --before dh_auto_install binary-indep
	dh_auto_install
	cd storage/HandlerSocket-Plugin-for-MySQL && make DESTDIR=$(TMP) install
	cd UDF && make DESTDIR=$(TMP) install
	dh --after dh_auto_install --before dh_installinit binary-indep
	dh_installinit --name=mysql
	dh --after dh_installinit binary-indep

install:
	echo "Making binary"
	dh --before dh_auto_install install
	dh_auto_install
	cd storage/HandlerSocket-Plugin-for-MySQL && make DESTDIR=$(TMP) install
	cd UDF && make DESTDIR=$(TMP) install
	dh --after dh_auto_install --before dh_installinit install
	dh_installinit --name=mysql
	dh --after dh_installinit install
