# 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}.8
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/"
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-gh-115133-Fix-tests-for-XMLPullParser-with-Expat-2.6.patch
        0150-Update-Lib-test-test_xml_etree.py.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-gh-115133-Fix-tests-for-XMLPullParser-with-Expat-2.6.patch \
  0150-Update-Lib-test-test_xml_etree.py.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=('9e06008c8901924395bc1da303eac567a729ae012baa182ab39269f650383bb3'
            'e54ee66ccebd3ccdaf79bc38beec84f35e4ec668c57deebe8a00be9996773d7a'
            'd5f2a6afbe495331ee0a03053d48ab81a40536827dbebff3d518f8dd935739ef'
            '93cc12a5200a18d667a5e8775b34b60aebf2d18d545ed0eccd2cf32c7b0d16dc'
            '3967105d61f94c995bff16ee5e1dbde979e49d8adcd774f17c85b5dd42324e79'
            '8585334812de73f51298bcd5a8daa00d637cf00b2d03d512a953cd0f9e650772'
            '0a08149feca58801ccb77f654a4c2b133a87e0168d781be8a6e847b786235cb1'
            '9b6c3641aaee1b338ac550d2129b620e850e7cb6740ced8c19a2c67c83f1f03a'
            '151f192c71bdb1436b856dc0bca343ef0145c2ea8fe003cc253510522397c2ef'
            '4f1339e904761692c8fe28246c3b51216eb7bec1e6aaa315a7d12a7a4ec47227'
            '8158f6e9a774e8138f8a0904149093818a2dd0e1337612c09e9e061ab86293bb'
            '974615cf925d8950491b4ad87de77e58db56028229a05b678a80147c836b7c1a'
            '8f0bf2e07d76678cb704440621588c3baf03b8dcd5a0e103a1e5298934d9ce90'
            'd8262f366eaa3f3e071ca4362f7ec24d68b2188837e102d090f6c770d9bbb446'
            'a2b2d813a15ae316fa289982906fee0572a885fbf85c4ef5af01c74f0798f892'
            '5d529906cfccecc9d1683f3f2aeeee181eebfeb54d1a673548ec5f71914790c0'
            '98827c372627e3713d6ae181a6b29d212aa940ec309988fd8e45242b65a00991'
            '10a0957a169b0d250eb799b6d75fa0dea2fc9bdeecdd0dfa6a077a326912c55a'
            '3ca8fbd69041df04988326da15c8f09b3bdae519c0d83b3b384afa5a15cd95bb'
            '908a90862fbb32516bf5c817125bcaff31286721d311f8b030d5a5099b50b5a4'
            '4f660d0085df0b280919d4e0636862cbb77903d00a230810b9ba5c95fc567336'
            'f8dd9bf6c91bae2f3d330a48764ebb0591ff9eaa757b447f1380c973024a2eaa'
            '0e2d5ce6f5eec26e51fc79d760f8e8ab052a59141667ecc63403865ddfd778e5'
            'd09eacfa87b6ffbca4a8885baa3918a55e87010d33f6db54453bc84021ff66e7'
            'cfb6ed031c0124b99563dba0389d1537718fdcc34b86e2a3786aedee2b5485f8'
            'acb59b5977142027eeaf159c1a8225aec13f5fa5fc63d41adee44fb9f5014313'
            '231cf67811e7773939fee289290bd4b3dee07a26c7b728dc004bfea5a878613b'
            '0a6b78279b8abffba3a52d19946ad9eb92f2612d34b4ab53e8829483118620b9'
            '27f82b630549800e794ea10547bebbf40ba144be1f0d14a3ae880c523cd40898'
            '5f8505392f2e1a13082a8850d7b478d72ed640ed355dcd1ff3ae6d87f7ef69ca'
            '6c97ca0c52465b5b5d7732c831c35ce83a276b8c1f51ebd4df7bd56ef4b19221'
            '30ab1f75f688415b2c08e59e5703108c43c81e607df56a59ca42f6b39319fa50'
            '3e2a861c95912cf1b5f864d29c300b825179e0ceafaeb83e36f9c52729216d87'
            '14b37713a75a8499492fb4d26108e06e2959f69f7aff6752df008185f22edb40'
            '81efe3309a70c038fdc1a4dd51ec037ffe3f58e24061850458043095c3cd8f45'
            '334b1fd38f06e76659a3bd804ac173b8a0b020bf8248988e409c4f8a0474f21c'
            '6e9ebe2bbe598206293eb9dfda24e806e378ad3dba58d846d80f741062c607b1'
            '53e50ccee1a8b07e3b49f20c8868c642ee18b6d93fd5f57392aa4fb0a64011bb'
            '6b9c1b79c4e79ace56e6f7884f66c7c5daaaa48c8eb896f9f3a6f2644345421f'
            '4d27b145ca1b5e928979eca139c34b6b4ba9d684c46e05cbbbbbb9dd6861c245'
            'e273e1d61ba02b4a726f39cb9f23e6cbc5917fa900124575fa51a6a59fc19f07'
            '96b5f8f1c160e8cb2c33fc84527948f6a4a7214e957e6959fbd3c90cd1c7aea9'
            '571fa7928664b9f0a5e2f67dd49da9c5684cc3f45d4cded647e73634e965d679'
            '66e9536464818eee5faa90c2a6e109b37b371d424195528e81579b9c1c7966be'
            '04ca8fb1d48cf4180ffa38229b33651dd788fba6ab301ee685bc52bc53b09169'
            '264f93bbf761f5ee9ee95472b1bb600c72b4781564ce7490d351fb13693ce2dc'
            '4871a1bb56f6c5c2bc7c0ed58426c9e5063162e12f7e76f156f28a472af4244e'
            '97ec4e262f248e4ca1508dbecb884c6ff6465bb87f9949f5d4cd7f754c819179'
            '2e2a42f79d98aba3983e3bae3c4b4dde75a8eba7c7e9ea19cb082e0b1dd45414'
            '97f9eaecde5c902a6b335f6e9ac0b5d9730b6e58dcc138ab586daf3a298c41ff'
            '558ee506c7d48f20220352df4273a637e0221837937fed2203c1c459b750a43e'
            'c4c9f5b75d9071a612286ea77606b310dc92e6cc5634b64e2466893f6d79fe3b'
            '9114fbf282bff7afd6403513f28cbfea830bc52ba66108afcd041cb002e4dd7b'
            'e85321fbf636d15eddf1cde7af75b92aa49060e76b395d16056f3ea227ed212f'
            'f8715489f568463b388a30b6d9f6ea0b4bd8fe6a63a5a2ebf522d63217dbe681'
            '744224de152815973caf98f10b08be5746dfc69caccb280425962ec14e1a3b29'
            '8ed6d464a9f17397cd3a0ad9b3291cf14fc10a00c6f9466c98a8c2103a17c445'
            'b7a96ee7ef9c0e5bf6b62a92a1769c104aa29c888e3b2118387a4b007a2c23fb'
            '4d81ee2ecc3f52d8e36d66b6af5b47b491672781192b386bd8c57f12a6f69fb9'
            '18e1a25d5ac473ca6235b8a0bbd2beca2c021b8d42b8bc4570b0772dbc3c70db'
            '72de368a811881c1bc01b0f58f2f99519ff3a525f4a2eac3728e8e99cc81d94f'
            'd03b87b8c0aa087b238682272ca1192b13fd9d24291caf7c2bff0b87e7ed9ed6'
            'e9e7ead539a2eec0d58ee634c0b858128e7069acd364a400038a0e2549cf3be1'
            '071558d4e201d81d942c8313a087ad99d3a783436477bcd604c1eef51e9f96f3'
            'b347bb3a7fa1dcf55f3d4fb21879fb7dc52ebfffc5af6930998edb8f9ca4ad68'
            'f651bb3b294dbb5ffa14291cc0f9e846d412b03b59ebb33b10ce164bed6d9178'
            '3b876e16627c05b6a2bf43613f77c786044bfa169d96b686904c2893228a152a'
            '9c04a28c81b98504ad68d70fc841599d9ac00b07625b59545b4b0e3b67fcec30'
            '391f6c87f38591459da3368a7f1108525ba345676a297020f8386a08ee31254c'
            'd9ec8694459b0950a264aed47ce88a5556d8478ef52d5dd27c0df08ed82e2555'
            'aea514cff4c1ac7dbde3fe47fdb39984d9312358a12a136113b04bbc65eecd99'
            '28cb2cb4e37d9acd9871aface9c5c0f359e6d087f0dfd9e0c8aeb5489f5fca3f'
            'cb4c81cce6efae9ab03cef021be239f4a6cd6a2b1c48a65c260a6a94a8f5ce81'
            '09247d81cef06a4e8efa53e526a95aa5b2dda41186ddf66b87a372794a5e3914'
            '173eeb1f66a64c35810ec28a83b65dc4a00507a0b99ba1d64653a09b23b24502'
            'f31d068efd12cd37bf93594285bfab1a99401fd1649b7e85c8e36bd742e68054'
            'c984996fbb1cb2b8c63d4b746c9b0e5bbf56d83de6e6aa71e645d5376b9d73d9'
            'caa564c1dfead6f64a5af88d5c57422a5f08e7595219bfef84121f3d5ca1171c'
            '04b6f91341463b7745602a9ec2db9b8a8d681ea1176be8d14e7e533491d1bd55'
            '07c53ae9d7392a831b050485a31170533ce42b5b03f5322342dd6f52ffba0347'
            '65de0034305251278f5451738f520ac2bf898de10d65393c1af09cc1b07da344'
            'cc93a782355b2d7b28f9541531ba3b86cf2cc19ac8b93909b13114683f410328'
            '23599c362ec2dd4dc044c05c3dc248b7a01b758da64cd66e7d1897979fd6acd4'
            '6c8671517acd16372b9f56980846a5788e56f4d873e91eddb9dadb94d65f68cc'
            '3ca4fa23047522daf70dae6518dbd38968ca4db9dccccd9fb04d7a1c52afd86b'
            '873cbad01daa7da74fb730ad4f52b15be1b6d4d52c085969dd3c85cb89e5b907'
            '1cd4d67fbd64c65b5ad5e0a8e19dcf216ad7e1adc0a1ce716be34e30450659b3'
            '00c256bb027b5cd0cd4f8535d7beebdd0eb9d162b3f222baed52b3696ea55da4'
            'bd5808f418deb57d4b527998912d436660f40947854934dc99e44adf4ab74abb'
            '9fb4d7088d0e985d4d671c7ad9857e10eaa96c826925814d8e05a069405c96b5'
            'bb58613befe1939969f2b54d3de4f1a03e3cea7a5aacad8e37ef322bb8677269'
            'ea23fe051657780060706e031f3a21b4fe66199cdc64dd7f104dba054a67e85d'
            '7621e62919d61f192d96031a3123c12604826f5e976b72e7dee455841529c7ea'
            'b55ce5af1942421177e670f3f1806208250c24278e4562af0e4af7e6eaefddbf'
            '391be47bb97d8905638f31ead37b074cb0af94bf322618e1dc83502937ac6cb7'
            '03b3eb2f0613fe362500dbec8fba86267a016dc2d841736a7146c7f55383014a'
            '93aa0e673755561f5c285156dd4646ce375880e09f3d80eb9fce6e443f0eb22d'
            'bda5bcb777c7199a9fe5714b1bec1cf30dcc37491581bd2f66a78cb36b6e2e89'
            'c52e5d8cdd2960c5a7412cc902ff03afc6b91af700ae9897b5c81570d7c56c8c'
            '3f112612b18b9593390e8d701f6fc5b9434ffea1c32d914adc16e0c32ae69799'
            '7c0e4a1dbe30f000fcbb10592816e4b5f90cfb3f6753001e6f343597753ec7a6'
            'fc4b0c7cf5ac25915713088cc58acadbb67fb1d6b6ce9f289ff89a524cbf3f45'
            '7871615c26e360b3c0825cdb12acf805aa8d756f8b53c0b5c7aa0deebc23736e'
            '2b0229c148c323a72fe69ed68ec0503226687544fcb0a01674048e48f8a78984'
            '4e01f14e0214a1509e9d785a582fa6cd5766be924e7c772c500fb06235cb9bef'
            '3f0f12c74275f63f2dfc1a31b489ec97b2cce5504416d0906dde33f973cea394'
            'c7c44c661c7e1d23d2624099222d8c57bc3daa2f04d3e397696beb3f5fb07e8d'
            '1d80e960d01e903bcdcea7c65fbedca44595da6c7e878d99052c23fd6bde6a74'
            'e923a4d683f480abab21321c96cb1ae0f94e937af69288c2221244d9e8e6b1ac'
            '65587a2c5f4eea4a925a627e6378b3946a566a4579bbb7786b2003c45d550272'
            'b78e954290757530e429b6edac0936767ef5fd55e0e9aaafd7bda2f38421aef7'
            'afa73bd3fb301cc3f3cf3781cd467c5cf7a8a8c3283ac1d5a62e9273f43db17f'
            '79a86569d4fb80b2eef42b921db6d4c6b535c6cac63285aeff97cb427da82d3a'
            'fa6e6a3d7c0569e13c2eda3c346f0f4919c5049927e1cf54a89ba070bf5339eb'
            '7c6d15bf574f7d5c3b89d3c5688aa1f4c54de76d163fc04eaa695e80833ad760'
            '645cde52641172afb6f7f43634ea9d29f5e7b26525ed9ded609e313a355c1675'
            'f60e015611288dea266bfec491704c0de3d19917bea7e1b4d450013afd952659'
            '9b65b3eb578f60f7f06a53505d9baf1d78aee3f90c088078661c3812232b7ad3'
            'bf6e08f1e484492f13e3e705cd47b0018823102c62f5a4fff2905e126514d47d'
            'f93202ce032c25ce546598dea25a7414136ca15aedf1e5b56c9facabe37c95b5'
            'f79e49965b62b133097436424105e10bf0984093e994dbe8bbe10bf68f4c1306'
            'fc2cb8601f27bf710de977b7328ebfd93d094dd0a9f00cd2c0b8b28c4a8ad40c'
            '19401b74db8d064f4e7d228a190b595477e6b439348b558e8920a22e0641dda7'
            '0e0f9e0ec359a245b121747cb35b9d19b31948d95ad7bc041ce882de0d17d10b'
            '70bbc995cc259e330f6d7394bd0ef49326955f8d6ded4abe2bb1069831cc828d'
            '4d09cd7c9edc8eb665fccb5fe3aadedee283e6554a88460e0c0ca6c3923b7861'
            'e4621247a6a497c59a50e5f78d498d3251a1929bc3acc510787e9d7f4d2fd247'
            'c60976493b3ef1072f2e88de1a4e077aefcf00e75c190b1b6ccf1057f85d08eb'
            '657b5796f3e06bd729232df070dea432b0afe5b8ffd7d389ce44e33b238cad99'
            'a5973996146339c8ab6b6c09f6f3eadc8f4a4f730faa2c1fe3d8b1eabf17e8b9'
            'd7c263d72fe2debe075235c8e478d52df715bafd3f31132fe71b660c488f106a'
            'cf4e0b187f21878a7b5e0a2b991725105b3cd055f7cc3ce6a98a0c04d7d2436a'
            'c0b80935eceb02383f1249609577bf1b59e1b723aa7f34fcd13c2b051b280fbe'
            '1adb94751167dc3c22bf7334ebcea93f366c92b19fe13bc36919a96d485b039e'
            '9d6521e7d6350ae91d33d6a59c72fd49e4dc50d749263fcddb7386028a75649b'
            '638f54053a625ac73b0acf9bec68c5858fc99529140719faab5252702151cbbf'
            '7970593c26efcc477c20dd9943aba2d6e763db49cb4e89edb76d3811a1515e8b'
            '1aafcab8ae44b64ff5da5a6199c26593d29d48ce25560b5402a3c6b55d6144e8'
            'b42283a44a147d778d07b16f1ceed26f69e63b3288166011c2df2c0c235e9ea3'
            '2dd7ae9fed507db583a4ae846bdc59b1885a1ec4b7005055ec807f304273d66f'
            '039aa39bf3fff440d564d84e06a23363d533143c5fb9a9f6f650ce5e652832d6'
            'fe872102356884a3de62fe03cb220053e5be07b58b5b323d37c65bdc5e7a230e'
            'ffda0667488b76ba3607ff903e36f28d75782a5a34a41fe2f6881ba52d52a7a4'
            '96bac9994b0410cd96821bdd1e6d1f43a6dc2e5bd7d754a56f21d77306b14051'
            '31714659f50378d23837a2782aa6d52fb56c55396ee9e5020f37d3b94727f36b'
            'c86ccfa9a8ac4aba98c7af9143481e3471b58625f407079c515a63c04ffd33ac'
            'eb2128d87b036066afb7ca55e7911b0846728abafa3004b282919993064e5ccf'
            'c476377e101d68e86b019d68eb28c121844c054ff5b769dd8bf31c098ee38cc8'
            '0c670066f47ebfbfacb998b8efe310d49bc9461010c1d2a46524e4ba4c517866'
            '77687af07326bdb1e667a9e2b2363f4f85334cad2c7b2aa98da753016747936d'
            'a92f44cd50eb109b8d465d2736c4f0f4f9d7b4cab8c4ce7389c8412bed40eda4')
