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

_realname=widelands
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.3
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"
        '0001-wideland-1.2.1-relative-datadir.patch')

sha256sums=('8468b6bc0ddb70749c09c5603109ceeb79b95f3602d3aa55ecfad84f8ea82571'
            'a9a2e909449d3f808f4d3df53d781806bc47f868937810787faf688e3532367f')

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

  # 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"
}
