# 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=8.11.1
pkgrel=5
pkgdesc="Evented I/O for V8 javascript (mingw-w64)"
arch=('any')
url="https://nodejs.org/"
license=("MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-python2" 'make')
depends=("${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-http-parser"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${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'
        '0009-Remove-no_rand_screen-from-s_client-invocation.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'
        '0014-Fix-linking-of-cctest-in-mingw-w64.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'
        '0019-Do-not-let-nghttp2-pretend-that-ssize_t-is-int.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'
        '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}/0009-Remove-no_rand_screen-from-s_client-invocation.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}/0014-Fix-linking-of-cctest-in-mingw-w64.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}/0019-Do-not-let-nghttp2-pretend-that-ssize_t-is-int.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"
}

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-openssl \
    --shared-zlib \
    --shared-cares \
    --with-intl=system-icu \
    --shared-http-parser \
    --without-perfctr \
    --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=0x0600 make out/Release/obj/gen/src/inspector/protocol/Forward.h
  CXXFLAGS=-D_WIN32_WINNT=0x0600 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}/${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=('40a6eb51ea37fafcf0cfb58786b15b99152bec672cccf861c14d1cca0ad4758a'
            '50ae0cebb123b4617fb79961ae6f95fb2bb0da17d0dcf5daa89f54f3b8c8261d'
            'fb3aaa1d81b0330d6771c0127aea12f19dcdef8ff21bc7c6037e87f320228ce0'
            '7a0f96ba5b8639807a22c592d25e87bceae623d382c41bfc7f994367aa9b8ffb'
            '8c21a199822d3e106eacbc8e4ef923508293026edc8c55ad838c316cea5bda45'
            '891eaf02a89b3e632d71f8331a80ae26f3abb78b6e0c14e811ae6f3148459bdc'
            'ca3d3829d5e7f2778854f1a14ab0c46e18398a979805093b419e66cc01b3d940'
            'd4165599af3ac6008bd1d99c51c35ca13c848a2631da937e4c45e63a0d3c598f'
            'a70ee9d8122c5a4178028fe9c337746f2cbe1427ce24e502a1c1301e57401815'
            'a383724fd58925bda9fd9a149da79b8afdde4fd55a1298c7cdda297a2d3f5bab'
            '7966b11c2e413f74d00d5078ca065e3f14957750ae7741a0520f17f203097a2b'
            '0bf9e2d240b5d53435e278066e8cb0bd695b8fd4e6cce4b48a67bfa22f1b68c9'
            'b37fba2c8bb254905993b41d0ed319fa02bc37c88446ef0b2341ee92cc42faae'
            '89fde4ba2d92f007f3f27896f9253f6a3b7668595b7f68cae2756e6256a861ac'
            'd84636872a1f8d3692cf8748c8d9fb21d0314b23a0ec89b30065ddbd058176bb'
            '2005eb72281e1c727411015ced471b37999ccd062d60856cabd68032ed303601'
            '4fd604e2889e1741f8a7ed5b3e945d401eed2b13086daa4544384da4c04698d1'
            '3e5da564180238aeaf6e79b3273838465bed962fc65bbe8f0657e09ab0cabee3'
            '3a83695ebb617341b687133cd5319e0762c2f8af880800ac36de232311ccb903'
            '2d91635773140d50b2e2065898b89550fecf76ca984954e8bc53dc2ef2650714'
            'f7144d6fe1efee3d9134d92d3757c9bcc3f570dbca5a4fbadf3efd6492ba345f'
            'b68ecff38ab7e1a22274a48f0487cebe2769e9222d75da96f8eab2732d79efb9'
            '44a8d757584cb725760c9d9bd661fb23ee0a2b555ffca8a449573e4d03db7dcc'
            '8cc3ef3ce149e5f0869d434538a9bd0a934afc52f0f571f6436694ce11587d12'
            '284251ff443506cd61530a026afb421d0084e4ad25c037bcfe3a045246d729a2')
