#!/usr/bin/make -f
NJOBS := $(shell echo ${DEB_BUILD_OPTIONS} | sed -rn 's/.*parallel=([0-9]*).*/\1/p')
FMEDIAINFO := $(shell dpkg -l | grep mediainfo >/dev/null && echo "-i" || echo " ")
FCRYPTOPP := $(shell cat /etc/issue | grep "Ubuntu 1[4-8]\|Ubuntu 19.04\|Debian.* [8]" >/dev/null && echo "" || echo "-q")
DPKGXZ := $(shell cat /etc/issue | grep "Ubuntu 23\|Lunar Lobster\|Ubuntu 22\|Jammy Jellyfish\|Ubuntu 21.10" >/dev/null && echo "-- -Zxz" || echo "")
EXTRAQMAKE := $(shell cat /etc/issue | grep "Ubuntu 1[2345679]\|Ubuntu 18.10\|Ubuntu 20.10\|Ubuntu 21\|Debian.* [789]" >/dev/null && echo "DEFINES+=MEGASYNC_DEPRECATED_OS" || echo "")
FULLREQS := $(shell cat /etc/issue | grep "Ubuntu 12.04" >/dev/null && echo "" || echo '"CONFIG += FULLREQUIREMENTS"')
INSTALL3RD := $(shell dpkg --print-architecture | grep -i "i386\|arm" >/dev/null && echo "" || echo 'install3rd')
EXTRAQMAKEFREEIMAGE := $(shell dpkg --print-architecture | grep -i "i386\|arm" >/dev/null && echo "" || echo 'QMAKE_RPATHDIR="/opt/mega/lib"')
export PATH := /opt/mega/bin:$(PATH)
QMAKESPEC := $(shell cat /etc/issue | grep "Ubuntu 14.04\|Ubuntu 16.04\|Debian.* 8\|Ubuntu 18.04\|Debian.* 9" >/dev/null && echo "QMAKESPEC" || echo "NONQMAKESPEC")
QTINSTALL := $(shell cat /etc/issue | grep "Ubuntu 14.04\|Ubuntu 16.04\|Ubuntu 18.04\|Debian.* 9\|Debian.* 10" >/dev/null && echo "installqt" || echo "")
DEB_TESTING := $(shell cat /etc/issue | grep "Debian" > /dev/null && cat /etc/apt/sources.list.d/debian.sources | grep -v "^\#" | grep testing > /dev/null && echo "testing")
export $(QMAKESPEC) := /opt/mega/mkspecs/linux-g++

MEGA_BUILD_ID := $(shell cat MEGA_BUILD_ID || echo "1")

build: build-stamp
build-stamp:
	dh_testdir
	sed -i -E "s/USER_AGENT([^\/]*)\/(([0-9][0-9]*\.){3})(.*)\";/USER_AGENT\1\/\2$(MEGA_BUILD_ID)\";/g" MEGASync/control/Preferences.cpp
	sed -i -E "s/BUILD_ID = ([0-9]*)/BUILD_ID = $(MEGA_BUILD_ID)/g" MEGASync/control/Preferences.cpp
	sed -i -E "0,/megasync \(([0-9.]*)[^\)]*\)/s//megasync \(\1-$(MEGA_BUILD_ID).1)/" debian.changelog || :
	./configure -g $(FCRYPTOPP) $(FMEDIAINFO)

	# Link dynamically with freeimage if pdfium is required.
	if [ -n "$(INSTALL3RD)" ]; then  \
	  ln -sfr $(CURDIR)/MEGASync/mega/bindings/qt/3rdparty/libs/libfreeimage*.so $(CURDIR)/MEGASync/mega/bindings/qt/3rdparty/libs/libfreeimage.so.3; \
	  ln -sfn libfreeimage.so.3 $(CURDIR)/MEGASync/mega/bindings/qt/3rdparty/libs/libfreeimage.so; \
	else \
	  rm $(CURDIR)/MEGASync/mega/bindings/qt/3rdparty/libs/libfreeimage.so $(CURDIR)/MEGASync/mega/bindings/qt/3rdparty/libs/libfreeimage.so.3 || : ; \
	fi

	qmake-qt5 $(FULLREQS) $(EXTRAQMAKE) $(EXTRAQMAKEFREEIMAGE) || qmake $(FULLREQS) $(EXTRAQMAKE) $(EXTRAQMAKEFREEIMAGE) || /usr/lib/*/qt5/bin/qmake $(FULLREQS) $(EXTRAQMAKE) $(EXTRAQMAKEFREEIMAGE)
	lrelease-qt5 MEGASync/MEGASync.pro || lrelease MEGASync/MEGASync.pro || /usr/lib/*/qt5/bin/lrelease MEGASync/MEGASync.pro || /usr/lib/*/qt4/bin/lrelease MEGASync/MEGASync.pro
	make -j$(NJOBS)
	echo "fs.inotify.max_user_watches = 524288" > 99-megasync-inotify-limit.conf
	echo "SUBSYSTEM==\"block\", ATTRS{idDevtype}==\"partition\"" > 99-megasync-udev.rules
	if [ -n "$(DEB_TESTING)" ]; then echo $(DEB_TESTING) > version; fi
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp

install: build
	dh_auto_install --destdir=debian/megasync -- INSTALL_ROOT=$(CURDIR)/debian/megasync
	dh_install MEGASync/megasync /usr/bin/
	dh_install 99-megasync-inotify-limit.conf /etc/sysctl.d/
	dh_install 99-megasync-udev.rules /etc/udev/rules.d/
	if [ -n "$(DEB_TESTING)" ]; then dh_install version /usr/share/doc/megasync; fi

installqt:
	for i in `ldd MEGASync/megasync | grep opt | awk '{print $$3}'`; do a=`echo $$i | awk -F "/" 'BEGIN{OFS="/"}{NF--;print $$0}' | sed "s#target/##g"`; mkdir -p $(CURDIR)/debian/megasync/$$a; install $$i $(CURDIR)/debian/megasync/$$a; done
	mkdir -p $(CURDIR)/debian/megasync/opt/mega/plugins/platforms/
	mkdir -p $(CURDIR)/debian/megasync/opt/mega/plugins/platforminputcontexts/
	mkdir -p $(CURDIR)/debian/megasync/opt/mega/plugins/imageformats/
	mkdir -p $(CURDIR)/debian/megasync/opt/mega/plugins/iconengines/
	mkdir -p $(CURDIR)/debian/megasync/opt/mega/plugins/bearer/
	install -p /opt/mega/lib/libQt5XcbQpa.so.*.*.* $(CURDIR)/debian/megasync/opt/mega/lib/libQt5XcbQpa.so.5 || :
	install /opt/mega/plugins/platforms/libqxcb.so $(CURDIR)/debian/megasync/opt/mega/plugins/platforms/
	install /opt/mega/plugins/platforms/libqvnc.so $(CURDIR)/debian/megasync/opt/mega/plugins/platforms/
	install /opt/mega/plugins/platforms/libqoffscreen.so $(CURDIR)/debian/megasync/opt/mega/plugins/platforms/
	install /opt/mega/plugins/platforms/libqminimal.so $(CURDIR)/debian/megasync/opt/mega/plugins/platforms/
	install /opt/mega/plugins/platforms/libqlinuxfb.so $(CURDIR)/debian/megasync/opt/mega/plugins/platforms/
	install /opt/mega/plugins/platforminputcontexts/libqtvirtualkeyboardplugin.so $(CURDIR)/debian/megasync/opt/mega/plugins/platforminputcontexts/
	install /opt/mega/plugins/platforminputcontexts/libibusplatforminputcontextplugin.so $(CURDIR)/debian/megasync/opt/mega/plugins/platforminputcontexts/
	install /opt/mega/plugins/platforminputcontexts/libcomposeplatforminputcontextplugin.so $(CURDIR)/debian/megasync/opt/mega/plugins/platforminputcontexts/
	install /opt/mega/plugins/imageformats/libqwebp.so $(CURDIR)/debian/megasync/opt/mega/plugins/imageformats/
	install /opt/mega/plugins/imageformats/libqwbmp.so $(CURDIR)/debian/megasync/opt/mega/plugins/imageformats/
	install /opt/mega/plugins/imageformats/libqtiff.so $(CURDIR)/debian/megasync/opt/mega/plugins/imageformats/
	install /opt/mega/plugins/imageformats/libqtga.so $(CURDIR)/debian/megasync/opt/mega/plugins/imageformats/
	install /opt/mega/plugins/imageformats/libqsvg.so $(CURDIR)/debian/megasync/opt/mega/plugins/imageformats/
	install /opt/mega/plugins/imageformats/libqjpeg.so $(CURDIR)/debian/megasync/opt/mega/plugins/imageformats/
	install /opt/mega/plugins/imageformats/libqico.so $(CURDIR)/debian/megasync/opt/mega/plugins/imageformats/
	install /opt/mega/plugins/imageformats/libqicns.so $(CURDIR)/debian/megasync/opt/mega/plugins/imageformats/
	install /opt/mega/plugins/imageformats/libqgif.so $(CURDIR)/debian/megasync/opt/mega/plugins/imageformats/
	install /opt/mega/plugins/iconengines/libqsvgicon.so $(CURDIR)/debian/megasync/opt/mega/plugins/iconengines/
	install /opt/mega/plugins/bearer/libqnmbearer.so $(CURDIR)/debian/megasync/opt/mega/plugins/bearer/
	install /opt/mega/plugins/bearer/libqgenericbearer.so $(CURDIR)/debian/megasync/opt/mega/plugins/bearer/
	install /opt/mega/plugins/bearer/libqconnmanbearer.so $(CURDIR)/debian/megasync/opt/mega/plugins/bearer/
install3rd:
	mkdir -p $(CURDIR)/debian/megasync/opt/mega/lib
	install $(CURDIR)/MEGASync/mega/bindings/qt/3rdparty/libs/libfreeimage.so.* $(CURDIR)/debian/megasync/opt/mega/lib

# Build architecture-independent files here.
binary-indep: build install $(QTINSTALL) $(INSTALL3RD)
	# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
#	dh_installdebconf
	dh_installdocs
	dh_installexamples
	dh_installmenu
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
	dh_installcron
	dh_installman
	dh_installinfo
#	dh_undocumented
	dh_installchangelogs
	dh_link
	dh_strip --dbg-package=megasync-dbg
	dh_compress
	dh_fixperms
#	dh_makeshlibs
	dh_installdeb
#	dh_perl
	dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info
	dh_gencontrol
	dh_md5sums
	dh_builddeb $(DPKGXZ)

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install

