# -*- 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: Portfile 92102 2012-04-18 19:12:36Z ciserlohn@macports.org $

PortSystem              1.0

name                    nodejs-devel
version                 0.7.8
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               nodejs

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

checksums               rmd160  01ad5dd606f1747d969b413f2253810705f8ec50 \
                        sha256  59722d4a71c26963a8bbd31aadc64aeff2c0a2902fb4d9542d161f458487355e

distname                node-v${version}

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

post-patch {
    foreach f [concat ${worksrcpath}/configure \
                   ${worksrcpath}/tools/node-waf \
                   ${worksrcpath}/tools/waf-light \
                   ${worksrcpath}/tools/gyp/gyp \
                   ${worksrcpath}/tools/gyp_addon \
                   ${worksrcpath}/tools/gyp_node \
                   ${worksrcpath}/deps/uv/gyp_uv \
                   ${worksrcpath}/tools/gyp/test/mac/gyptest-sourceless-module.gyp \
                   ${worksrcpath}/tools/gyp/test/mac/gyptest-postbuild-static-library.gyp \
                   [rec_glob ${worksrcpath} *.py]] {
        reinplace "s|/usr/bin/env python|${configure.python}|" ${f}
    }
}

configure.args          --without-ssl
configure.args-append   --without-npm

variant python25 conflicts python26 python27 description {Use python 2.5} {
    depends_lib         port:python25
    configure.python    ${prefix}/bin/python2.5
}

variant python26 conflicts python25 python27 description {Use python 2.6} {
    depends_lib         port:python26
    configure.python    ${prefix}/bin/python2.6
}

variant python27 conflicts python25 python26 description {Use python 2.7} {
    depends_lib         port:python27
    configure.python    ${prefix}/bin/python2.7
}

if {![variant_isset python25] && ![variant_isset python26]} {
    default_variants +python27
}

variant ssl description {Add secure socket layer support} {
    depends_build-append    port:pkgconfig
    depends_lib-append      port:openssl
    configure.args-delete   --without-ssl
}

default_variants        +ssl

# 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} \
                    CPP=${configure.cpp} \
                    CFLAGS=${configure.cflags} \
                    LDFLAGS=${configure.ldflags} \
                    PYTHON=${configure.python}

destroot {
    set bindir ${destroot}${prefix}/bin
    set libdir ${destroot}${prefix}/lib/node
    set incdir ${destroot}${prefix}/include/node
    set incpdir ${destroot}${prefix}/include/node/uv-private
    set docdir ${destroot}${prefix}/share/doc/${name}

    xinstall -d ${bindir}
    xinstall -d ${libdir}
    xinstall -d ${incdir}
    xinstall -d ${incpdir}
    xinstall -d ${docdir}

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

# install headers
    xinstall -m 644 -W ${worksrcpath} \
        src/node.h \
        src/node_buffer.h \
        src/node_object_wrap.h \
        src/node_version.h \
        deps/v8/include/v8-debug.h \
        deps/v8/include/v8-preparser.h \
        deps/v8/include/v8-profiler.h \
        deps/v8/include/v8-testing.h \
        deps/v8/include/v8.h \
        deps/v8/include/v8stdint.h \
        deps/uv/include/uv.h \
        deps/uv/include/ares.h \
        deps/uv/include/ares_version.h \
        ${incdir}

# install privat uv headers
    xinstall -m 644 -W ${worksrcpath} \
        deps/uv/include/uv-private/eio.h \
        deps/uv/include/uv-private/ev.h \
        deps/uv/include/uv-private/ngx-queue.h \
        deps/uv/include/uv-private/tree.h \
        deps/uv/include/uv-private/uv-unix.h \
        deps/uv/include/uv-private/uv-win.h \
        ${incpdir}

# install the wafadmin tool
    eval xinstall -m 644 [rec_glob ${worksrcpath}/tools/wafadmin *.py] ${libdir}

# install docs
    xinstall -m 644 -W ${worksrcpath} \
        AUTHORS \
        ChangeLog \
        LICENSE \
        README.md \
        ${docdir}
}

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

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