# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=installer
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=0.7.0
pkgrel=5
pkgdesc="A low-level library for installing from a Python wheel distribution (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_references=(
  'purl: pkg:pypi/installer'
)
url='https://github.com/pradyunsg/installer'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-python-flit-core"
  "${MINGW_PACKAGE_PREFIX}-cc"
  "git"
)
_launcher_commit="8c455825fc1308d34a1a47238a9eb6d56f732142"
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        "simple_launcher"::"git+https://bitbucket.org/vinay.sajip/simple_launcher.git#commit=${_launcher_commit}"
        "001-launcher-relative.patch"
        "002-launcher-stderr-missing.patch"
        "003-launcher-secure-api-shim.patch"
        "004-zero-reservered-fields-in-startupinfo.patch")
sha256sums=('a26d3e3116289bb08216e0d0f7d925fcef0b0194eedfa0c944bcaaa106c4b631'
            '0dc8e2a1e21943a2c81ef4fd22c058a561626b2ba0efa38784df1f4a28a71e8b'
            'a03dc0cb9272a233831ab41ed55d9a64196ab10a750d0517c67745cc5214eb79'
            'e95b68c14bffddd6dd859bdb2c8feb733bbcd5c9d4a37105df4196bfa01923c5'
            '0b6ab782f1550ea0e52a8869cf9c78c405a9ed9dda1e9d9eb60a4126787db208'
            'ccb341afa03809257d0c9c6c5d323b9998d21ff972789eb8c1734d5bc2a7fbff')

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

  # To keep the generated launchers and scipts relocatable:
  # * For python launchers we use "<launcher_dir>\<interpreter>.exe" which simple_launcher understands
  # * For old-style scripts we use '/usr/bin/env <interpreter>' which at least cygwin understands
  patch -Np1 -i "${srcdir}/001-launcher-relative.patch"

  cd "${srcdir}/simple_launcher"

  # https://github.com/pypa/pip/issues/10444#issuecomment-1031092176
  patch -Np1 -i "${srcdir}/002-launcher-stderr-missing.patch"

  patch -Np1 -i "${srcdir}/003-launcher-secure-api-shim.patch"

  patch -Np1 -i "${srcdir}/004-zero-reservered-fields-in-startupinfo.patch"
}

build() {
  cd "${srcdir}/simple_launcher"
  windres --input launcher.rc --output resources.res --output-format=coff

  local _gui_cflags="$CFLAGS $CPPFLAGS -mwindows"
  local _cli_cflags="$CFLAGS $CPPFLAGS -D_CONSOLE"
  local _gui_lflags="$LDFLAGS -lshlwapi"
  local _cli_lflags="$LDFLAGS -lshlwapi"

  case "${MINGW_CHOST}" in
  i686-w64-mingw32)
    cc $_cli_cflags -o t32.exe launcher.c resources.res $_cli_lflags
    cc $_gui_cflags -o w32.exe launcher.c resources.res $_gui_lflags
    ;;
  x86_64-w64-mingw32)
    cc $_cli_cflags -o t64.exe launcher.c resources.res $_cli_lflags
    cc $_gui_cflags -o w64.exe launcher.c resources.res $_gui_lflags
    ;;
  aarch64-w64-mingw32)
    cc $_cli_cflags -o t64-arm.exe launcher.c resources.res $_cli_lflags
    cc $_gui_cflags -o w64-arm.exe launcher.c resources.res $_gui_lflags
    ;;
  *)
    echo "Unsupported CHOST ${MINGW_CHOST}" && false
    ;;
  esac

  cd "${srcdir}/${_realname}-${pkgver}"

  rm src/installer/_scripts/*.exe
  cp "${srcdir}/simple_launcher/"*.exe src/installer/_scripts/
  # git add src/installer/_scripts/

  python -m flit_core.wheel
}

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

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