# 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=6
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)
sha256sums=('SKIP'
            '84bf13c5deefa35aaa53ad42e9c515414c70834e41c0afaf5950ac956541d452'
            '305259abd8f74ddf7beffbf7983c55e966e87854623b0bec0b274af97a4a5126'
            '9452ca1ba64e6051e5aaa638f493e55f5ac265668ce6fdf95da5e8ad2f108ddf'
            '5f02b889c0139607a39cf4103896cb151bebeb179a4d83fe9827bf8c65edd477'
            '78db0160e07137729d51b822228820f38486940e2838ddca5461bb3d67916e0a'
            'bffddcf5f6ed1b7427b85e1610f26c4d126b88837a97d4f884baace7d99d63d3'
            '3c9f2550615372a793f0d4d703a71745fa024f606b779d0ac7fd03e674b43345'
            '5997bd18443da1b1f016684f0cc1215cc99e6b6606429784ab322d6266da0cc9'
            'e00d4b6e58834527990cda7f3afa31996cab6c27d9502b6407735f4b4505deac'
            '944755bf45e495d8407cb53467320d158f7010711bf5a334e8e908de1f6905e1'
            '5f1d71d6083a25752525d5c3557670b065894155a07accbb6283e69f0cdc2db4'
            'f2809fa78d45f1491fc720f94b01295e024a322bb3df524173cc091aba1e5e5f'
            '6de8807d615f9c87cee6726a03bee51a4433a73a5931615de81b78507e1fe3f5'
            '2321f4348b952569351ad9c77a39015fb37fad012e33573a225eceead9cf94c3'
            'ce175ad58f94cbe6747ef5f79280e2399b5f092e4eac5db39e529836b5a2dc54'
            '27c9a2b8b3883dab492c66965209d5569b8668ece3c47b18b6ab86ca70f30e8c'
            '2a31ca793e9fd84fe7f2fdb2e68457bb381034cd53f10d00887550e9e529ffac'
            'c725f9fe3c43326f3014a54733765b4e52610e513322bfad04c543691e00b0fe'
            'daaa36b18adb580cb811a0197b85f97804e570108d61975e4cf7862b42cd1cd9'
            '046a6dbb24d8ff554bdb12d2c4e18851fca914a61e6488d3c149e9b3e2327473'
            '096583d58a280a0c0256847445d6c84fafaec8c1897568337d4f0416f5333325'
            'fe5d8aff2d4d5f26137aab0166fa7f7d7387c8cfdb4fa670aee7dee2421dd22f'
            'a005539be93f3f779cfdb90432002eb71be33b18eb11c9b57b7f865816402535'
            '5ee7976d9919bcf8f65c546eee4ac63889cf773240db6ce12f2c81c4a98ed399'
            'ec3353e8c3bb74eebdcfb96b6f6b44ed3a4fdcb84c002bf3c5cf71cbf7de6665'
            'e6b1a34eae4ff68f4fe762c235c1589910dc3612032e8eacbe7f4b926b8e2604'
            '98c1b657df8fb2b4215cdac2debd0eda70b6f527d76b2043948d533d32427de7'
            '7d32d5afaa5ec645da5b4bbf3d4c27cac54f9f9cdf19c2b9a80bfe384b59a2bd'
            'dd1aeea552c4b71479ea96bca040dda8a8fed6527e7adb51e533d85909f475d5'
            '2c8a7acf7faa570e77c305b1fa821dd98d1c89e82bb99c26b76423e045704505'
            'e02fa338f4a5dbd0578c24b24e1df43c7b0fe5f38714d186b81dba9e92b25851'
            'e80eceb629147bf232d9b1b48d0311c196bf4d9e17a7d5be03543ad1bdb5bbe4'
            '282e86372bf08b446109ea3a97fac84580c3089cf567a49ace5df561f521004e'
            'bbe813d6e7620c485ccd90505a0e8190c9112dc7b9efa91942105ea8a58ccfc9'
            'beb0f4f65af391468622b98d29d154fba6d2000ebc9ad4df686f9981b8cc61d9'
            'f3e08ec0a99648a358e55c6e4a23065b1c1e8d07df759ae593a17e4e7fa478ae'
            '7b276f0ad7451364e19ec73b7eb7917378dd95f45108fcad4a82968e8e61be30')

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

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
#
