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

PortSystem          1.0
PortGroup           github 1.0

github.setup        OpenNI OpenNI 1.5.7.10 Stable-
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
name                openni
categories          graphics
maintainers         ryandesign openmaintainer
license             LGPL-3+
supported_archs     i386 x86_64

# Remove this when updating to the next version
distname            ${name}-${version}

description         APIs for natural interaction devices

long_description    The OpenNI framework provides a set of APIs for accessing \
                    natural interaction devices, including support for voice \
                    and voice command recognition, hand gestures, and body \
                    motion tracking.  Requires Java 6 for OS X, at least to \
                    build and appears to fail with other versions.  Java 6 \
                    available at https://support.apple.com/kb/DL1572?locale=en_US

checksums           rmd160  2d78c4fce0908b1e4801649b6390c0d6a8afc68f \
                    sha256  4b3ac2f946849d6a2b6fa12f199025a9c141fda93d4d4bda967a3fb32268b5ce

depends_build       path:bin/doxygen:doxygen \
                    path:bin/dot:graphviz

depends_lib         path:lib/pkgconfig/libusb-1.0.pc:libusb

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

build.dir           ${worksrcpath}/Platform/Linux/CreateRedist

post-extract {
    file attributes ${build.dir}/RedistMaker -permissions a+x
    # DOS to UNIX line endings so we can patch.
    reinplace "s|\r||g" ${worksrcpath}/Source/OpenNI/XnOpenNI.cpp
}

patchfiles          patch-Platform-Linux-Build-Common-CommonCppMakefile.diff \
                    patch-Platform-Linux-Build-Common-Platform.x86.diff \
                    patch-Platform-Linux-CreateRedist-install.sh.diff \
                    patch-Source-OpenNI-XnOpenNI.cpp.diff

post-patch {
    reinplace "s|@PREFIX@|${prefix}|g"  ${build.dir}/install.sh \
                                        ${worksrcpath}/Platform/Linux/Build/Common/CommonCppMakefile \
                                        ${worksrcpath}/Source/OpenNI/XnOpenNI.cpp

    # Mavericks system headers have a function called "equivalent" with which this #define interferes.
    # https://github.com/OpenNI/OpenNI2/issues/52
    foreach sample {NiHandTracker NiSimpleViewer NiUserSelection NiUserTracker NiViewer} {
        reinplace "s|equivalent|openni_equivalent|g" ${worksrcpath}/Samples/${sample}/glh/glh_linear.h
    }
}

use_configure       no

variant universal {}

build.cmd           ./RedistMaker
build.env           "CXX=${configure.cxx} [get_canonical_archflags cxx]"
build.target

pre-destroot {
    destroot.dir    [glob ${worksrcpath}/Platform/Linux/Redist/*]
}
destroot.cmd        ./install.sh
destroot.target
destroot.destdir    -c ${destroot}
destroot.keepdirs   ${destroot}${prefix}/var/lib/ni

set libs {libnimMockNodes.dylib libnimCodecs.dylib libnimRecorder.dylib}

post-activate {
    foreach lib ${libs} {
        system "${prefix}/bin/niReg -r ${prefix}/lib/${lib}"
    }
}

pre-deactivate {
    foreach lib ${libs} {
        system "${prefix}/bin/niReg -u ${prefix}/lib/${lib}"
    }
}
