#!/usr/bin/make -f

PACKAGE = $(shell dh_listpackages)
TMP     = $(CURDIR)/debian/$(PACKAGE)
ARCHLIB := $(shell perl -MConfig -e 'print $$Config{vendorarch}')

export DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow

export MYSQL_DIR=$(CURDIR)/t/testdb
export MYSQL_UNIX_PORT=$(MYSQL_DIR)/mysql.sock
export MYSQL_PIDFILE=$(MYSQL_DIR)/mysql.pid
export MYSQL_USER=$(shell whoami)
export MYSQL_PASS=
export MYSQL_DBNAME=test

%:
	dh $@ --with perl_dbi

override_dh_auto_configure:
	LC_ALL=C.UTF-8 dh_auto_configure
	sed -i 's/x86_64-linux-gnu-gcc/x86_64-linux-gnu-g++/g' Makefile
	sed -i 's:-lperconaserverclient::g' Makefile
	sed -i 's:CCCDLFLAGS = -fPIC:CCCDLFLAGS = -fPIC -fpermissive:' Makefile
	sed -i 's:-L/usr/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu/libperconaserverclient.a -L/usr/lib/x86_64-linux-gnu:g' Makefile

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# prepare mariadb/mysql server
	#sh $(CURDIR)/debian/tests/pkg-perl/smoke-setup
	#dh_auto_test
	# tear down mariadb/mysql server
	#sh $(CURDIR)/debian/tests/pkg-perl/smoke-cleanup
	$(info Test can be started only when DB is running)
endif

override_dh_auto_install:
	dh_auto_install
	find $(TMP)/usr -name '*.pod' | xargs -r chmod 0644
	[ ! -f $(TMP)/$(ARCHLIB)/DBD/mysql/INSTALL.pod ]           || $(RM) -v $(TMP)/$(ARCHLIB)/DBD/mysql/INSTALL.pod
	[ ! -d $(TMP)/$(ARCHLIB)/Bundle/ ]                         || $(RM) -vr $(TMP)/$(ARCHLIB)/Bundle/
	[ ! -f $(TMP)/usr/share/man/man3/Bundle::DBD::mysql.3pm ]  || $(RM) -v $(TMP)/usr/share/man/man3/Bundle::DBD::mysql.3pm
	[ ! -f $(TMP)/usr/share/man/man3/DBD::mysql::INSTALL.3pm ] || $(RM) -v $(TMP)/usr/share/man/man3/DBD::mysql::INSTALL.3pm
