# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=widelands
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.2.1
pkgrel=1
pkgdesc="A realtime strategy game with emphasis on economy and transport (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://widelands.org/'
msys2_repository_url='https://github.com/widelands/widelands'
msys2_references=(
  'archlinux: widelands'
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-SDL2_mixer"
         "${MINGW_PACKAGE_PREFIX}-SDL2_image"
         "${MINGW_PACKAGE_PREFIX}-SDL2_ttf"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-lua"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-dbus"
         "${MINGW_PACKAGE_PREFIX}-minizip")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-asio")
source=("${msys2_repository_url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "${msys2_repository_url}/commit/c0b44ccc04df35a9a23ca9be3e05f5d3a5428f6f.patch"
        "${msys2_repository_url}/commit/a66872f35abe45a2eba6c112dd978bd9e2c08d7b.patch"
        "${msys2_repository_url}/commit/a20d8d240e68c5ae704bbb754472130ae72ebfa9.patch"
        "${msys2_repository_url}/commit/937a1695cf0494bba559b489497ca99afdf95983.patch"
        '0001-wideland-1.2.1-relative-datadir.patch')

sha256sums=('799bfd32048ef20118c48e21f3fc843ae0451c42bb8bf2eabcb9b26bf6fe54b4'
            'c560c9125fae38fbe89556f82e4df9e40b250060dc5c6b64c0535bb2158e18ea'
            '0fb1249b4f3dedcb6efe2fe9210976aac0ba1d15d617eb61e1a5b7de6e679b79'
            'b3bbb877a87c9fbb68be62def4c37a1981fed44fddbedb61e950a06242e880a3'
            'efde065633f72780a08b6e8a60f26630532079c76eb0a1866fd3e5faf1ac3be1'
            'd01990e76cf884a464f491d7c79bd60443e30a752a91a9e20fa12e69f478e73f')

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

  # Backport of https://github.com/widelands/widelands/pull/6665 (fixes build with asio 1.34)
  patch -Np1 -i "${srcdir}/c0b44ccc04df35a9a23ca9be3e05f5d3a5428f6f.patch"
  # Backport of https://github.com/widelands/widelands/pull/6522 (fixes build with GCC 15)
  patch -Np1 -i "${srcdir}/a66872f35abe45a2eba6c112dd978bd9e2c08d7b.patch"
  patch -Np1 -i "${srcdir}/a20d8d240e68c5ae704bbb754472130ae72ebfa9.patch"
  # Backport of https://github.com/widelands/widelands/pull/6698 (fixes build with GCC 15)
  patch -Np1 -i "${srcdir}/937a1695cf0494bba559b489497ca99afdf95983.patch"

  # Search datadir relative to ${MINGW_PREFIX}/bin/..
  patch -Np1 -i "${srcdir}/0001-wideland-1.2.1-relative-datadir.patch"
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_{SHARED,STATIC}_LIBS=ON \
      -DWL_INSTALL_BINDIR=bin \
      -DWL_INSTALL_DATADIR=share/widelands \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

check() {
  cmake --build "build-${MSYSTEM}" --target test
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  # Fix wrong locations of some installed files (upstream bug)
  # See also FS#72240
  rm "${pkgdir}${MINGW_PREFIX}"/COPYING
  mv "${pkgdir}${MINGW_PREFIX}"/{VERSION,ChangeLog,CREDITS} "${pkgdir}${MINGW_PREFIX}"/share/widelands

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