# Contributor (MSYS2): David Macek <david.macek.0@gmail.com>
# Maintainer (Arch Linux): Alexander Rødseth <rodseth@gmail.com>
# Contributor (Arch Linux): Angel Velasquez <angvp@archlinux.org>
# Contributor (Arch Linux): Ionut Biru  <ibiru@archlinux.ro>
# Contributor (Arch Linux): William Rea <sillywilly@gmail.com>
# Contributor (Arch Linux): Allan McRae <mcrae_allan@hotmail.com>

_realname=geany
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_realpkgver=2.1.0
pkgver=${_realpkgver%.*}
pkgrel=2
pkgdesc='Fast and lightweight IDE (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.geany.org/'
msys2_repository_url="https://github.com/geany/geany"
msys2_references=(
  'archlinux: geany'
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gtk3"
         "${MINGW_PACKAGE_PREFIX}-adwaita-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-gtk-update-icon-cache"
         "${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-python-docutils"
             'perlxml'
             'intltool')
optdepends=("${MINGW_PACKAGE_PREFIX}-geany-plugins: various extra features")
source=("https://github.com/geany/geany/archive/${_realpkgver}/${_realname}-${_realpkgver}.tar.gz"
        #https://download.geany.org/${_realname}-${pkgver}.tar.bz2{,.sig}
        "0002-Use-FHS.patch"
        "0003-export.patch")
validpgpkeys=('ACA0246889FB96B63382111724CCD8550E5D1CAE'
              '457990C86F91F33DE05EBF1BCC03633F700990F2')
sha256sums=('95a9bfa1c176684fb6ccd528ba9c948f0c2d777d2370bb0e9c3502ea575ceb3d'
            '2022839da5d1bae68e9201be2e1980fc5c2fb0d021c2a09105f5c617aa190cdd'
            '208d738f58858bbaba05e2c931cfe26034d2a848a21eb301ae11121cbbe2bde1')

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

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

  apply_patch_with_msg \
    0002-Use-FHS.patch

  # https://github.com/geany/geany/pull/3067
  apply_patch_with_msg \
    0003-export.patch
}

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

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  meson setup \
    --prefix="${MINGW_PREFIX}" \
    --wrap-mode=nodownload \
    --auto-features=enabled \
    --buildtype=plain \
    -Dhtml-docs=disabled \
    -Dpdf-docs=disabled \
    -Dapi-docs=disabled \
    -Dmac-integration=disabled \
    -Dvte=false \
    -Dgtkdoc=false \
    -Dplugins=true \
    ../${_realname}-${_realpkgver}

  meson compile
}

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

  DESTDIR="${pkgdir}" meson install

  install -Dm644 "${srcdir}"/${_realname}-${_realpkgver}/src/geany-run-helper.bat \
    "${pkgdir}"${MINGW_PREFIX}/libexec/${_realname}/geany-run-helper.bat
  install -Dm644 "${srcdir}"/${_realname}-${_realpkgver}/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
