# Contributor: William Pitcock <nenolod@dereferenced.org>
# Contributor: Jose-Luis Rivas <ghostbar@riseup.net>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: Dave Esaias <dave@containership.io>
# Contributor: Tadahisa Kamijo <kamijin@live.jp>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
#
# secfixes:
#   6.11.1-r0:
#     - CVE-2017-1000381
#
setvar pkgname = 'nodejs'
# Note: Update only to even-numbered versions (e.g. 6.y.z, 8.y.z)!
# Odd-numbered versions are supported only for 9 months by upstream.
setvar pkgver = '6.11.4'
setvar pkgrel = '0'
setvar pkgdesc = ""JavaScript runtime built on V8 engine - LTS version""
setvar url = ""http://nodejs.org/""
setvar arch = ""all""
setvar license = ""MIT""
setvar depends = ""ca-certificates""
setvar depends_dev = ""libuv""
# gold is needed for mksnapshot
setvar makedepends = ""$depends_dev python2 openssl-dev zlib-dev libuv-dev linux-headers
	paxmark binutils-gold http-parser-dev ca-certificates c-ares-dev""
setvar subpackages = ""$pkgname-dev $pkgname-doc $pkgname-npm::noarch""
setvar provides = ""nodejs-lts=$pkgver""  # for backward compatibility
setvar replaces = ""nodejs-current nodejs-lts""  # nodejs-lts for backward compatibility
setvar source = ""https://nodejs.org/dist/v$pkgver/node-v$pkgver.tar.gz
	dont-run-gyp-files-for-bundled-deps.patch
	ppc-fix-musl-mcontext.patch""
setvar builddir = ""$srcdir/node-v$pkgver""

proc prepare {
	default_prepare

	# Remove bundled dependencies that we're not using.
	rm -rf deps/http_parser deps/openssl deps/uv deps/zlib
}

proc build {
	cd $builddir

	./configure --prefix=/usr \
		--shared-zlib \
		--shared-libuv \
		--shared-openssl \
		--shared-http-parser \
		--shared-cares \
		--openssl-use-def-ca-store

	# We need run mksnapshot at build time so paxmark it early.
	make -C out mksnapshot BUILDTYPE=Release
	paxmark -m out/Release/mksnapshot
	make

	# paxmark so JIT works
	paxmark -m out/Release/node
}

# TODO Run provided test suite.
proc check {
	cd "$builddir"/out/Release

	./node -e 'console.log("Hello, world!")'
	./node -e "require('assert').equal(process.versions.node, '$pkgver')"
}

proc package {
	cd $builddir

	make DESTDIR="$pkgdir" install

	# It's strange, but it really needs to be paxmarked again...
	paxmark -m "$pkgdir"/usr/bin/node

	cp -pr "$pkgdir"/usr/lib/node_modules/npm/man "$pkgdir"/usr/share
	local d; for d in doc html man {
		rm -r "$pkgdir"/usr/lib/node_modules/npm/$d
	}
}

proc dev {
	setvar provides = ""nodejs-lts-dev=$pkgver""  # for backward compatibility

	default_dev

	# Remove some junk.
	rm -r "$subpkgdir"/usr/lib/node_modules
	rmdir -p "$subpkgdir"/usr/lib || :
}

proc npm {
	setvar pkgdesc = ""A package manager for JavaScript""
	setvar depends = "$pkgname"
	setvar replaces = ""nodejs-current-npm $pkgname""  # $pkgname for backward compatibility

	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/npm "$subpkgdir"/usr/bin/

	mkdir -p "$subpkgdir"/usr/lib/node_modules
	mv "$pkgdir"/usr/lib/node_modules/npm "$subpkgdir"/usr/lib/node_modules/
}

setvar sha512sums = ""ab71d06b460dca4b1153796fa1824d87a49ea47f907e3934ab160c5276dc8f61f110839065e2e89daa1454a8abd586488d9e3d162009b76436b8630c720da25c  node-v6.11.4.tar.gz
a8be538158b7c96341a407acba30450ddc5c3ad764e7efe728d1ceff64efc3067b177855b9ef91b54400be6a02600d83da4c21a07ae9d7dc0774f92b2006ea8b  dont-run-gyp-files-for-bundled-deps.patch
54a96cdc103bdffa9ba5283f59c64a35774e272f3a944d6475e3f669f95f7d75bcca6db3b12b9af76ea463f531763105aeabb302872652ced6a2bcb66f1eace0  ppc-fix-musl-mcontext.patch""