# 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}.10
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
        0150-gcc-make-incompatible-pointer-types-as-warning.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 \
  0150-gcc-make-incompatible-pointer-types-as-warning.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=('07a4356e912900e61a15cb0949a06c4a05012e213ecd6b4e84d0f67aabbee372'
            '5b492698c31e44ededdf985d6735b6eeffae5814aed8b6d920209546ad21013a'
            '632998666f07b2d64d72ca07e937b41583d21f3e4dd7d698abb7a64f61bfe8c3'
            '09bf6e2c76960aa807e488ae3a503b114e99a702f37a15a4a1a7afeeb66236b9'
            'cbf769c54271486cf74ef11d38ad2fbe497f2da35e6d96c83394e9fc590ac315'
            '6a427a5022c10e2ff38ef447904cb0f4a6f311c2fced4b907ca51bbe3b6ef7b7'
            'a033782eea36698ae205b66b06d2ae4814f28237174224a2a0a05908a17ca4db'
            '1c830d762a718ef9d73d6582de2528c0013c3708468857a81a04b66d16e6f9d3'
            '051c2368c0249a896a04d1bccef0f35ff84f42414de7a18bd345672c1085585d'
            '4ab47dd88496e49c2b021695a4d68274a29b1bea953ce83966c873a834bbed05'
            '34ddad159a079459f96f481379a1b1e9b80856eb0e8e8d388d9909cae27a8808'
            '966f6e84970e80a154f0ada8dce1f726c58403b10733691282fa3df4705e6987'
            '47b3598b9b5def057bfb35b6de2ad386dd3af483a7aac2d56e858cb7f48a1c22'
            'b12a69e9463994005a0b0221e16d9c76f6e8e99b098faf7db2b449fb8a09a633'
            'e6b37e958b02e2de5e9f261f3c68c6b15ff18c91aae7b681211548c737b972a7'
            'e9255b92d44dd06cf88055f5b5924025c070e450d08bf86f8d455946353901a0'
            '7ffd3a6b9fbe669d77281a15b2ef57b895a40eeffc0367d6fa37dbdfc51c228f'
            'e3fd2476af570ebcab5c9d6f7da3bd45d86e3c761a6b6bc8a166d8d5934526ef'
            '36bac8a0f0c3e533fd436ff95f5e016b730ad51b2c74f064205f6fe1926bc88e'
            '4a511a9a3fc483c9be6cee3f695fd647071ce69b014794b227414e95b68fe84e'
            '08a1730f3f35f0302706753ba6e93d64db1c4f73092c3f67e5304e4a91e0566e'
            '322dc4a9408eb85ac356eec62fea7d4a1d2e3d1d5bbbdd23741f0e3cf85daae3'
            '03fe293574ea36db79b3a4a9915d064a91a2ab444f00c48ab4d04d703659329f'
            'e69dd4ef5c412d8ad47a7ba95bdc68deae3c89757e3ad9c90568b84034b902a1'
            'eb22d23fdcbd8b914ff2ccba00eebd14b70f3256c745ab9040c7d8049025e595'
            '045fd3c89fb8ffd57f19b7b09bdf1e3e657da776af9b18e9be25cae3866bd677'
            'ab2ddf7524430cff83cb45328465c047b58737e8c5622151c037fcc983a6daf9'
            'a65d3e00baeba6091629b478390dd5be971eb15b9de0cf9c891e6594dac96638'
            'aa5b2807f69a1c7807ddd61da5d5f749c1ba97b978ceeb4d97a9ab84395d0f3e'
            'e1578395715dae2f33efea7c3a6dcf0e94cc3460dddc0a72d8e525b894911e17'
            '9cad1dd9b728d3a0730f6bae803a5f18a38a2e93a6bbe6c8d72c7427b2988b29'
            '1bd8bb5db435de71040b6b018273a6609dd3859fb5f3b0b21f32782cc548def0'
            '2a9a678548ce8d1d164e373f7f593dc93aec72726de3582da6e003947ea3b064'
            'fd240dad2e3bcc8bbaa8178909c54e3c889d7d620a3bff3a07595bb39f95cae7'
            '1ff82c1663c2df637e68f5a4c9dfb76e7dd47b4ee8da4b9ba9beec08761d853f'
            'ad7bb6db642da5195bfa5cd0c3a425b1c7b0e409f1fb05ba6a05b37ceefce8a2'
            'e81d4cc7764601cc1de09b074d99584f8e90ab5baf9cdb6697d1c5b5f828f7f2'
            'ef463abf5b9b2cdca7e30089d4981c1446a169cdce80ddb41c099cef58cd2f4f'
            'c8f764af3f077c20bb44d4c21fad09a2f904577136f75c8b1778ddfd5c19e437'
            'e225ebccc89bf349f9644189be1faca6330f1e336fdbf323df6d243985b2a39f'
            'dc535da87496c5b9ebee0f7a474d1b81194f3bca4933414718dcd428accc6b72'
            '7926b05cc75a1f6c231c2c9e817b12643d14e0c6e63bf19292ff679bc59f7c97'
            '08180c858fcee306df21763e5c56f7a264373d287b77d2b63c3e36700427b44b'
            'cee794f9c34457d6defa1f1c83c00ba47a7f6a54eff4f34769e1744e30d3675d'
            'b4d07968249b231ad8f2be70033d47540848dde4575c8ed6fb093f9e514b5dd4'
            'a24c7c9eda8d8484de3c5e2f116844d4ce2d9e69549d2e52a429241fb1341015'
            'bb0215d46749f2c61fd8aea01625584c67c21143c0f6321c4311bc899ebe2e9b'
            '3566ee076fa520527bfe680b8e1653377b272863741bbcc702b62c7df094c13c'
            '6df4752cf51509121c5453470322d96d13068575fbddb542ba15ef14d65936c0'
            'ad9abe1fbbf209fb34f51b297756c4058f5533a3d850a610561980f37f4b0540'
            '761a3e6b8094b5225232764283045a2408c7852f8821b29268701b53028261f8'
            '46dee7f78a646e08bb4c2b189312755c7a56b1f7cadd67c940c1a9275d59a68d'
            '2a58f073263cdb6e1849bb06ba176829b8f74777488438c72f6ff0721077c6e3'
            'ac45578b0585e8ae0519548e5c906ce01fcbd1592244590a2d461f80b3f20de2'
            'cb7221eb0455437e0cd1fdb0e29486f780fe68034fce9c6ee172342826dc2424'
            '1ff64b704f5de302fa018bcbe6fe591f9094f9c9d8a383c17716c8daa45630bf'
            'a9fd1b0293975f2787b0596534324064abfbc65edb0c609c8cf9d79e2dd0f75b'
            'd9a5060e4365485689d5e45cbd9dafd995256a0e2ce4782b2743a84e57556125'
            '53ac0529f9b3e8332bd6be10db64ae2f42eedb03c55e54fd58a0f2bfb8c8eeab'
            '1170cccfdb93e1e73cca74c023f757ae826c862996e23c067d08f4e6c7e925a1'
            'd7d9870fa1ac80058cbf42816adede28a1d4646c7ba8806e20591d9bbf8e955f'
            '7ec5ccee806bc39beea93730bea1f947b12b6ff3f54354e557e76d13128d0e30'
            'b9fb6b13563ede55a35861d660fd1570e756513c51f8cd1fa36fff80c1e0b0b9'
            'a06a9c45e76f845d612229e225d62fe83aa4cedb6603a86a70e8012e9ee43801'
            '99921ae24ea43c18059c086845bbfaf8d14c31a7252151d5245f55d26bc75300'
            '7a90f7315be06ebb0588b8820c09ae8459af7e02db79eff3d0b537fe477c33bb'
            '6364572f22e554e0810aa09ec5578e4738dd2a040f61a741bea3ffca1781d171'
            'd97e29453a213244a2b5f9cfb4b57632f15d9b9bebed039acb641a158b6140a5'
            'e0e1de1a02c0c79395cc285c3955d74c7b4a363c5325bff3e2f1621d1c4c3a31'
            'a670f836fdca58ea66ca37c7dc49e340313a1bf409136e82ca940fdad25e9800'
            'f4869ac165a3a6865337b41031e2e4ccdfc3dc74aaf25734cd60e5cc6dad3d7c'
            '5ddb6bcfb70d068c2fa93ed173b6b09bf085b7af9ac681e64c5da0970722b2af'
            '1b5e64c419a2e5e47b4978c4c79659b5e5353b2d23f8bf60a2f35eae4662f3c5'
            '2468626d997676f02702048bbde78bdeeea6e0dc64c92702aa63ac56fda80cbf'
            '08d83a76a2edc3a1fc3606083f36111e9e21f7d1c2f25303d66db395119e2a58'
            '6caee3a361106c9f481e33a7ee8385aec09c644e2f4bebdf31c8abbd2b4896ae'
            'fc256b03346324b6c372f3c9176c51b44f04dfe724d5e2e93b58b70628ff85bd'
            '4030c5f19043fd444ee1df9211eca11c165455d59c08fcb4dd77070fc61e52b7'
            'f294745f1a46400c5186155d2c8a1827dbda246be2c7bee7df8cb3dc9d126a53'
            '2e3d238dfe457a0fbb01a3954d9d4aa6db12b5aa08bd4352170c5b26969374bb'
            '22fe586a048ddbbabaa9d5d33c6d11908f33d8e31b10e70a590b52337d52b55d'
            'be5d9c3ebbfed7659ffd24e4e7ece8881382bca0f61f98f73fe80f347bfd0d8a'
            '4ca38de7bdd4047837ba2f7ec68e36f6730c9590803054354fa19886c8ae61b5'
            'a9eb37ccc6abc306f622ff6856b6d5563bb0acfa2472f7a3e1586bf19d68aae0'
            'cededc40d499405377cef1a9bc0fe3bd21e893165371ccafd4f282f7351f2d13'
            'd3d436d5fa7165aebc6a3482803e886b0f2c86895ca59f13cc36532cd0744ffb'
            'c8c3c14fdbdf5ea6c3d986515f152253dca955a4add792aaa680b2adc9266e8b'
            '381912473eda31bcf8c2d335accdc830901ab9389fb3246ea27464bf8f13da02'
            '943241de4193ed1b1845cf5b65c45ac2e589f4c432a3c06d968a1f9527bc0b44'
            '127ab06b680894551e7f96a0ab0e915771b87aa534835babec92bbc912e1160f'
            'dc2c7e71c416a4c8271831e60a4bf36be10f3d3fe9484bfe81aca5aaea23b007'
            'edf0635403de286faaa59704d6534d4501ee0ee95480337a339cc231884661f5'
            '36fe1c5e8fcb702c374ecdcb803448626b851337b8d3d9624c77b485ec25bd53'
            '4fef9f1116aa9b62d4bf852b00b96d4d193c51021a45e1d4f72aee4f50fa27b8'
            '26fbc02a6114d15cb359c8a385e9d6d1f344bda93aab98dc0e466dea8b512408'
            '2c99a7d44954e9d96fe03bc1c893e675a6bd28ebe8594a42ee6d8f03c02786f9'
            '40074acb98bc7a749839fbf6928279a3fc19e6e10b5d8316114074edc32abc1c'
            'b7df1def4ecefcedd7e7e396d9d74b55f00d5a25741a64dcffe8d0543725c88d'
            'da6059642b015045043daa08f68c1a5f63c2a54ebe042ecb940c8d23e13764e7'
            'c58259f32673dc9146d8215a878be57f395cae7214e50d16cc72c1766e82293d'
            '4fd410da4d525245c0ab9b570c634fcf2e88822bf8c957d897d1d2d5d8fe2295'
            '8a22b629d415a20de5409d7ea1b8f91c7ec9dc841e2abd221cdb2a82ab5b022c'
            '6e73f4a3b64a27c70112de05d75dc9007ce7cf8d23bd24d715b43414e59c58d9'
            '0b1760d997e7129e60c569c0f3f0b8f3a5fe78539316ee8547e49a33eac8ca68'
            'fcb6042ad444266ede2aa854ab5f270d36ef988a0eb099c08508b3c518e34cdf'
            '181b1aae41147d1b0eee5b8f17e8818c5a96b8c208d9e969957480e2695aa930'
            '3261b49b9f9a7529758fda82503ca3983ef5c25c03818942b0e5413416ca0f47'
            'ffcde918ae6707f5ec8d8e1f24b31cbd72d1262a0f2382d1bf41821e1957bdfb'
            '2d56e7a14f391b6919a743f5a57ee4fe0c75b89b3e146d95de2a19b1cde173dc'
            '48d84a4af668b549acbb5009ea2afb2c62f4a3814ade9b0c58ebdf871f58e88c'
            'a739d7f841e4009da2cbfe7a29d785ce98a5a63e69821988c6d2718a031c4d9c'
            '6f63dc21f16e1d79ee960c42fcea09a7a78c1041b5330e7533ed1da55e0bbae4'
            '2cd794527bb71ec8d7c3efc1975e0881d52b81370cd4ec5db4efbaafc75bc562'
            '926b22898b5225714341ca76ada017988f3f65772372f0cbe1bd2adeb6818bbf'
            'a8349085b9cbe023d3696f52d73481af2ca947aedfb293775646cb290465e6f5'
            'a92c804ec2b49e2004a6cd45a0771115496319f82db77c7d6cc54e7d19ef268e'
            '94887074549d7bc9dcad332fc3306093e7eb15c4bf74ab60dd18c149c083dc38'
            '0837c057a33202449c67bce47863e8996525cdedeaf7a1f55e77ff7e9f2a51b2'
            '0d0f2b5579862b0927c6246eae3028f65d0bb02d4e60e31e4616422c659e4710'
            '357c9e4df153d39acbfba7e280656c067a28011c1cd21a9263ce237015b6af91'
            '4d5d2c67cfd5c96b0eb72847f37c4ab2a14383b9e71a857a9a950923311c2a14'
            '8b1ee7cfabe079407cbab763ac286a57e3e2234cc0e1bd23f4e7a83b0e905fca'
            '29a479ce24cd17b547f1c14fa79f1d86abdfdaade465c81c1fb907824f576b44'
            'e2b20c7826f7195a59819aa1bda09182354d9b71437781cc4710e70731a7b6df'
            '90352bb965834bfaa14c61045cfc55998fa573c40dddf5fe24c202a81948c04e'
            '7505e5a242bdbe08007b2e6e9cb838acb8c845f3d062179908b8455317661920'
            'ef0ac4fd5b027db2b826460168992f484247f6db7c87aad2feb104c83c4dab1e'
            '4ae6e6ddedb427ca0246d87453c6b2a23aaee9865f971a32fbb8edc31ab4654f'
            '2e7d3c5a18783e4549bfa9331ab02c1b11c7b8f5073f2bc742a133194f93ed7f'
            '4cd27188c1b8c44eb684a1a60940442c1bbbe79e3507da9af68c233cfcff1f33'
            'c8ceef3a86741534255cb1402514a64324e1cbb28c11493b4185ae015b171c40'
            '1b1b140e52a2410df38fe6004bb8e25f1dc8208f4dd87b505cbc14022dd93a15'
            '038e002154e2b9731b53bc7f805ec7797ca53e172965715631008cbf890879e6'
            '7487e9a124db5421a38abfe76805c3d875b6199fd0746e93c654a3e075c35fad'
            '861a6bd305ef66d27d46891d9d896ea5967650eabf143dffad8e979c785e84b5'
            '400594f70ad763ecffda4958eab98858fb4644762510d4f7084f80e5958f2f78'
            'c03f0ad97586bd7d94e04f09d7771c7a9d85c5f187128967dd59c71dd685fe8d'
            '2815034976f60369ea4e4c71c4c65cc82d9bd7161ce85abe06b5c104ebbc1a99'
            '9562698ea655c350196632790f3140d7b81ceabc63b72e422399e1bf94ab38ee'
            '2a18e480827be168b6aa089388be939675ada730ab70b3efb410fe9a00b9e3fa'
            '05699337a8dd54c55b5fef3b9e69bbfc26e728f7ec08a9b5881278bdec77acd0'
            'ff868345167c8e6c776c97e3893ba0fa44dbe38bad21c7c9cf80420d0b89d228'
            'b031a3e22c1bfee8c3a458d11032b9cb5f722f8e957a319246e2417dc188b4e3'
            'f938b2d3eba58d8b6aba5a8bbf1328836a828de0555db06c973cd838d5e02bf9'
            'f4c102f5b845fcfe44205a904ad7e0e8ba712d9bcc745dbef30bc6b0213a954c'
            '7691666414aefcc1d02430200bf59f3f31d407605a75f3e68c589e8db4e4b6e5'
            '1ba3abb5c06bc634d3dc57407bc6d01ec5e8fcd921493d3c342a7369d3faac59'
            'ef8c2f49ffd860f5818c441f9600367ff56988b7889f11d1a338b067ddb99fdf'
            '63124e5c0ff91681181cfca705ed735e797494a192d5324cd01501d93c769101'
            '0bd5ba7e65ae2ccaa2a8e6de7d12ce2964d2439ef973e9f0cf26e3bd7945ffa6'
            'b9d8983562a280536c1e98b057910b113875f26cea42d9ba2af0b00469e92858')
