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

pkgbase=msys2-runtime
pkgname=('msys2-runtime' 'msys2-runtime-devel')
pkgver=3.6.9
pkgrel=1
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}
        msys2-runtime.commit
        0001-Fix-msys-library-name-in-import-libraries.patch
        0002-Rename-dll-from-cygwin-to-msys.patch
        0003-Add-functionality-for-converting-UNIX-paths-in-argum.patch
        0004-Add-functionality-for-changing-OS-name-via-MSYSTEM-e.patch
        0005-Move-root-to-usr.-Change-sorting-mount-points.-By-de.patch
        0006-Instead-of-creating-Cygwin-symlinks-use-deep-copy-by.patch
        0007-Automatically-rewrite-TERM-msys-to-TERM-cygwin.patch
        0008-Do-not-convert-environment-for-strace.patch
        0009-strace.cc-Don-t-set-MSYS-noglob.patch
        0010-Add-debugging-for-strace-make_command_line.patch
        0011-strace-quiet-be-really-quiet.patch
        0012-path_conv-special-case-root-directory-to-have-traili.patch
        0013-When-converting-to-a-Unix-path-avoid-double-trailing.patch
        0014-msys2_path_conv-pass-PC_NOFULL-to-path_conv.patch
        0015-path-conversion-Introduce-ability-to-switch-off-conv.patch
        0016-dcrt0.cc-Untangle-allow_glob-from-winshell.patch
        0017-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch
        0018-Add-debugging-for-build_argv.patch
        0019-environ.cc-New-facility-environment-variable-MSYS2_E.patch
        0020-Introduce-the-enable_pcon-value-for-MSYS.patch
        0021-popen-call-usr-bin-sh-instead-of-bin-sh.patch
        0022-Expose-full-command-lines-to-other-Win32-processes-b.patch
        0023-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch
        0024-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch
        0025-kill-kill-Win32-processes-more-gently.patch
        0026-Cygwin-make-option-for-native-inner-link-handling.patch
        0027-docs-skip-building-texinfo-and-PDF-files.patch
        0028-install-libs-depend-on-the-toollibs.patch
        0029-POSIX-ify-the-SHELL-variable.patch
        0030-Handle-ORIGINAL_PATH-just-like-PATH.patch
        0031-uname-allow-setting-the-system-name-to-CYGWIN.patch
        0032-Pass-environment-variables-with-empty-values.patch
        0033-Optionally-disallow-empty-environment-values-again.patch
        0034-build_env-respect-the-MSYS-environment-variable.patch
        0035-Revert-Cygwin-Enable-dynamicbase-on-the-Cygwin-DLL-b.patch
        0036-Avoid-sharing-cygheaps-across-Cygwin-versions.patch
        0037-uname-report-msys2-runtime-commit-hash-too.patch
        0038-Cygwin-Adjust-CWD-magic-to-accommodate-for-the-lates.patch
        0039-Cygwin-Fix-segfault-when-XSAVE-area-sizes-are-unalig.patch)
sha256sums=('f05acd30000aa003b1bb040cdf24ac4c85e85d0bc924126e5cafc66de975342d'
            '5f9262953542e823f7b5535257936dab5209d472aabd379b14584fe45c5311c8'
            'eae9773ed4d226f1938aceb4e0f25047ff3844758677a2c0f8a8b98bd7701015'
            'ed02e5bd61ac289e8c28c921bd71c7fa1a84f551a7f148ef622c51e06a49de51'
            '0ee828e4e50cbe51d3fbdfa79d84224c3a78b1fff09bac8daf3cd6ee7ee2c75a'
            'e9f102b493346ce1e5d6225c1ea4b2f36c5c8ba84eb71099ac6715912aa8b440'
            'e9e8248687c125e8eb4a1fc45bac267c47d93827e2b8874eafb79eb2e63dfa1b'
            '248767e78c49c8ab2b91743c34789178b1c9668b1fed77da79f40f12bbf2be19'
            '2639cfbcb80e5da6bc2a8ad41f47c4894bbdfc89a148d126f3b713a9b47f0368'
            '2b83d85a6ca2a96ef3c8ab56c3d23a3084024c919a718f171f139c590cdcdf7d'
            'da2eeb7ae75ad1ffabf431900056729790e7f440a3da33bfa7edb8f7a2b3921c'
            'e7336da9c905170735ff9de8c0562191b8c1c71aed221b934eb48a9b793e602d'
            '2619e6ad1f68c3070328f0d10e33d681bc8f9e9189923618971b18838fda8260'
            'c9bd56116558518eedf8c8a3575760e309632fe2dd7bb32366b80a8a153a69db'
            '560eb571bfff79cc3f75cae2870809ec294c40f79cdfd8a65d94eb555c3a13b2'
            '2733ac838a2d635d12e179df6468af0b428d847482c52b1f09632ef6dc0bf836'
            '25ef519613a5493bd64fe0007eab9e231a1cbcabd76b1b3c98ae839b40f04256'
            '880652020b2b97ce404af188ce542f3afebaba0b8ed7171001ab6aa62b507450'
            'd646ceead203dd39c6fdfc295b62898e55cc9b235e0d581881f8299626be084b'
            'e1de2e93c61b9043c1fbbf1a9d7af2043339ffde0e1af3eaafb4592b0086be62'
            'bd001e24fd8edff62d27029b285514f0226ebb6d0fbcff6910bbf5987ca4c0b2'
            'ab95c51ae637668634c1a4cfc2d95f5feafab06a4d02db2df686d9bbe6800f34'
            '904759986984643f153d30ed376f9e3d29ee471e7d023b544935f76597c9a966'
            '53fd297e4ea2caa56ae967a94be48699977760d7c3ee5c96719518f9b978340a'
            '89ae31bc2bb3ceb0720db2f7c9f564907d8f59c267f20ce342d8e35ee80c5eb8'
            '9677dfccbf64eae5fd69444bb260e6401cbcd0a01955c967bf016baf0e899a96'
            '9db2b2535f0518d0ec5cbdbc18ef0256f0524218d1bdda3e56406c797bf2cb31'
            'a96eed1e9ecd32591fd553eff27165360bb41b5e545be75aee898ef753681b58'
            '375164a1b411fa35e2040877ba8d388782df4b9c9b1e54292855b19d91df835e'
            'a65149f3ba075ea39f03aecc18902f6be43ab9d7e9df235a954f9b512a9468cd'
            '4aa0c3fd37f1c63a20a9b9b8eda6953447061aa03391ac633d48e999a589ba5d'
            '5e0e292da05344e8cf1e30aeb35345738159caadea1f008e8237789570ec8616'
            '803c54e37f1cba31462bdc1e4bb03d133281627df32f8dc1052088e12549e808'
            '0151534a25b34891a574e479bda9617291d1050ddaf56d5fd79e29b3cf2d15f8'
            '938ab09de563e50d189a70ed526f57ddffb544af930904526ebf3e22c60ce2d8'
            '3decd0d0e4d5fc15e5d997c587a80b7d164709ed55e543f16bd6acdcd9747228'
            'c4d0e7f8910b75a11f4221fa73098fee23737d2c91957703283c83a9b53607ae'
            '69803c6c6dc6939ec27c71dc3fc21fc9d71cb6475f5a0c395f73a8027b733fe6'
            '9d991051bf5ce05fc882fb8bfc5b6c2239e7eb0cbe71b1db61c52599b581da9c'
            '9256c38f9d41e37b31d080df14f1d49633caa1e5a17a3e7234d5c2ac67e191a7'
            '8f7fa3b5b61921be8ea51b6b7d158e1d024c2e70261db9078ae0c207471143c2')

# 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-Fix-msys-library-name-in-import-libraries.patch \
  0002-Rename-dll-from-cygwin-to-msys.patch \
  0003-Add-functionality-for-converting-UNIX-paths-in-argum.patch \
  0004-Add-functionality-for-changing-OS-name-via-MSYSTEM-e.patch \
  0005-Move-root-to-usr.-Change-sorting-mount-points.-By-de.patch \
  0006-Instead-of-creating-Cygwin-symlinks-use-deep-copy-by.patch \
  0007-Automatically-rewrite-TERM-msys-to-TERM-cygwin.patch \
  0008-Do-not-convert-environment-for-strace.patch \
  0009-strace.cc-Don-t-set-MSYS-noglob.patch \
  0010-Add-debugging-for-strace-make_command_line.patch \
  0011-strace-quiet-be-really-quiet.patch \
  0012-path_conv-special-case-root-directory-to-have-traili.patch \
  0013-When-converting-to-a-Unix-path-avoid-double-trailing.patch \
  0014-msys2_path_conv-pass-PC_NOFULL-to-path_conv.patch \
  0015-path-conversion-Introduce-ability-to-switch-off-conv.patch \
  0016-dcrt0.cc-Untangle-allow_glob-from-winshell.patch \
  0017-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch \
  0018-Add-debugging-for-build_argv.patch \
  0019-environ.cc-New-facility-environment-variable-MSYS2_E.patch \
  0020-Introduce-the-enable_pcon-value-for-MSYS.patch \
  0021-popen-call-usr-bin-sh-instead-of-bin-sh.patch \
  0022-Expose-full-command-lines-to-other-Win32-processes-b.patch \
  0023-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch \
  0024-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch \
  0025-kill-kill-Win32-processes-more-gently.patch \
  0026-Cygwin-make-option-for-native-inner-link-handling.patch \
  0027-docs-skip-building-texinfo-and-PDF-files.patch \
  0028-install-libs-depend-on-the-toollibs.patch \
  0029-POSIX-ify-the-SHELL-variable.patch \
  0030-Handle-ORIGINAL_PATH-just-like-PATH.patch \
  0031-uname-allow-setting-the-system-name-to-CYGWIN.patch \
  0032-Pass-environment-variables-with-empty-values.patch \
  0033-Optionally-disallow-empty-environment-values-again.patch \
  0034-build_env-respect-the-MSYS-environment-variable.patch \
  0035-Revert-Cygwin-Enable-dynamicbase-on-the-Cygwin-DLL-b.patch \
  0036-Avoid-sharing-cygheaps-across-Cygwin-versions.patch \
  0037-uname-report-msys2-runtime-commit-hash-too.patch \
  0038-Cygwin-Adjust-CWD-magic-to-accommodate-for-the-lates.patch \
  0039-Cygwin-Fix-segfault-when-XSAVE-area-sizes-are-unalig.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() {
  pkgdesc="Posix emulation engine for Windows"
  conflicts=('catgets' 'libcatgets' 'msys2-runtime-3.6')
  replaces=('catgets' 'libcatgets' 'msys2-runtime-3.6')

  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-devel() {
  pkgdesc="MSYS2 headers and libraries"
  depends=("msys2-runtime=${pkgver}")
  conflicts=('libcatgets-devel' 'msys2-runtime-3.6-devel')
  replaces=('libcatgets-devel' 'msys2-runtime-3.6-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/

  # compatibility with Cygwin toolchains
  cp "${srcdir}"/dest/usr/${CHOST}/lib/libmsys-2.0.a "${pkgdir}"/usr/lib/libcygwin.a
}
