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

PKGEXT='.pkg.tar.xz'
pkgbase=msys2-runtime
pkgname=('msys2-runtime' 'msys2-runtime-devel')
pkgver=3.1.7
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-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)
sha256sums=('SKIP'
            '05d43b352507d4677f7fa5ce8aef767989fd01420cf6c51e2ee89404a4e5e0b5'
            'e02a8cd70a1dea961c98861186b1eee61c946f13ba161edb4917f621304ca688'
            'c8d382564b8b88b9eee8493184274903558ed18be8861d7c88d7943ef6673c4a'
            '85f433003ecefaccbdff41c55bc75962fc9f61a861c99c58755cbdb2a2d34243'
            'b39f9b8108a94ebdf6e407dfb1bc553e28776bf6f1e90db61b88341a55185616'
            '3862c515ef24786957a092edf0596e972d0d7eb19a43ec35fb016737e8505016'
            '20663644b922a16e60b4c8b386b67aef2c149cbbd0bbe8e5610bd5bf1373b0e3'
            'ace47a9ac5026c4ca27172ab8561f6e356c2b0240c9eecba5b1d9918d9bd92c0'
            'a141dff613de448cc0dcb837abf4815dae25c50ca1da3be5391d5d26215b5298'
            'a971a8dcaff53567d4d3d10ea4fabdee55ee6d9fa2c4322937cb5baa7750b29e'
            '81f068d495e08bf89f7426dec23076b6595d38632e115dcbb58bf55573ca8874'
            'c9433ff2772aae331fe519d843a49ff137546e2a0dfe3764f39c4c4aea1c0816'
            'f30a00b3f7c5e283ff65bdabc3644e902840438728397c7887f5f6561da43673'
            '1889e4632b321a189c8d384ef67afa572363f9f0314f99208117cd6d3115a0c2'
            'd7e19f0d97faf8196179fcf3483b7e094a01866604aeebfbd4b7f4ca2d8c8389'
            '1324bc87fa477bf75d8b9e5c62f14c7a853a8ae9b7b6f8f8ebe515afacbe4741'
            'bcc75a700d0366803efcb7d935575b8a932ad20f5b025c540a256922d6e98b7f'
            '21fb351df4a11404164ad104855c050d915ad065498da99d1f5dc73b2985b71c'
            '2c239436e1e233a5ad4b4deb074a0049097214f69f5b9928adfe141b5774d71a'
            'b5a8441020abbfa7a639bc88088500e9cbcedbff5249d327ed12f80c816e8b5a'
            '20df26489b7903684d431b054c13369db5d3ad81e46fb9ab9a44a6d349594252'
            'b43c6ecebc6fec9b3ef8349dab58d46649901791be4efa12087b2993fed5194e'
            'e35b35b5538266ed3834ccfadb768a6af15f2d3ddbe4d9f5c5775fefb1ef9376'
            '55cad29551d572f6eb4fc5014e0a70172471a2cb1697e32e6987419c30c25887'
            '7eda58471b242c0cce4bf621b991ee24a55c993d8076a2c5ddba96b2fa8021bc'
            'efbb68f4ede3fd6e10d0646d57c3ce4ab73e0b1d7638fc7da2a72f2c3105a6ad'
            'aa59267ad0a646d66a6dc5da9b50ad3d567ba7742f91f5ff10c7a8dba494fc3a'
            '0dde7b9bea4cc8008afb62448eabab477b2b2eebdad8a97defc9bf59fc195b29'
            '2304a85c93d9c530d02848c697d0ff0d824889c7c51ba85ce1d18fd630ceefb4')

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

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
#
