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

_realname=nodejs
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=11.15.0
pkgrel=1
pkgdesc="Evented I/O for V8 javascript (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://nodejs.org/"
license=("MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-nasm"
             "make")
depends=("${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-http-parser"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-nghttp2"
         "${MINGW_PACKAGE_PREFIX}-libuv"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "winpty")
optdepends=("${MINGW_PACKAGE_PREFIX}-npm: nodejs package manager")
options=('!emptydirs' '!strip') # 'debug')

source=("https://nodejs.org/dist/v${pkgver}/node-v${pkgver}.tar.xz"
        '0001-Fix-python-invocation-to-python2.patch'
        '0002-Fix-system-icu-build.patch'
        '0003-Include-win32-headers.h-in-v8-atomicops.h.patch'
        '0004-Define-localtime_s-for-MinGW.patch'
        '0005-Remove-.lib-suffix-on-linked-libraries.patch'
        '0006-Define-_WIN32_WINNT-in-node.gyp.patch'
        '0007-Skip-sys-resource-and-unneeded-includes-in-node.cc.patch'
        '0008-Link-with-municode-and-extern-wmain-for-Unicode.patch'
        '0010-Fix-incorrect-test-assumptions-for-MinGW.patch'
        '0011-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-cctest-allow-building-with-municode.patch'
        '0016-format-macros.h-do-not-rely-on-__STDC_FORMAT_MACROS.patch'
        '0017-Do-not-get-confused-by-the-presence-of-winpthread.patch'
        '0018-Add-missing-include-to-build-condition-variable.cc-s.patch'
        '0020-inspector-use-namespaced-icu-symbols.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-gyp-mingw.patch'
        '0025-mingw-missing-features.patch'
        'node')

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

  patch -Np1 -i "${srcdir}/0001-Fix-python-invocation-to-python2.patch"
  patch -Np1 -i "${srcdir}/0002-Fix-system-icu-build.patch"
  patch -Np1 -i "${srcdir}/0003-Include-win32-headers.h-in-v8-atomicops.h.patch"
  patch -Np1 -i "${srcdir}/0004-Define-localtime_s-for-MinGW.patch"
  patch -Np1 -i "${srcdir}/0005-Remove-.lib-suffix-on-linked-libraries.patch"
  patch -Np1 -i "${srcdir}/0006-Define-_WIN32_WINNT-in-node.gyp.patch"
  patch -Np1 -i "${srcdir}/0007-Skip-sys-resource-and-unneeded-includes-in-node.cc.patch"
  patch -Np1 -i "${srcdir}/0008-Link-with-municode-and-extern-wmain-for-Unicode.patch"
  patch -Np1 -i "${srcdir}/0010-Fix-incorrect-test-assumptions-for-MinGW.patch"
  patch -Np1 -i "${srcdir}/0011-detect-architecture.patch"
  patch -Np1 -i "${srcdir}/0012-Use-shell-wrapper-script-for-npm-on-MSYS2-MinGW-w64.patch"
  patch -Np1 -i "${srcdir}/0013-Revert-to-FHS-installation-paths-on-Windows-tests-no.patch"
  patch -Np1 -i "${srcdir}/0015-cctest-allow-building-with-municode.patch"
  patch -Np1 -i "${srcdir}/0016-format-macros.h-do-not-rely-on-__STDC_FORMAT_MACROS.patch"
  patch -Np1 -i "${srcdir}/0017-Do-not-get-confused-by-the-presence-of-winpthread.patch"
  patch -Np1 -i "${srcdir}/0018-Add-missing-include-to-build-condition-variable.cc-s.patch"
  patch -Np1 -i "${srcdir}/0020-inspector-use-namespaced-icu-symbols.patch"
  patch -Np1 -i "${srcdir}/0021-Use-wide-character-Win32-API-functions-to-handle-wch.patch"
  patch -Np1 -i "${srcdir}/0022-Disable-MSVC_HACK.patch"
  patch -Np1 -i "${srcdir}/0023-Make-sure-that-__rdtsc-is-declared.patch"
  patch -Np1 -i "${srcdir}/0024-gyp-mingw.patch"
  patch -Np1 -i "${srcdir}/0025-mingw-missing-features.patch"
}

build() {

  local -a extra_config

  cd "${srcdir}"
  sleep 5

  [[ -d ${CARCH} ]] && rm -rf ${CARCH}
  mv node-v${pkgver} ${CARCH}
  cd ${CARCH}

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

  local opt_cpu="x64"
  if [ "${CARCH}" = "i686" ]; then
    opt_cpu="x86"
  fi

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

  ${MINGW_PREFIX}/bin/python2 tools/gyp_node.py -f make
  touch config.gypi
  # Work around the problem where the protocol files would be generated into a
  # subdirectory relative to deps/v8/src/inspector instead of relative to the
  # top-level directory.
  #CXXFLAGS=-D_WIN32_WINNT=0x0601 make out/Release/obj/gen/src/inspector/protocol/Forward.h
  CXXFLAGS=-D_WIN32_WINNT=0x0601 make V=1

  # 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}/${CARCH}"

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

package() {
  cd "${srcdir}/${CARCH}"

  ${MINGW_PREFIX}/bin/python2 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"

  install -D -m644 LICENSE \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/nodejs/LICENSE"

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

  install -m755 "${srcdir}/${CARCH}/deps/npm/bin/npm.cmd" "${pkgdir}${MINGW_PREFIX}/bin/npm.cmd"
}

sha256sums=('68a776c5d8b8b91a8f2adac2ca4ce4390ae1804883ec7ec9c0d6a6a64d306a76'
            '0e3b9ec420393c6d56fb5dcabb60acc7a1f50000ebea3a8bbbf6ecb87a00e404'
            'b5357ea485814c179b3b1131f985abf4e3992791d7008d7f9c60dde3cd800401'
            '7a0f96ba5b8639807a22c592d25e87bceae623d382c41bfc7f994367aa9b8ffb'
            '8c21a199822d3e106eacbc8e4ef923508293026edc8c55ad838c316cea5bda45'
            'a8640f1b7b90469ecc5314dd710ee18ae33ef18133263fdb497ff302dd38bc41'
            'afdc52f2aa8b2234ff0824521ad284bf6e3e1261fbd76e96a146643e6e47e4ff'
            'f15e5266eece74443bedfa9d6d1243083ef30b27cdf621cb4b4267866660387c'
            '6e1535166dc3fc75540f0e5e3fc25253cf488cb40a98a4b23f7b24e0059ca9f5'
            '59b052ff5e50f4ab49619bad1d8c388f8a5183256361452e262ffa9e079c99b6'
            '495b44761dde93a242850304038bd47fdc665d4bbc59e4b027c192afebc9283a'
            'b37fba2c8bb254905993b41d0ed319fa02bc37c88446ef0b2341ee92cc42faae'
            '9a29cb99c53dc39187d2cbb7e559130944063e07342802b9d77cfb037c8d9b30'
            '2005eb72281e1c727411015ced471b37999ccd062d60856cabd68032ed303601'
            '4fd604e2889e1741f8a7ed5b3e945d401eed2b13086daa4544384da4c04698d1'
            '3e5da564180238aeaf6e79b3273838465bed962fc65bbe8f0657e09ab0cabee3'
            '3a83695ebb617341b687133cd5319e0762c2f8af880800ac36de232311ccb903'
            'f7144d6fe1efee3d9134d92d3757c9bcc3f570dbca5a4fbadf3efd6492ba345f'
            '71e6b87542013c68641bcc8f98a769236f7181125960b2dcd1e7af516b1447ac'
            '44a8d757584cb725760c9d9bd661fb23ee0a2b555ffca8a449573e4d03db7dcc'
            '8cc3ef3ce149e5f0869d434538a9bd0a934afc52f0f571f6436694ce11587d12'
            '01eab68f9de8b7cbcae774b25c931a7f503a3419f20464ba1a7df9eea9fa7710'
            '6e223d2996f4cc02080c7b57521da6f7d29b41f41d4e225f8d1c941d25c1f963'
            '284251ff443506cd61530a026afb421d0084e4ad25c037bcfe3a045246d729a2')
