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

pkgbase=msys2-runtime
pkgname=('msys2-runtime' 'msys2-runtime-devel')
pkgver=3.2.0
pkgrel=2
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-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-CI-Upload-the-build-artifacts.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-Expose-full-command-lines-to-other-Win32-processes-b.patch
        0028-Disable-the-cygwin-GitHub-workflow.patch
        0029-Do-not-show-Error-dialogs-by-default.patch)
sha256sums=('SKIP'
            'd6032bec7df8503d87a011083154b24a8a261bfd704c8199dca5fa6c87908b60'
            '2b829e772918a238124c06606dfb3789635f7dadd83c0fe1b7f0cff4f530d3eb'
            'bd4451be3c2ec3df3c0d3308313e4dc0a6895981f18ab2f1b1cd73e5bda9e6dd'
            '842141328b1a16517511e417cd98e28ab43d4fe0fc1fa7251a56d15e86fc64e0'
            'a6db29bf709ef28b1ce44ded23c4a6901cd649e41358c10fecf6d974ca9c371d'
            '1b4dd53e627986d91f4beda2c6411d0c5ea8e2b9bcb6239e1ff867740ca01a88'
            '7acefc1cf93abb5fac85060e223863b4127d3a8ec54986ef556452ede9e2f319'
            '552acb9f54aeddacaca23906ec7a6b46513626206ee0eab3e1fd8d3b7724b406'
            '998290705dbe939cc356d41bafdb58254b0161a1fcace327523514041c5fd066'
            '3091768d449b0a41772acafdf0a1b506274bf2d4466c08c517f012383baa18ad'
            '5db993cc238350d5b30830ecb03a9860ce335483cbd32d8c79affb1cf03efffb'
            '1b99a2333cd94b4e5095b373ca88e327645d68f9835fd04eb684c6913645f70a'
            'af369d29de42147828f9d960b6017024f4c9c62794723c860b81e79a29c962b2'
            'a855f924837e60433d39e84d4ae5befea1b2c6bbb8d3bf7c442184aaae4e1a1c'
            '6f8b902c611de300ada5ff1a80948e771e3bb435d7fb0db5f9f4962d91cd39cd'
            '793ff55e80f6c41c87e2f0661e1d91e56eb89c13e42e6fb7650c58dfc5dde244'
            '2563740da37e04b4deaff20e3e968b90d673e5bc6140c33132982d5201bfe568'
            '3fec9f8cf42784be57de2cee2d1fccb3d25724fc5f3e46bbc20a3ebf7a7bcc38'
            '58f5a43bb23f092c904615bf9e41156e5f2ad79371a2eab92777a1fa0c95e56c'
            '022aabfd75ee57d4f815ffce016bb41ec6719bdfb5f639dd3332302283b5b344'
            '8574a0d81d74b18377f240d0233aeac6335dc885b45f0a313dc3fb3ba9dff118'
            '23d5563185cfeb16424afff6b4098ffe442c2ce345101194a1fb4dfd999169c6'
            '9bd64e1ad2546982353f912a587df51bf1684013736e73bfc0f24669bdbeda8f'
            '7bf28c668c49d862a624af76a3b549829c3024d0fb9798667474487a7127786d'
            '364dbb9032aece79003badcd068c489c3222acb4e7e4360db48d34a25145bdde'
            'c1cb2706e4ad0bd162151153f1c087e03e6c19f40611abcfa31e2c409b35102d'
            'a302207244650d7230d63c5afc065a4d1eba2c14effe3e9c4fe9ec31b9faa789'
            '892db0bbefb70ed962f46642974a77560a0e8829b09e2e2924ee0fe7d8c51a89'
            '7e31db8bf617d8588c4ff42b2a8cc5377c73479ece06d01ad9141c8d718ff22d')

# 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-CI-Upload-the-build-artifacts.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-Expose-full-command-lines-to-other-Win32-processes-b.patch \
  0028-Disable-the-cygwin-GitHub-workflow.patch \
  0029-Do-not-show-Error-dialogs-by-default.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
#
