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

_realname=tiled
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.10.2
pkgrel=1
pkgdesc='A general purpose tile map editor, built to be flexible and easy to use (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url='https://www.mapeditor.org'
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-qt6-base"
         "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
         "${MINGW_PACKAGE_PREFIX}-qt6-svg"
         "${MINGW_PACKAGE_PREFIX}-gtk-update-icon-cache"
         "${MINGW_PACKAGE_PREFIX}-shared-mime-info"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-qbs"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools"
             "${MINGW_PACKAGE_PREFIX}-python")
optdepends=("${MINGW_PACKAGE_PREFIX}-python: Python plugin"
            "${MINGW_PACKAGE_PREFIX}-qt6-imageformats: Support for additional image formats (including WebP)"
            "${MINGW_PACKAGE_PREFIX}-qt6-translations")
source=("$_realname-$pkgver.tar.gz::https://github.com/mapeditor/tiled/archive/v${pkgver}.tar.gz")
sha256sums=('0768a83a0ed72efb57aa77fe6884305f0e551d04a255b62590212cc3c8bbd43b')

build() {
  rm -rf build-${MSYSTEM} | true
  cp -r "${srcdir}/${_realname}-${pkgver}" "${srcdir}/build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"

  if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]]; then
    _profile=clang
  else
    _profile=mingw
  fi

  if check_option "debug" "y"; then
    _build_type=debug
  else
    _build_type=release
  fi

  export PYTHONHOME="${MINGW_PREFIX}"/bin
  export MSYS2_ARG_CONV_EXCL="qbs.installPrefix:"

  "${MINGW_PREFIX}"/bin/qbs.exe setup-toolchains \
    --type $_profile \
    "${MINGW_PREFIX}"/bin/${CXX}.exe $_profile

  "${MINGW_PREFIX}"/bin/qbs.exe config profiles.${_profile}.moduleProviders.Qt.qmakeFilePaths ${MINGW_PREFIX}/bin/qmake6.exe

  "${MINGW_PREFIX}"/bin/qbs.exe resolve \
    profile:$_profile \
    qbs.installPrefix:"${MINGW_PREFIX}" \
    qbs.buildVariant:${_build_type} \
    modules.Qt.core.lreleaseName:lrelease-qt6 \
    projects.Tiled.windowsLayout:false \
    projects.Tiled.installHeaders:true

  "${MINGW_PREFIX}"/bin/qbs.exe build
}

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

  "${MINGW_PREFIX}"/bin/qbs.exe install --install-root "${pkgdir}"

  for _file in COPYING LICENSE.{APACHE,BSD,GPL}
  do
    install -Dm644 "${_file}" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/${_file}"
  done
}
