#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

PACKAGE := mysql-workbench

# parallel build by default on linux
ifeq ($(DEB_HOST_ARCH_OS),linux)
    ifeq ($(findstring parallel=,$(DEB_BUILD_OPTIONS)),)
        export DEB_BUILD_OPTIONS+=parallel=$(shell getconf _NPROCESSORS_ONLN)
    endif
endif
$(info Info: DEB_BUILD_OPTIONS:$(origin DEB_BUILD_OPTIONS)=$(DEB_BUILD_OPTIONS))
export DEB_BUILD_MAINT_OPTIONS=hardening=+all,-bindnow

%:
	dh $@ --parallel --max-parallel=4 --with autoreconf,python2

override_dh_autoreconf:
	#NOCONFIGURE="1" ./autogen.sh
	dh_autoreconf

override_dh_auto_configure:
	dh_auto_configure -- --libdir=/usr/lib \
		--exec-prefix=/usr/lib/$(PACKAGE) \
		--enable-grt-generation \
		--enable-dependency-tracking
		#--with-commondirname

override_dh_auto_build:
	# rebuild files generated by swig
	$(MAKE) -C library/forms/swig --makefile Makefile.swig
	# rebuild files generated by flex/bison
	cd library/sql-parser/yy_gen-tool/yy_gen-tool/ \
	&& bison -dl parser.yy \
	&& flex -B -oparser.lex.cc parser.lex
	# generate icon
	convert images/icons/MySQLWorkbench-32.png debian/mysql-workbench.xpm
	# generate documentation for mysql-workbench-docs package
	#cd doc && ./DoxygenRun.sh
	# build all the rest
	dh_auto_build --max-parallel=4

override_dh_auto_install:
	dh_auto_install --max-parallel=4 --destdir=$(CURDIR)/debian/tmp
	# .desktop to unix line endings
	sed -i 's/\r//g' debian/tmp/usr/share/applications/*.desktop
	# remove *.la files
	find . -name '*.la' -delete -printf 'removing %p\n'
	# move plugins to "/usr/lib/mysql-workbench" where they can be found by M-W
	mv $(CURDIR)/debian/tmp/usr/lib/$(PACKAGE)/plugins/* \
           $(CURDIR)/debian/tmp/usr/lib/$(PACKAGE)/
	# wipe empty dirs if any
	find $(CURDIR)/debian/tmp -type d -empty -delete
	# install link to launcher to /usr/bin/
	dh_link -p$(PACKAGE) usr/lib/$(PACKAGE)/bin/mysql-workbench \
            usr/bin/mysql-workbench

override_dh_compress:
	dh_compress --exclude=.mwb

override_dh_makeshlibs:
	dh_makeshlibs --noscripts

# -dbg package will be over 150 MiB
# gracefully handle stripping if -dbg package (un-)commented in debian/control
override_dh_strip:
	[ -d "$(CURDIR)/debian/$(PACKAGE)-dbg" ] \
        && dh_strip --dbg-package=$(PACKAGE)-dbg \
        || dh_strip

override_dh_builddeb:
	dh_builddeb -- -Zxz

VER = $(shell dpkg-parsechangelog | perl -ne 'print $$1 if m/Version:\s*([\d\.]+)/')
get-orig-source: $(PACKAGE)-$(VER)
	@echo "Clean-up..."
	find $(PACKAGE)-$(VER) -type f -name '*.cmd' -delete -printf 'removing %p\n'
	find $(PACKAGE)-$(VER) -type f -name '*.vc*' -delete -printf 'removing %p\n'
	find $(PACKAGE)-$(VER) -depth -type d -name 'macosx' -exec $(RM) -r {} \; -printf 'removing %p\n'
	find $(PACKAGE)-$(VER) -depth -type d -name 'windows' -exec $(RM) -r {} \; -printf 'removing %p\n'
	cd $(PACKAGE)-$(VER) \
	&& $(RM) -r -v \
             build/msi \
             build/res \
             build/zip \
             ext/scintilla/cocoa \
             ext/scintilla/win32 \
             ext/cppconn/win \
             ext/connector-python \
             ext/ctemplate \
             ext/tinyxml \
             ext/python \
             ext/HTMLRenderer \
             ext/mysql-utilities \
             frontend/mac \
             library/base.windows \
             library/forms/cocoa \
             library/sql-parser/yy_purify-tool/dist/*.jar \
             MySQLWorkbench.xcodeproj \
             res/fonts \
             tools/utils_wrapper
	@echo "Packing..."
	tar cJf "$(PACKAGE)_$(VER)+dfsg.orig.tar.xz" "$(PACKAGE)-$(VER)" \
	&& $(RM) -r $(PACKAGE)-$(VER)

$(PACKAGE)-$(VER):
	uscan --noconf --verbose --force-download --rename --repack --destdir=. --download-version $(VER)
	mkdir $(PACKAGE)-$(VER) \
	&& tar xf $(PACKAGE)_$(VER).orig.tar.gz --directory $(PACKAGE)-$(VER) --strip-components 1 \
	|| $(RM) -r $(PACKAGE)-$(VER)
	#$(RM) $(PACKAGE)_$(VER).orig.tar.gz
