# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Saul Ibarra Corretge <saghul@gmail.com>
# Contributor: Frode Solheim <frode@fs-uae.net>

_primary_python="yes"
_pybasever=3.11
_realname=python

if [[ "${_primary_python}" == "yes" ]]; then
  pkgbase="mingw-w64-${_realname}"
  pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  provides=("${MINGW_PACKAGE_PREFIX}-python3"
            "${MINGW_PACKAGE_PREFIX}-python${_pybasever}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python${_pybasever}"
            "${MINGW_PACKAGE_PREFIX}-python2<2.7.16-7")
  replaces=("${MINGW_PACKAGE_PREFIX}-python3"
            "${MINGW_PACKAGE_PREFIX}-python${_pybasever}")
else
  pkgbase="mingw-w64-${_realname}${_pybasever}"
  pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}${_pybasever}")
fi
pkgver=${_pybasever}.6
pkgrel=2
pkgdesc="A high-level scripting language (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=('spdx:PSF-2.0')
url="https://www.python.org/"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-libffi"
         "${MINGW_PACKAGE_PREFIX}-mpdecimal"
         "${MINGW_PACKAGE_PREFIX}-ncurses"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-tcl"
         "${MINGW_PACKAGE_PREFIX}-tk"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-tzdata")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-autotools"
  "autoconf-archive"
)
#options=('debug' '!strip')
source=("https://www.python.org/ftp/python/${pkgver%rc?}/Python-${pkgver}.tar.xz"
        0001-sysconfig-make-_sysconfigdata.py-relocatable.patch
        0002-build-add-with-nt-threads-and-make-it-default-on-min.patch
        0003-Define-MS_WINDOWS-and-others-when-compiling-with-MIN.patch
        0004-configure-add-MACHDEP-and-platform-on-MINGW.patch
        0005-Add-default-configuration-for-MINGW.patch
        0006-configure-enable-largefile-support-by-default-for-Mi.patch
        0007-Add-PC-to-CPPFLAGS-and-to-SRCDIRS-on-Mingw.patch
        0008-posixmodule.c-Define-necessary-headers-for-compiling.patch
        0009-Build-winreg-by-default-when-compiling-for-MINGW.patch
        0010-pyport.h-Add-support-for-MINGW.patch
        0011-configure-add-options-so-that-shared-build-is-possib.patch
        0012-Add-dynamic-loading-support-for-MINGW.patch
        0013-Ignore-main-program-for-frozen-scripts-on-MINGW.patch
        0014-Add-missing-library-for-multiprocessing-module.patch
        0015-selectmodule-add-support-for-MINGW.patch
        0016-setup.py-add-libraries-to-fix-compile-of-ctypes-on-M.patch
        0017-Detect-winsock2-and-setup-_socket-module-on-MINGW.patch
        0018-Enable-some-windows-specific-modules.patch
        0019-sysconfig-MINGW-build-extensions-with-GCC.patch
        0020-sysconfig-treat-MINGW-builds-as-POSIX-builds.patch
        0021-Add-support-for-stdcall-without-underscore.patch
        0022-Use-replace-instead-rename-to-avoid-failure-on-windo.patch
        0023-Customize-site-for-MINGW.patch
        0024-add-python-config-sh.patch
        0025-setup.py-add-export_symbols-to-fix-compile-of-ctypes.patch
        0026-mingw-prefer-unix-sep-if-MSYSTEM-environment-variabl.patch
        0027-msys-cygwin-semi-native-build-sysconfig.patch
        0028-sysconfig-mingw-sysconfig-like-posix.patch
        0029-cross-dont-add-multiarch-paths-if-cross-compiling.patch
        0030-mingw-use-backslashes-in-compileall-py.patch
        0031-msys-convert_path-fix-and-root-hack.patch
        0032-mingw-pdcurses_ISPAD.patch
        0033-msys-monkeypatch-os-system-via-sh-exe.patch
        0034-msys-replace-slashes-used-in-io-redirection.patch
        0035-remove_path_max.default.patch
        0036-dont-link-with-gettext.patch
        0037-ctypes-python-dll.patch
        0038-gdbm-module-includes.patch
        0039-use-gnu_printf-in-format.patch
        0040-mingw-fix-ssl-dont-use-enum_certificates.patch
        0041-fix-using-dllhandle-and-winver-mingw.patch
        0042-Add-AMD64-to-sys-config-so-msvccompiler-get_build_ve.patch
        0043-MINGW-link-with-additional-library.patch
        0044-install-msilib.patch
        0045-fix-signal-module-build.patch
        0046-build-build-winconsoleio-and-_testconsole.patch
        0047-expose-sem_unlink.patch
        0048-cygpty-isatty.patch
        0049-disable-broken-gdbm-module.patch
        0050-build-link-win-resource-files-and-build-pythonw.patch
        0051-disable-readline.patch
        0052-fix-isselectable.patch
        0053-_xxsubinterpretersmodule.patch
        0054-configure.ac-fix-inet_pton-check.patch
        0055-set-venv-activate-path-unix.patch
        0056-pass-gen-profile-ldflags.patch
        0057-pkg-config-windows-must-link-ext-with-python-lib.patch
        0058-importlib-bootstrap-path-sep.patch
        0059-pathlib-path-sep.patch
        0060-warnings-fixes.patch
        0061-fix-build-testinternalcapi.patch
        0062-extend-MS_WINDOWS-flag.patch
        0063-clang-arm64.patch
        0064-configure.ac-set-MINGW-stack-reserve.patch
        0065-Don-t-use-os.pathsep-to-find-EOF.patch
        0066-Fix-extension-suffix-for-c-extensions-on-mingw.patch
        0067-Change-the-get_platform-method-in-sysconfig.patch
        0068-distutils-compiler-customize-mingw-cygwin-compilers.patch
        0069-distutils-compiler-enable-new-dtags.patch
        0070-distutils-MINGW-build-extensions-with-GCC.patch
        0071-distutils-use-Mingw32CCompiler-as-default-compiler-f.patch
        0072-distutils-find-import-library.patch
        0073-distutils-avoid-circular-dependency-from-time-module.patch
        0074-distutils-generalization-of-posix-build-in-distutils.patch
        0075-distutils-mingw-sysconfig-like-posix.patch
        0076-distutils-get_versions-fixes.patch
        0077-distutils-install-layout-as-posix.patch
        0078-distutils-msys-convert_path-fix-and-root-hack.patch
        0079-distutils-mingw-build-optimized-ext.patch
        0080-distutils-cygwinccompiler-dont-strip-modules-if-pyde.patch
        0081-distutils-get-compilers-from-env-vars.patch
        0082-distutils-add-windmc-to-cygwinccompiler.patch
        0083-distutils-fix-msvc9-import.patch
        0084-distutils-mingw-add-LIBPL-to-library-dirs.patch
        0085-distutils-Change-the-get_platform-method-in-distutil.patch
        0086-build-Fix-ncursesw-include-lookup.patch
        0087-tests-fix-test_bytes.patch
        0088-time-fix-strftime-not-raising-for-invalid-year-value.patch
        0089-ctypes-find_library-c-should-return-None-with-ucrt.patch
        0090-build-Disable-checks-for-dlopen-dlfcn.patch
        0091-Fix-install-location-of-the-import-library.patch
        0092-Set-MSYS2_ARG_CONV_EXCL-for-the-shared-Python-module.patch
        0093-build-Integrate-venvlauncher-build-installation-into.patch
        0094-configure.ac-set-_WIN32_WINNT-version.patch
        0095-configure.ac-don-t-check-for-clock_-functions.patch
        0096-expanduser-normpath-paths-coming-from-env-vars.patch
        0097-CI-test-the-build-and-add-some-mingw-specific-tests.patch
        0098-Prefer-sysconfig.python_build.patch
        0099-Define-PY3_DLLNAME-to-fix-build.patch
        0100-distutils-remove-checks-for-ancient-gcc-binutils.patch
        0101-distutils-split-CC-env-var-before-passing-to-subproc.patch
        0102-_testconsole.c-Fix-casing-path-sep.patch
        0103-Return-consistent-architecture-markers-for-python-on.patch
        0104-distutils-add-back-gcc_version.patch
        0105-fix-mingw-cross-compiling-in-setup.py.patch
        0106-handle-ncursesw-pkg-config-when-cross-compiling.patch
        0107-mingw_smoketests-fix-_UCRT-condition.patch
        0108-Modify-sys.winver-to-match-upstream.patch
        0109-Change-user-site-packages-path-to-include-the-enviro.patch
        0110-configure-Include-a-header-in-the-check-for-_beginth.patch
        0111-configure.ac-Default-to-without-c-locale-coercion-on.patch
        0112-Fix-failing-tests.patch
        0113-distutils-add-build-root-to-libdirs-when-building-un.patch
        0114-Don-t-change-os.sep-with-an-empty-MSYSTEM-env-var-no.patch
        0115-def-VPATH-when-compiling-Python-sysmodule.c.patch
        0116-Make-_Py_CheckPython3-extern.patch
        0117-link-with-bcrypt.patch
        0118-correctly-find-native-python.patch
        0119-Add-extra-flags-for-_bootstrap_python.patch
        0120-posixmodule-undefine-HAVE_OPENDIR.patch
        0121-getpath.py-add-support-for-mingw.patch
        0122-Don-t-build-_posixsubprocess-on-Windows.patch
        0123-_ssl-link-with-ws2_32.patch
        0124-Fix-building-_socket-module.patch
        0125-Always-normalize-path-in-abspath.patch
        0126-Include-winsock.h-when-checking-for-netdb-function.patch
        0127-include-_multiprocessing-semaphore.c-on-win32.patch
        0128-configure-build-mmap-module-on-win32.patch
        0129-venv-creation-fixes.patch
        0130-move-the-shutdown-function-where-winsock.h-is-includ.patch
        0131-configure.ac-set-BUILDEXEEXT-and-EXEEXT.patch
        0132-configure.ac-fix-building-some-test-modules.patch
        0133-Don-t-convert-sysconfig.get_config_var-VPATH-to-an-a.patch
        0134-Always-convert-to-before-passing-though-pathcch-func.patch
        0135-Build-venvlauncher.exe-from-PC-launcher.c.patch
        0136-getpath.py-fix-dirname.patch
        0137-getpath-use-normpath-on-all-generated-paths.patch
        0138-pathconfig-normpath-sys.path-0.patch
        0139-smoketests-add-some-tests-for-sys-site-paths.patch
        0140-Search-DLLs-only-on-paths-added-using-add_dll_direct.patch
        0141-Allow-picking-up-include-lib-dirs-from-CFLAGS-LDFLAG.patch
        0142-Build-and-install-libpython3.dll.patch
        0143-setup.py-don-t-prepend-the-system-library-directorie.patch
        0144-Port-GetPythonImport-to-mingw.patch
        0145-LoadLibraryExW-make-sure-to-only-use-backslashes-for.patch
        0146-Use-shared-instead-of-mdll.patch
        0147-fixup-sysconfig-make-_sysconfigdata.py-relocatable.patch
        0148-mingw_smoketests-add-a-test-to-check-if-sysconfig-re.patch)

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

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

 apply_patch_with_msg 0001-sysconfig-make-_sysconfigdata.py-relocatable.patch \
  0002-build-add-with-nt-threads-and-make-it-default-on-min.patch \
  0003-Define-MS_WINDOWS-and-others-when-compiling-with-MIN.patch \
  0004-configure-add-MACHDEP-and-platform-on-MINGW.patch \
  0005-Add-default-configuration-for-MINGW.patch \
  0006-configure-enable-largefile-support-by-default-for-Mi.patch \
  0007-Add-PC-to-CPPFLAGS-and-to-SRCDIRS-on-Mingw.patch \
  0008-posixmodule.c-Define-necessary-headers-for-compiling.patch \
  0009-Build-winreg-by-default-when-compiling-for-MINGW.patch \
  0010-pyport.h-Add-support-for-MINGW.patch \
  0011-configure-add-options-so-that-shared-build-is-possib.patch \
  0012-Add-dynamic-loading-support-for-MINGW.patch \
  0013-Ignore-main-program-for-frozen-scripts-on-MINGW.patch \
  0014-Add-missing-library-for-multiprocessing-module.patch \
  0015-selectmodule-add-support-for-MINGW.patch \
  0016-setup.py-add-libraries-to-fix-compile-of-ctypes-on-M.patch \
  0017-Detect-winsock2-and-setup-_socket-module-on-MINGW.patch \
  0018-Enable-some-windows-specific-modules.patch \
  0019-sysconfig-MINGW-build-extensions-with-GCC.patch \
  0020-sysconfig-treat-MINGW-builds-as-POSIX-builds.patch \
  0021-Add-support-for-stdcall-without-underscore.patch \
  0022-Use-replace-instead-rename-to-avoid-failure-on-windo.patch \
  0023-Customize-site-for-MINGW.patch \
  0024-add-python-config-sh.patch \
  0025-setup.py-add-export_symbols-to-fix-compile-of-ctypes.patch \
  0026-mingw-prefer-unix-sep-if-MSYSTEM-environment-variabl.patch \
  0027-msys-cygwin-semi-native-build-sysconfig.patch \
  0028-sysconfig-mingw-sysconfig-like-posix.patch \
  0029-cross-dont-add-multiarch-paths-if-cross-compiling.patch \
  0030-mingw-use-backslashes-in-compileall-py.patch \
  0031-msys-convert_path-fix-and-root-hack.patch \
  0032-mingw-pdcurses_ISPAD.patch \
  0033-msys-monkeypatch-os-system-via-sh-exe.patch \
  0034-msys-replace-slashes-used-in-io-redirection.patch \
  0035-remove_path_max.default.patch \
  0036-dont-link-with-gettext.patch \
  0037-ctypes-python-dll.patch \
  0038-gdbm-module-includes.patch \
  0039-use-gnu_printf-in-format.patch \
  0040-mingw-fix-ssl-dont-use-enum_certificates.patch \
  0041-fix-using-dllhandle-and-winver-mingw.patch \
  0042-Add-AMD64-to-sys-config-so-msvccompiler-get_build_ve.patch \
  0043-MINGW-link-with-additional-library.patch \
  0044-install-msilib.patch \
  0045-fix-signal-module-build.patch \
  0046-build-build-winconsoleio-and-_testconsole.patch \
  0047-expose-sem_unlink.patch \
  0048-cygpty-isatty.patch \
  0049-disable-broken-gdbm-module.patch \
  0050-build-link-win-resource-files-and-build-pythonw.patch \
  0051-disable-readline.patch \
  0052-fix-isselectable.patch \
  0053-_xxsubinterpretersmodule.patch \
  0054-configure.ac-fix-inet_pton-check.patch \
  0055-set-venv-activate-path-unix.patch \
  0056-pass-gen-profile-ldflags.patch \
  0057-pkg-config-windows-must-link-ext-with-python-lib.patch \
  0058-importlib-bootstrap-path-sep.patch \
  0059-pathlib-path-sep.patch \
  0060-warnings-fixes.patch \
  0061-fix-build-testinternalcapi.patch \
  0062-extend-MS_WINDOWS-flag.patch \
  0063-clang-arm64.patch \
  0064-configure.ac-set-MINGW-stack-reserve.patch \
  0065-Don-t-use-os.pathsep-to-find-EOF.patch \
  0066-Fix-extension-suffix-for-c-extensions-on-mingw.patch \
  0067-Change-the-get_platform-method-in-sysconfig.patch \
  0068-distutils-compiler-customize-mingw-cygwin-compilers.patch \
  0069-distutils-compiler-enable-new-dtags.patch \
  0070-distutils-MINGW-build-extensions-with-GCC.patch \
  0071-distutils-use-Mingw32CCompiler-as-default-compiler-f.patch \
  0072-distutils-find-import-library.patch \
  0073-distutils-avoid-circular-dependency-from-time-module.patch \
  0074-distutils-generalization-of-posix-build-in-distutils.patch \
  0075-distutils-mingw-sysconfig-like-posix.patch \
  0076-distutils-get_versions-fixes.patch \
  0077-distutils-install-layout-as-posix.patch \
  0078-distutils-msys-convert_path-fix-and-root-hack.patch \
  0079-distutils-mingw-build-optimized-ext.patch \
  0080-distutils-cygwinccompiler-dont-strip-modules-if-pyde.patch \
  0081-distutils-get-compilers-from-env-vars.patch \
  0082-distutils-add-windmc-to-cygwinccompiler.patch \
  0083-distutils-fix-msvc9-import.patch \
  0084-distutils-mingw-add-LIBPL-to-library-dirs.patch \
  0085-distutils-Change-the-get_platform-method-in-distutil.patch \
  0086-build-Fix-ncursesw-include-lookup.patch \
  0087-tests-fix-test_bytes.patch \
  0088-time-fix-strftime-not-raising-for-invalid-year-value.patch \
  0089-ctypes-find_library-c-should-return-None-with-ucrt.patch \
  0090-build-Disable-checks-for-dlopen-dlfcn.patch \
  0091-Fix-install-location-of-the-import-library.patch \
  0092-Set-MSYS2_ARG_CONV_EXCL-for-the-shared-Python-module.patch \
  0093-build-Integrate-venvlauncher-build-installation-into.patch \
  0094-configure.ac-set-_WIN32_WINNT-version.patch \
  0095-configure.ac-don-t-check-for-clock_-functions.patch \
  0096-expanduser-normpath-paths-coming-from-env-vars.patch \
  0097-CI-test-the-build-and-add-some-mingw-specific-tests.patch \
  0098-Prefer-sysconfig.python_build.patch \
  0099-Define-PY3_DLLNAME-to-fix-build.patch \
  0100-distutils-remove-checks-for-ancient-gcc-binutils.patch \
  0101-distutils-split-CC-env-var-before-passing-to-subproc.patch \
  0102-_testconsole.c-Fix-casing-path-sep.patch \
  0103-Return-consistent-architecture-markers-for-python-on.patch \
  0104-distutils-add-back-gcc_version.patch \
  0105-fix-mingw-cross-compiling-in-setup.py.patch \
  0106-handle-ncursesw-pkg-config-when-cross-compiling.patch \
  0107-mingw_smoketests-fix-_UCRT-condition.patch \
  0108-Modify-sys.winver-to-match-upstream.patch \
  0109-Change-user-site-packages-path-to-include-the-enviro.patch \
  0110-configure-Include-a-header-in-the-check-for-_beginth.patch \
  0111-configure.ac-Default-to-without-c-locale-coercion-on.patch \
  0112-Fix-failing-tests.patch \
  0113-distutils-add-build-root-to-libdirs-when-building-un.patch \
  0114-Don-t-change-os.sep-with-an-empty-MSYSTEM-env-var-no.patch \
  0115-def-VPATH-when-compiling-Python-sysmodule.c.patch \
  0116-Make-_Py_CheckPython3-extern.patch \
  0117-link-with-bcrypt.patch \
  0118-correctly-find-native-python.patch \
  0119-Add-extra-flags-for-_bootstrap_python.patch \
  0120-posixmodule-undefine-HAVE_OPENDIR.patch \
  0121-getpath.py-add-support-for-mingw.patch \
  0122-Don-t-build-_posixsubprocess-on-Windows.patch \
  0123-_ssl-link-with-ws2_32.patch \
  0124-Fix-building-_socket-module.patch \
  0125-Always-normalize-path-in-abspath.patch \
  0126-Include-winsock.h-when-checking-for-netdb-function.patch \
  0127-include-_multiprocessing-semaphore.c-on-win32.patch \
  0128-configure-build-mmap-module-on-win32.patch \
  0129-venv-creation-fixes.patch \
  0130-move-the-shutdown-function-where-winsock.h-is-includ.patch \
  0131-configure.ac-set-BUILDEXEEXT-and-EXEEXT.patch \
  0132-configure.ac-fix-building-some-test-modules.patch \
  0133-Don-t-convert-sysconfig.get_config_var-VPATH-to-an-a.patch \
  0134-Always-convert-to-before-passing-though-pathcch-func.patch \
  0135-Build-venvlauncher.exe-from-PC-launcher.c.patch \
  0136-getpath.py-fix-dirname.patch \
  0137-getpath-use-normpath-on-all-generated-paths.patch \
  0138-pathconfig-normpath-sys.path-0.patch \
  0139-smoketests-add-some-tests-for-sys-site-paths.patch \
  0140-Search-DLLs-only-on-paths-added-using-add_dll_direct.patch \
  0141-Allow-picking-up-include-lib-dirs-from-CFLAGS-LDFLAG.patch \
  0142-Build-and-install-libpython3.dll.patch \
  0143-setup.py-don-t-prepend-the-system-library-directorie.patch \
  0144-Port-GetPythonImport-to-mingw.patch \
  0145-LoadLibraryExW-make-sure-to-only-use-backslashes-for.patch \
  0146-Use-shared-instead-of-mdll.patch \
  0147-fixup-sysconfig-make-_sysconfigdata.py-relocatable.patch \
  0148-mingw_smoketests-add-a-test-to-check-if-sysconfig-re.patch
 
  autoreconf -vfi
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("--enable-optimizations")
    # Upstream defaults to -O3, so we can do too
    CFLAGS+=" -O3"
    # FIXME: https://github.com/msys2-contrib/cpython-mingw/issues/10
    # _extra_config+=("--with-lto")
  else
    CFLAGS+=" -O0 -ggdb"
    CXXFLAGS+=" -O0 -ggdb"
    _extra_config+=("--with-pydebug")
  fi

  case "${CARCH}" in
    i686)
      LDFLAGS+=" -Wl,--large-address-aware"
    ;;
  esac

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

  ../Python-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --enable-shared \
    --with-system-expat \
    --with-system-ffi \
    --with-system-libmpdec \
    --without-ensurepip \
    --enable-loadable-sqlite-extensions \
    --with-tzpath=${MINGW_PREFIX}/share/zoneinfo \
    "${_extra_config[@]}"

  make
}

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

  # Some basic tests to ensure nothing major or MSYS2 specific features are broken
  ./python.exe "../Python-${pkgver}/mingw_smoketests.py"
  MSYSTEM= ./python.exe "../Python-${pkgver}/mingw_smoketests.py"
}

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

  make -j1 install DESTDIR="${pkgdir}"

  # gdb pretty printers for debugging Python itself; to use:
  # python
  # sys.path.append('C:/msys64/mingw64/share/gdb/python3')
  # import python_gdb
  # reload(python_gdb)
  # end
  if [[ "${_primary_python}" == "yes" ]]; then
    [[ -d "${pkgdir}${MINGW_PREFIX}"/share/gdb/python3/ ]] || mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/gdb/python3/
    cp -f python.exe-gdb.py "${pkgdir}${MINGW_PREFIX}"/share/gdb/python3/python_gdb.py
  fi

  # some useful "stuff"
  install -dm755 "${pkgdir}${MINGW_PREFIX}"/lib/python${_pybasever}/Tools/{i18n,scripts}
  install -m755 "${srcdir}/Python-${pkgver}"/Tools/i18n/{msgfmt,pygettext}.py "${pkgdir}${MINGW_PREFIX}"/lib/python${_pybasever}/Tools/i18n/
  install -m755 "${srcdir}/Python-${pkgver}"/Tools/scripts/{README,*py} "${pkgdir}${MINGW_PREFIX}"/lib/python${_pybasever}/Tools/scripts/

  # fixup shebangs
  for fscripts in 2to3 2to3-${_pybasever} idle3 idle${_pybasever} pydoc3 pydoc${_pybasever}; do
    sed -e '1 { s|^#!.*$|#!/usr/bin/env python'"${_pybasever}"'.exe| }' -i "${pkgdir}${MINGW_PREFIX}"/bin/${fscripts}
  done

  # default aliases for all scripts/binaries
  if [[ "${_primary_python}" == "yes" ]]; then
    # Default names are aliases for Python now
    cp "${pkgdir}${MINGW_PREFIX}"/bin/python3.exe "${pkgdir}${MINGW_PREFIX}"/bin/python.exe
    cp "${pkgdir}${MINGW_PREFIX}"/bin/python3w.exe "${pkgdir}${MINGW_PREFIX}"/bin/pythonw.exe
    cp "${pkgdir}${MINGW_PREFIX}"/bin/python3-config "${pkgdir}${MINGW_PREFIX}"/bin/python-config
    cp "${pkgdir}${MINGW_PREFIX}"/bin/idle3 "${pkgdir}${MINGW_PREFIX}"/bin/idle
    cp "${pkgdir}${MINGW_PREFIX}"/bin/pydoc3 "${pkgdir}${MINGW_PREFIX}"/bin/pydoc
  fi

  # delete anything that could conflict with the primary Python
  if [[ "${_primary_python}" != "yes" ]]; then
    rm "${pkgdir}${MINGW_PREFIX}"/bin/python3.exe
    rm "${pkgdir}${MINGW_PREFIX}"/bin/python3w.exe
    rm "${pkgdir}${MINGW_PREFIX}"/bin/python3-config
    rm "${pkgdir}${MINGW_PREFIX}"/bin/idle3
    rm "${pkgdir}${MINGW_PREFIX}"/bin/pydoc3
    rm "${pkgdir}${MINGW_PREFIX}"/bin/2to3
    rm "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/python3-embed.pc
    rm "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/python3.pc
    rm "${pkgdir}${MINGW_PREFIX}"/share/man/man1/python3.1
  fi
}

sha256sums=('0fab78fa7f133f4f38210c6260d90d7c0d5c7198446419ce057ec7ac2e6f5f38'
            'e8bc6992aab35878f1b797527801516cf20fde18907ab5f0f6035e9e2d98aa56'
            '7b2974bc0267869a74c9d90aa9b17d70053c50d7ada1ed74d88764773c49581b'
            '3ec9d31277c4c106b59d8f7f4d7e31df7b88732f22aff4cc2db139ee3dbd1518'
            '749e625b35d0fde9a2a57254ee4234d1e32429c58f98398318eb0ecb9bd4e27f'
            'a15912e751dd102ea7449a78d194886f55539f4556bb902221ae9e5fcd123200'
            '7626edeadaf44f1af787cd5e476b0dcfcca0ef370c5590bc6798aaa46e5534b1'
            '2f9739488e8421ad03c286a579339c7cc252902753c8fddef4894ffbd26d4e88'
            '5a344d6af569ce480dcfd26e29a3abf731c8daa135fb6224aa8c1b556c2ca666'
            '20e5079cc15042ceb675661bbaa88fa2af199ba3a8e6d806cb43d619729209a8'
            'e3d1e970c00e278fedc430e34452d35607a449a14cba6f5c507d14c2eb7e251c'
            'f7cf45c9aaea47843f52e9dea79570d7a401917d75ba0f6ed9b3bcbab670c19d'
            '3f5fe5c86aafe08fbacba7820ab24657c3e399f272a3835fb4199ecceae40b33'
            'a8d2670e592b36456b74b414f5cd96e744108739f12bda315dedea947ecd686e'
            '07fee799357bc17f91a977109bdf071077c8624d2c97a5378b664ef28d409925'
            '0fc92b3415260e7975ce92fdfdca89af57fda5823dc7a3bc8082c4abf8dba23a'
            '24e78dea4eaa0a10437ce0aa8b16f99eb9fced16c5402f2f1fac189898c571ae'
            'ff107971b80158cb48908a6f737f5847be402e7b8cf53768e7d117aa71fcca2e'
            '1cbfa8c9e413b3190ec7f094dd3b95bbd9a8cddc9eb2a280d2a4b344e5c4448b'
            '022901885c408fbe7c7ddc2449245134d9dd336dbfe1146e6bb51d5ccefcb894'
            'be715419a2e606dac6b09d5a268d10dc650ec8239373819d0884ced6f0ce731a'
            '28b61b54de5bd733ba36f2080b348780d579c9f0ab194084d1fc8370f17bc49c'
            '10b08f0ca3766aaabbfc2f41b3250ed31d8f06b8a6e477bab66efa2764b2a3cc'
            '6a6a35c141610e90d3b19f37e9532ef438bf8e62a51d2bed51c08e65de140e19'
            '90d4787e9e955377b4865dfcc32f743a95aabca5d1572091784c7a84286f9d58'
            '71c198140ff229103181a9a55dc4ee7250235824d49d46647a572d2f9c7e10e5'
            'd37dc5516bf40c8fbba33a9732a260ceacfaa2fd7aa8734f8f1a1980e70c1917'
            '656ecf804abc172bc95b9505a9842ba7d4188d83fcee909a910750a6ea70ad5f'
            'a83fc1669276b90b1f40673076a04fef1476d05d5b690bcc1d37c2ddc7bb0df8'
            'ed0cc5f3f20893b31ed5f3a584dbedad7625a4ee14d76398c165a7811eee82d8'
            'f54db4337e12d6034fa2901bcdd0a56ff8c2cc8ad16c91f3990fd5007c884be1'
            '89321d7c5bb6f2dae174454f3ae547767887acb29c912fb6ec75bc7a5c1eb8f5'
            'ebc7f46403d5fbd809fadb00b0773dfa86b455040f6e942eb6c870b3c3542a54'
            '5c6bd289c9b9b4696f5f4ec2d6f6f959a85c97ac909bb468afe85a3cb8af57b9'
            '2d11a20e5484ec3f8704a3c3752d0389a46b655089263e6d1cbe5fa633d595f6'
            '22d9247ad06941e9c29ba54e65dcf7c269d86e4b41d5366b133a696d99dd9d32'
            '9ef4a44c8833ec4c227e2d5111983eafccdfb0ed4507e6f56d08fee0b2e3cf1b'
            '82a4147a3b2bc533dcc21b4ea9d3cb663896dcc5b5621d1824a4880d53c0b822'
            '43a2ae7ae7a6b3998c03426dcc29ea534669c5147e57e803be066aba2a6d92b4'
            '9854816e49b41e0c002fd6e8b27245ed831bbdccf52483aace2686c305450deb'
            '7d7c188465990df00fa46d5d6dc2571524537f17429374fa332ce73885a7a059'
            'c0594353fa71213ae8e1c08b0bdbb1c4c192ab9a9136af73fcef7df53012269d'
            'c5920b2e25216a94a0e5aed6852df27ef279fc57cb8731af860154c2e1d384f9'
            '15744a4c4ebeb16e707630a88bac29cb18e73be026842ae9da6b110e06b75cdb'
            '662c196bceb17ba43c7ee4dafd22550b68d15d9c6956fa86529220aa0ca9204b'
            '6741f45eafb7ed312e7314c327962cdc9b473e9a21b9825d84eb2382220940fd'
            '4b3d023f7385011eebb85f0ca10e151a8c1dcd0372e23e9768bc05c501f764b9'
            '17f48b69aadc148ebe412453952ebc43eef4f21dfcbd634423efaa6d686edfc2'
            'd7b9a9897baedff1277d7abdf7b50982b98bf552d512372813f299a26e166c73'
            '6f656ea592fbc2ef44202e1adec14b45d786bd9a9d75ad569f56e9c8598a6799'
            'f4b503d18f5d41f0f1e493f815cac35cb9ca3806ce850360486937a3b129300c'
            '56c3f770b4d4970ae2bb26bf2a821b94114ea2a479cd450e233fc053588e2fcf'
            '773690f96a029ccbfcdad83309aacb868cd4c1b05aa91020429b9e90a2fb1ec1'
            'e0243be5a7af2af69d76defe25373ed34ceb20890bcca0ac5e523103780a3202'
            '533b299bc186a6b67f2010067c2c3ab1e9936514687e0f09e98407de94f491af'
            'da918b90878486e7532f18397224166a7472f17208af324eb5da1691e1d6fe80'
            'b6154b3d6dcf3d020c8d37041dd7f7b3d047998855b2f0bcd0a5efb896e5cf40'
            '9ad8fb3141c09415b3b8a73959d3cf3217b38dbe3042cc0a88fbacad98fa4a21'
            '7475984f438512ef343d030bd5596e0cbcd126f31732a33ee66151d2cfa5713a'
            'fca6186cdb75e7b2bd3147b48f4e98a4f2a3ff84afbeb027012bfbfba4bb75e5'
            'efe81b53c9e94ad85dba3f031057152f6b66eff225583721bb1eb08bf30dcb54'
            '0f9f090c7eb6f52a038f66e46b7dd5714fc7bedd3a9a666cb31fa78f2529a3ec'
            'b3d066fed180d55b776977c017c602d13d38faa5eb0c23e278c14cd73f7275af'
            '1aaab982b6c81663e2523abbbbecf74ed52d0251ad83ca51aeac367760726c04'
            '5c3032b8cb8262e6c72fe9f0d4294b0d3c7cc9d831b995817f20e3bbba56faeb'
            '2af41b02725996b85673b8b8d173bd210d901500743ca36a7a6249059c88bd9d'
            'd05965fd9f49b79d96662b720b0eb2674662d959702fe54fb15036996f5a120e'
            '9dc3bd39d095b0fded17a3f6b1bb9c87848147602b8c825684332c8d9cc785d2'
            '587ac14d5c20ad884ed6de2902a22e72d318a4a880893d3d24c2703e4d19b55a'
            '64a337bda4069c4bd506b2977b84bfcf545e75fec9ba064046a3984d027b67fa'
            'f80ccc030df322768d3d02bac5a55c7527348b4d6f7f7e3ffe1f64cc9c151781'
            'd7f23966dda784b184ed6fe19697d4bcde3c38e5fa9ffd4131dcd6fd22cbde6b'
            '93e3daea18b692f834f5a37153f2215fbbb10f424ae492f0ba716efa042fdbe5'
            '6d76ca8563ff90b1a6e76eda5159da60030816d204be30964006e9fcadcff59d'
            '70cd9adc0780d6f0b0d4dd36ebe5047a67ebaede0609febd6e893db3403b90cb'
            '476ed4b9a2cb67aaa2b2f85151d9e4ff98051a61523039991d4241a9d941a6a0'
            '6420101763602086b6705fef4984b02479a2be5b08124db895ca6ec649cceb54'
            'de39801e88923eeeec93c18d9b82859c0b17606b2f4a94bb5be08fad98e817ea'
            '7867142eb067c54780eec234355b0468ee5ce09e97749165eaf362c27593ee29'
            '535836211634fbce2d9386ed813dfcdb31807a264205049bbfb6d1669b012690'
            '6271c5187661451ab4eb0af4b73f0f999c11dd11c444f3363ccf3327af671181'
            '76cb0fd327806d2b8aaa28dd73e24948686ecf54f235eaae9829d6bf1582604f'
            '21f050fa6330d58d5f8aa568f54fba73d3aa9caa5d1ea843b6de4d258078e2c2'
            'd6f60a4414a32b933adc47008030eed4dbb9e41192306da4f83c432e2ae23cab'
            '222c8da2b83cabc3f5d43eaf33aefa7c61b8205b0876e3333de8b91e98aad4ad'
            'e612f66727c3fa9e13f02943b7ccde40544dc12c306e7884c0d46cc06885fdc3'
            '2d3716225147264abb8b7d9a86605220b3155673d1214d3fc784086b738cb144'
            'ae0826b4830da952f21195eb2b71530451255a7f1f3f238cc3b9e00b428eb1f9'
            'f418af01bca5b99497a3dc64381356b7c80e82c14aa5d256f686a16296309dbe'
            '2290e005afe36862a3d7f79978cf30dd544645389820d1a75874d31f349f6c12'
            '08a93886e5862b91e37a1d0e45b68e6307e7618bd1f037da77c7e8f1b3caaf03'
            'c4c3abc2c318bf3bf7b241590841a5932fc3ec0bd7b05c3b65c81afa50d10097'
            'f2f9baecf73265948f9bd3d71fabfcd7c93fa1529eea7f743004ae97f6e8b4a1'
            'ad640f295a3304f1411d355dd05014b818c43f467e56f655fd28b806f4884d10'
            '92bb17c6ba4e19feac8a57c08f84e051701b73822d9265edc26cef4c500b9a2c'
            'd4bc24e178468aa8bc8794137411d26a5f8a40ea4ca723c5910c4f950fd71863'
            '5b3ab4782be568da71b712ac4c6d991598b944598ced5ca47c061ec88230b101'
            '46b671aa8b4aedffd73bd228314b59dd087a5379569d133e414752556b3388c4'
            'b76941957a9c0ae88a0f8feda009df3f6a3427f1bcd22995f86a77553a71dd52'
            'e255e0aaaca0fcb24878457c428162169a3ffdb8508da0e4ccc673ce127121b4'
            '37b1add60294a6bdc6f8f1f263d2a2d713c3ce40e2ad9259fe17c3316ff98e30'
            '59cec64ae616d1ea9fae4cd9258b6c79eb04225ade49d69a18c3d3d9c6e7ac41'
            '326f04df28bff61a8de72d7a4ebcf2b5d69d65be636675d42e99395a64647915'
            '8bd736d1f0b0cc60878368e28017aeef5e5b6c2c264f757c8fbacd77ae904402'
            '91fd356c161ae3503955b0ea0b0b700546c503b4c9eae72a1a50be125e19bfcc'
            'bcf0c5861ddb641effe97809a1ebcc0bc3b74f5d7112eb8cc9bfc700ff39141e'
            '236a1d7a2ac00d98fdbdd09313d61866693c6ac6e5b6eff2c0548b6e635a8934'
            '7e6fe2e58523420a69ab3a99d8da16b42fc714ed9c1afe0b4701fdd3c5b5f749'
            'b8b75c5e0fe51c70d98756d467ec4534244c3b2d453ba9fd974c4273c8a77244'
            '06622b5485f68c7f9773d0bca17739d51a80df4d65769d349011e07d4a6c0d63'
            'be188de10f978c8b586af001462bfbac9ce7a17be27d8095d47d48821a92f90c'
            '2508578c0da7cbd4fc2a703839f9641b4b81b6af187adb86d1581354d6a07102'
            '991c1ee895e3b28f457af1013fdea8d7f23c9b2e5f3168d7e1bc4116b8372cd2'
            '28a366f49fa7760fe1e6b3c967f18b2c6cfff82117fe66f36894c81d9ad10edb'
            '591f40765b87f50d984332ea76d6165388ffb6a6f9cc3e750663efc996314e0f'
            '55c907626983dce965470f8db075eaf3864ec488778085391aea92e25da55d1c'
            '22853539fb102784227d7ffe6da78350f767c05c74d841749b9d75dd3c7878d2'
            '8ab3e796b372b4c9468544e33de01bc1c07b5c90c6e3181b5782cfbc0663ad0b'
            '64d11336f3c39a9d94595ee562feb8edbb9a5db7bc6a62e60c098b57c0a90da4'
            'c2748c494ccce93a93a2e11b1ddc66d5c380bcf790e4e3cb43f67a05189d2f5a'
            '3ef569264112bb0421a22b0730eb38bcb1abcab92481efe2e3316601bcafe18b'
            '74c6ee48d1a560eb0188a5ebf4e974a88b20c39d23063d2ffcdfc005c91be641'
            '85077af45268749ed4c64b32246d4c4b5c16c14e3cb22bbf68fe58fabaa6cfcb'
            '868742b69069b033681758deeb436bbb01d0785f3825db6dbb0d56e02d258837'
            'a5f79661a71bf1a51723b601f91d64c67826242b5ed27182714fbe6fe4990e0a'
            '111540c4ee45ae33d62582ed17d8402a63f82ae5752ca6fb3210c2165b3def1c'
            '19e62b89159153cb8e44d4a9cc68c02e2cd33adb6b9c8b8813ee525fc3bd895e'
            '45bd5bf8fd53e3f7aef11ecb0601f651c1077862c1c452ad83bdb58a26ba3f86'
            '4755bb696646b799969798b260b8a91d85dd8402aa0c2a0eb4529a5bc8afcf65'
            '0f6beb0d308fed7ce428759e3046f794854c1f80a595578a1e9eab0b0ecd0fde'
            '8e1e9946da23eb99cb72c927ff9b1fecae473a54848abcc20f9e3da5d8527e5e'
            'cd67e82fa30abb6848c0a58ad288ce7ef7a6381b52eaa1b58988ad2afa27ad34'
            'a71f9b7d01e9506931ff0b097471d9b4680c9c6fefccbab6360fc10dbcd097b5'
            'b9b110b0d5efc40c6aeb08f9ded20b994b3b57829871dab90dc08cdf0ac430c2'
            'dfe654d4f62e3447c0239baa6a385ae1b5695af158afa4e66c562232444122ca'
            'eaa5fab6a667b7b1e00186568b3125d4f1a74a4e3a9709b3930b0e1ac38ee499'
            'fa65c9acbc560cba03c42df5bc61640cd84a8a0afd24b4cd4007712725123868'
            '6d66ec12f031b1adafb6ba7319093a1f439a6370feda902ccbf42b1eb3af66c4'
            '8bf1adde50598e17928d9cce3aa45d5dec794171bb0eb704e9c2435a018bb94d'
            '1b48b6f0a01e75956034401fcdab68a076753e69e92169ae9d77179470b97d29'
            'ae47357aabcfcd2aa810b8bd090576005a426f4903a84f485daa71c425b3c9d5'
            '3aa781e6fa06d5c117220a3fa6a293ed1f943cfdf944f96d6ec934f8a3ed5c38'
            '8cbb64fbaac528fc563a8d74109bc22668a9d10c349c8fecf0378dc3cdb51d2a'
            'c42adc1061b81f77f7a3dfabf455cc1894e82f6b65b35468621ba27d471ae21e'
            '06704b6f161aa2607b1d3f7183fbc9f57e7b3f1b3a1ad97abcf7ef007c952029'
            'ab6d51169505a411c5a08d8f836b2bc96952f1bcc0ba72da121f1f174775138d'
            '4ef06fe46863bf8d5b82ea936bb1819e80cc00b22fcd4bdc5e933071399c148c'
            '21852e0a64c8867f709de718cf56a6a5b780b4c802c0684ddfcdacff366c1088'
            '3a30bac3621d37284abd9b4af37030be30457a44cf4e80b7e2c97ce91e1b2003')
