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

pkgbase=msys2-runtime
pkgname=('msys2-runtime' 'msys2-runtime-devel')
pkgver=3.1.7
pkgrel=8
pkgdesc="Cygwin POSIX emulation engine"
arch=('i686' 'x86_64')
url="https://www.cygwin.com/"
license=('GPL')
makedepends=('cocom'
             'git'
             'perl'
             'gcc'
             'make'
             'mingw-w64-cross-crt-git'
             'mingw-w64-cross-gcc'
             'mingw-w64-cross-zlib'
             'zlib-devel'
             'gettext-devel'
             'libiconv-devel'
             'diffutils')
# options=('debug' '!strip')
source=('msys2-runtime'::git://sourceware.org/git/newlib-cygwin.git#tag=cygwin-${pkgver//./_}-release
        0001-Add-MSYS2-triplet.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-Do-not-create-cygwin-symlinks.-Instead-use-deep-copy.patch
        0007-Automatically-rewrite-TERM-msys-to-TERM-cygwin-With-.patch
        0008-Do-not-convert-environment-for-strace.patch
        0009-Special-case-for-converting-root-directory-to-have-t.patch
        0010-dcrt0.cc-Untangle-allow_glob-from-winshell.patch
        0011-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch
        0012-strace.cc-Don-t-set-MSYS-noglob.patch
        0013-Add-debugging-for-build_argv.patch
        0014-Add-debugging-for-strace-make_command_line.patch
        0015-environ.cc-New-facility-environment-variable-MSYS2_E.patch
        0016-Fix-native-symbolic-link-spawn-passing-wrong-arg0.patch
        0017-QueryUnbiasedInterruptTime-must-be-load-from-kernel3.patch
        0018-fixup-Do-not-create-cygwin-symlinks.-Instead-use-dee.patch
        0019-strace-quiet-be-really-quiet.patch
        0020-Default-to-disable_pcon.patch
        0021-Introduce-the-enable_pcon-value-for-MSYS.patch
        0022-Don-t-create-WSL-symlinks-for-MSYS2.patch
        0023-popen-call-usr-bin-sh-instead-of-bin-sh.patch
        0024-CI-add-a-GHA-for-doing-a-basic-build-test.patch
        0025-Set-up-a-GitHub-Action-to-keep-in-sync-with-Cygwin.patch
        0026-recursiveCopy-Always-close-the-used-handle-with-Find.patch
        0027-fixup-Add-functionality-for-converting-UNIX-paths-in.patch
        0028-Expose-full-command-lines-to-other-Win32-processes-b.patch
        0029-Cygwin-pty-Prevent-garbled-output-when-pseudo-consol.patch
        0030-squash-Do-not-create-cygwin-symlinks.-Instead-use-de.patch
        0031-fixup-Don-t-create-WSL-symlinks-for-MSYS2.patch
        0032-Cygwin-avoid-GCC-10-error-with-Werror-parentheses.patch
        0033-Cygwin-avoid-GCC-10-error-with-Werror-narrowing.patch
        0034-Cygwin-avoid-GCC-10-error-with-Werror-narrowing.patch
        0035-CI-Upload-the-build-artifacts.patch
        0036-Fix-msys-library-name-in-import-libraries.patch
        0037-Cygwin-winlean.h-remove-most-of-the-extended-memory-.patch
        0038-uname-limit-sysname-to-MSYS-or-MINGW.patch
        0039-fixup-uname-limit-sysname-to-MSYS-or-MINGW.patch)
sha256sums=('SKIP'
            '766fc79dd4348444fff3bc6c10babfdfdee237873b0538aa3a383474e115ac01'
            'c563d3eec08774fb7f370a7f9fba0b9ea45e2a215cf67beddc1b74eafddbac8b'
            '6b8e537c4b7b9b8c8d7e655a59dae85827f84e9e5a8d9f26b6d099725aa9571b'
            '38c955afc46ea1e0e5d6073b764865e6e29f27bed0d92f2dfb313dd7be5f6b7c'
            'aeda3bc63859de7f48914288093f78df1ba1fdf91e5c8ca639022d74ab03d5ab'
            'ace8cf9f8b576eb4546cceb38c6875ee757360973f0668a43d1f4e9ab89da77a'
            'db32b46cbe72dad9d33a71ebae809182849cf437ef9804ded59703480150502e'
            '2a4936d5d95423b76963d3151f1fe03a83c4766aadd04a8c1eb8af091d4a788c'
            'ec4c2a580e0971a3244de9b8f315e5388a06d3d75e05a4688254ee572398b909'
            '29cc2154a8d3d31282cf8b272efa15f1d682fd3a31dcd85fdf98770cebdb12c7'
            '9f9dd13382b3a6b0715ab34f2f1d9bcbec168c4f5fb8dd37ef1a97313dd0fec1'
            '11bfca785e7255001c1ad053f5188418282ff268d294eec9d50f4f5565a81e85'
            'a2262504bc9c89f4854653c93ffa91544931ead0ddcbef9e78f298a9b8eb34fa'
            '6fc4bd80ae662a2b6bc065d0186de0597745d9befd4f6177a627037641e4f6c3'
            'b2476c36cd563480553d1ed623a8ede58aa5a8ab0ecfd26c1e3844a687187875'
            '38501944aad349f491e9640a13eeee4f9e3cf2a313f99de240e84c662422d953'
            '75b5247478ad82d97c6b61a70857b7a6d467c7820bd2c54eee54c5de041960ca'
            '6123ad7256772114791f650f8733b8ce6343fd25cfddee527089895be3ba0ec4'
            '878d0417aef03aa0a23eb916c6ecacd362a497c007496fb7a797839988474847'
            'bf0eb696bf8a6afddf2afe7b2b6a299daf056d09093858a0919b644c987082b9'
            '65e8932d89d12a9aef4b986ca94592a6d77e3ff2907421af6c707244fed5eb44'
            '3bff2a6bca1b567ca69a7495c457c7619ad456c43a5ad7d3059a329da565eceb'
            'bd583fdcfae17c379744cdbe6b54f335104687f72df4806c2432cecc98b22f3d'
            '35490230975629668304d7db63b05e0b64edc28454a1e67c4b0c6991ca4c7344'
            '72a8b17f7a58095148f6f88b72bd524909f971f9e65232a4f7be04ebeb27e5f6'
            '33bdf8a17b4e13af17ec11e67cd0fddab500736dde86ed8f399b7c3232311d35'
            'd126f0249f7292b4d14573103a52f220c424e614f53ae11b00800baabb3a72ed'
            '9c58056934dade83d3333aa0ffd6c9c1d3f92d8f301ffd16451fbb333b726378'
            '75ac74563eaf221f401ad4476ea56c539f97a7495d5eaa7163ea1b0a2694edb4'
            'c4d382d490458336b0b551cb306589189c459dec9b9fca0ea5cfe0fb8f9d68be'
            'db0df18b40a3d2089652202332229bf170b774e7681fe55c9698d414de75156f'
            'c2ca504d806eddeb7b48eb5ff06311ff2afb6218add70d73ccc4dedf262a3f58'
            '8caa885874ff41199f1b21dadd3c42f6cc7e5c5855a6ec00bc131af1bde7e390'
            'fa2d57a2c9c4992403c9f5efef77e6a08fb4122a71139ad2b3a5ded5dda2339b'
            'bd0b699e836463580182f6f72628b8abb92ea71f422dd86eb9b66c3aca104a4c'
            '797f904663b604515602654fa88a515dc58dfc0def702696486e3ef97ec93cec'
            '4348aeaaa959ff812af209c6995f2e173fa76fba75c79c3495f65172be586863'
            '4a96bc11e1189a5bec3917430c8db8ac94bc3ba1c74e516dbc302ecf5578492b'
            '364a04469a911f355b5fcb2b86e3726ff96c93b91474895f7adac1290741ccc7')

# 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-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-Do-not-create-cygwin-symlinks.-Instead-use-deep-copy.patch \
  0007-Automatically-rewrite-TERM-msys-to-TERM-cygwin-With-.patch \
  0008-Do-not-convert-environment-for-strace.patch \
  0009-Special-case-for-converting-root-directory-to-have-t.patch \
  0010-dcrt0.cc-Untangle-allow_glob-from-winshell.patch \
  0011-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch \
  0012-strace.cc-Don-t-set-MSYS-noglob.patch \
  0013-Add-debugging-for-build_argv.patch \
  0014-Add-debugging-for-strace-make_command_line.patch \
  0015-environ.cc-New-facility-environment-variable-MSYS2_E.patch \
  0016-Fix-native-symbolic-link-spawn-passing-wrong-arg0.patch \
  0017-QueryUnbiasedInterruptTime-must-be-load-from-kernel3.patch \
  0018-fixup-Do-not-create-cygwin-symlinks.-Instead-use-dee.patch \
  0019-strace-quiet-be-really-quiet.patch \
  0020-Default-to-disable_pcon.patch \
  0021-Introduce-the-enable_pcon-value-for-MSYS.patch \
  0022-Don-t-create-WSL-symlinks-for-MSYS2.patch \
  0023-popen-call-usr-bin-sh-instead-of-bin-sh.patch \
  0024-CI-add-a-GHA-for-doing-a-basic-build-test.patch \
  0025-Set-up-a-GitHub-Action-to-keep-in-sync-with-Cygwin.patch \
  0026-recursiveCopy-Always-close-the-used-handle-with-Find.patch \
  0027-fixup-Add-functionality-for-converting-UNIX-paths-in.patch \
  0028-Expose-full-command-lines-to-other-Win32-processes-b.patch \
  0029-Cygwin-pty-Prevent-garbled-output-when-pseudo-consol.patch \
  0030-squash-Do-not-create-cygwin-symlinks.-Instead-use-de.patch \
  0031-fixup-Don-t-create-WSL-symlinks-for-MSYS2.patch \
  0032-Cygwin-avoid-GCC-10-error-with-Werror-parentheses.patch \
  0033-Cygwin-avoid-GCC-10-error-with-Werror-narrowing.patch \
  0034-Cygwin-avoid-GCC-10-error-with-Werror-narrowing.patch \
  0035-CI-Upload-the-build-artifacts.patch \
  0036-Fix-msys-library-name-in-import-libraries.patch \
  0037-Cygwin-winlean.h-remove-most-of-the-extended-memory-.patch \
  0038-uname-limit-sysname-to-MSYS-or-MINGW.patch \
  0039-fixup-uname-limit-sysname-to-MSYS-or-MINGW.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

  "${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/
  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"
  groups=('msys2-devel')
  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
#
