# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=meld
pkgbase=mingw-w64-${_realname}3
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}3"
pkgver=3.23.0
pkgrel=3
pkgdesc="Visual diff and merge tool (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://meldmerge.org/"
msys2_repository_url='https://gitlab.gnome.org/GNOME/meld'
msys2_references=(
  'archlinux: meld'
)
license=("spdx:GPL-2.0-or-later")
depends=("${MINGW_PACKAGE_PREFIX}-gtk3"
         "${MINGW_PACKAGE_PREFIX}-gtksourceview4"
         "${MINGW_PACKAGE_PREFIX}-adwaita-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-gtk-update-icon-cache"
         "${MINGW_PACKAGE_PREFIX}-gsettings-desktop-schemas"
         "${MINGW_PACKAGE_PREFIX}-python-cairo"
         "${MINGW_PACKAGE_PREFIX}-python-gobject")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-rcedit"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-itstool"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=(https://download.gnome.org/sources/${_realname}/${pkgver%.*}/${_realname}-${pkgver}.tar.xz
        '001-relocate.patch'
        '002-no-update_desktop_database.patch'
        'pyscript2exe.py')
sha256sums=('983c2a4240e025a2109c7738198710e9d6b063c910b048332d14690cf538c2a6'
            'e7445dd7d7280dfba815909e6e52de3eb549653f5a02c2744a15707c68428332'
            'f2087bd98576626d0293adb4c0d4d3c1adbf5bd0d7ab1bf83836592984d75200'
            '73a804caedf21542a898280fdd0ad6cb290be3df478726fce6693c3a67815247')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 --binary -i "${srcdir}/${_patch}"
  done
}

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

  apply_patch_with_msg \
    "001-relocate.patch" \
    "002-no-update_desktop_database.patch"
}

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

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/meson.exe setup \
      --prefix="${MINGW_PREFIX}" \
      --wrap-mode=nodownload \
      --auto-features=enabled \
      --buildtype=plain \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/meson.exe compile
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson.exe install

  python3 "${srcdir}/pyscript2exe.py" "${pkgdir}${MINGW_PREFIX}/bin/meld"

  ${MINGW_PREFIX}/bin/rcedit.exe "${pkgdir}${MINGW_PREFIX}/bin/meldw.exe" \
    --set-icon "../${_realname}-${pkgver}/data/icons/org.gnome.meld.ico"

  MSYS2_ARG_CONV_EXCL="-d" \
    python -m compileall -q -d"${MINGW_PREFIX}/lib" "${pkgdir}${MINGW_PREFIX}/lib"
}
