# Maintainer: Martchus <martchus@gmx.net>

# All my PKGBUILDs are managed at https://github.com/Martchus/PKGBUILDs where
# you also find the URL of a binary repository.

_reponame=tagparser
_pkgname=tagparser
_android_arch=arm64-v8a
_pkg_arch=aarch64
_android_platform=22
_prefix=/opt/android-libs/$_pkg_arch

pkgname=android-$_pkg_arch-$_pkgname
pkgver=9.3.0
pkgrel=1
arch=('any')
pkgdesc="C++ library for reading and writing MP4/M4A/AAC (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags (Android, $_pkg_arch)"
license=('GPL')
depends=("android-$_pkg_arch-c++utilities")
optdepends=("$_pkgname-doc: API documentation")
checkdepends=("android-$_pkg_arch-cppunit" "android-$_pkg_arch-openssl")
# FIXME: run tests with qemu
#checkdepends+=('qemu-binfmt-arm' "android-$_pkg_arch-linker")
makedepends=('cmake' 'ninja' 'iso-codes' 'android-ndk' 'android-sdk')
#conflicts=("android-$_pkgname-$_android_arch")
#replaces=("android-$_pkgname-$_android_arch")
url="https://github.com/Martchus/${_reponame}"
source=("${_pkgname}-${pkgver}.tar.gz::https://github.com/Martchus/${_reponame}/archive/v${pkgver}.tar.gz")
sha256sums=('5aa10fee7019bcd88053bea8a805379e07e257121eb1b06a7c2c8a6c86576dc6')
options=(!buildflags staticlibs !strip !emptydirs)

prepare() {
  cd "$srcdir/${PROJECT_DIR_NAME:-$_reponame-$pkgver}"
}

build() {
  cd "$srcdir/${PROJECT_DIR_NAME:-$_reponame-$pkgver}"

  local android_sdk_path=${ANDROID_SDK_ROOT:-/opt/android-sdk}
  local android_ndk_path=${ANDROID_NDK_ROOT:-/opt/android-ndk}
  local include_dir=$_prefix/include

  cmake \
    -G Ninja \
    -DCMAKE_SYSTEM_NAME=Android \
    -DCMAKE_SYSTEM_VERSION=$_android_platform \
    -DCMAKE_ANDROID_ARCH_ABI=$_android_arch \
    -DCMAKE_ANDROID_SDK="$android_sdk_path" \
    -DCMAKE_ANDROID_NDK="$android_ndk_path" \
    -DCMAKE_ANDROID_STL_TYPE=c++_shared \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=$_prefix \
    -DCMAKE_FIND_ROOT_PATH="$android_ndk_path/sysroot;$_prefix" \
    -DCMAKE_EXE_LINKER_FLAGS="-Wl,-rpath-link,$_prefix/lib" \
    -DNO_DOXYGEN=ON \
    -DBUILD_SHARED_LIBS:BOOL=ON \
    .
  ninja
}

check() {
  cd "$srcdir/${PROJECT_DIR_NAME:-$_reponame-$pkgver}"

  # can not run the tests so compile them at least
  ninja tests

  # FIXME: see c++utilities

  #if [[ -z $TEST_FILE_PATH ]]; then
  #  msg2 'Skipping execution of testsuite because the environment variable TEST_FILE_PATH is not set.'
  #  return
  #fi

  #export LD_LIBRARY_PATH=$PWD:$prefix/lib:$android_ndk_path/sources/cxx-stl/llvm-libc++/libs/arm64-v8a:$android_ndk_path/platforms/android-28/arch-arm64/usr/lib
  #ninja check
}

package() {
  cd "$srcdir/${PROJECT_DIR_NAME:-$_reponame-$pkgver}"
  DESTDIR="${pkgdir}" ninja install

  # add tests to the package for possibly executing them later under Android
  if [[ -f ${_pkgname}_tests ]]; then
    mkdir "${pkgdir}/$_prefix/bin"
    cp ${_pkgname}_tests "${pkgdir}/$_prefix/bin"
  fi
}
