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

pkgbase=msys2-runtime
pkgname=('msys2-runtime' 'msys2-runtime-devel')
pkgver=3.6.2
pkgrel=1
pkgdesc="Cygwin POSIX emulation engine"
arch=('x86_64')
url="https://www.cygwin.com/"
license=('GPL')
msys2_references=(
  'cygwin: cygwin'
  "cpe: cpe:/a:cygwin:cygwin"
)
makedepends=('cocom'
             'git'
             'perl'
             'gcc'
             'mingw-w64-cross-crt'
             'mingw-w64-cross-gcc'
             'mingw-w64-cross-zlib'
             'zlib-devel'
             'gettext-devel'
             'libiconv-devel'
             'autotools'
             'xmlto'
             'docbook-xsl')
# re zipman: https://github.com/msys2/MSYS2-packages/pull/2687#issuecomment-965714874
options=('!zipman')
source=('msys2-runtime'::git://sourceware.org/git/newlib-cygwin.git#tag=cygwin-${pkgver}
        msys2-runtime.commit
        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.patch
        0009-Do-not-convert-environment-for-strace.patch
        0010-strace.cc-Don-t-set-MSYS-noglob.patch
        0011-Add-debugging-for-strace-make_command_line.patch
        0012-strace-quiet-be-really-quiet.patch
        0013-path_conv-special-case-root-directory-to-have-traili.patch
        0014-When-converting-to-a-Unix-path-avoid-double-trailing.patch
        0015-msys2_path_conv-pass-PC_NOFULL-to-path_conv.patch
        0016-path-conversion-Introduce-ability-to-switch-off-conv.patch
        0017-dcrt0.cc-Untangle-allow_glob-from-winshell.patch
        0018-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch
        0019-Add-debugging-for-build_argv.patch
        0020-environ.cc-New-facility-environment-variable-MSYS2_E.patch
        0021-Introduce-the-enable_pcon-value-for-MSYS.patch
        0022-popen-call-usr-bin-sh-instead-of-bin-sh.patch
        0023-Expose-full-command-lines-to-other-Win32-processes-b.patch
        0024-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch
        0025-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch
        0026-kill-kill-Win32-processes-more-gently.patch
        0027-Cygwin-make-option-for-native-inner-link-handling.patch
        0028-docs-skip-building-texinfo-and-PDF-files.patch
        0029-install-libs-depend-on-the-toollibs.patch
        0030-POSIX-ify-the-SHELL-variable.patch
        0031-Handle-ORIGINAL_PATH-just-like-PATH.patch
        0032-uname-allow-setting-the-system-name-to-CYGWIN.patch
        0033-Pass-environment-variables-with-empty-values.patch
        0034-Optionally-disallow-empty-environment-values-again.patch
        0035-build_env-respect-the-MSYS-environment-variable.patch
        0036-Revert-Cygwin-Enable-dynamicbase-on-the-Cygwin-DLL-b.patch
        0037-Avoid-sharing-cygheaps-across-Cygwin-versions.patch
        0038-uname-report-msys2-runtime-commit-hash-too.patch
        0039-Cygwin-Adjust-CWD-magic-to-accommodate-for-the-lates.patch)
sha256sums=('91198341c33343cd6e37594dbb77b2e30e4d5f84a74c7a51a00392c11d137a8e'
            '4ebc9e2fff832a05c73d9abc21962543b7342e4c69be03607b3971ab5d22e229'
            'e4082aff332507d6d5fefa5b7da2fcbbaa8b593ca158dbb1fe026a26b0e09d91'
            '1cd177791994917d0c4df391fdc96993c3275eddd2b5f475532c0f478b2be8eb'
            '81d0f97d558b413c1b6fc592c7c0c7cac0b8f1c650e6fb13c01f4ee25b049fce'
            '0f5ba9d723da51eca3b33926b5974a274130e5cb8a013b8b0e847fc5263671e7'
            '41b69ef12d0609eee3c59ec60cf0c7abb84ab2d344dd4703ea2eeda9f9192a22'
            '04b910e51e21bbac9f5995718c2b332ff6ef6ceb651a3665648c5e3e3fa5f2eb'
            '748d384c9c7dacb8461d5522fa516be2cbd4277ce825bdbf91abc863dfc9a028'
            '3dfacf35ed80c356de3aabd8a4264d1303a3e5f7a053ae662c40b5b345987a3a'
            'ce0fb0d9004afe458bb0aa0e3cca429b87bf4764fbd012bec2151895aecc1b83'
            '10371ab7734fa99c4f9fa5ff0c62ed6051a5d90707ad04e06c13d5a008b47ade'
            '1d6b14cae5ef4b785f9770fbec1d9ced4e154f2c1dde859af568374ea134413f'
            'ae39e2a3ad98b86dc163a5cc7de87220f5735d7277f05043eacbaafbfea0a421'
            'b025654f67d1678fc150900114c5406e8553aa0ff553a81e9741bb7dd3d7386a'
            '129c8b838beb02da77d5ef79b3b851b0b66ec83f349d1472e81ce89b6512f594'
            'd94748ebc7d9f9b6845c620f15f0fd2076419036771e18d80b1473a7353c79ed'
            '3d9d4fb6d25c4ff9270c6a04e97d4ca8156475912ca7801d036d7ca0e4a6c47b'
            '62f18613e62a1c7dc6f3cfd5f05150af8cdc57ccb38c77e344617b6283bb1cad'
            '5608744ac944ce213b044d9d85aa1500ec73132581b6ff3017be7c90b00ea1b1'
            'de0f847705ab0f31b937ed2e2ad2981d5b2dc8435f9fc07a742f056cea545eb2'
            '5a90d74e5d3638c65c6b181ddcb3eb88eaa711f4c33b72d5e9c5adc3e0adcb39'
            '15d0eae37f4ff6520a134c6f675f1513202a5e450290ce9a58816406ea774071'
            '73d5ffc5227c096d9f5adb212cf2402b437004dd1dabaf4a56313e8cdd6b80fe'
            '1515cf6fda14a6dfa7601fd954921a4e735ed99f77c6d07a0cc872700bdff762'
            '91fd3f46ac105f861e00cad7218b1ff732d5dafc0dd9ae7307be86b45a0d8e48'
            '392edc87336d41a7684273485c7738ab54bb02cf72dab1dbec70e35ef3ede179'
            '051460bd08399d77ad34ccb577f170cf93ff4b5d55594c5fc6247e3f6bf8b2be'
            '1f4936c3d3ff31d69cba9c7e85a7d9b31577de30d4be7f36f6a27872dae2ff03'
            '4e69697249b749851a60a8b914fb5ca0b2e25d912ce6c45e73b4f235eed0e247'
            '67a849079e676c49142b41a4f0d388006a0f9db0f904e231b050a6c8bdd9c087'
            'a501e97b6ffbf0925bd3cf161f5cdeea7f43b8cad4ab26eed6f0a81fc2eff377'
            'c97766499ac0a73416168af1d7c5daa2bc950fa5d0c0af0e331da95bdce6e2a9'
            '2c5b0b7f6496192459e62e67d81d61ad44c3ec5752c5beabdb5253a34f0b1c23'
            'e89c149d2b5288e3ba7c52d9b2fdf6b37a8439a3938b4bc0138c9b99eb2b648a'
            'a9cf7a3f3eb9c68a5a54fee4f5cb5c4b51bfd4b55f3fe8b71aa4de8dfe85f3d7'
            'd29368428e74fb320052473d868bfcae9450a6efd371d160584676735ca4bd1a'
            '3062f2d69088411c08c8f17fd82fe475dcdfb13acbe0d29ec6d2bfb15062a472'
            'cac365f158c05feccfcf52557adb6e5de62a269a0b673b16eb2046e7c574be8e'
            'fc982bdf206d2412dc999ea6042653841c3aa1bd062d8cef6e5ac3dae4ae5837'
            '29d8d963f58295de6ceedb8be5f7c9f21c4fd14912e3ca06672ff26c51acdb66')

# 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 apply "${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.patch \
  0009-Do-not-convert-environment-for-strace.patch \
  0010-strace.cc-Don-t-set-MSYS-noglob.patch \
  0011-Add-debugging-for-strace-make_command_line.patch \
  0012-strace-quiet-be-really-quiet.patch \
  0013-path_conv-special-case-root-directory-to-have-traili.patch \
  0014-When-converting-to-a-Unix-path-avoid-double-trailing.patch \
  0015-msys2_path_conv-pass-PC_NOFULL-to-path_conv.patch \
  0016-path-conversion-Introduce-ability-to-switch-off-conv.patch \
  0017-dcrt0.cc-Untangle-allow_glob-from-winshell.patch \
  0018-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch \
  0019-Add-debugging-for-build_argv.patch \
  0020-environ.cc-New-facility-environment-variable-MSYS2_E.patch \
  0021-Introduce-the-enable_pcon-value-for-MSYS.patch \
  0022-popen-call-usr-bin-sh-instead-of-bin-sh.patch \
  0023-Expose-full-command-lines-to-other-Win32-processes-b.patch \
  0024-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch \
  0025-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch \
  0026-kill-kill-Win32-processes-more-gently.patch \
  0027-Cygwin-make-option-for-native-inner-link-handling.patch \
  0028-docs-skip-building-texinfo-and-PDF-files.patch \
  0029-install-libs-depend-on-the-toollibs.patch \
  0030-POSIX-ify-the-SHELL-variable.patch \
  0031-Handle-ORIGINAL_PATH-just-like-PATH.patch \
  0032-uname-allow-setting-the-system-name-to-CYGWIN.patch \
  0033-Pass-environment-variables-with-empty-values.patch \
  0034-Optionally-disallow-empty-environment-values-again.patch \
  0035-build_env-respect-the-MSYS-environment-variable.patch \
  0036-Revert-Cygwin-Enable-dynamicbase-on-the-Cygwin-DLL-b.patch \
  0037-Avoid-sharing-cygheaps-across-Cygwin-versions.patch \
  0038-uname-report-msys2-runtime-commit-hash-too.patch \
  0039-Cygwin-Adjust-CWD-magic-to-accommodate-for-the-lates.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"
  CXXFLAGS="$OPTIM -pipe -ggdb"

  # otherwise it asks git which appends "-dirty" because of our uncommited patches
  CFLAGS+=" -DCYGPORT_RELEASE_INFO=${pkgver}"

  (cd "${srcdir}/msys2-runtime/winsup" && ./autogen.sh)

  "${srcdir}"/msys2-runtime/configure \
    --with-msys2-runtime-commit="$(cat "${srcdir}/msys2-runtime.commit")" \
    --prefix=/usr \
    --build=${CHOST} \
    --sysconfdir=/etc \
    "${extra_config[@]}"
  LC_ALL=C make
  LC_ALL=C make -j1 DESTDIR="${srcdir}"/dest install

  rm -rf "${srcdir}"/dest/etc

  # split debug info from msys-2.0.dll
  cd "${srcdir}"/dest
  objcopy --add-gnu-debuglink=/dev/null --only-keep-debug usr/bin/msys-2.0.dll usr/bin/msys-2.0.dbg
  objcopy -g --add-gnu-debuglink=usr/bin/msys-2.0.dbg usr/bin/msys-2.0.dll usr/bin/msys-2.0.dll.new
  mv -f usr/bin/msys-2.0.dll.new usr/bin/msys-2.0.dll
}

package_msys2-runtime() {
  pkgdesc="Posix emulation engine for Windows"
  conflicts=('catgets' 'libcatgets' 'msys2-runtime-3.6')
  replaces=('catgets' 'libcatgets' 'msys2-runtime-3.6')

  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"
  depends=("msys2-runtime=${pkgver}")
  conflicts=('libcatgets-devel' 'msys2-runtime-3.6-devel')
  replaces=('libcatgets-devel' 'msys2-runtime-3.6-devel')
  # strip breaks the split debug info.  msys2/msys2-pacman#52
  options=('!strip')

  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/

  # compatibility with Cygwin toolchains
  cp "${srcdir}"/dest/usr/${CHOST}/lib/libmsys-2.0.a "${pkgdir}"/usr/lib/libcygwin.a
}
