##
 # @file build/MEGAsync/MEGAsync/PKGBUILD
 # @brief script to generate package megasync for ArchLinux
 #
 # (c) 2013-2016 by Mega Limited, Auckland, New Zealand
 #
 # This file is part of the MEGA SDK - Client Access Engine.
 #
 # Applications using the MEGA API must present a valid application key
 # and comply with the the rules set forth in the Terms of Service.
 #
 # The MEGA SDK is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 #
 # @copyright Simplified (2-clause) BSD License.
 #
 # You should have received a copy of the license along with this
 # program.
##

# Maintainer: Pablo Martin <pm@mega.nz>
pkgname=megasync
pkgver=MEGASYNC_VERSION
pkgrel=`cat MEGA_BUILD_ID || echo "1"`
epoch=
pkgdesc="Get more control over your data"
arch=('i686' 'x86_64')
url="http://mega.nz/#sync"
license=() #TODO
groups=()
depends=('glibc>=2.33' 'gcc-libs>=10.2.0' 'qt5-base>=5.15' 'qt5-tools>=5.15' 'icu>=69.1' 'sqlite' 'openssl>=3.0.0' 'zlib' 'qt5-svg>=5.15' 'qt5-x11extras' 'bzip2' 'xz' 'libraw' 'double-conversion>=3.1.5' 'systemd')
makedepends=('unzip' 'wget' 'ca-certificates' 'qt5-tools' 'ffmpeg-mega' 'pdfium-mega' 'bzip2' 'xz')
checkdepends=()
optdepends=('sni-qt: fix systray issue on KDE and LXQt', 'nautilus-megasync: for integration with nautilus')
provides=("megasync=${pkgver}" 'ffmpeg-mega')
conflicts=('megatools')
replaces=()
backup=()
options=(debug strip)
install=megasync.install
changelog= #TODO
source=("./${pkgname}_$pkgver.tar.gz"
)
noextract=()
md5sums=('MD5SUM') # generated with makepkg -g
validpgpkeys=()

prepare() {
	cd "$pkgname-$pkgver"
	sed -i -E "s/USER_AGENT([^\/]*)\/(([0-9][0-9]*\.){3})(.*)\";/USER_AGENT\1\/\2${pkgrel}\";/g" MEGASync/control/Preferences.cpp
	sed -i -E "s/BUILD_ID = ([0-9]*)/BUILD_ID = ${pkgrel}/g" MEGASync/control/Preferences.cpp
}

build() {
	_bindir=$pkgdir/usr/bin
	cd "$pkgname-$pkgver"
	./configure -e -q -g
	# Link dynamically with freeimage if pdfium is required.
	ln -sfr $PWD/MEGASync/mega/bindings/qt/3rdparty/libs/libfreeimage*.so $PWD/MEGASync/mega/bindings/qt/3rdparty/libs/libfreeimage.so.3; \
	ln -sfn libfreeimage.so.3 $PWD/MEGASync/mega/bindings/qt/3rdparty/libs/libfreeimage.so; \
	qmake-qt5 "CONFIG += FULLREQUIREMENTS" QMAKE_RPATHDIR="/opt/mega/lib" MEGA.pro || qmake-qt4 "CONFIG += FULLREQUIREMENTS" QMAKE_RPATHDIR="/opt/mega/lib" MEGA.pro #qmake would fail when qt4 installed (due to some path missconfigurations)
	lrelease-qt5 MEGASync/MEGASync.pro || lrelease-qt4 MEGASync/MEGASync.pro 
	make ${BUILD_PARALLEL_JOBS}
}

check() {
	cd "$pkgname-$pkgver"
#	make -k check  DESTDIR=$_bindir THE_RPM_BUILD_ROOT=$pkgdir
}

package() {
	_bindir=$pkgdir/usr/bin
	mkdir -p $_bindir

	cd "$pkgname-$pkgver"
	make install TARGET=$_bindir/megasync INSTALL_ROOT=$pkgdir
	install -m 755 -p MEGASync/megasync $_bindir/megasync
	mkdir -p $pkgdir/etc/sysctl.d/
	echo "fs.inotify.max_user_watches = 524288" > $pkgdir/etc/sysctl.d/99-megasync-inotify-limit.conf
	mkdir -p $pkgdir/etc/udev/rules.d/
	echo "SUBSYSTEM==\"block\", ATTRS{idDevtype}==\"partition\"" > $pkgdir/etc/udev/rules.d/99-megasync-udev.rules
	mkdir -p $pkgdir/opt/mega/lib
	install -D MEGASync/mega/bindings/qt/3rdparty/libs/libfreeimage.so.* $pkgdir/opt/mega/lib
}
