##
 # @file build/MEGAsync/MEGAShellExtDolphin/PKGBUILD
 # @brief script to generate package dolphinmegasync 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.
##

pkgname=dolphin-megasync
pkgver=EXT_VERSION
pkgrel=`cat MEGA_BUILD_ID || echo "1"`
epoch=
pkgdesc="MEGA Desktop App plugin for Dolphin"
arch=('x86_64')
url="https://mega.io/desktop"
license=() #TODO
depends=('megasync>=5.3.0' 'dolphin')
makedepends=('grep' 'cmake' 'extra-cmake-modules' 'kde-sdk-meta')
provides=("dolphin-megasync=${pkgver}")
conflicts=()
replaces=()
backup=()
options=()
changelog= #TODO
source=("./${pkgname}_$pkgver.tar.gz")
noextract=()
md5sums=('MD5SUM')
validpgpkeys=()

KF_VERSIONS=""

prepare() {
	# Get all kio packages installed, to build for all available kf versions
	for p in $(pacman -Qqs "^kio.?$")
	do
		# Get KF version by checking where it installs files
		KF_VERSION=$(pacman -Ql $p | grep -m1 -oP '/usr/include/KF\K\w+(?=/)')
		KF_VERSIONS+=" ${KF_VERSION}"
	done
}

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

build() {
	for k in ${KF_VERSIONS}
	do
		local cmake_options=(
		-B build-kf$k
		-S $pkgname-$pkgver
		-W no-dev
		-D CMAKE_BUILD_TYPE=None
		-D CMAKE_INSTALL_PREFIX=/usr
		-D KF_VER=$k
		)
		cmake "${cmake_options[@]}"
		cmake --build build-kf$k
	done
}

package() {
	for d in build-kf*
	do
		DESTDIR="$pkgdir" cmake --install ${d}
	done
}
