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

_realname=leocad
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=25.09
_libver=25.08
pkgrel=1
pkgdesc='A CAD program for creating virtual LEGO models (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://leocad.org'
msys2_repository_url='https://github.com/leozide/leocad'
msys2_references=(
  "cpe: cpe:/a:leocad:leocad"
)
license=('spdx:GPL-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-qt6-base"
         "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-zlib")
optdepends=("${MINGW_PACKAGE_PREFIX}-qt6-tools: Designer plugin")
source=("${msys2_repository_url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "${msys2_repository_url}/releases/download/v${pkgver}/Library-${_libver}.zip"
        '0001-leocad-23.03-fix-install.patch'
        '0002-leocad-25.09-fix-build.patch')
sha256sums=('db9e129ac35fde3c184510a23fd57c61d1bc5d19d3eac2a4a23f6b73b9f87bd5'
            '848404645811b00128eb20203b3706a6f2fb8ee68a214debc820f4384dab76ed'
            '850e7fb4f1d43f6d865284e7338628c5d29aa20d03b99f71c2fb4ac356ad3fba'
            '34a2ed3422038172529daac95caaaf8fead67738d006fb61dfa375b454adbf4b')

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

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

  apply_patch_with_msg \
    0001-leocad-23.03-fix-install.patch \
    0002-leocad-25.09-fix-build.patch
}

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

  MSYS2_ARG_CONV_EXCL="INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/qmake6.exe \
    QMAKE_LRELEASE=${MINGW_PREFIX}/bin/lrelease-qt6.exe \
    INSTALL_PREFIX=${MINGW_PREFIX} \
    DISABLE_UPDATE_CHECK=1 \
    ../${_realname}-${pkgver}/${_realname}.pro

  make
}

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

  make INSTALL_ROOT="${pkgdir}${MINGW_PREFIX}" install

  install -Dm644 "${srcdir}/library.bin" -t "${pkgdir}${MINGW_PREFIX}/share/leocad"
}

# vim:set ts=2 sw=2 et:
