# Maintainer: Alberto Fanjul Alonso <albfan@gnome.org>

_realname=libgit2-glib
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=1.2.0
pkgrel=6
pkgdesc="GLib wrapper for libgit2 (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://wiki.gnome.org/Projects/Libgit2-glib"
license=('spdx:LGPL-2.1-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-libgit2"
         "${MINGW_PACKAGE_PREFIX}-libssh2"
         "${MINGW_PACKAGE_PREFIX}-glib2")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-vala"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc"
             "${MINGW_PACKAGE_PREFIX}-python-gobject"
             "${MINGW_PACKAGE_PREFIX}-gi-docgen")
source=("https://download.gnome.org/sources/${_realname}/${pkgver%.*}/${_realname}-${pkgver}.tar.xz"
        "0001-Unbreak-micro-version.patch"
        "0002-build-Fix-build-against-and-require-libgit2-1.8.0.patch"
        "0003-Adjust-API-for-libgit2-1.9.0.patch")
sha256sums=('1331dada838f4e1f591b26459d44126a325de762dc3cd26153a31afbdfe18190'
            'e0a83aba5b41cc5ff22e96c041a63b77ef4f1fac887eaf920722ed65ee51921b'
            '699f40712af3447fdd710c77833dddea3fec61515b7c3b2c48b0fcab00aad552'
            '966988388982978b468fff29433873b7c93ab640da4ac775366e019e0546491d')
msys2_repository_url="https://gitlab.gnome.org/GNOME/libgit2-glib"

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

  # fix a micro issue
  patch -Np1 -i ../0001-Unbreak-micro-version.patch

  # use libgit2 1.8
  # https://gitlab.gnome.org/GNOME/libgit2-glib/-/merge_requests/40
  patch -Np1 -i ../0002-build-Fix-build-against-and-require-libgit2-1.8.0.patch

  # use libgit2 1.9
  patch -Np1 -i ../0003-Adjust-API-for-libgit2-1.9.0.patch
}

build() {
  mkdir -p "build-${MSYSTEM}" && cd "build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    meson setup \
    --prefix="${MINGW_PREFIX}" \
    --buildtype plain \
    -Dvapi=true \
    -Dtranslate_windows_paths=true \
    -Dgtk_doc=true \
    ../${_realname}-${pkgver}

  meson compile
}

package_libgit2-glib() {
  meson install -C "build-${MSYSTEM}" --destdir "${pkgdir}"

  # overwrite symlinks with actual file
  rm "${pkgdir}${MINGW_PREFIX}"/share/vala/vapi/{ggit-1.0.deps,ggit-1.0.vapi}
  cp "${pkgdir}${MINGW_PREFIX}"/share/vala/vapi/{libgit2-glib-1.0.deps,ggit-1.0.deps}
  cp "${pkgdir}${MINGW_PREFIX}"/share/vala/vapi/{libgit2-glib-1.0.vapi,ggit-1.0.vapi}

  mkdir -p dest${MINGW_PREFIX}/share
  mv "${pkgdir}${MINGW_PREFIX}"/share/gtk-doc dest${MINGW_PREFIX}/share/gtk-doc

  install -Dm644 "${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}

package_libgit2-glib-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mv dest/* "${pkgdir}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
