# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=hexchat
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.16.2
pkgrel=4
pkgdesc='A popular and easy to use graphical IRC (chat) client (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://hexchat.github.io/'
msys2_repository_url="https://github.com/hexchat/hexchat"
msys2_references=(
  "cpe: cpe:/a:hexchat_project:hexchat"
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-dbus-glib"
         "${MINGW_PACKAGE_PREFIX}-desktop-file-utils"
         "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-gtk2"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-pango")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools"
             "${MINGW_PACKAGE_PREFIX}-iso-codes"
             "${MINGW_PACKAGE_PREFIX}-luajit"
             #"${MINGW_PACKAGE_PREFIX}-perl"
             "${MINGW_PACKAGE_PREFIX}-python-cffi")
optdepends=("${MINGW_PACKAGE_PREFIX}-enchant: Spell check"
            "${MINGW_PACKAGE_PREFIX}-iso-codes: Display language names instead of codes"
            "${MINGW_PACKAGE_PREFIX}-luajit: Lua plugin"
            #"${MINGW_PACKAGE_PREFIX}-perl: Perl plugin"
            "${MINGW_PACKAGE_PREFIX}-python-cffi: Python plugin")
source=("https://github.com/hexchat/hexchat/releases/download/v${pkgver}/hexchat-${pkgver}.tar.xz"
        0001-meson-add-winpthread.patch
        0002-fix-plugin-search.patch)
sha256sums=('2e88340a8da274b87373ec0740746da78120cc6fbfdd201a4dd6999cac790e4a'
            '39dc5fe131bcd8575e0d3fd274fa4bf3b59e1d9873ad629ff176028619edf268'
            '02d87882b514628acdb05ffe21a716eb74a97dc28eae517f88a1db3c62f0c19f')

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/0001-meson-add-winpthread.patch"
  patch -p1 -i "${srcdir}/0002-fix-plugin-search.patch"
}

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

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("--buildtype=release")
  else
    _extra_config+=("--buildtype=debug")
  fi

  #
  # Options are from upstream/.github/workflows/msys-build.yml
  #
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe \
    --prefix="${MINGW_PREFIX}" \
    "${_extra_config[@]}" \
    --wrap-mode=nodownload \
    -Dtext-frontend=true \
    -Dwith-upd=false \
    -Dwith-perl=false \
    "../${_realname}-${pkgver}"

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

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/meson.exe test || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson.exe install

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