# 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=21.6.2
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'
)
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
depends=("${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${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-base64-disable-inline-assembly-on-x86.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 "${srcdir}/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-base64-disable-inline-assembly-on-x86.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 "${srcdir}/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

  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 "${srcdir}/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 "${srcdir}/node-v${pkgver}"

  ${MINGW_PREFIX}/bin/python tools/install.py install "${pkgdir}" "/${MINGW_PREFIX}"

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

  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}

  # this is a bit redundant but the python install script only installs the posix npm / npx and corepack wrappers.
  # im not fluent enough in python to change this so atm this will have to do (ps. do not use install it leaves the older scripts if upgrading).

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

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

sha256sums=('191294d445d1e6800359acc8174529b1e18e102147dc5f596030d3dce96931e5'
            'd566e67401e496085b3e78cd4046f1973727036b932697d447032d9a8af24801'
            'e9a8bd133ec58cdf1987ec4c3dd7f0b2e290ebd708d0f988f56e28c01f376767'
            '2505e7faba4884010b89ec68f319667160929c9c7cbdcaae17d9eedba8195726'
            '38f7e93770f3788e7d23915c8fe2e3e625f2191f827a389115930c9a2313aec8'
            '5cb60fcd16378926fa89ffd114b8244bb063bc8a890f141858952084a9aabd0d'
            '55501a2154f6fe37fce3728a38c8775d03532a9ddfa32214ab63112fd482836c'
            '8a685608e4ef8d2a53222135dfbc51bb337b043939dcd7f50683acd8787f4fe1'
            'c2daca3eec876a98adcbb35c7438afe24beb8a1953b62947912f7ea25c63fcb9'
            '6f5f3f76c44b2857459b8f9a8e0eefd737d4c2d89f02b7545762fb151d71d85f'
            '317033198f387743e760efc8953f2e2fb056128693f504415e0fe8bc872de0dc'
            'a5d0ea7197f8f16bd3ad5869f43b71d3b3dbc2ef222f0ecec49ce4a813c41442'
            '99e64bacf33db5cee211eba00841368d22b40030a91e0475c93c4fe4f76f27d1'
            'ec5e6dcb3207682b99990af05a93f4f1e653307fa928c4c48e0fdf1e60000861'
            '08ca21ae3202c5081b23b7b86c5dd8a93e4c85924edc939278873c09935e64d6'
            '208e9816d851245271a78516e86505c03f3fc7918663372974a128e2f1f09c62'
            'c7d56e545bf21328b0e9f88add15dc7bc39f3adb90d2079bf3448cc8746fa9e3'
            '29807f224790d5dd4503ae18d1953ecf827e9199a7da80abf129b1c36e6edcb1'
            '4606ae71cbee41bd15edf771393f4e45b379eaf79d53657a88c866b56fec1d34'
            '7d498f8090ab05458f28a5d2c60ef8511d2b48bc5368fda06922c35ca2584a22'
            '8b992960c07839e2040c693dc75cd8347a53630e5c9757b3dfb8c4248458433e'
            '00018068327e4bb84fd8043245a9f989c4c0008019fa6e0bab99d11ad7c8bf27'
            'b1d14a5bc854bbee934a5b04cc2f9c5559fa7d54db45db92a4663e7117150640'
            '67d40fde6aaa108db2de4d946874857607bade8fa0ee76d77b759439fa80f246'
            '492758c11d287d88ea035d84f5ab3eb0dc76da0b8552f6ad13c4cd89597f4183'
            'd5cc3e2bf8b506dce2a96105eb6a6ff300088a7b488cbe6d390efdb7d6c630e5'
            'b07a5125182104cd43a1bd07a637096962fc475664e2292cc4bd79a69ca442da'
            '55bd40a5774e034c5c4854c3523bdcb2e19b641f181d127b2b5ad0631a154ec0'
            '94c7e9410e8026f6c0ccbe867771c416b08eb6008a094bfd8e12258d2ba39641'
            '1e6777fbea1c2365e642758dac71d1242e27b152e2df7bc60e72ef70d6cf240c'
            'e1d9c6fd7c3533ad9cb1a102ebaad0df9c05024b7a643257ae41d968182ef789'
            '0bab8631eff9c287af4d3067e2c520ce0c81415e225011f9e75eae5d2bffac0d'
            '284251ff443506cd61530a026afb421d0084e4ad25c037bcfe3a045246d729a2')
