# Maintainer: Martchus <martchus@gmx.net>
<%== content_for 'additional_contributors' %>\

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

<%== content_for 'comment_header' %>\
_android_arch=<%= $variant_prefix_part =~ s/android-// && $variant_prefix_part %>
pkgname=android-$_android_arch-qt6-<%== $qt_module %><%== $static_suffix %>
<%== content %>\
options=('!strip' '!buildflags' 'staticlibs' '!emptydirs')
groups=(android-${_android_arch}-qt6)
_pkgfqn="qt<%== $qt_module %>-everywhere-src-${_qtver}"
<%== include 'fragments/qt6-source' %>\
<%== include 'fragments/source_file_list', relevant_files => $patch_files %>)
sha256sums=('<%== stash('git_commit') ? 'SKIP' : $qt_module_sha256 %>'\
<%== include 'fragments/sha256_list', relevant_files => $patch_files %>)

% if ($patch_files->size || content_for('prepare')->size) {
prepare () {
  cd $_pkgfqn

% if ($patch_files->size) {
  # apply patches; further descriptions can be found in patch files itself
  for patch in "$srcdir/"*.patch; do
    msg2 "Applying patch $patch"
    patch -p1 -i "$patch"
  done
% }
<%== content_for 'prepare' %>\
}

% }
build() {
  source android-env ${_android_arch}
<%== content_for 'before_build_config' %>\
  android-${_android_arch}-cmake<%== $static_suffix %> -G Ninja -B build-$_android_arch -S $_pkgfqn \\
% if ($static_variant) {
    -DCMAKE_FIND_ROOT_PATH="${ANDROID_PREFIX}/static;${ANDROID_PREFIX}" \\
    -DCMAKE_INSTALL_PREFIX:PATH="${ANDROID_PREFIX}/static" \\
% }
% else {
    -DCMAKE_FIND_ROOT_PATH="${ANDROID_PREFIX}" \\
% }
    -DQT_BINARY_DIR=${ANDROID_PREFIX_BIN} \\
    -DQT_INCLUDE_DIRS_NO_SYSTEM=ON \\
    -DQT_HOST_PATH=/usr \\
    -DANDROID_SDK_ROOT=${ANDROID_HOME} \\
    -DANDROID_NDK_ROOT=${ANDROID_NDK_HOME} \\
    -DANDROID_STL="c++_shared" \\
    -DFEATURE_pkg_config=ON<%== content_for 'additional_cmake_args' %>
% if(0) {
    -DFEATURE_pkg_config=ON \\
    -DOPENSSL_CRYPTO_LIBRARY:FILEPATH=${ANDROID_PREFIX_LIB}/libcrypto.so \\
    -DOPENSSL_SSL_LIBRARY:PATH=${ANDROID_PREFIX_LIB}/libssl.so \\
    -DOPENSSL_INCLUDE_DIR:FILEPATH=${ANDROID_PREFIX_INCLUDE} \\
    -DJPEG_LIBRARY_RELEASE:PATH=${ANDROID_PREFIX_LIB}/libjpeg.so \\
    -DJPEG_INCLUDE_DIR:FILEPATH=${ANDROID_PREFIX_INCLUDE} \\
    -DPNG_LIBRARY_RELEASE:PATH=${ANDROID_PREFIX_LIB}/libpng.so \\
    -DPNG_PNG_INCLUDE_DIR:FILEPATH=${ANDROID_PREFIX_INCLUDE}<%== content_for 'additional_cmake_args' %>
% }
  cmake --build build-$_android_arch
}

package() {
<%== content_for 'before_package_install' %>\
  DESTDIR="$pkgdir" cmake --install build-$_android_arch<%== content_for 'after_package_install' %>

% unless ($static_variant) {
% unless (stash('no_libs')) {
  # Drop QMAKE_PRL_BUILD_DIR because reference the build dir
  find "$pkgdir/${ANDROID_PREFIX}" -type f -name '*.prl' \\
    -exec sed -i -e '/^QMAKE_PRL_BUILD_DIR/d' {} \;

% }
% }
  find ${pkgdir}/${ANDROID_PREFIX} -type f -name 'lib*.so' -exec ${ANDROID_STRIP} -g --strip-unneeded {} \;
  find ${pkgdir}/${ANDROID_PREFIX} -type f -name 'lib*.a' -exec ${ANDROID_STRIP} -g {} \;
  [[ -d "${pkgdir}/${ANDROID_PREFIX}/share/doc" ]] && rm -r "${pkgdir}/${ANDROID_PREFIX}/share/doc" || true

  install -d "$pkgdir"/usr/share/licenses
  ln -s /usr/share/licenses/android-$_android_arch-qt6-base<%== $static_variant ? '-static' : '' %> "$pkgdir"/usr/share/licenses/$pkgname

}
