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

_realname=pyinstaller
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=6.16.0
pkgrel=1
pkgdesc='Bundles a Python application and all its dependencies into a single package (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://pyinstaller.org/'
msys2_repository_url='https://github.com/pyinstaller/pyinstaller'
msys2_references=(
  'purl: pkg:pypi/pyinstaller'
)
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-binutils"
         "${MINGW_PACKAGE_PREFIX}-pyinstaller-hooks-contrib"
         "${MINGW_PACKAGE_PREFIX}-python-altgraph"
         "${MINGW_PACKAGE_PREFIX}-python-packaging"
         "${MINGW_PACKAGE_PREFIX}-python-pefile"
         "${MINGW_PACKAGE_PREFIX}-python-pywin32-ctypes"
         "${MINGW_PACKAGE_PREFIX}-python-setuptools")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-hatchling"
             "${MINGW_PACKAGE_PREFIX}-python-installer")
options=('!strip')
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz")
noextract=("${_realname}-${pkgver}.tar.gz")
sha256sums=('53559fe1e041a234f2b4dcc3288ea8bdd57f7cad8a6644e422c27bb407f3edef')

if [[ ${CARCH} == aarch64 ]]; then
  source+=("https://files.pythonhosted.org/packages/py3/${_realname::1}/${_realname}/${_realname}-${pkgver}-py3-none-win_arm64.whl")
  sha256sums+=('d0af8a401de792c233c32c44b16d065ca9ab8262ee0c906835c12bdebc992a64')
fi

prepare() {
  export MSYS=winsymlinks:lnk
  tar zxf "${_realname}-${pkgver}.tar.gz"

  cd "${_realname}-${pkgver}"/PyInstaller/bootloader

  # Remove unused bootloaders
  if [[ ${CARCH} == aarch64 ]]; then
    # Add ARM64 bootloader
    cp -r ${srcdir}/Pyinstaller/bootloader/Windows-64bit-arm .

    for bl in $(ls | grep -v Windows-64bit-arm | grep -v images); do rm -rf $bl; done
  else
    for bl in $(ls | grep -v Windows-64bit | grep -v images); do rm -rf $bl; done
  fi
}

build() {
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"

  python -m build --wheel --skip-dependency-check --no-isolation
}

package() {
  cd "python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 COPYING.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.txt"
}
