# 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=24.8.0
pkgrel=2
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')
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")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-winpthreads")
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
        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)
sha256sums=('1c03b362ebf4740d4758b9a3d3087e3de989f54823650ec80b47090ef414b2e0'
            'a3ed80fc1b098c3fe10e593d2a110dd44b5e91872e2b82e3c6644409a9a9ffce'
            '19b95bb9882c4e952d667279c4652384e7f4b3c2b9d190c73398d2a82692be80'
            'df4ee2b86ebb029d26d65480c758e33e6d30a8423235b168f7bcf6ffaf963e67'
            'b3bdd65071a373d013b2d1d39a4d286ee1ff2ca7ba854139fddfe10d64d9b1d8'
            '23138078f8efb7ef06bfe379f7ffbcbd2f7db226dadf6ba8eaad336073464ff5'
            '1e7bbd76250a09802f5e692055cd1cd270f2e01397727222263f217702f177da'
            '0fb141e001f1f68d26beda2c0c7b10c08908346ec73301225115efd002b324d0'
            '86b41eb44c434c9c6524eda925ecbb50d61b84617171af06e220d8c7e3aa8407'
            '4d651f257b62f21fcb906920e6e3dd5d8f35160c6a295fca3c3031bf88caf747'
            '244bb3afcb8cacaa6e251731cdc036a4cb444069b6fd2b2fe508006fe9a830c1'
            '498a4344a502e99a89f5b9271878f58e7116e00dc073d7fb693c953bf5e20925'
            '67953237885a5b990f326b7155780ea082d933ee40d071cba22953d9d8299d3d'
            '9a4e74650dfd23b80803469bb7928a775d1ba812a4e59939f73ff4c8a12ee90f'
            '208e9816d851245271a78516e86505c03f3fc7918663372974a128e2f1f09c62'
            '0825ce613a44893027359f37bc92aabe2d4e13af285ef3fcc4ce5f1187283cb4'
            'b86888ea96a1c1447ac7c5c221ada6dd273f4d136d291d027cc464ff2d808126'
            '941f12ce16a0df6d8c5ecf65f165f7311f3ead4903889df872ef3b7206e25237'
            '7d498f8090ab05458f28a5d2c60ef8511d2b48bc5368fda06922c35ca2584a22'
            'e599605b3994c17514a2efcc84109c01d716a9bb5c819873187721e78a28d079'
            '18ca0c99da6761aeba3b2e8b6fa23787d7d3114e1798fecd9037e9a7a8bc93c1'
            '34f7acaca209070758e5a3d38ea49abd211e8333aae2bfba8407f86237dc517f'
            'd4ee69098fc1d00b960ceef3a9affaa4c5a1f9d75d004ba8389889f6b2d89c3d'
            '492758c11d287d88ea035d84f5ab3eb0dc76da0b8552f6ad13c4cd89597f4183'
            '03662d63d260aca7c97b5671d6905f7c236c91eb7a0585109c6c9ad63f1e94cc'
            'b74430e6949b3eff70026a82c10dc2465272c983ee50ca44d53784653d1247c4'
            '5612385ed47697c86fd75d174dc240f29715329c4f152e85a6f1fae8abc44d56'
            'cabfffd83ac34c33de592bda2621285a4937842edc6bdcbe312743f3495354c2'
            'f136436c52b6fa837d1a9988f5989df672ce80a221032731cf508c5980be9b7e'
            '8db352ad6be42ac3ab14500796e328d18d6ccb24bb57f1c606751de59b1bbc7a'
            '3ec18a527cd853294089c78c1356eaf904ce85df7a3fa534999cfd58249e00fb'
            '284251ff443506cd61530a026afb421d0084e4ad25c037bcfe3a045246d729a2')

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 \
    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

  find 'deps/npm/node_modules' -depth -name '*.orig' -exec rm -f {} ';'
}

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