# 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=5
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
        0030-uname-limit-sysname-to-MSYS-or-MINGW.patch
        0031-CI-give-the-cygwin-sync-job-explicit-write-permissio.patch
        0032-Cygwin-path_conv-Try-to-handle-native-symlinks-more-.patch
        0033-Cygwin-console-Fix-race-issue-regarding-cons_master_.patch)
sha256sums=('SKIP'
            '605f3f31dcca983fad2659f2d8f3531217e3f133757b40b0977e6a17c406c147'
            '5d120d24ce55ef08bf459781610e32c4a8e5e0eac73971a60e80590c6432d940'
            '06edd4fef57a3a77c0542dbabedb4f35cf0e64fccb5edaeb2dbd5ab305a5e001'
            '1a28f4fd164215332b081e405def363af157b1abc91b0d98550dccbf84a9b682'
            '7698d6afc29103701cc213caaa0eaab429f791bbd8595ad82fe1c0416ac807d6'
            'f022cee7fe1fc4149fb02b95256fe86e12361cfb5e66040eac9dbf7f162e4a4f'
            '7d17457c9b3a7a8a3cc8d5e313d56f013ad899e6756f3816c575ee31b2bc299e'
            'ce0dc0502fdce4cad50a71fcc27c175dac193603c2dc4c8a701f43ffa63d39a6'
            'e08513c22a68e627324ce942f6cac3980f42a972e3c119da0b0a03d728a0701e'
            '21f2592d3fa4b836eee8b2a01a84b069442e99af4181d5298e147d461c81468a'
            '786126329d6bcd1e2cd6ca8a118049784e3db033e80566fde8341729701d4da9'
            '47d566113baf08c56a8a7f4d516e5ce817d7936cb1b796032b30c65acce39dda'
            '04eddbcddb02c8026dde666b7388cd1871fdeb9072ff36b6270c101fb3cebb88'
            '314ab067f049c48de232e518539752404aca6980faf4e3a6f26679a64f1eac51'
            'a6028b372da7ef8010788b5c22e66096c2c968b6a0bf944b90fbdd9e8c0d354e'
            '7d24ae6654b8dce1d1c641c239e84d3c518c564c1dcbca46a7a03c75771ca634'
            'e9de713a0996ed18a5d1c3dce8b2d905375b715e3e8945ba354206d61dc43f09'
            '3d0822b9c68968d7db2f6750cca0619ff4eedba3b8b9b784d5848f6d041a960b'
            'c9252d30813a080a7f124c383c941be97535abd31babc55e17b33f666678f140'
            '65b665e16b096ea1a7476a8fcd83a3287f9427cbe781f52dc6a2a186afc165b3'
            '81bab02c5930d01e813cb295099bdc8a10e84e1a38dabae3e99eb9b5ec50d401'
            'c4831a348e83c30d17af54a637558986e50b34bb49be063d32df850a4e23a580'
            'ab99f9903b5a8d785e0a7e0a6cc15f1de79d7d9b2bbff513e05c5b524bec845e'
            'de6e6f9e8a94eccf016617a4925a04a4c18ba5cfd3acdfc3f26325963b111e56'
            '4853cdfd022e6f6b58d564e24e16094b916ab5646cefab3192d90807b6775bc2'
            'a6b342c1a58ac83004c10933a58b5533a3d157d2d3336ac2019183d7e6c87d65'
            '6a2dffa890ea1694a76333388bfcdb5bcc67d3a431e70064342002662909d366'
            'd18402a2c75e099a9cafd0ce7eba01476728c825812d4516ebc240e3c494fe4e'
            '969ace2675ebec747d0ccfe2540210ebac9974973b8b93325aecb53351514b76'
            '301422428472f693ad3a659b1279c969d5457eaab77be4dc0a389b2f5c5b6a55'
            'acbf8e7fea8089d474a63d8e12fd68e13f33967911eb785895183619c4307092'
            'ab78d325691fa6af631a73c3292537263b58a53eb0ab35c65d431a8536e8e12c'
            '679c11a3c6f96aa99c7d6bf6529d7cd22fbb23b24a06196ab06a9dfec82355fe')

# 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 \
  0030-uname-limit-sysname-to-MSYS-or-MINGW.patch \
  0031-CI-give-the-cygwin-sync-job-explicit-write-permissio.patch \
  0032-Cygwin-path_conv-Try-to-handle-native-symlinks-more-.patch \
  0033-Cygwin-console-Fix-race-issue-regarding-cons_master_.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
#
