#!/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 python2

override_dh_clean:
	$(RM) -r CMakeFiles antlr-wb-prefix/tmp ext/antlr-runtime/.libs
	dh_clean debian/mysql-workbench-help.tar.xz

override_dh_auto_configure:
	# rebuild files generated by swig
	$(MAKE) -C library/forms/swig --makefile Makefile.swig
	dh_auto_configure -- \
            -DUSE_UNIXODBC=ON \
            -DCMAKE_INSTALL_PREFIX=/usr \
            -DREAL_EXECUTABLE_DIR=/usr/lib/$(PKG)

override_dh_auto_build:
	# 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

debian/mysql-workbench-help.tar.xz:
	tar -C debian -caf $@ mysql-workbench-help --owner=root --group=root --mode=a+rX

override_dh_auto_install: debian/mysql-workbench-help.tar.xz
	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'
	# install link to launcher to /usr/bin/
	dh_link -p$(PKG) \
            usr/lib/$(PKG)/mysql-workbench usr/bin/mysql-workbench

override_dh_compress:
	dh_compress --exclude=.mwb

override_dh_makeshlibs:
	dh_makeshlibs --noscripts

# help `dh_shlibdeps` to find local libraries: 'dpkg-shlibdeps: error: couldn't find library'
override_dh_shlibdeps:
	LD_LIBRARY_PATH="debian/$(PKG)/usr/lib/$(PKG):$(LD_LIBRARY_PATH)" dh_shlibdeps

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

## semi-automated manpage generation (to invoke manually)
MAN_NAME=mysql-workbench \\- MySQL Database Design, Administration and Development Tool
mysql-workbench.1:
	help2man --no-info --manual="mysql-workbench" --name="$(MAN_NAME)" mysql-workbench > $@
	perl \
            -E 's{^.*?\s+\\-\s+mysql-workbench.*}{$(MAN_NAME)}; # correcting NAME section'     \
            -E 's{^\.SH DESCRIPTION}{.SH SYNOPSIS};             # correcting SYNOPSIS section' \
            -E 's{^mysql\\-workbench\K\\-bin}{};                # correcting executable name'  \
            -E 's{^Options:}{.SH OPTIONS};                      # create OPTIONS section'      \
            -pi $@

## http://wiki.debian.org/onlyjob/get-orig-source
UVER  = $(shell dpkg-parsechangelog -l$(PKD)/changelog | perl -ne 'print $$1 if m{^Version:\s+(?:\d+:)?(\d.*)(?:\-\d+.*)};')
DTYPE = +dfsg
VER  ?= $(subst $(DTYPE),,$(UVER))
.PHONY: get-orig-source
get-orig-source: $(PKG)_$(VER)$(DTYPE).orig.tar.xz $(info I: $(PKG)_$(VER)$(DTYPE))
	@

$(PKG)_$(VER)$(DTYPE).orig.tar.xz:
	@echo "# Downloading..."
	uscan --noconf --verbose --rename --destdir=$(CURDIR) --check-dirname-level=0 --force-download --download-version $(VER) $(PKD)
	$(if $(wildcard $(PKG)-$(VER)),$(error $(PKG)-$(VER) exist, aborting..))
	@echo "# Extracting..."
	mkdir $(PKG)-$(VER) \
        && tar -xf $(PKG)_$(VER).orig.tar.* --directory $(PKG)-$(VER) --strip-components 1 \
        || $(RM) -r $(PKG)-$(VER)
	@echo "# Cleaning-up..."
	find $(PKG)-$(VER) -type f -name '*.cmd' -delete -printf 'removed %p\n'
	find $(PKG)-$(VER) -type f -name '*.vc*' -delete -printf 'removed %p\n'
	find $(PKG)-$(VER) -depth -type d -name 'macosx' -exec $(RM) -r {} \; -printf 'removed %p\n'
	find $(PKG)-$(VER) -depth -type d -name 'windows' -exec $(RM) -r {} \; -printf 'removed %p\n'
	cd $(PKG)-$(VER) \
        && $(RM) -r -v \
             build/msi \
             build/res \
             build/zip \
             ext/Aga.Controls \
        0000-ext/antlr-runtime \
             ext/scintilla/.hgtags \
             ext/scintilla/cocoa \
             ext/scintilla/win32 \
             ext/HTMLRenderer \
             frontend/mac \
             library/base.windows \
             library/forms/cocoa \
             library/sql-parser/yy_purify-tool/dist/*.jar \
             MySQLWorkbench.xcodeproj \
             plugins/wb.doclib/res/DocLibrary/* \
             res/fonts \
	#$(RM) -v $(PKG)_$(VER).orig.tar.*
	@echo "# Packing..."
	find -L "$(PKG)-$(VER)" -xdev -type f -print | sort \
        | XZ_OPT="-7v" tar -caf "$(PKG)_$(VER)$(DTYPE).orig.tar.xz" -T- --owner=root --group=root --mode=a+rX \
        && $(RM) -r "$(PKG)-$(VER)"
