# 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}.9
pkgrel=1
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/"
msys2_references=(
  "cpe: cpe:/a:python:python"
  "cpe: cpe:/a:python_software_foundation:python"
)
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-mingw_smoketests-add-a-test-to-check-if-sysconfig-re.patch
        0148-Makefile-Add-a-dependency-on-LDLIBRARY-for-the-share.patch
        0149-CI-skip-test.test_tools.test_makefile.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-mingw_smoketests-add-a-test-to-check-if-sysconfig-re.patch \
  0148-Makefile-Add-a-dependency-on-LDLIBRARY-for-the-share.patch \
  0149-CI-skip-test.test_tools.test_makefile.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=('9b1e896523fc510691126c864406d9360a3d1e986acbda59cda57b5abda45b87'
            'b274344f4dfbf7ed66bce72b7b2c33de38646a7c16e65d3df02229d7fa0a5c9e'
            '1923cabcc8c3d829b556f9255c34a8eeecb87491832c0ab8deb023fc446ff659'
            '2e8d47dde74a95ce987fecf85d64199fb1451fe35227ea099ca041043ee36be8'
            'f6d66e4f348262896b5128d197d6c1b3accffe854b8a3e2e0d416b5607598245'
            'e1da7ca979bf180319e47b23774a0ca33cede56b99f1a1377a0c8e7fd011cbaa'
            '035c2d8d4056849d076e1faa83bf8e1cc0dfb2acac0665debb94af4037219258'
            'b963d092c4387df2dd754a8b174b1dd4dbf1c9ca026d257862ad85b81c4fbf55'
            '3fd559651d72167506870b3d016c8e95d57c5963bc4ee8982821d6960a1b0e3c'
            'c71faebe24314b077ccedb9ce37a73ec93074c024c655a6924307e35392d9cef'
            '3d875a7ae31e3560a0c98557f75e8e482f80eee53dd788d209da59faa7deb161'
            '90247a4180699ba60e5090fa9cb942d4288c3772ca5a131b2f9e79b37afe5c40'
            'e25bbe378f4d9b67c1b0d8f1eea433ed2298e9d6219ff727e6ab00bc969fda9e'
            'cff91c869b9319d80bdcf8331bc9f5b02b582def684f884af12d20d16ae4e903'
            '79e1ea910e39f62eee1119c654ad660967fb08fac1f6b0fe13bf5c77fa07554f'
            '484bc24c8e0ed510239d98e3d9ba37f9d0bccf18bbc58452af7e341f0dfa5f5a'
            'ace133a92b382b767bb461bf59667370acd765791ce80968d9acae83d16cdc66'
            'e7e79f9450d94b8f052e67de11b85782527551fef4ad5b21db4d3e28302281fa'
            'fc7c82711fe3c70a479f26e75f5d379468579062aabcc0805faf66ea6be0fec9'
            'e7fc8fb865aa616e06953f06dd8673717044fc669c0a295c6fc7dfa44a16a3c0'
            '451a484c7659d5f6ad3e9ac1530e4c4ac33e588a2847e1de4430b9f297d82763'
            '7c21e667729de37a3f0be7eab3e808cd091ab4f05a97deffb81b6864eaa9d856'
            '3ffbdf223649fb7900fece333a8bf40839268ed5d4493b549d5f08f49d9e544c'
            '4f494adafa8ead40d793e0f456955ec9601f53835fb83cc603542365ce15c5b9'
            '65d4b4ef99dec90ae22a86d04a40ba4411626a6d8c071225f6e6deb546babac3'
            '00b928fb7d138de3ea613a04ffab1fc15babf70517cee7ee6d3e8536929137d9'
            'bd546c15c5c7b99cb67d2860afe325967292b86e873b93733fa0eb27968e1ab4'
            '446a51192adc58e39a8682c00c55f0fff1e31fa3567f35dbf4bb8bbe25924258'
            '190152a155218da963a1eac13d127dbf15abca5afd772b9705d1cbf3096397f7'
            'df156d4e4abb927aaea792b7fc05ab839124a6d4c6283e25f3745f704b7a5cbc'
            '8633728662eb82b4743207c2fbabfbe8a5d865b31d947c60bcca8db7e47f8830'
            '9547dbf0d596fb1af9737c8ddc86da5b190631028bfb101cf21333aa484ef811'
            '9e879752ee3e6deeb182a614b46264945e5bd106848b0d156d65fb7cf96104be'
            'cf26eba9ddf6adcf99ab641a5db0acd1d4b4274ad70b19ccc3d06f8ec9049f6b'
            '10474012c6301eee767c45b3ba84ba064fcf4f999794806fbaf01e4a175708bc'
            '3eb24446ea66cda4d526a3d4b18fc82c41787f55e93e7cd62b15ede92c587962'
            'bd1d8cddd818a10cad6a6ea5498bd9c3b8318e9b20df820717fb90f7d9c3f376'
            'ae4f2c835359b57490c51c5d2ca04b7ee52179a6e77f5ac47910fa3f65c52874'
            '7251864ffd99475c79fb891b7f51f8c5389599176d6a029f2ef5cb5c6853b1df'
            '580a14d844735bdb0779740470304460c8bb907fb8e2523cb89c3e44b9c1f965'
            '7c2437b89e731ca7ba9e58b45724d5e3d8cf8204dcc6c0ea1de90634a295d3af'
            'b4a7ff0e0f7a459e8849c75d113ad13fc5c25ad5dc00e59872a308537e3da2c9'
            '954c2bbb09172fa0a67c27cf8bd20e048b7eec022591f544c17b4ecda104099f'
            'cf9f2c0cd7f65d7818d0b7531a3122e776a4a517eb06096198c4e6c97ab80148'
            '28abd9f181f2615320b147d656e7d62647500b02a271ece8bf173139106d63a7'
            '04632cca191b590e48f24744df83ba25d426f4653a4d94fdc53843d1f0fc6972'
            '35b6f1d3300709f700101f1d765054aebd8b722a29afb113710e5127e5d2564a'
            '4b4e0b3f6609ef2da97c146b2343dc422ab8b85bb47e4134e9228a458ae21861'
            'd1fd4187fca930271e0b9c3ae404a26e9051a3fea5394ae4a230fceff738d771'
            'bf883f1dcc5e373e51840a4cc8324ec8f6e4588b7a82439d87d6701a23e1603e'
            'ee7638ad53235309b86be69f30834b5e0e5aee71e3539d6b7a56dfb35c49a4f5'
            'bb6a0f9cb8609ac570af45f5ac4369d6fbe3bbd627a779805ef7e053850c2a08'
            '666659a21970a0fe4c64abd0bf48bd2e19482ed0577e1740386a03b7f088d152'
            '923a496c3e037bf1657505accd8930d3323175a94a7a83d62166ad623fba3df2'
            'd7fc80ce13f7677c611e3747a16497c431930fd4302a95bc200f74d5b9a52450'
            '67efdb0eb692aede4438b4cee5e6050be8f45f4773292b0b8f1df9a1ba476047'
            '2b71205a402e6e390d4a10d8024aa269d935d21e3db73d0e43a2b0d2233d3f80'
            '152e1daad5d43e4ca7d04d0a971db44077ecce2ee02abad13f1748a00c84cec7'
            '98c76fd8f56c5b4e4424c7d708241ccbcbe37ded2e37f5f8098db5f4f8c48162'
            'f831c3946d0ede7ee0078571d534bea03651c5184de667e068b4689098d7725a'
            '98ca3bbab40bf6d5066271e09129799f2000b17f9f6ab0949529786f61c8edbb'
            'c2b1c6cf911851abb109ea856a0b1710d741467be72478d043721345a8420d23'
            'b4bffd2dc76d23cd9215908d261f0542c594687af2f01322ec7b065d887c5af1'
            '5b15aa1ef08467350dc03411a269b99b635583c8e5e9b1bd47558cdf19564edb'
            '629c83e3d81032a4f8f4e28283800c902894b96be017a021e8b09adb6fb96b0c'
            '1a8a1ee6e0962e74ce9fa4fb9cabd858e3e0407f661d672ab0b9b19ff460172e'
            'd604e19fe0eb0129e9107926784ffe553e96deda6f4e9f4f9f270a5ee4a029e5'
            '0e689e2a17e1a97967a21b531637beb94ce7a3b03cb4cfda7cab804660195ccc'
            'dc8be2a7d621ef1138cc4e8a3e8b7b8a99789f133826d667edb72541cbe4f067'
            '6c5a51a34f2151b812cea311acc1ebbeef1424e0b12a2ff932ef425e5dd7134b'
            '3e8240d045b8270a722a3f278d3e303f94a328765b715d01690df55f3d9554f8'
            '52a6fc118bf9e78c156f9dc4805bdbb754475a2f7512cd392ca55e6bfffe4044'
            '299d9128dc716887b1fba6bfff22cac0f22deb87903eb59b44930b4e0b4d84c1'
            '7e1281cac5bc5eabc01678272dfd45ee02ef6ecf60c7d7860c5ad9a1c83a9cf6'
            'b5ea6d61e5750576cc6202a1e3b0fd22e97dc9dd1f9512cb79c3bf9a80a2dcdd'
            '05029915c1ded9634b63200f6401dddaed7e854b783e16f1f72516295e988a42'
            'edf746ca345d5060c7e1d8f4186365ac680b891b531098956b9089bd6254311f'
            '58fc42259070c95f40b1095fc9ceca71625387b2d0c5f4ac98d2678e1b5b3e18'
            '3c01ee6e5a6bdddc18fd9678599f9cb555136981e74ae494d34fbd8f8c57ce4e'
            '8b2a958d638477ee014c0a5ad9561185b432e9cb5175dbc9721f6d7572b2de70'
            'f359d91da1c36cb957e390106e3ed34c11b1ad35fbc874784dae24ae021e8e3f'
            '2e1980de9fa2d169ae371788fe56d054af9de633be673da5f5c7ac9e1991566c'
            'c194b1d8f731362548f2a40d865d2a3e6bc5e78e93acc153807affffefc3acd6'
            '576a98dd5f4694995ae3db9745c399471a63e0e15f45d22c07ae9400b2dfcdaf'
            '482132c062e24d2c2f2380c581e251a281c3017e4549bfd1bfef92f7b2bd910c'
            '23212982f9e152f9041326334ec39a78419646491ee4a5abe5bf506d4e4c2925'
            '57cd7a75bdb7505c80eb8798320fb62acc86094d0b3f8bbc8592caac4204d0e3'
            'a62625860d7c23a3c21d44eea66f57fd68b6e8c85d97045cf638df8a434061f0'
            'adb1b50f49f596e91e9b74d43d0dd274b5c505c3e6234bbfd65d8394d45a3f82'
            '6326c5de40d02e60e60a29de697e50ffedf48687640c5b648156f1894c30bbf9'
            '7bfcf842d5d040cfefd687dd3b8a1529a963d1f1072edfe9da7c90d10afdbda1'
            'e5fc27ed7ddd67cc18c76c903f217784e554cb5ed994fa0152e9306690794085'
            '046c776966998c2d052d13ffa80010c08a8633c15eec3f18fb014bd0c9a4dd93'
            'c7e90e7624adc6dace078f5d7b2e3207acd654cc69cabca4d4cdad954a30d148'
            'f497b5c6ea1b80ed6d2704a2479edb7f2c41c12eb6b398213fbd7fb870cb65e3'
            '5ef857dc527e00b9a079c2a59ac5d0dd63e89e8131343975fca8c03a6ac1e665'
            'cdb4249d8a89329c4edcdb579b556e31e6682ef93662ad449956109dec75e484'
            '5152671b1f614e3ee37ebcf4f7b2c20817ba0913001499e4b628276b541fa7e2'
            '290731a45dd3bda76c4acd7017ba4ba95b636f3927d66d35fb2b18e5af00b59b'
            'e9ff737b4e1bf1a610a0b5467aa1a1f77b750ce71881e3e353a8fe089f52ae76'
            '894afaf81c23a16468c91c14b556d908c42be5e6b5683d6d84fae8e7844b7462'
            '90c6f6ab83d937d6b14589fcc30a1d45b1c6f0fa299bd958a372b571ff3bcccc'
            '593cac5e6ffd4326be2fd5dfd52dbeddea31d1a16a0cbbd4e1879b98dfc71b47'
            '57d82cc97d63040207658844e044947bfe2fd606fad25d1daa8f166fcb779985'
            '862e5f70c4b227c547b6fc6d8fc2597b424e021760e2a1aec5a6523c30ef876f'
            'bb7e1193be4826c97a407b11e08ed85e771d8f6918303541a56dfd4c07853b26'
            '5d8ff828a9fa4883d2bc0798f1f4af60c2d98f8609677b3db8c70e288801353e'
            '70bcd5626bc2b33128ea49e3ad5f4ac0572287262d1815c1579d1885bbd29bd9'
            '16de364c25cb724ebea3b56200707d6807d5c5f49b7c50d10f141b19b0dd4e7d'
            'b68500f33e9a6ced79781bc22ec48ead0bb87db1f039013f0e81b98de45af328'
            '52ed44c1b45ee9a2c152a1deb6953eb59bc1aad921a8ff8207682c49f7c1e839'
            '48da416190b42d80b3d1707f2c26490ccdeacf59fb94fccf83d5475dd4c8fa00'
            '96a62e4b9c556f4688fc2c837cc074643939b952531d0b38df4a18434b2941a5'
            'cdc05b29eb4b29fff3441342c05a00b855d90d9dd0034e2e1736503ee9cb87e6'
            'd7107d91f55729b67af952790d440d212f9bd85edbc80f11d201eab4b0064b90'
            '58cc66d92b7e40edbaa6519448038e33dcc402133bd9f6955dd68b3e4d0c6755'
            '98943c960cc2f191c75d1a1d5bd552395d834ce2c1c52d0694e3aa9aafe9c86c'
            '3c58dfa22f514e2d0a69da87615cda128aec9ddd8863c99559c10693ab767591'
            'f05857a2db404730c5bce3f249a9d0defbbfbcb54aa8377480d5291687e2937c'
            '573a18172079708f18a7752b0645a0414f543edaeefb84a810a8d471a90e19e3'
            'bc481c23b03ce8baa38513ef5d18119a46f8030b5e458b104dfd86ae3fadda7c'
            '7f85a0833311a16ae4131c12c277b701c2e050561f612ab418729f33acc1ab48'
            'd85d94ac043c9139f1d3509bf7a5c78ebec32fc061ffccc105c3162b4e9e2933'
            '86dbe45c014b1843b5c9a05169ef8fd80b140b186a14069f3a3a67c3cd741ec8'
            '699ae7c9efa41325eca331064aa7dcc7bd2fea65f6d38ef6a50403c6fd3e3636'
            'c060c971618bf5a0013ecf35ce997a19ba7ade213439cbf9bf4f38bf5de39011'
            '6d81e36dfb1e1ae4734fdec6c4542fec7cdd83c3a8cf6b9feddc9ef6fc1deafa'
            '9641f5880e1a9662a5a1f4e8e4ad8563d0a54a665d98efb25cbf940f99c872a2'
            '4f018043669379ae999e3ce85f264859aa7161a07f2d2af4e32187ff25ecc458'
            'a2cb66e2d89a9d169ada61d62fa4a9967c91f2fabe25ef31e08045e48750760a'
            '10c9bd893b3039ed019942069e06206da29dfeaef0cda73eaa7299c2f3998a66'
            '628927e1b577232767ba29d107e24e91e4afd32690f216a2a94074bc067706e7'
            '541a674ccac544222b3eed1ccf2c4a34bb1b74c310a3eb8b1f2d0b849e539246'
            '04775159ea6f35767901890eb29f9fce0e6ae5c6e86d00bd36aded4a5281fdd3'
            '46510526ce27a1eaf29cec41a52064644ff3ecaec8f3adc8ba7906891a2ee5f7'
            '5b8f91f312a537a39ff8acb85128149ec9f4b885435af6f0d54fc1bd0bd479f5'
            'dccadc0a37e9f55ff06808e2b08d136ad1b741fa05c32c93bb42d3d9d93169fd'
            '01e01f25d5c4426e83ba14d4cd974af9cf7b7c4b5d69b4abd5dd3c5ce7bfb1e0'
            '505429cc3adcd87f3be53dce5f7c8cb14fe46d887847cb83bdb2c3675b954362'
            '0362adb60e44f8bfb4ef0ba1d8e07a84efc2ec603470cf7a1435dd295cd139b9'
            '9cd699b911dc8a3e7d33fb1da8f8795754449e940cb7f8b90c6564903c0c73cf'
            '53cedbc601072914f3e61d9e029ec7f6f16e16a482576a2b6f2a351420b8edd7'
            '0022bf8285df93db3c3dc26bc55b67332be6fb13506964c1c8ac5a8209cb9e16'
            '9380be2f1f840ae518fe4917737425459b8488a36ecaaaa2a3f44bab8d72dd8b'
            '924968a62510316dee3f8ebdf73fd813f5bfab2ed391c4bdb0ee385970b74722'
            'ef9741c76d308d32104f7af36320c577ece7634a3829469c6e6b10908a39733a'
            '1d5dce6560388f02d29bb52911393e221b72a95085518b20cf8b3972b5c71379'
            'fd83c90fa1a967f15f8ecc80a29380c2759582765b49ca8151b540d56d557e22'
            '31fd5d6d6ba7f9199783ff22bfb7738650ae040a6d8e8bbcbb7bc986db3a93bf'
            '07e489f7b314f0910af741d54c1f7dfa3f42c2c980314f22c884c31db4777f27')
