# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

pkgbase=msys2-runtime-3.3
if [ "$CARCH" == "i686" ]; then
  pkgname=('msys2-runtime' 'msys2-runtime-devel')
else
  pkgname=('msys2-runtime-3.3' 'msys2-runtime-3.3-devel')
fi
pkgver=3.3.6
pkgrel=16
pkgdesc="Cygwin POSIX emulation engine"
arch=('i686' 'x86_64')
url="https://www.cygwin.com/"
msys2_references=(
  'cygwin: cygwin'
  "cpe: cpe:/a:cygwin:cygwin"
)
license=('GPL')
makedepends=('cocom'
             'git'
             'perl'
             'gcc'
             'mingw-w64-cross-crt'
             'mingw-w64-cross-gcc'
             'mingw-w64-cross-zlib'
             'zlib-devel'
             'gettext-devel'
             'libiconv-devel'
             'autotools'
             'xmlto'
             'docbook-xsl')
# re zipman: https://github.com/msys2/MSYS2-packages/pull/2687#issuecomment-965714874
options=('!zipman')
source=('msys2-runtime'::git://sourceware.org/git/newlib-cygwin.git#tag=cygwin-${pkgver//./_}-release
        0001-Add-MSYS2-triplet.patch
        0002-Fix-msys-library-name-in-import-libraries.patch
        0003-Rename-dll-from-cygwin-to-msys.patch
        0004-Add-functionality-for-converting-UNIX-paths-in-argum.patch
        0005-Add-functionality-for-changing-OS-name-via-MSYSTEM-e.patch
        0006-Move-root-to-usr.-Change-sorting-mount-points.-By-de.patch
        0007-Instead-of-creating-Cygwin-symlinks-use-deep-copy-by.patch
        0008-Automatically-rewrite-TERM-msys-to-TERM-cygwin-With-.patch
        0009-Do-not-convert-environment-for-strace.patch
        0010-Special-case-for-converting-root-directory-to-have-t.patch
        0011-dcrt0.cc-Untangle-allow_glob-from-winshell.patch
        0012-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch
        0013-strace.cc-Don-t-set-MSYS-noglob.patch
        0014-Add-debugging-for-build_argv.patch
        0015-Add-debugging-for-strace-make_command_line.patch
        0016-environ.cc-New-facility-environment-variable-MSYS2_E.patch
        0017-Fix-native-symbolic-link-spawn-passing-wrong-arg0.patch
        0018-QueryUnbiasedInterruptTime-must-be-load-from-kernel3.patch
        0019-strace-quiet-be-really-quiet.patch
        0020-Default-to-disable_pcon.patch
        0021-Introduce-the-enable_pcon-value-for-MSYS.patch
        0022-popen-call-usr-bin-sh-instead-of-bin-sh.patch
        0023-Expose-full-command-lines-to-other-Win32-processes-b.patch
        0024-Do-not-show-Error-dialogs-by-default.patch
        0025-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch
        0026-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch
        0027-kill-kill-Win32-processes-more-gently.patch
        0028-Cygwin-make-option-for-native-inner-link-handling.patch
        0029-docs-skip-building-texinfo-and-PDF-files.patch
        0030-install-libs-depend-on-the-toollibs.patch
        0031-POSIX-ify-the-SHELL-variable.patch
        0032-Handle-ORIGINAL_PATH-just-like-PATH.patch
        0033-uname-allow-setting-the-system-name-to-CYGWIN.patch
        0034-fixup-Revert-Default-to-disable_pcon.patch
        0035-Pass-environment-variables-with-empty-values.patch
        0036-Optionally-disallow-empty-environment-values-again.patch
        0037-build_env-respect-the-MSYS-environment-variable.patch
        0038-Cygwin-pty-Fix-Bad-address-error-when-running-cmd.ex.patch
        0039-When-converting-to-a-Unix-path-avoid-double-trailing.patch
        0040-amend-Special-case-for-converting-root-directory-to-.patch
        0041-msys2_path_conv-pass-PC_NOFULL-to-path_conv.patch
        0042-Cygwin-Implicitly-support-the-dev-fd-symlink-and-fri.patch
        0043-dumper-avoid-linker-problem-when-libbfd-depends-on-l.patch
        0044-Stop-assuming-that-there-are-no-spaces-in-POSIX-styl.patch
        0045-Cygwin-Fix-compiling-with-w32api-headers-v11.0.0.patch
        0046-Cygwin-console-Fix-hangup-of-less-on-quit-after-the-.patch
        0047-Adjust-CWD-magic-to-accommodate-for-the-latest-Windo.patch
        0048-fixup-Add-functionality-for-converting-UNIX-paths-in.patch
        0049-fixup-Add-functionality-for-converting-UNIX-paths-in.patch
        0050-nlsfuncs-work-around-an-overzealous-GCC-warning.patch
        0051-Move-_cygheap_start-into-.cygheap-section.patch
        0052-Cygwin-dumper-also-link-with-libzstd-as-libbfd-may-r.patch
        0053-Cygwin-configure-Add-option-to-disable-building-dump.patch
        0054-Work-around-fragile-include-in-binutils.patch
        0055-Cygwin-find_fast_cwd-don-t-run-assembler-checking-co.patch
        0056-cygthread-suspend-thread-before-terminating.patch
        0057-fixup-cygthread-suspend-thread-before-terminating.patch
        0058-fixup-cygthread-suspend-thread-before-terminating.patch
        0059-Cygwin-revert-use-of-CancelSyncronousIo-on-wait_thre.patch
        0060-Cygwin-cache-IsWow64Process2-host-arch-in-wincap.patch
        0061-Cygwin-uname-add-host-machine-tag-to-sysname.patch
        0062-Cygwin-Fix-compatibility-with-w32api-headers-v13.patch)
sha256sums=('1495f90ffd49a3ef4835a879881fa5dbb28dc6aa3ee55be3c4327bbab471e9b8'
            'c375315e58181ee5589b7966101aa095de3f864a579c3c3f0f0683595d4e428d'
            '01ea2b131cf5a3b27fdbc458019eac14e45a36782ce3ce33e62328eefcd2d02e'
            '475ddea4d86605ab097f98ec764951a9d647ea3100dea0e1620f57044ea4a114'
            '6775247d7a213268bbb2315f764ebc9faf9967b70a95dbc6e9de53fe04142433'
            '91e2a72a980aeefadc215cd36147c56666679bbcd68c60120278b6270454b710'
            '54cd198f3ce3931884220c8938bd0f348e0889c20b36d110ded9302ec7364c34'
            '31a4bcb985e03c45f16644a1604ebc649dc50586968050f8819035b84f80bcf3'
            'e9cfe4d79ac294e19e648f51a8bb491231204d8eb9c6a2e998a7cc2506ca47ea'
            '816e98ee5ad15d3cfb75598e2e20e11272ac6867a7bd9b0a3fea0ac0ba4daf69'
            'e895d76936f4668bb7b67581ef642e845968dfb86e21d654dde98727f0ebabb2'
            'f416f109d323618ff3d7240ea7845d5ab3221a8a6c6074e36a34e62fbaabdbfd'
            'e710a9eed8bd767d97acb2d2cdc56eef880e02b20f1b14fdf462678954e3a896'
            'ce72bc18fcf101d14c0623c1d459b141ba883934939c70d198d69932ec4873c7'
            '5f5850cd725c4df6ce6cc86d07da1ed696e119d8bb80602380c4941aa10d0784'
            '8df334362506dbe9de56017c0df1d3459b5c6e29860b73a5e9ebd0421b2f3a6a'
            '988a2d365302c73ab482bd1f0d4edea8e687de7fdf068e5bec3aafaf33d63a58'
            'f9aa893686405b8b92b3d585b7a7dff35f9345a64a282f75d8962e92f86bea25'
            'b3073a6b2acf836f5b3286ff8be66633ed4f93280d50186635a3fcfb4cdc3745'
            '52cd166461fc2cc3c880c174160516a93d456dad44f382aed518aa9bfa36303f'
            'cee973f136152e6a5cd987bba85937584c9459aff1c2777134a99526b2d5512d'
            '221dacc354d4f27443665bfe32b114235ef7305c55c9189c77f636c46522ee80'
            'bd985989895e51688072d7751976ecb83720c8e0786126d7025ba355b64f6edc'
            '19cf0b89327e148763d19608cffbbc29fd4309048a65ede9dff808eea4858727'
            '20d35886e3bf1c2f4fa7f3996739d674fd0eb26be357268219c522c35187ec3e'
            'ff01c5404f80dd4d013316930f26063877f561c8698a1223269cf06f0df56d16'
            '6c249925a88679544572fb4810119e008bb47ae2412cd261b02c786ee1c93dc4'
            '3fd1c68bc249d52bb464390f2f0c5f1401882cc179f2b6f4adfb4e4b496804d9'
            'c7ab2e59d4ff4016dd3336bf34f79a71a28ba892e6b6a005b618519071496db8'
            '8a2b9b9ae1d376d2868967cb64a804d1ee2f6075b9a9b2747570e3da2c816d1e'
            '00096872abc028236ebec61fba79483159b88b4f8fe7217026d9371a69dd7d0f'
            '611f6d4835e9c1525448dc85bb62fced7375fc13d42379d448d8e15ada0f4f4a'
            'a2eb84544c7e4ccd73d2388a80d04d4fa9e4bc26d77815d134f56ab44c4589e5'
            'a84b205133f13ed41b154198c0ea60875bddb523711e77a870954b17eb2636de'
            '1681880728535c3057ebe0326031a68cac077bbef4fbf15f50aa0a29b3d965cf'
            'd8418588d9bf4c3bbf6d354e2fb126acebc6351aecf74b1f762b5c4ea22b6e46'
            'efced6714a787dd2f482ac9e96d7c72bed2d794642b32b1bf7d6e2d614a9d8fb'
            'dfd1e3ea9638665a9b4c851da8d43d4d5555050e5acbc2be99705bfec46653d1'
            '675dc4d1aa6a7c10e67aa33e5886d3cb2c32afe509b58b94bd170f61798a1d27'
            'b62d3609d57c18e9ded3d538326d9637ce1fe09188ed9d319145addb853274fd'
            'c9867ef9adea4146e5da4458bdd41ea911692ea14543c06623ed44ede63f1e81'
            'a703c6e4cd69df18e39c9043632523809b33ac9ed32c9724cdc72d20a06b25bb'
            'bd6db82cfa9c5be1ce990fb2af002f46c0991478bb0b829011ccb55341112450'
            '66837166b7094d8ec61d60fca43b6b852907de71aaf3593cbd024e4a6fe750a7'
            'a500aa443bb23c3b8cbb3dc8439e36249246894e031bca855dbde32add55a187'
            'ce0bb0bb681de79411b86c8cb1230583ad0af101a341739f1e0d1d72a9af5672'
            '9581f3b95c9fcaf6f9d47e98e801be778e05124088e61afa4434b6fbdaf9c185'
            '43b9f4304956b92ae62489e37d26af5ebb05e51e39c3254d7750cc6a29baccb7'
            '40d9cb8d0c492bf54532b6b2aae559970cb2aa9159a4be58161b1e682d51e3b4'
            '1a716d94cc8c65d7774d7f2d2d666d8ed4e694ad9c6d23479bf3efb2fa176362'
            'a799ad2c2225e917df782ef59be4a7ed6f4a9aaafea1ff3ba58721229fce6eff'
            'df54b846ad2a0583a7d52aef4ed8bb4c4183fdd9e99108b8c15a960e677daca0'
            '96d51768fd4c1814b0ba4c104b4b0fcfe560ab9a2402fd0939098b31df8000a4'
            'c27c0267d45a85e14dc970b29e09c417ae44700f867b1af017debb91b685421f'
            'd5cc90aa4cba718892439f7c67276c77c718ecfb5be691b7438ce55e16557270'
            '2d5a808bfa9b0c5c2e055552829d960aa3e6197592b8c51e5558de9de51ed87f'
            '67a060066f26446f21e08c8fed4fc855529b6e786f0789c9b5385881026d3fd0'
            'f4e2a1595e7fe9bdceb17f7e909d6eb51b7a8d3f1a2d0b2ad938e2127308d9b4'
            'df1d1cdcc2349c88fef991ab0f520c0873da51c347ec64ded17b4f688512fddc'
            '8eaf698b404506e45a3651ea0cecba78680e52da291613a8c6683b6b1f1043da'
            '63edc5575d19ce7566d9b726442dafd34f58be423f667325e5b7cc808647bbc9'
            '1735df76f61bac7d075407a0f3ecf8d4fffce7b6f7a42136ccaf34a534a7ab0c'
            '261f4791766db9d8d2ceec7a310f91f009e24a5c604e402b963e1b0a5555f951')

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

apply_git_am_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    git apply "${srcdir}/${_patch}"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}"/msys2-runtime
  if test true != "$(git config core.symlinks)"
  then
    git config core.symlinks true &&
    /usr/bin/git reset --hard
  fi
  del_file_exists winsup/cygwin/msys2_path_conv.cc \
    winsup/cygwin/msys2_path_conv.h
 apply_git_am_with_msg 0001-Add-MSYS2-triplet.patch \
  0002-Fix-msys-library-name-in-import-libraries.patch \
  0003-Rename-dll-from-cygwin-to-msys.patch \
  0004-Add-functionality-for-converting-UNIX-paths-in-argum.patch \
  0005-Add-functionality-for-changing-OS-name-via-MSYSTEM-e.patch \
  0006-Move-root-to-usr.-Change-sorting-mount-points.-By-de.patch \
  0007-Instead-of-creating-Cygwin-symlinks-use-deep-copy-by.patch \
  0008-Automatically-rewrite-TERM-msys-to-TERM-cygwin-With-.patch \
  0009-Do-not-convert-environment-for-strace.patch \
  0010-Special-case-for-converting-root-directory-to-have-t.patch \
  0011-dcrt0.cc-Untangle-allow_glob-from-winshell.patch \
  0012-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch \
  0013-strace.cc-Don-t-set-MSYS-noglob.patch \
  0014-Add-debugging-for-build_argv.patch \
  0015-Add-debugging-for-strace-make_command_line.patch \
  0016-environ.cc-New-facility-environment-variable-MSYS2_E.patch \
  0017-Fix-native-symbolic-link-spawn-passing-wrong-arg0.patch \
  0018-QueryUnbiasedInterruptTime-must-be-load-from-kernel3.patch \
  0019-strace-quiet-be-really-quiet.patch \
  0020-Default-to-disable_pcon.patch \
  0021-Introduce-the-enable_pcon-value-for-MSYS.patch \
  0022-popen-call-usr-bin-sh-instead-of-bin-sh.patch \
  0023-Expose-full-command-lines-to-other-Win32-processes-b.patch \
  0024-Do-not-show-Error-dialogs-by-default.patch \
  0025-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch \
  0026-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch \
  0027-kill-kill-Win32-processes-more-gently.patch \
  0028-Cygwin-make-option-for-native-inner-link-handling.patch \
  0029-docs-skip-building-texinfo-and-PDF-files.patch \
  0030-install-libs-depend-on-the-toollibs.patch \
  0031-POSIX-ify-the-SHELL-variable.patch \
  0032-Handle-ORIGINAL_PATH-just-like-PATH.patch \
  0033-uname-allow-setting-the-system-name-to-CYGWIN.patch \
  0034-fixup-Revert-Default-to-disable_pcon.patch \
  0035-Pass-environment-variables-with-empty-values.patch \
  0036-Optionally-disallow-empty-environment-values-again.patch \
  0037-build_env-respect-the-MSYS-environment-variable.patch \
  0038-Cygwin-pty-Fix-Bad-address-error-when-running-cmd.ex.patch \
  0039-When-converting-to-a-Unix-path-avoid-double-trailing.patch \
  0040-amend-Special-case-for-converting-root-directory-to-.patch \
  0041-msys2_path_conv-pass-PC_NOFULL-to-path_conv.patch \
  0042-Cygwin-Implicitly-support-the-dev-fd-symlink-and-fri.patch \
  0043-dumper-avoid-linker-problem-when-libbfd-depends-on-l.patch \
  0044-Stop-assuming-that-there-are-no-spaces-in-POSIX-styl.patch \
  0045-Cygwin-Fix-compiling-with-w32api-headers-v11.0.0.patch \
  0046-Cygwin-console-Fix-hangup-of-less-on-quit-after-the-.patch \
  0047-Adjust-CWD-magic-to-accommodate-for-the-latest-Windo.patch \
  0048-fixup-Add-functionality-for-converting-UNIX-paths-in.patch \
  0049-fixup-Add-functionality-for-converting-UNIX-paths-in.patch \
  0050-nlsfuncs-work-around-an-overzealous-GCC-warning.patch \
  0051-Move-_cygheap_start-into-.cygheap-section.patch \
  0052-Cygwin-dumper-also-link-with-libzstd-as-libbfd-may-r.patch \
  0053-Cygwin-configure-Add-option-to-disable-building-dump.patch \
  0054-Work-around-fragile-include-in-binutils.patch \
  0055-Cygwin-find_fast_cwd-don-t-run-assembler-checking-co.patch \
  0056-cygthread-suspend-thread-before-terminating.patch \
  0057-fixup-cygthread-suspend-thread-before-terminating.patch \
  0058-fixup-cygthread-suspend-thread-before-terminating.patch \
  0059-Cygwin-revert-use-of-CancelSyncronousIo-on-wait_thre.patch \
  0060-Cygwin-cache-IsWow64Process2-host-arch-in-wincap.patch \
  0061-Cygwin-uname-add-host-machine-tag-to-sysname.patch \
  0062-Cygwin-Fix-compatibility-with-w32api-headers-v13.patch
}

build() {
  [[ -d "${srcdir}"/build-${CHOST} ]] && rm -rf "${srcdir}"/build-${CHOST}
  mkdir -p "${srcdir}"/build-${CHOST} && cd "${srcdir}"/build-${CHOST}

  # Gives more verbose compile output when debugging.
  local -a extra_config
  if check_option "debug" "y"; then
    export CCWRAP_VERBOSE=1
    OPTIM="-O0"
    extra_config+=(--enable-debugging)
  else
    OPTIM="-O2"
  fi

  CFLAGS="$OPTIM -pipe -ggdb -Wno-error=deprecated -Wno-error=stringop-truncation -Wno-error=missing-attributes -Wno-error=maybe-uninitialized -Wno-error=format-overflow" #-Wno-error=class-memaccess
  CXXFLAGS="$OPTIM -pipe -ggdb -Wno-error=deprecated -Wno-error=stringop-truncation -Wno-error=missing-attributes -Wno-error=maybe-uninitialized -Wno-error=format-overflow -Wno-error=overloaded-virtual -Wno-narrowing" #-Wno-error=class-memaccess

  (cd "${srcdir}/msys2-runtime/winsup" && ./autogen.sh)

  "${srcdir}"/msys2-runtime/configure \
    --prefix=/usr \
    --build=${CHOST} \
    --sysconfdir=/etc \
    "${extra_config[@]}"
  LC_ALL=C make
  LC_ALL=C make -j1 DESTDIR="${srcdir}"/dest install

  rm -rf "${srcdir}"/dest/etc
}

_package_msys2-runtime() {
  mkdir -p "${pkgdir}"/usr
  cp -rf "${srcdir}"/dest/usr/bin "${pkgdir}"/usr/
  cp -rf "${srcdir}"/dest/usr/libexec "${pkgdir}"/usr/
  rm -f "${pkgdir}"/usr/bin/msys-2.0.dbg
  rm -f "${pkgdir}"/usr/bin/cyglsa-config
  rm -f "${pkgdir}"/usr/bin/cyglsa.dll
  rm -f "${pkgdir}"/usr/bin/cyglsa64.dll
  rm -f "${pkgdir}"/usr/bin/cygserver-config
  cp -rf "${srcdir}"/dest/usr/share "${pkgdir}"/usr/
}

package_msys2-runtime-3.3() {
  pkgdesc="Posix emulation engine for Windows"
  options=('!strip')
  provides=("msys2-runtime=${pkgver}")
  conflicts=('catgets' 'libcatgets' 'msys2-runtime' 'msys2-runtime-3.5')
  replaces=('catgets' 'libcatgets')

  _package_msys2-runtime
}

package_msys2-runtime() {
  pkgdesc="Posix emulation engine for Windows"
  options=('!strip')
  conflicts=('catgets' 'libcatgets' 'msys2-runtime-3.3')
  replaces=('catgets' 'libcatgets' 'msys2-runtime-3.3')

  _package_msys2-runtime
}

_package_msys2-runtime-devel() {
  mkdir -p "${pkgdir}"/usr/bin
  cp -f "${srcdir}"/dest/usr/bin/msys-2.0.dbg "${pkgdir}"/usr/bin/
  cp -rLf "${srcdir}"/dest/usr/${CHOST}/include "${pkgdir}"/usr/
  rm -f "${pkgdir}"/usr/include/iconv.h
  rm -f "${pkgdir}"/usr/include/unctrl.h
  # provided by libtirpc
  rm -fr "${pkgdir}"/usr/include/rpc/

  cp -rLf "${srcdir}"/dest/usr/${CHOST}/lib "${pkgdir}"/usr/
}

package_msys2-runtime-3.3-devel() {
  pkgdesc="MSYS2 headers and libraries"
  depends=("msys2-runtime-3.3=${pkgver}")
  provides=("msys2-runtime-devel=${pkgver}")
  options=('staticlibs' '!strip')
  conflicts=('libcatgets-devel' 'msys2-runtime-devel' 'msys2-runtime-3.5-devel')
  replaces=('libcatgets-devel')

  _package_msys2-runtime-devel
}

package_msys2-runtime-devel() {
  pkgdesc="MSYS2 headers and libraries"
  depends=("msys2-runtime=${pkgver}")
  options=('staticlibs' '!strip')
  conflicts=('libcatgets-devel' 'msys2-runtime-3.3-devel')
  replaces=('libcatgets-devel' 'msys2-runtime-3.3-devel')

  _package_msys2-runtime-devel
}
