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

pkgbase=msys2-runtime-3.6
pkgname=('msys2-runtime-3.6' 'msys2-runtime-3.6-devel')
pkgver=3.6.0
pkgrel=5
pkgdesc="Cygwin POSIX emulation engine"
arch=('x86_64')
url="https://www.cygwin.com/"
license=('GPL')
msys2_references=(
  'cygwin: cygwin'
  "cpe: cpe:/a:cygwin:cygwin"
)
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}
        https://patch-diff.githubusercontent.com/raw/msys2/msys2-runtime/pull/271.patch
        signal-crash-backport.patch
        msys2-runtime.commit
        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.patch
        0009-Do-not-convert-environment-for-strace.patch
        0010-strace.cc-Don-t-set-MSYS-noglob.patch
        0011-Add-debugging-for-strace-make_command_line.patch
        0012-strace-quiet-be-really-quiet.patch
        0013-path_conv-special-case-root-directory-to-have-traili.patch
        0014-When-converting-to-a-Unix-path-avoid-double-trailing.patch
        0015-msys2_path_conv-pass-PC_NOFULL-to-path_conv.patch
        0016-path-conversion-Introduce-ability-to-switch-off-conv.patch
        0017-dcrt0.cc-Untangle-allow_glob-from-winshell.patch
        0018-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch
        0019-Add-debugging-for-build_argv.patch
        0020-environ.cc-New-facility-environment-variable-MSYS2_E.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-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch
        0025-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch
        0026-kill-kill-Win32-processes-more-gently.patch
        0027-Cygwin-make-option-for-native-inner-link-handling.patch
        0028-docs-skip-building-texinfo-and-PDF-files.patch
        0029-install-libs-depend-on-the-toollibs.patch
        0030-POSIX-ify-the-SHELL-variable.patch
        0031-Handle-ORIGINAL_PATH-just-like-PATH.patch
        0032-uname-allow-setting-the-system-name-to-CYGWIN.patch
        0033-Pass-environment-variables-with-empty-values.patch
        0034-Optionally-disallow-empty-environment-values-again.patch
        0035-build_env-respect-the-MSYS-environment-variable.patch
        0036-Revert-Cygwin-Enable-dynamicbase-on-the-Cygwin-DLL-b.patch
        0037-Avoid-sharing-cygheaps-across-Cygwin-versions.patch
        0038-uname-report-msys2-runtime-commit-hash-too.patch
        0039-fixup-Instead-of-creating-Cygwin-symlinks-use-deep-c.patch
        0040-Cygwin-Adjust-CWD-magic-to-accommodate-for-the-lates.patch
        0041-Cygwin-console-tty-restore-really-restores-the-previ.patch)
sha256sums=('89d856fbf9f8d753a644651f95ccd769a43135d93b1bc322582f6fb87ebf4cea'
            '875c09c99cfa98b7480e7ec37ee211315f3f60b59c14a70760a4f8e2b9cc3c17'
            '4931b8afe369c1e7a97639e75cf8342c60103e07699ed2acf0c910bf014ae352'
            '564998f09ca51148aba183e52ac8e78169c98d3e1a92e163f56a00cd7c840a68'
            '5945b95c6b3caf18cc8356802e9af1770746a76c27d0931b897d48d072603fcf'
            '8f2def0add5397487e2470fd7219bd80233e4d43e9f78d92d042907fede0570a'
            '0cebee2ed462579029512e46e4bd3c7bddd4d92823ef69803a938007c66d2843'
            '713adc344ee3c08831c658e27ef137efd65860416621cf6e76a94b2559c9e6c9'
            '89ac72ab4dcb0191b998597fb0c06c1f1ccc5b53a1a7b68f963deca6bbfe29bb'
            '2d77422697b212c0815927aa5470bf0d7d410e9fc8297890f203563aa448044e'
            'ab64b84b9d28322bf1d6cebd54d60fca1b6283024fb43bff9ca623d7d83e3f87'
            '90c14ad139f0bdfe9c03fd41460066895bc651e8190502ac33c25803efc8eaf4'
            '5f783ecb179e38ae8500bb47972b08dd1658db8aeb71f2b35c85ee0432fed22c'
            '67630db2bfb41b823079957c36c8d3f9fdc5456facd89e16790098e8afad59b3'
            'dd04c52b0a4b59068e7bef0a5ef9b5bcf7f4c01eff06928ad5d489da69746206'
            'b7cc9389e5aa92eefe3877641c604bcfa3cc333d7957cfb3c4d68400ab869927'
            'adccafe2e1c5f32c36542a9ad78cb668f330c2e67ddeeef34c7eaf04d01bf06b'
            'aec979057d38cb7ef3e0858663583515b7cfa94e7155e6deb6d32bb15d5b86e5'
            'f2fe79f9067ea97752a2e7916cbab4830f07be16310d6dbbdaf897cf58dd7d5b'
            '14e693ff6b927e629adf601f4d3ce238734f3f0495f1e92ab9ac620b71336aa7'
            '612a914ab83b126c4ceaa1855db504a73f2efd2a7b404983b01b0087a1bb279b'
            '12dafc8b6181b62853faf99904bf92f20528d645ce6e4134287968a6a6617fd7'
            '85bd430a310cfd70e6a0943adacd3931cd82e6efa096c2daaf7c017ed7d09e5f'
            '729b0ed836b96f198d8a13a41539a56f53f50b76d47bd65e6424f32bf8815c2f'
            'caf2e58994a3c1942d29d44c90be1478d5f1cf712c8d135d135b8dc4c9dccde1'
            'f3f6e7a5cfb7673be0042b8135864371a94cbcbfb1d864d7e9d64d027c35b038'
            'd6872a21670cac99b9b15987786cef0d374f49a64c9ab875ca62294371a74d28'
            'cdf6ebbca528f29cc01986eea0ae36266c1e0e0ce64cd5e74bde3074109dbaa3'
            '32c46ae9bc6601bc0da4b20c6392c84195d1da85e1812dc8412d715aaa7f88cc'
            '5fdb0e83c650abdb9e614c3bcde0e98aeaa61d883c89abddff6047312db4295a'
            '3ad497d3d106f36e118181e6ec9690a2ebf949c37ffc0d24ed73e3500d1bc895'
            '987ded6952cdf274a266f42dbc3bd9827155b9de33ec8695e0f3dd8f85396de8'
            '0f916eaae8e8bacc31f13123b89bcd74f4785e460ce7fc207d5d94c192273968'
            '370d77f666ecb202f79e7704f637a391faaf5aab02e83ca2b8300c9a7f1eed69'
            'c96472df3e15d097104e03ec4444b82d7d5e7f28db7042e90b77f5b200d4e8dc'
            'a8446d14b31b50a305bfe5f6f130715d2a5b7892b539667713a7cd968cf1b9ab'
            '0bc6b43765de0c102cbb855063c6c7b67c1b02b23313803d16b8cdcd8c6caf11'
            'a63338650ea90be0bcee179e69b0cd96ed5c24b5aae0401133d6b6bae1cce71d'
            'e0fd2926ed9dba1d0768f8b221a9607583296a9044cd14e63b06bc79088ac360'
            '65dc12c962d6cf9c637c157396d67a18640853bd0c890087a9a39da6fdbaca52'
            '4850fd4c82bc090726f57d1a1c562cb5a5e654172444ee4eba6abe3021880abb'
            'eafc8a892ad6cd89b2d0c2ae6e788ede27c42501b82650d569a95671b0046fa3'
            '384001ba02f597aad992624aed9d63cc7f1c70af842b09eac6ea628aa24947ad'
            '278461f45bf55fa054a7f59feb87bf6a4ed251b03bf91edc12c088c85418f4e3'
            'ff3c524f8802b79d268ad94ba7d0c2684741dc047c1ef5781081b02218333252')

# 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 271.patch

  # https://cygwin.com/git/?p=newlib-cygwin.git;a=commit;h=d71aeccff4d219b1cc7a6b0d17dcea7e5bb1b2e9
  # https://cygwin.com/git/?p=newlib-cygwin.git;a=commit;h=7f67575711f91ee0b738f836e2ed93c2e41b5248
  apply_git_am_with_msg signal-crash-backport.patch

 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.patch \
  0009-Do-not-convert-environment-for-strace.patch \
  0010-strace.cc-Don-t-set-MSYS-noglob.patch \
  0011-Add-debugging-for-strace-make_command_line.patch \
  0012-strace-quiet-be-really-quiet.patch \
  0013-path_conv-special-case-root-directory-to-have-traili.patch \
  0014-When-converting-to-a-Unix-path-avoid-double-trailing.patch \
  0015-msys2_path_conv-pass-PC_NOFULL-to-path_conv.patch \
  0016-path-conversion-Introduce-ability-to-switch-off-conv.patch \
  0017-dcrt0.cc-Untangle-allow_glob-from-winshell.patch \
  0018-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch \
  0019-Add-debugging-for-build_argv.patch \
  0020-environ.cc-New-facility-environment-variable-MSYS2_E.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-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch \
  0025-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch \
  0026-kill-kill-Win32-processes-more-gently.patch \
  0027-Cygwin-make-option-for-native-inner-link-handling.patch \
  0028-docs-skip-building-texinfo-and-PDF-files.patch \
  0029-install-libs-depend-on-the-toollibs.patch \
  0030-POSIX-ify-the-SHELL-variable.patch \
  0031-Handle-ORIGINAL_PATH-just-like-PATH.patch \
  0032-uname-allow-setting-the-system-name-to-CYGWIN.patch \
  0033-Pass-environment-variables-with-empty-values.patch \
  0034-Optionally-disallow-empty-environment-values-again.patch \
  0035-build_env-respect-the-MSYS-environment-variable.patch \
  0036-Revert-Cygwin-Enable-dynamicbase-on-the-Cygwin-DLL-b.patch \
  0037-Avoid-sharing-cygheaps-across-Cygwin-versions.patch \
  0038-uname-report-msys2-runtime-commit-hash-too.patch \
  0039-fixup-Instead-of-creating-Cygwin-symlinks-use-deep-c.patch \
  0040-Cygwin-Adjust-CWD-magic-to-accommodate-for-the-lates.patch \
  0041-Cygwin-console-tty-restore-really-restores-the-previ.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"
  CXXFLAGS="$OPTIM -pipe -ggdb"

  # otherwise it asks git which appends "-dirty" because of our uncommited patches
  CFLAGS+=" -DCYGPORT_RELEASE_INFO=${pkgver}"

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

  "${srcdir}"/msys2-runtime/configure \
    --with-msys2-runtime-commit="$(cat "${srcdir}/msys2-runtime.commit")" \
    --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

  # split debug info from msys-2.0.dll
  cd "${srcdir}"/dest
  objcopy --add-gnu-debuglink=/dev/null --only-keep-debug usr/bin/msys-2.0.dll usr/bin/msys-2.0.dbg
  objcopy -g --add-gnu-debuglink=usr/bin/msys-2.0.dbg usr/bin/msys-2.0.dll usr/bin/msys-2.0.dll.new
  mv -f usr/bin/msys-2.0.dll.new usr/bin/msys-2.0.dll
}

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

  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.6-devel() {
  pkgdesc="MSYS2 headers and libraries"
  depends=("msys2-runtime-3.6=${pkgver}")
  provides=("msys2-runtime-devel=${pkgver}")
  conflicts=('libcatgets-devel' 'msys2-runtime-devel')
  replaces=('libcatgets-devel')
  # strip breaks the split debug info.  msys2/msys2-pacman#52
  options=('!strip')

  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/
}
