# Contributor: Fabien Chouteau <chouteau@adacore.com>

_realname=langkit
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=24.0.0
pkgrel=3
pkgdesc="A Python framework to generate language parsers (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url="https://github.com/AdaCore/langkit"
msys2_references=(
  'aur: langkit'
  'gentoo: dev-ada/langkit'
)
license=('spdx:Apache-2.0 WITH LLVM-exception')
depends=("${MINGW_PACKAGE_PREFIX}-python-docutils"
         "${MINGW_PACKAGE_PREFIX}-python-e3-core"
         "${MINGW_PACKAGE_PREFIX}-python-funcy"
         "${MINGW_PACKAGE_PREFIX}-python-mako"
         "${MINGW_PACKAGE_PREFIX}-python-yaml")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc-ada"
             "${MINGW_PACKAGE_PREFIX}-gprbuild"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
options=("!strip")
source=("https://github.com/AdaCore/langkit/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-Transition-create-project.py-to-setuptools-entrypoint.patch::https://github.com/AdaCore/langkit/commit/059030ba0.patch"
        "0002-Add-langkit.scripts-package-to-archive.patch::https://patch-diff.githubusercontent.com/raw/AdaCore/langkit/pull/651.patch"
        "0003-Fix-file-extension-for-create-project-executable.patch::https://patch-diff.githubusercontent.com/raw/AdaCore/langkit/pull/652.patch")
sha256sums=('0ceb35394717bbebdf71a22f26d1f40c36ad4861a4b137d22d8964a89657752d'
            '2c64565f3dfcb6f97dc5d614c5e65b938d2957df1cfc4c6285f6daf96e1db2d8'
            '8909b188fce2e8465aa55f4b6f2f936b3caf9ba033a50aa9a68df44d30991ecf'
            'd846382ba8dd2a7e89223a8ec0b372c576c4b444086fab7817ca34213cf99d1a')

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

  # Setuptools was generating a wrong shebang line for the 'create-project.py'
  # wrapper script, which prevented the script from running.
  # Fixed in master branch, will appear in next release after 24.0.0:
  # https://github.com/AdaCore/langkit/commit/059030ba0ebf47cebb5b18f39a7b2812e18c3fc2
  patch -p1 -i "${srcdir}/0001-Transition-create-project.py-to-setuptools-entrypoint.patch"

  # Fix crash of create-project.py: "ModuleNotFoundError: No module named 'langkit.scripts'"
  # https://github.com/AdaCore/langkit/pull/651
  patch -p1 -i "${srcdir}/0002-Add-langkit.scripts-package-to-archive.patch"

  # Rename 'create-project.py.exe' to 'create-project.exe'
  # https://github.com/AdaCore/langkit/pull/652
  patch -p1 -i "${srcdir}/0003-Fix-file-extension-for-create-project-executable.patch"
}

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

  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

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

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

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