# 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=15
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
        0034-Revert-Cygwin-path_conv-Try-to-handle-native-symlink.patch
        0035-Reapply-Cygwin-path_conv-Try-to-handle-native-symlin.patch
        0036-Cygwin-Handle-virtual-drives-as-non-symlinks.patch
        0037-Cygwin-suppress-FAST_CWD-warnings-on-ARM64.patch
        0038-Cygwin-Add-missing-IsWow64Process2-declaration.patch
        0039-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch
        0040-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch
        0041-kill-kill-Win32-processes-more-gently.patch
        0042-Fix-64-vs-32-bit-type-confusion-in-swprintf.patch
        0043-Don-t-fall-back-to-ExitProcess-if-process-handled-Ct.patch
        0044-getprocaddr-refactor-cleanup.patch
        0045-use-IsWow64Process2-to-detect-arm-cases.patch
        0046-Revert-Cygwin-Handle-virtual-drives-as-non-symlinks.patch
        0047-Cygwin-respect-PC_SYM_FOLLOW-and-PC_SYM_NOFOLLOW_REP.patch
        0048-Cygwin-make-option-for-native-inner-link-handling.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'
            '109e3b9fedfa8809e67fdd43cf73d55568016409654e51386073c5542dded8a0'
            'a5110bbaa001eac34af2f524badc818cb373d266ecd05001788cadca069ca5a3'
            '665103d7d6313327126f73fec6abd63573b534a36068c383e030e579e5c6fdef'
            '82de99d61139f19929ed2c4faf8da3d84767dc5d8f609a00f9bd8dea3c594e7d'
            '82385daeed795666cf9e2e26a97e13417b714eba1011765285866f5467a4f0cf'
            '8475880df4ae8f109ed006c81177d50da5c5067bb63cb2dd7c2718993dd6b74d'
            '460adc6de940e25332efec7a6615bb95088ae0652870a1c0b4f37c72671444fe'
            'cc0bfded281675af09ce94d847e814f77fd5cbcd86b09b7436a0e91b7697b8f4'
            '04ee4b9ca58bfa8a3c0370d1cecadf50c9c02daf6a4e5ccd4610336155538b71'
            '34f9a822fd2cbfd3a556f30ccea6f1dd6bb66b9edf0cd506c0cbb69b6ae437d7'
            '54dd5a62e0b9491ed3d1e2ac934297864001c0d6751723342c92a332f105d603'
            '526a6466d84e6aa83e0efccded72caaea398f9f2388f6752abaadc011635aa00'
            '39735120e3e4316e2683a831bea08f292aaf79201fe2a0c67bdf34be284a056d'
            '7795780219829aef7388de9e9877fb9fc5d5dc48243ff27354c70af4b993f581'
            '93325dc9cf26f1e6e9d41d3487c3a4151d75a8202e3245822bef4e9efe44803e')

# 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 \
  0034-Revert-Cygwin-path_conv-Try-to-handle-native-symlink.patch \
  0035-Reapply-Cygwin-path_conv-Try-to-handle-native-symlin.patch \
  0036-Cygwin-Handle-virtual-drives-as-non-symlinks.patch \
  0037-Cygwin-suppress-FAST_CWD-warnings-on-ARM64.patch \
  0038-Cygwin-Add-missing-IsWow64Process2-declaration.patch \
  0039-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch \
  0040-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch \
  0041-kill-kill-Win32-processes-more-gently.patch \
  0042-Fix-64-vs-32-bit-type-confusion-in-swprintf.patch \
  0043-Don-t-fall-back-to-ExitProcess-if-process-handled-Ct.patch \
  0044-getprocaddr-refactor-cleanup.patch \
  0045-use-IsWow64Process2-to-detect-arm-cases.patch \
  0046-Revert-Cygwin-Handle-virtual-drives-as-non-symlinks.patch \
  0047-Cygwin-respect-PC_SYM_FOLLOW-and-PC_SYM_NOFOLLOW_REP.patch \
  0048-Cygwin-make-option-for-native-inner-link-handling.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/
  cp -rf "${srcdir}"/dest/usr/libexec "${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
#
