# -*- 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 114119 2013-11-29 22:31:11Z jmr@macports.org $

PortSystem          1.0

name                virtualbox
version             4.2.16
# Note: On virtualbox version changes please update the checksums of all
# subports, and update extension_pack_build to match the current build
# of the extension pack.
# Also remember to revision bump port ld64
set extension_pack_build   86992

categories          emulators
platforms           darwin
license             GPL-2
maintainers         nomaintainer

description         open source virtualization technology from Oracle
long_description \
    VirtualBox is a powerful x86 and AMD64/Intel64 virtualization product for \
    enterprise as well as home use. Not only is VirtualBox an extremely \
    feature rich, high performance product for enterprise customers, it is \
    also the only professional solution that is freely available as Open \
    Source Software.

homepage            http://www.virtualbox.org/
master_sites        http://download.virtualbox.org/virtualbox/${version}

if {${name} == ${subport}} {

    PortGroup           qt4 1.0
    # virtualbox installs a kernel extension so it has to build
    # for the same architecture as the kernel
    set kernel_arch [exec uname -m]
    switch ${kernel_arch} {
        i386 -
        x86_64 {
            supported_archs ${kernel_arch}
        }
        default {
            supported_archs i386 x86_64
        }
    }

    universal_variant   no

    distname            VirtualBox-${version}
    use_bzip2           yes

    checksums           rmd160  a49d044f6935199a71f6bdf94fd9082f9888d2d6 \
                        sha256  ff0f8916d039120554d9d73c7cae1863d6161933374374ec2f5313892046d45a

    depends_lib-append          port:curl \
                                port:libidl \
                                port:libxml2 \
                                port:libxslt \
                                port:openssl \
                                port:libsdl

    patchfiles                  patch-build.diff \
                                patch-startup.diff
    platform darwin 11 {
        patchfiles-append patch-src-VBox-GuestHost-OpenGL-include-GL-glext.h.diff
    }

    configure.compiler          gcc-4.2

    # Use the apple-gcc-4.2 compiler because Xcode 4.2 no longer
    # provides gcc-4.2 and builds with llvm-gcc-4.2 crash.
    if {![file executable ${configure.cc}]} {

        depends_build-append    port:apple-gcc42
        depends_skip_archcheck-append apple-gcc42
        patchfiles-append       patch-apple-gcc42.diff
        configure.compiler      apple-gcc-4.2
    }

    configure.pre_args-delete   --prefix=${prefix}

    configure.args              --with-qt-dir=${prefix} \
                                --with-openssl-dir=${prefix} \
                                --with-gcc=${configure.cc} \
                                --with-g++=${configure.cxx}

    # VirtualBox uses kBuild.
    build.cmd                   ". env.sh && kmk"
    build.env-append            VERBOSE=1
    build.args-append           KBUILD_VERBOSE=full

    # This is the open source edition of VirtualBox.
    worksrcdir                  VirtualBox-${version}

    set kext_dir                /Library/Extensions
    set startup_items_dir       /Library/StartupItems

    post-patch {
        if {[vercmp $xcodeversion "4.4"] >= 0} {
            set oldest_supported_SDK 10.7
        } elseif {[vercmp $xcodeversion "4.1"] >= 0} {
            set oldest_supported_SDK 10.6
        } else {
            set oldest_supported_SDK "10.[expr [lindex [split ${macosx_deployment_target} "."] 1] - 1]"
        }

        reinplace "s|@APPLICATIONS_DIR@|${applications_dir}|g" \
            ${worksrcpath}/LocalConfig.kmk
        reinplace "s|@MACOSX_DEPLOYMENT_TARGET@|${oldest_supported_SDK}|g" \
           ${worksrcpath}/LocalConfig.kmk
        reinplace "s|@KEXT_DIR@|${prefix}${kext_dir}|g" \
            ${worksrcpath}/src/VBox/Installer/darwin/VBoxStartupItems/VirtualBox/VirtualBox

        if {[variant_isset vde2]} {
            reinplace "s|@PREFIX@|${prefix}|g" ${worksrcpath}/include/VBox/VDEPlugSymDefs.h
        }
    }

    destroot {

        set release_dir [lindex [glob -- ${worksrcpath}/out/darwin.*/release/dist] 0]

        xinstall -m 755 -d ${destroot}${applications_dir}
        copy ${release_dir}/VirtualBox.app ${destroot}${applications_dir}
        copy ${release_dir}/sdk ${destroot}${applications_dir}/VirtualBox.app

        # Set the owner and group to root:wheel, which is required for kernel extensions
        # and possibly startup items.

        xinstall -m 755 -o root -g wheel -d ${destroot}${prefix}${kext_dir}

        foreach kext [glob -- ${release_dir}/*.kext] {
            copy ${kext} ${destroot}${prefix}${kext_dir}
        }

        xinstall -m 755 -o root -g wheel -d ${destroot}${prefix}${startup_items_dir}
        copy ${worksrcpath}/src/VBox/Installer/darwin/VBoxStartupItems/VirtualBox ${destroot}${prefix}${startup_items_dir}

        # Create proxies for binaries bundled with VirtualBox.app.

        foreach app_proxy [list VirtualBox VBoxBalloonCtrl VBoxManage VBoxHeadless] {

            set app_proxy_file [open ${destroot}${prefix}/bin/${app_proxy} w]

            puts $app_proxy_file "#!/usr/bin/env bash"
            puts $app_proxy_file "exec -- ${applications_dir}/VirtualBox.app/Contents/MacOS/${app_proxy} \"\$@\""

            close $app_proxy_file

            file attributes ${destroot}${prefix}/bin/${app_proxy} -permissions "+x"
        }

        foreach executable [list VirtualBox VirtualBoxVM VBoxHeadless VBoxNetAdpCtl VBoxNetDHCP] {
            file attributes ${destroot}${applications_dir}/VirtualBox.app/Contents/MacOS/${executable} -permissions "u+s"
        }
    }

    pre-activate {

        set head_path ${applications_dir}

        while {${head_path} != "/"} {

            if {[file attributes ${head_path} -owner] != "root" || [file attributes ${head_path} -group] != "admin"
                || [expr [file attributes ${head_path} -permissions] & 00002] != 0} {
                error "VirtualBox requires the \"${applications_dir}\" directory and its parent directories to be\
root:admin owned and not world writeable."
            }

            set head_path [file dirname ${head_path}]
        }
    }

    variant vde2 description {Enable support for VDE} {

        depends_lib-append      port:vde2
        patchfiles-append       patch-vde.diff
        configure.args-append   --enable-vde
    }

    variant vnc description {Enable support for VNC} {

        depends_lib-append      port:LibVNCServer
        configure.args-append   --enable-vnc
    }

    default_variants            +vde2 +vnc

    startupitem.create          yes
    startupitem.name            VirtualBox
    startupitem.start           "${prefix}${startup_items_dir}/VirtualBox/VirtualBox start"
    startupitem.stop            "${prefix}${startup_items_dir}/VirtualBox/VirtualBox stop"
    startupitem.restart         "${prefix}${startup_items_dir}/VirtualBox/VirtualBox restart"
    startupitem.pidfile         none

    livecheck.type              regex
    if {[lindex [split ${version} .] 2] == 0} {
        livecheck.version       [join [lrange [split ${version} .] 0 1] .]
    }
    livecheck.url               ${homepage}
    livecheck.regex             "VirtualBox (\\d+\\.\\d+(?:\\.\\d+)?) released!"

}

subport virtualbox-guest-additions {

    license                 VirtualBoxPUEL

    description             guest additions for VirtualBox

    long_description        ${description}

    supported_archs         noarch

    distfiles               VBoxGuestAdditions_${version}.iso

    checksums               rmd160  335365fbe7406aa0a0d3c0b886f0330b611b6ece \
                            sha256  cf2b0722da984a5f41a7814ee288262cbdf4c785630b2a7726c3daa6c5caa0c5

    depends_run             port:virtualbox

    extract.only

    use_configure           no

    build {}

    destroot {
        set dir ${destroot}${applications_dir}/VirtualBox.app/Contents/MacOS/additions
        xinstall -d ${dir}
        copy ${distpath}/${distfiles} ${dir}/VBoxGuestAdditions.iso
    }

    livecheck.type          none
}

subport virtualbox-extension-pack {

    # We want to match the supported arch
    set kernel_arch [exec uname -m]
    switch ${kernel_arch} {
        i386 -
        x86_64 {
            supported_archs ${kernel_arch}
        }
        default {
            supported_archs i386 x86_64
        }
    }
    array set archs_map {
         i386       "darwin.x86"
         x86_64     "darwin.amd64"
    }
    set extension_archs {}
    foreach {arch} ${supported_archs} {
        lappend extension_archs $archs_map($arch)
    }
    set name_extpack    Oracle_VM_VirtualBox_Extension_Pack
    set app_dir VirtualBox.app
    set lib_dir ${app_dir}/Contents/MacOS
    set ext_dir ${lib_dir}/ExtensionPacks

    version             ${version}-${extension_pack_build}

    license             VirtualBoxPUEL

    description         Oracle VM VirtualBox Extension Pack
    long_description    ${description}

    depends_run         port:virtualbox

    distname            ${name_extpack}-${version}

    checksums           rmd160  b41596eba7d8b402e0030eaa651898cacc8b1d3b \
                        sha256  8f88b1ebe69b770103e9151bebf6681c5e049eb5fac45ae8d52c43440aa0fa0d

    worksrcdir          ${name_extpack}
    extract.suffix      .vbox-extpack
    extract.mkdir       yes

    use_configure       no

    build {

        # The pre-built libraries use weird prefixes and Oracle recommends setting
        # DYLD_LIBRARY_PATH to deal with this. I would rather fix the paths in the
        # libraries at install time.
        foreach {arch} $extension_archs {

            foreach lib [glob -directory ${worksrcpath}/${arch} *.dylib*] {

                system "install_name_tool -id ${applications_dir}/${ext_dir}/${name_extpack}/${arch}/[strsed ${lib} /^.*\\///] ${lib}"

                # Then for each dependent dylib with a weird path that this dylib
                # references, fix the reference to use ${lib_dir}.
                foreach dep [exec otool -L ${lib}] {

                    if [string match "/Applications/${lib_dir}/*" ${dep}] {

                        system "install_name_tool -change ${dep} ${applications_dir}/${lib_dir}/[strsed ${dep} /^.*\\///] ${lib}"
                    }
                }
            }
        }
    }

    destroot {

        xinstall -d -o root -g admin -m 755 ${destroot}${applications_dir}/${ext_dir}/${name_extpack}
        foreach {f} [glob -directory ${worksrcpath} -type f *] {

            xinstall -o "root" -g "admin" -m 0644 ${f} \
                ${destroot}${applications_dir}/${ext_dir}/${name_extpack}
        }
        foreach {arch} $extension_archs {

            xinstall -o "root" -g "admin" -m 0755 -d \
                ${destroot}${applications_dir}/${ext_dir}/${name_extpack}/${arch}
            foreach {f} [glob -directory ${worksrcpath}/${arch} *] {

                xinstall -o "root" -g "admin" -m 0644 ${f} \
                    ${destroot}${applications_dir}/${ext_dir}/${name_extpack}/${arch}
            }
        }
    }

    livecheck.type      regex
    livecheck.url       https://www.virtualbox.org/wiki/Downloads
    livecheck.regex     "/Oracle_VM_VirtualBox_Extension_Pack-(\\d(?!\\${extract.suffix}).*)\\${extract.suffix}"
}
