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

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

PKD  = $(abspath $(dir $(MAKEFILE_LIST)))
PKG  = $(word 2,$(shell dpkg-parsechangelog -l$(PKD)/changelog | grep ^Source))

# 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))

# minimise needless linking
export DEB_LDFLAGS_MAINT_APPEND= -Wl,--as-needed

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

# use --as-needed only if supported by dh-autoreconf (to simplify backporting)
DH_AS_NEEDED=$(shell dpkg --compare-versions $$(dpkg --status dh-autoreconf | grep Version | cut -d' ' -f2) ge 6 && echo --as-needed)
override_dh_autoreconf:
	#NOCONFIGURE="1" ./autogen.sh
	dh_autoreconf $(DH_AS_NEEDED)

override_dh_auto_configure:
	dh_auto_configure -- --libdir=/usr/lib \
            --libexecdir=/usr/lib/$(PKG)/bin \
            --exec-prefix=/usr/lib/$(PKG) \
            --enable-grt-generation \
            --enable-dependency-tracking \
            --with-unixodbc \
                --with-odbc-cflags=-I/usr/include \
                --with-odbc-libs=-lodbc
	#--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 -v $(CURDIR)/debian/tmp/usr/lib/$(PKG)/plugins/* \
              $(CURDIR)/debian/tmp/usr/lib/$(PKG)/
	# wipe empty dirs if any
	find $(CURDIR)/debian/tmp -type d -empty -delete
	# install link to launcher to /usr/bin/
	dh_link -p$(PKG) usr/lib/$(PKG)/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
override_dh_strip:
	[ -d "$(CURDIR)/debian/$(PKG)-dbg" ] \
        && dh_strip --dbg-package=$(PKG)-dbg \
        || dh_strip

override_dh_builddeb:
	dh_builddeb -- -Zxz

VER ?= $(shell dpkg-parsechangelog -l$(PKD)/changelog | perl -ne 'print $$1 if m{Version:\s*([\d\.]+)}')
DTYPE := +dfsg
.PHONY: get-orig-source
get-orig-source: $(PKG)_$(VER)$(DTYPE).orig.tar.xz
	@

$(PKG)_$(VER)$(DTYPE).orig.tar.xz: $(PKG)-$(VER)
	@echo "Clean-up..."
	find $(PKG)-$(VER) -type f -name '*.cmd' -delete -printf 'removing %p\n'
	find $(PKG)-$(VER) -type f -name '*.vc*' -delete -printf 'removing %p\n'
	find $(PKG)-$(VER) -depth -type d -name 'macosx' -exec $(RM) -r {} \; -printf 'removing %p\n'
	find $(PKG)-$(VER) -depth -type d -name 'windows' -exec $(RM) -r {} \; -printf 'removing %p\n'
	cd $(PKG)-$(VER) \
        && $(RM) -r -v \
             build/msi \
             build/res \
             build/zip \
             ext/Aga.Controls \
        0000-ext/antlr-runtime \
             ext/scintilla/cocoa \
             ext/scintilla/win32 \
             ext/cppconn \
             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..."
	XZ_OPT="-7v" tar -caf "$(PKG)_$(VER)$(DTYPE).orig.tar.xz" "$(PKG)-$(VER)" --owner=root --group=root --mode=a+rX \
        && $(RM) -r $(PKG)-$(VER)

$(PKG)-$(VER):
	@echo "# Downloading..."
	uscan --noconf --verbose --rename --repack --destdir=$(CURDIR) --check-dirname-level=0 --force-download --download-version $(VER) $(PKD)
	mkdir $(PKG)-$(VER) \
        && tar -xf $(PKG)_$(VER).orig.tar.gz --directory $(PKG)-$(VER) --strip-components 1 \
        || $(RM) -r $(PKG)-$(VER)
	#$(RM) $(PKG)_$(VER).orig.tar.gz
