# Maintainer: Martell Malone <martellmalone@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>
# Contributor: David Macek <david.macek.0@gmail.com>
# Contributor: Mateusz Mikuła <mati865@gmail.com>
# Contributor: Alethea Rose <alethea@alethearose.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=nodejs
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=22.5.1
pkgrel=1
pkgdesc="An open-source, cross-platform JavaScript runtime environment (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://nodejs.org/"
msys2_repository_url='https://github.com/nodejs/node'
msys2_documentation_url='https://nodejs.org/download/docs'
msys2_references=(
  'archlinux: nodejs'
  "cpe: cpe:/a:nodejs:node.js"
  "cpe: cpe:/a:nodejs:nodejs"
)
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-winpthreads")
depends=("${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "winpty")
source=("https://nodejs.org/dist/v${pkgver}/node-v${pkgver}.tar.xz"
        0002-Fix-system-icu-build.patch
        0004-Define-localtime_s-for-MinGW.patch
        0005-Remove-.lib-suffix-on-linked-libraries.patch
        0006-Define-_WIN32_WINNT-in-node.gypi.patch
        0007-Skip-sys-resource-and-unneeded-includes.patch
        0008-Link-with-municode-and-extern-wmain-for-Unicode.patch
        0010-Fix-incorrect-test-assumptions-for-MinGW.patch
        0011-Properly-detect-architecture.patch
        0012-Use-shell-wrapper-script-for-npm-on-MSYS2-MinGW-w64.patch
        0013-Revert-to-FHS-installation-paths-on-Windows-tests-no.patch
        0015-Remove-v8-export-private-refs.patch
        0016-Do-not-rely-on-__STDC_FORMAT_MACROS.patch
        0017-Do-not-get-confused-by-the-presence-of-winpthread.patch
        0021-Use-wide-character-Win32-API-functions-to-handle-wch.patch
        0022-Disable-MSVC_HACK.patch
        0023-Make-sure-that-__rdtsc-is-declared.patch
        0024-Fix-gyp-mingw.patch
        0025-Fix-globalPrefix-on-mingw.patch
        0026-Embed-manifest-in-node-exe.patch
        0101-node-buildflags-fixes.patch
        0102-set-cxx-standard-to-gnu.patch
        0103-node-gyp-support-mingw-toolchain.patch
        0104-v8-fix-static-assert-implementations.patch
        0105-v8-fix-conflicting-functions.patch
        0106-v8-support-clang-in-mingw-mode.patch
        0107-v8-disable-system-instrumentation.patch
        0108-v8-disable-etw-stack-walking.patch
        0109-v8-builtin-deps-fixes.patch
        0110-v8-heap-use-proper-sources.patch
        0201-v8-buildflags-fixes.patch
        0202-v8-builtin-deps-buildflags-fixes.patch
        node)

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "node-v${pkgver}"

  apply_patch_with_msg \
    0002-Fix-system-icu-build.patch \
    0004-Define-localtime_s-for-MinGW.patch \
    0005-Remove-.lib-suffix-on-linked-libraries.patch \
    0006-Define-_WIN32_WINNT-in-node.gypi.patch \
    0007-Skip-sys-resource-and-unneeded-includes.patch \
    0008-Link-with-municode-and-extern-wmain-for-Unicode.patch \
    0010-Fix-incorrect-test-assumptions-for-MinGW.patch \
    0011-Properly-detect-architecture.patch \
    0012-Use-shell-wrapper-script-for-npm-on-MSYS2-MinGW-w64.patch \
    0013-Revert-to-FHS-installation-paths-on-Windows-tests-no.patch \
    0015-Remove-v8-export-private-refs.patch \
    0016-Do-not-rely-on-__STDC_FORMAT_MACROS.patch \
    0017-Do-not-get-confused-by-the-presence-of-winpthread.patch \
    0021-Use-wide-character-Win32-API-functions-to-handle-wch.patch \
    0022-Disable-MSVC_HACK.patch \
    0023-Make-sure-that-__rdtsc-is-declared.patch \
    0024-Fix-gyp-mingw.patch \
    0025-Fix-globalPrefix-on-mingw.patch \
    0026-Embed-manifest-in-node-exe.patch

  apply_patch_with_msg \
    0101-node-buildflags-fixes.patch \
    0102-set-cxx-standard-to-gnu.patch \
    0103-node-gyp-support-mingw-toolchain.patch \
    0104-v8-fix-static-assert-implementations.patch \
    0105-v8-fix-conflicting-functions.patch \
    0106-v8-support-clang-in-mingw-mode.patch \
    0107-v8-disable-system-instrumentation.patch \
    0108-v8-disable-etw-stack-walking.patch \
    0109-v8-builtin-deps-fixes.patch \
    0110-v8-heap-use-proper-sources.patch

  apply_patch_with_msg \
    0201-v8-buildflags-fixes.patch \
    0202-v8-builtin-deps-buildflags-fixes.patch
}

build() {
  cd "node-v${pkgver}"

  local -a extra_config
  if check_option "debug" "y"; then
    extra_config+=( --debug )
  fi

  local opt_cpu="x64"
  if [[ ${CARCH} == aarch64 ]]; then
    opt_cpu="arm64"
  fi

  if [[ ${MSYSTEM} == CLANG* ]]; then
    extra_config+=( --clang-cl=1 )
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ./configure \
    --prefix="${MINGW_PREFIX}" \
    --dest-os=win \
    --dest-cpu=${opt_cpu} \
    --shared \
    --shared-cares \
    --shared-openssl \
    --shared-zlib \
    --with-intl=system-icu \
    "${extra_config[@]}"

  make

  # flatten the nested node_modules/ hierarchy
  test -z "$(find deps/npm/node_modules | grep '^.\{140,\}$')" ||
  (cd deps/npm &&
   cp -R . ../npm.bup &&
   rm -rf node_modules &&
   ../../out/Release/node.exe ../npm.bup/bin/npm-cli.js install --production)
}

check() {
  cd "node-v${pkgver}"

  # A few tests still fail due to a bug with pausing/resuming file streams
  ${MINGW_PREFIX}/bin/python tools/test.py --mode=release -J \
    cctest doctool inspector message parallel sequential tick-processor || true
}

package() {
  cd "node-v${pkgver}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  python tools/install.py install --dest-dir="${pkgdir}" --prefix="${MINGW_PREFIX}"

  mv -v "${pkgdir}${MINGW_PREFIX}/bin/node"{.exe,_exe}
  install -Dm755 "${srcdir}/node" "${pkgdir}${MINGW_PREFIX}/bin/node"
  mv -v "${pkgdir}${MINGW_PREFIX}/bin/node"{_exe,.exe}

  # install the cmd-based wrappers
  cp -f ${srcdir}/node-v${pkgver}/deps/npm/bin/node-gyp-bin/node-gyp.cmd "${pkgdir}${MINGW_PREFIX}/bin"
  cp -f ${srcdir}/node-v${pkgver}/deps/npm/bin/{corepack,npm,npx}.cmd "${pkgdir}${MINGW_PREFIX}/bin"

  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/nodejs"
  cp -r doc/api/{*.html,assets} "${pkgdir}${MINGW_PREFIX}/share/doc/nodejs"

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/nodejs/LICENSE"
}

sha256sums=('924f381a32cf26b6bedbe95feedde348450f4fd321283d3bf3f7965aa45ce831'
            'cb56e4f4160dae1041448be64760296634a3aa86fb163ccbfd645f4ae96d2026'
            'e9a8bd133ec58cdf1987ec4c3dd7f0b2e290ebd708d0f988f56e28c01f376767'
            'c420caaa03bc1c809f48ddcb19157ba0284e33fb6cffff8354c9fc4aa75e2afc'
            '38f7e93770f3788e7d23915c8fe2e3e625f2191f827a389115930c9a2313aec8'
            '5cb60fcd16378926fa89ffd114b8244bb063bc8a890f141858952084a9aabd0d'
            'b04f402e65556bcda04f021c7f159ed85238c3517b40cdf38aaba53169754666'
            '8a685608e4ef8d2a53222135dfbc51bb337b043939dcd7f50683acd8787f4fe1'
            'c2daca3eec876a98adcbb35c7438afe24beb8a1953b62947912f7ea25c63fcb9'
            '63cc0a5996fcbdc0959ede2755740493b7c3eda5d878dacc76f6ddec22321c65'
            '5d986e7f7654778f02e624eb2ac15765e35559bac2c9aa37dae010bc329e05bd'
            '1e9c1d3c3d49ab922c002439a383175208a9c38e6a41dfc66a6ea52e60b0618f'
            '99e64bacf33db5cee211eba00841368d22b40030a91e0475c93c4fe4f76f27d1'
            'ec5e6dcb3207682b99990af05a93f4f1e653307fa928c4c48e0fdf1e60000861'
            '08ca21ae3202c5081b23b7b86c5dd8a93e4c85924edc939278873c09935e64d6'
            '208e9816d851245271a78516e86505c03f3fc7918663372974a128e2f1f09c62'
            'c7d56e545bf21328b0e9f88add15dc7bc39f3adb90d2079bf3448cc8746fa9e3'
            '335a58064e22d0ca140be69060951054dbabf5c4c929d7831209609da48aa506'
            '701cef4edacfd6b2512bb7a42ef388eb704bc500108062af4ead0439667da416'
            '7d498f8090ab05458f28a5d2c60ef8511d2b48bc5368fda06922c35ca2584a22'
            '601ca4c84533c88fe30b5bbfa3f9cabc51a7316b8ce88e56a641b10861d66135'
            '18ca0c99da6761aeba3b2e8b6fa23787d7d3114e1798fecd9037e9a7a8bc93c1'
            'b1d14a5bc854bbee934a5b04cc2f9c5559fa7d54db45db92a4663e7117150640'
            '67d40fde6aaa108db2de4d946874857607bade8fa0ee76d77b759439fa80f246'
            '492758c11d287d88ea035d84f5ab3eb0dc76da0b8552f6ad13c4cd89597f4183'
            'd5cc3e2bf8b506dce2a96105eb6a6ff300088a7b488cbe6d390efdb7d6c630e5'
            'b07a5125182104cd43a1bd07a637096962fc475664e2292cc4bd79a69ca442da'
            '22330afcfd297e680062f89d62816afa7b3e6b316950927fe31c6eff31642f26'
            '788858123f565ae0526b438f11ead5abbc2ec9239565e295c256d71d8a0d2d20'
            '0ecb7cad2ff7332bf1885a9c7f477413b1cd68bbe9026854f376c1aeb5a378df'
            '5147ddf0d5905ce83a700b7028dc93879a2570ce49e70efe7a3e37d20d4607cd'
            'ef4304e43d86f79adceabe04068e5b19b0b24b880731abc02517230b83484638'
            '284251ff443506cd61530a026afb421d0084e4ad25c037bcfe3a045246d729a2')
