# 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.6
pkgrel=3
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)
sha256sums=('SKIP'
            '52c78022ec6504103a8daa334ed330472905ebbf58438d9379b33fc7a9841d23'
            '590b7c9f9002d4d0d3dc8bd9bf2dd855aeb91de70ad9736361f25a5f7a4682c9'
            '5a30e599a8df672c536ed468c3ff46c26184cad0e57f6493dccb09db7d94070c'
            'a01689ba155f9436d38c109518f1837287e6cacbc59b7478bfc4b65bce456201'
            'de61ec66384f897d404fac7724ce0b4a815ffdff189afa9b8507a8701c84e6bf'
            '4ee6d03f416c4293ea46e5e4e1f03ed394445be8d156356007804fc9384a7276'
            '1d3670d7d01a97c685bcb30009a329d0b51aa6101930b5d81aa1951c23f74b69'
            'dceeddecd65fbe14e40685a6e4202b59ad71d37c84812a4fbc7da84157f4f26c'
            'bc9c7ac7ec12ca5f9017116147dc6a6b1230dcc9fea4e0cb5c40295b44767f6a'
            '409d70c160d28bfb4ecb084e3cad936a253205438dda34c0dec0425bd3c2f066'
            '49fd75701ec2d50e4552bf23d742ac43b23b3feb52cc711db18160452c12cdfe'
            'a0771a8e65ab511f1e85fbb1031f2e029806d28ba1efe76ed2109812d7d7f0dd'
            'f84ed6fa41220e9dd23aac09101d3b44765679322bdac9877aa0616d7b8aff8e'
            '5e91dd7dc50c021af5f19cadaffcfe8d3b053c883a5f55c7b13b38d478a20727'
            '43a6f27b95c1873729fa2f0b1a519d860c8267c6b5fb7791aae803ac108c35c3'
            '7327837b56642f485a02335ffca54486736173ee42a568dd03695a8a5029d99b'
            '04adf25e96a5cb674cf8b191485fd2f8cd76372137799c9f5ed307a9c3524b11'
            '1e8ccfadd4c9aa14a0f89ce54ca2c357f771b449fbdd682fe5d7db31f8162a4b'
            '4ff36d01ada7a0706787003227012ba624cce10c5e7bdffd6c2f33afdc29cdfb'
            '6c81ef46b75b575f2d579509ecbd2a8fcaf0b3a71a25cefcf1cc5e777eb0831e'
            'fdf4a5e03263925ebc9cf571f4ae745b7d5b4ccac06d07fe3be403738de5da6c'
            '4d6f3a111a23afb110acd38660c8a3d6c45cb5fca5503080e737fd529fab3a42'
            'a1e2bf8dfb9ad47468b1295f11784f90820857e733ac352b7a9b35411134456e'
            '2679dde3632a0fad925d6010ad81055dce1671b10a13ef288168594773ac4c1b'
            'f1461123ed89e0cf0ff19eeb84179fe0652ebe72bf9930bcaf331ef41717b687'
            '43bbe321dc90b58a2bb66ded2c7ab55d88a21536e99f88a97c4ec2644eeecee5'
            '2bf4beb2420f247c2ac5970f799f9e4c6a47cb733dc6d06b4452666dedd82666'
            '5ac52c87aeec3bf3a61e53afd7cc8d5fdf6e6b3223ba3c20bccb383bad3de7e9')

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

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
#
