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

pkgbase=msys2-runtime
pkgname=('msys2-runtime' 'msys2-runtime-devel')
pkgver=3.3.4
pkgrel=2
pkgdesc="Cygwin POSIX emulation engine"
arch=('i686' 'x86_64')
url="https://www.cygwin.com/"
license=('GPL')
makedepends=('cocom'
             'git'
             'perl'
             'gcc'
             'mingw-w64-cross-crt-git'
             '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-CI-add-a-GHA-for-doing-a-basic-build-test.patch
        0024-Set-up-a-GitHub-Action-to-keep-in-sync-with-Cygwin.patch
        0025-Expose-full-command-lines-to-other-Win32-processes-b.patch
        0026-Disable-the-cygwin-GitHub-workflow.patch
        0027-Do-not-show-Error-dialogs-by-default.patch
        0028-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch
        0029-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch
        0030-kill-kill-Win32-processes-more-gently.patch
        0031-Cygwin-make-option-for-native-inner-link-handling.patch
        0032-docs-skip-building-texinfo-and-PDF-files.patch
        0033-install-libs-depend-on-the-toollibs.patch
        0034-POSIX-ify-the-SHELL-variable.patch
        0035-Handle-ORIGINAL_PATH-just-like-PATH.patch
        0036-Cygwin-console-Maintain-ENABLE_-INSERT-QUICK_EDIT-_M.patch
        0037-Cygwin-pty-console-Fix-Ctrl-C-handling-for-non-cygwi.patch
        0038-Cygwin-pty-Pass-Ctrl-Z-EOF-to-non-cygwin-apps-with-d.patch
        0039-Cygwin-pty-Prevent-deadlock-on-echo-output.patch
        0040-Cygwin-pty-Revise-the-code-to-wait-for-completion-of.patch
        0041-Cygwin-pty-Discard-input-in-from_master_nat-pipe-on-.patch
        0042-Cygwin-pty-Fix-a-bug-in-tty_min-segpgid.patch
        0043-Cygwin-console-Fix-console-mode-for-non-cygwin-infer.patch
        0044-Cygwin-console-Set-console-mode-even-if-stdin-stdout.patch)
sha256sums=('SKIP'
            '10cdf6ef3dac311175d9a56df8907c449cf5c32f99f0ec797e2a85d05ed86cf0'
            '1101a29140b733cabc34fd0a0e222c2ddc629143eaf46fc0c4a219a1b9687e06'
            '1cd177b887135fc6427ebbe3fd9670cfd3560bdb6b05ab343f2da40b10eb5933'
            'fd8f4875021bb957c79bdde10a913de87794da740772bf78628eb4f3b81e3544'
            'b740f66d2fd2ba4e9bbea385e2291b75f4ab29efee87449ba1e27a4bdd7f3782'
            '6c6733a94c53916184a68cfe9951f6767f4827dda28212194697779eb882a36f'
            'ecbc0dc30bee67c3b46bfc4ace5114caa66b7b3633f2aba3ad798eb48f69e761'
            '4f7d3e34b861816bf4d5adce27746c2f5be45e2ec3c602ff9a43895d5d9e625f'
            '9ae673704bfd0c4db81ad2fea2f655e5d45d96aa73660b4bb54665ba2dd50717'
            'ae46a7b7f319b3e27cb0d01d6409005289c686e10b5ded073126c1037cd4e291'
            '033c1fbce111f01a1a8d6dae65f613bb50c43346f6dadd09cad190cac47e5a19'
            '38e16161ef9ee153d79210dfaa271e005eeb92af92295544e07d233cba6dae51'
            'f22718588e6d71b02dcb7a2bf08e19f21b34a4f0b9bbdc54207dc6f95263c245'
            'a8d289b19e349231bc5d56110ac37c8b8298f23fc54a6953e3540a9fd53a1945'
            '25c3b8c1be139cc7aa81be1209666538f1ccbae5fc6ede8358483f8d13e8403f'
            '3054f29188b32fba5e242e3c6c79ee681c591ad9acddcce47a91ef8dc684c239'
            '3732b6121792d691c9eb62ac9962fb837851506c95f0d6c2cd11b73cc3a329d7'
            '35b0bdc4689edf02de1447f3d1372651d512669a31808bedd4167e8769aaa930'
            '7f1854337525c6fbfe0e231c0fdd369baa81c7cbb4fe62fde88b275d2b6f84dd'
            'f0a7b54cb811760d47038c5e2de268a2cf2f62a104426a22ef29510c96f936ee'
            '4fc1e115954d642f44dde63673b31ca3e3b0b881bd0c411fdd159bdb638f92e7'
            'd201ac342352056d0f43609867bccd9acec8ba04db49f26d6b2e70803e8454cf'
            '8585d541e6e85a1c0a91b41851f91f806d88801ac55649e08434f149827132b5'
            '86c79e134effecce7baeee8e8eeb23d1f6d2774ca3bacaad666bfa68725d6d15'
            '347cc681272a20f74a2045ebed900a7b4dd5c802b61f7039436eaf6d5ac34230'
            '1717320182e77f8dc2a07bd77db027aec18c1d0a9d325224dc4e4e3e8b91d46f'
            '3c635d435b9bc7b639df8429aafee288efe658a6f0027c512b0f3a3531c75c24'
            '8fe4ce14eeb6d1fae97e611d74a19e8beb81dfbdc7e8d8d5f570bcd7ed2ddcf9'
            '6d8867b439699da05613a26b0329861da0ccaec9607b3341aaa4404f6c929287'
            'd9d4b1fc14eb209566201b420b89253c5a44be205c56c281df197c666c43a049'
            'e404746383dddc064f9cdc82573474217a22c0b4573da16445b0bb19adbd66b7'
            'bf0d5ac6d605c0503ecefad24b3ea4e54dfffa1ac4b003a50c02732b99303aef'
            '6097138f0af30de2562304e3ba9e822ef8eecafc050fe1a1661cfbe269abd70d'
            '9980074cbf1bcf19f45988ed260cb0f67569115f1de8ffc86aade0ec4d521458'
            'e69fb7620df519a33750675fd904fcc0773181b5a8bdbb15dad2f078456f7cd5'
            'f8624618db21cd8bf8f4c66f2418d47e73d9335d537ce5cdcc2d79f726585733'
            'eb50b49f9f0d523ef8023c73f8108b3b3fc5db47e8c6feb941c75cf23466a81f'
            'e114f753644165454ec1687a6713a6efea0523b1bb378fa2158eb4691e2136a3'
            '4c71b30d61de6fafa8e334e182d539b5df6baa4d9fbbbb0221a56b72974b9135'
            '4a8822cb2e0e7096449ab039c3c4ca7f121468a5efdda7dddb82c04147d3ae73'
            '7a3ae231c1222ba4c971e5cd7a83e665227256fa4d76078986f78e5d864f5509'
            'fefc75a87cbc52ac7439e45c9fe9f9d1dedbfa1bb7898f2472dffbb3beb345f1'
            'd74d30671080b154235cb49c87de06727c1250304993e298036f74f4cdca4301'
            'a590b6902fdd1804318a85b8ccb0c0e79baa4b17fe81047fc5e8374438ad6912')

# 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 am --committer-date-is-author-date "${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-CI-add-a-GHA-for-doing-a-basic-build-test.patch \
  0024-Set-up-a-GitHub-Action-to-keep-in-sync-with-Cygwin.patch \
  0025-Expose-full-command-lines-to-other-Win32-processes-b.patch \
  0026-Disable-the-cygwin-GitHub-workflow.patch \
  0027-Do-not-show-Error-dialogs-by-default.patch \
  0028-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch \
  0029-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch \
  0030-kill-kill-Win32-processes-more-gently.patch \
  0031-Cygwin-make-option-for-native-inner-link-handling.patch \
  0032-docs-skip-building-texinfo-and-PDF-files.patch \
  0033-install-libs-depend-on-the-toollibs.patch \
  0034-POSIX-ify-the-SHELL-variable.patch \
  0035-Handle-ORIGINAL_PATH-just-like-PATH.patch \
  0036-Cygwin-console-Maintain-ENABLE_-INSERT-QUICK_EDIT-_M.patch \
  0037-Cygwin-pty-console-Fix-Ctrl-C-handling-for-non-cygwi.patch \
  0038-Cygwin-pty-Pass-Ctrl-Z-EOF-to-non-cygwin-apps-with-d.patch \
  0039-Cygwin-pty-Prevent-deadlock-on-echo-output.patch \
  0040-Cygwin-pty-Revise-the-code-to-wait-for-completion-of.patch \
  0041-Cygwin-pty-Discard-input-in-from_master_nat-pipe-on-.patch \
  0042-Cygwin-pty-Fix-a-bug-in-tty_min-segpgid.patch \
  0043-Cygwin-console-Fix-console-mode-for-non-cygwin-infer.patch \
  0044-Cygwin-console-Set-console-mode-even-if-stdin-stdout.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=class-memaccess
  CXXFLAGS="$OPTIM -pipe -ggdb -Wno-error=deprecated -Wno-error=stringop-truncation -Wno-error=missing-attributes -Wno-error=maybe-uninitialized" #-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

  #pushd ${CHOST}/winsup/cygwin > /dev/null
  #LANG=C make libmsys2_s.a
  #cp libmsys2_s.a "${srcdir}"/dest/usr/${CHOST}/lib/
  #popd > /dev/null

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

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

  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}")
  options=('staticlibs' '!strip')
  conflicts=('libcatgets-devel')
  replaces=('libcatgets-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/
}

# return 0
# To hack on this:
# cd /c/repo-MSYS2/msys2-runtime/
# pushd src/build-i686-pc-msys
# LANG=C make && make -j1 DESTDIR=/c/repo-MSYS2/msys2-runtime/src/dest install
# popd
# makepkg -sRLf
# pacman -U msys2-runtime*.xz

# Quicker:
# open cmd.exe
# set "PATH=C:\\msys32\\usr\\bin;%PATH%"
# E:
# pushd E:\m2\repo-MSYS2\msys2-runtime\src\build-i686-pc-msys\i686-pc-msys\winsup\cygwin
# C:/msys32/usr/bin/bash -c "LANG=C && make"
# copy /y new-msys-2.0.dll C:\msys32\usr\bin\msys-2.0.dll
# popd
# C:
# C:/msys32/usr/bin/strace ls / > C:/strace.txt 2>&1
#
