TERMUX_PKG_HOMEPAGE=https://mega.io/
TERMUX_PKG_DESCRIPTION="Provides non UI access to MEGA services"
TERMUX_PKG_LICENSE="BSD 2-Clause"
TERMUX_PKG_MAINTAINER="@termux"
TERMUX_PKG_VERSION=("2.5.2"
					"10.9.0")
TERMUX_PKG_SRCURL=("https://github.com/meganz/MEGAcmd/archive/refs/tags/${TERMUX_PKG_VERSION[0]}_Linux.tar.gz"
					"https://github.com/meganz/sdk/archive/refs/tags/v${TERMUX_PKG_VERSION[1]}.tar.gz")
TERMUX_PKG_SHA256=(
	a088c001e9116357fcac92cd0b3d62fb53cc24ce546eb0fb4cc83c53df5eed7c
	811fcb952b09e765fa3559e370ae5f9b026cc8a297e125847acc8c77cfda01a8
)
TERMUX_PKG_AUTO_UPDATE=true
TERMUX_PKG_UPDATE_VERSION_REGEXP="\d+\.\d+\.\d+_Linux"
TERMUX_PKG_UPDATE_VERSION_SED_REGEXP='s/_Linux//'

# dbus is required for $PREFIX/var/lib/dbus/machine-id
TERMUX_PKG_DEPENDS="c-ares, cryptopp, dbus, ffmpeg, freeimage, libandroid-glob, libc++, libcurl, libicu, libsodium, libsqlite, libuv, mediainfo, openssl, pcre, readline, zlib"
TERMUX_PKG_EXTRA_CONFIGURE_ARGS="
-DUSE_FFMPEG=ON
-DUSE_PCRE=ON
-DUSE_LIBUV=ON
-DUSE_READLINE=ON
-DUSE_PDFIUM=OFF
-DFULL_REQS=OFF
-DVCPKG_ROOT=FALSE
-DENABLE_MEGACMD_TESTS=OFF
-DWITH_FUSE=OFF
"

termux_step_post_get_source() {
	rm -rf sdk
	mv "sdk-${TERMUX_PKG_VERSION[1]}" sdk

	# We use `config.h` generated by configure script instead of this one.
	local f="sdk/include/mega/config-android.h"
	if [ -f "${f}" ]; then
		rm -f "${f}"
		echo '#error DO NOT INCLUDE ME!' > "${f}"
	else
		termux_error_exit "Source file '${f}' not found."
	fi
}

termux_step_pre_configure() {
	export OBJCXX="$CXX"

	# -licui18n fixes 'ld.lld: error: undefined symbol: icu_78::Collator::createInstance'
	LDFLAGS+=" -landroid-glob -lpcrecpp -lz -licui18n"
	LDFLAGS+=" $($CC -print-libgcc-file-name)"

	# Fix build against FFmpeg 6.0:
	CPPFLAGS+=" -DCODEC_CAP_TRUNCATED=0"

	# treat Android as Linux like sdl2 package
	rm -rf "$TERMUX_PKG_SRCDIR/sdk/examples"
	find "$TERMUX_PKG_SRCDIR" -type f | \
		xargs -n 1 sed -i \
		-e 's/\([^A-Za-z0-9_]__ANDROID\)\(__[^A-Za-z0-9_]\)/\1_NO_TERMUX\2/g' \
		-e 's/\([^A-Za-z0-9_]__ANDROID\)__$/\1_NO_TERMUX__/g'
}
