##
 # @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=`cat MEGA_BUILD_ID || echo "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.37' 'sqlite' 'openssl>=1.1.1' 'zlib' 'libpsl' 'icu>=69.1' 'bash-completion' 'pcre' 'bzip2' 'xz')
makedepends=('unzip' 'wget' 'ca-certificates' 'ffmpeg-mega' 'pdfium-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"
	sed -i -E "s/(^#define MEGACMD_BUILD_ID )[0-9]*/\1${pkgrel}/g" src/megacmdversion.h
}

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

	#build dependencies into folder deps
	SQLITE3=$(grep -- -L ./contrib/build_sdk.sh >/dev/null && echo "-L" || echo "")
	mkdir deps || :
	bash -x ./contrib/build_sdk.sh -o archives \
	  -e -g -b -l -c -s -u -v -a -q -I -p $SQLITE3 deps/

	# Link dynamically with freeimage
	ln -sfr $PWD/deps/lib/libfreeimage*.so $PWD/deps/lib/libfreeimage.so.3
	ln -sfn libfreeimage.so.3 $PWD/deps/lib/libfreeimage.so

	CPPFLAGS="$CPPFLAGS -DREQUIRE_HAVE_PDFIUM -DREQUIRE_HAVE_FFMPEG -DREQUIRE_HAVE_LIBUV -DREQUIRE_USE_MEDIAINFO -DREQUIRE_USE_PCRE" LDFLAGS="$LDFLAGS -Wl,-rpath,/opt/megacmd/lib" \
	  ./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-pdfium \
	  --with-termcap=$PWD/deps --prefix=$PWD/deps --disable-examples \
	  --with-libmediainfo=$PWD/deps --with-libzen=$PWD/deps

	make ${BUILD_PARALLEL_JOBS}

	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
	mkdir -p $pkgdir/etc/sysctl.d/
	echo "fs.inotify.max_user_watches = 524288" > $pkgdir/etc/sysctl.d/99-megacmd-inotify-limit.conf
	mkdir -p $pkgdir/opt/megacmd/lib
	install -D deps/lib/libfreeimage.so.* $pkgdir/opt/megacmd/lib
}
