##
 # @file build/megacmd/megacmd/PKGBUILD
 # @brief script to generate package megacmd for ArchLinux
 #
 # (c) 2013-2016 by Mega Limited, Auckland, New Zealand
 #
 # This file is part of the MEGAcmd.
 #
 # MEGAcmd 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=megacmd
pkgver=megacmd_VERSION
pkgrel=1
epoch=
pkgdesc="MEGA Command Line Interactive and Scriptable Application"
arch=('i686' 'x86_64')
url="http://mega.nz/cmd"
license=("https://github.com/meganz/megacmd/blob/master/LICENSE")
groups=()
depends=('glibc>=2.28' 'gcc-libs' 'sqlite' 'openssl>=1.1.1' 'zlib' 'libpsl' 'icu>=63.1' 'bash-completion' 'pcre' 'bzip2' 'xz')
makedepends=('unzip' 'wget' 'ca-certificates' 'ffmpeg-mega' 'bzip2' 'xz')
checkdepends=()
optdepends=()
provides=("megacmd=${pkgver}" 'ffmpeg-mega')
replaces=()
backup=()
options=()
install=megacmd.install
changelog= #TODO
source=("./${pkgname}_$pkgver.tar.gz"
)
noextract=()
md5sums=('MD5SUM') # generated with makepkg -g
validpgpkeys=()

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

build() {
	cd "$pkgname-$pkgver"
	./autogen.sh

	#build dependencies into folder deps
	mkdir deps || :
	bash -x ./contrib/build_sdk.sh -o archives \
	  -e -g -b -l -c -s -u -v -a -q -p deps/

	CPPFLAGS="$CPPFLAGS -DREQUIRE_HAVE_FFMPEG -DREQUIRE_HAVE_LIBUV -DREQUIRE_USE_MEDIAINFO -DREQUIRE_USE_PCRE" ./configure --without-libraw --disable-shared --enable-static --disable-silent-rules \
	  --disable-curl-checks --with-sodium=$PWD/deps --with-pcre --with-cryptopp=$PWD/deps\
	  --with-curl=$PWD/deps --with-freeimage=$PWD/deps --with-readline=$PWD/deps \
	  --with-cares=$PWD/deps --with-libuv=$PWD/deps \
	  --with-termcap=$PWD/deps --prefix=$PWD/deps --disable-examples \
	  --with-libmediainfo=$PWD/deps --with-libzen=$PWD/deps

	make

	touch build-stamp
}

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

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

	cd "$pkgname-$pkgver"
	make install TARGET=$_bindir/megacmd INSTALL_ROOT=$pkgdir
	
	for i in src/client/mega-*; do install -D $i $_bindir/${i/src\/client\//}; done
	install -D src/client/megacmd_completion.sh $_confdir/bash_completion.d/megacmd_completion.sh
	install -D mega-cmd $_bindir/mega-cmd
	install -D mega-cmd-server $_bindir/mega-cmd-server
	install -D mega-exec $_bindir/mega-exec

}
