# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
# $Id$

PortSystem              1.0
PortGroup               compiler_blacklist_versions 1.0

name                    nodejs5
version                 5.12.0

categories              devel net
platforms               darwin
license                 {MIT BSD}
maintainers             ciserlohn

description             Evented I/O for V8 JavaScript

long_description        Node's goal is to provide an easy way to build scalable network programs in JavaScript. \
                        Node is similar in design to and influenced by systems like Ruby's Event \
                        Machine or Python's Twisted. Node takes the event model a bit further-it \
                        presents the event loop as a language construct instead of as a library.

conflicts               nodejs4 nodejs6 nodejs7

homepage                http://nodejs.org/
master_sites            ${homepage}dist/v${version}

checksums               rmd160  c5fd1aa1c8b440ddc41647f2d90119a0796e3cca \
                        sha256  250c12a561d7319e71e142ee92ab682494c7823d81ce24703c80eb52bdf9ba42

distname                node-v${version}

depends_build           port:pkgconfig

depends_lib             port:icu \
                        port:python27 \
                        path:lib/libssl.dylib:openssl

proc rec_glob {basedir pattern} {
    set files [glob -directory $basedir -nocomplain -type f $pattern]
    foreach dir [glob -directory $basedir -nocomplain -type d *] {
        eval lappend files [rec_glob $dir $pattern]
    }
    return $files
}

configure.python ${prefix}/bin/python2.7

patchfiles              patch-common.gypi.diff \
                        patch-tools-gyp-pylib-gyp-generator-make.py.diff

post-patch {
    foreach f [concat ${worksrcpath}/configure \
                   ${worksrcpath}/tools/gyp/gyp \
                   ${worksrcpath}/deps/cares/gyp_cares \
                   ${worksrcpath}/deps/v8/build/gyp_v8 \
                   ${worksrcpath}/deps/v8/tools/mingw-generate-makefiles.sh \
                   [rec_glob ${worksrcpath} *.py]] {
        reinplace "s|/usr/bin/env python|${configure.python}|" ${f}
    }
    foreach gypfile [rec_glob ${worksrcpath} *.gyp*] {
        reinplace "s|'python'|'${configure.python}'|" ${gypfile}
    }
    reinplace "s|python|${configure.python}|" ${worksrcpath}/deps/v8/build/toolchain.gypi
    reinplace "s|/usr/bin/env node|${prefix}/bin/node|" ${worksrcpath}/tools/doc/node_modules/marked/bin/marked
}

configure.args-append   --without-npm
configure.args-append   --with-intl=system-icu
configure.args-append   --shared-openssl
configure.args-append   --shared-openssl-includes=${prefix}/include/openssl
configure.args-append   --shared-openssl-libpath=${prefix}/lib

# V8 only supports ARM and IA-32 processors
supported_archs         i386 x86_64

universal_variant       no

# "V8 doesn't like cache."
configure.ccache        no

test.run                yes

use_parallel_build      no

switch $build_arch {
    i386 {
        configure.args-append   --dest-cpu=ia32
    }
    x86_64 {
        configure.args-append   --dest-cpu=x64
    }
}

build.args-append   CC=${configure.cc} \
                    CXX=${configure.cxx} \
                    CXX.host=${configure.cxx} \
                    CPP=${configure.cpp} \
                    CFLAGS="${configure.cflags}" \
                    CXXFLAGS="${configure.cxxflags}" \
                    LDFLAGS="${configure.ldflags}" \
                    PYTHON=${configure.python} \
                    V=1

destroot {
    set bindir ${destroot}${prefix}/bin
    set libdir ${destroot}${prefix}/lib
    set libndir ${libdir}/node
    set libddir ${libdir}/dtrace
    set incdir ${destroot}${prefix}/include/node
    set docdir ${destroot}${prefix}/share/doc/${name}

    xinstall -d ${bindir}
    xinstall -d ${libdir}
    xinstall -d ${libndir}
    xinstall -d ${libddir}
    xinstall -d ${incdir}
    xinstall -d ${docdir}

# install binaries
    xinstall -m 755 -W ${worksrcpath} \
        out/Release/node \
        ${bindir}

# install headers
    xinstall -m 644 -W ${worksrcpath} \
        src/async-wrap-inl.h \
        src/async-wrap.h \
        src/base-object-inl.h \
        src/base-object.h \
        src/env-inl.h \
        src/env.h \
        src/handle_wrap.h \
        src/node.h \
        src/node_buffer.h \
        src/node_constants.h \
        src/node_counters.h \
        src/node_crypto.h \
        src/node_crypto_bio.h \
        src/node_crypto_clienthello-inl.h \
        src/node_crypto_clienthello.h \
        src/node_crypto_groups.h \
        src/node_dtrace.h \
        src/node_file.h \
        src/node_http_parser.h \
        src/node_i18n.h \
        src/node_internals.h \
        src/node_javascript.h \
        src/node_object_wrap.h \
        src/node_root_certs.h \
        src/node_stat_watcher.h \
        src/node_version.h \
        src/node_watchdog.h \
        src/node_wrap.h \
        src/pipe_wrap.h \
        src/spawn_sync.h \
        src/stream_wrap.h \
        src/string_bytes.h \
        src/tcp_wrap.h \
        src/tls_wrap.h \
        src/tree.h \
        src/tty_wrap.h \
        src/udp_wrap.h \
        src/util-inl.h \
        src/util.h \
        src/v8abbr.h \
        deps/v8/include/v8-debug.h \
        deps/v8/include/v8-profiler.h \
        deps/v8/include/v8-testing.h \
        deps/v8/include/v8.h \
        deps/v8/include/v8-platform.h \
        deps/v8/include/v8-util.h \
        deps/v8/include/v8config.h \
        deps/uv/include/uv.h \
        deps/uv/include/uv-unix.h \
        deps/uv/include/uv-darwin.h \
        deps/uv/include/tree.h \
        deps/uv/include/uv-threadpool.h \
        deps/uv/include/pthread-fixes.h \
        deps/cares/include/ares.h \
        deps/cares/include/ares_version.h \
        deps/cares/include/nameser.h \
        ${incdir}

# install dtrace script
    xinstall -m 644 -W ${worksrcpath} \
        src/node.d \
        ${libddir}

# install manpage
    xinstall -m 644 -W ${worksrcpath} \
        doc/node.1 \
        ${destroot}${prefix}/share/man/man1

# install docs
    xinstall -m 644 -W ${worksrcpath} \
        AUTHORS \
        CHANGELOG.md \
        COLLABORATOR_GUIDE.md \
        CONTRIBUTING.md \
        GOVERNANCE.md \
        LICENSE \
        README.md \
        ${docdir}
}

compiler.blacklist *gcc* {clang < 300}

if {${os.major} < 10} {
    pre-fetch {
        ui_error "${name} ${version} requires Mac OS X 10.6 or greater."
        return -code error "incompatible Mac OS X version"
    }
}

livecheck.url       ${homepage}dist/
livecheck.type      regex
livecheck.regex     {v(5\.\d+\.\d+)}