##
 # @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: MEGA Linux Team <linux@mega.io>
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="https://mega.io/desktop"
license=() #TODO
groups=()
depends=('glibc>=2.33' 'gcc-libs>=10.2.0' 'qt5-base>=5.15' qt5-svg qt5-x11extras qt5-graphicaleffects qt5-declarative qt5-quickcontrols2 qt5-quickcontrols)
makedepends=(unzip zip tar cmake python3 autoconf-archive nasm qt5-tools qt5-declarative qt5-graphicaleffects 'qt5-base>=5.15')
checkdepends=()
optdepends=(
  'qt5-wayland: Wayland support'
  'xdg-desktop-portal: follow the desktop color scheme (using dbus)'
  'glib2: follow the desktop color scheme (using gsettings)'
  'nautilus-megasync: integration with nautilus'
  'dolphin-megasync: integration with dolphin'
  'thunar-megasync: integration with thunar'
  'nemo-megasync: integration with nemo'
)
provides=("megasync=${pkgver}")
conflicts=('megatools')
replaces=()
backup=()
options=(debug strip !lto)
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/VER_BUILD_ID([[:space:]]+)([0-9]*)/VER_BUILD_ID\1${pkgrel}/g" src/MEGASync/control/Version.h

	if [ -f /opt/vcpkg.tar.gz ]; then
		tar xzf /opt/vcpkg.tar.gz
	fi
}

build() {
	cd "$pkgname-$pkgver"

	megasrcdir="${PWD}"
	megabuilddir="${megasrcdir}/build_dir"

	if [ -d "${megasrcdir}/vcpkg" ]; then
		export VCPKG_DEFAULT_BINARY_CACHE=/opt/persistent/vcpkg_cache
		mkdir -p ${VCPKG_DEFAULT_BINARY_CACHE}
		vcpkg_root="-DVCPKG_ROOT=${megasrcdir}/vcpkg"
	fi

	cmake ${vcpkg_root} \
	      -DENABLE_DESKTOP_UPDATE_GEN=OFF \
	      -DENABLE_DESIGN_TOKENS_IMPORTER=OFF \
	      -DENABLE_DESKTOP_APP_TESTS=OFF \
	      -DCMAKE_BUILD_TYPE=RelWithDebInfo \
	      -S "${megasrcdir}" \
	      -B "${megabuilddir}"
	cmake --build "${megasrcdir}"/build_dir \
	              ${BUILD_PARALLEL_JOBS}
}

check() {
	cd "$pkgname-$pkgver"
}

package() {
	megasrcdir="${srcdir}/${pkgname}-${pkgver}"
	megabuilddir="${megasrcdir}/build_dir"
	
	cmake --install "${megabuilddir}" --prefix $pkgdir

	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
}
