# -*- 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 92382 2012-04-27 07:39:29Z royliu@macports.org $

PortSystem          1.0

name                virtualbox
version             4.1.14
# 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.
set extension_pack_build   77440

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
    revision            1

    # 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  68bb81486dad190a1bd74251ad32ee16d47be625 \
                        sha256  034cfd19d5dd701cd1375b994ad6f2d41c207b6760c6b8c5fe3005e53134dff8

    depends_lib-append          port:curl \
                                port:libidl \
                                path:lib/pkgconfig/libxml-2.0.pc:libxml2 \
                                path:lib/pkgconfig/libxslt.pc:libxslt \
                                path:lib/pkgconfig/openssl.pc:openssl \
                                path:lib/pkgconfig/sdl.pc:libsdl

    patchfiles                  patch-build.diff \
                                patch-startup.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"

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

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

    post-patch {

        reinplace "s|@APPLICATIONS_DIR@|${applications_dir}|g" \
            ${worksrcpath}/LocalConfig.kmk
    # TODO: The deployment target should be set dynamically, but, because of incompatibilities
    # with OpenGL headers in 10.7, it's fixed at 10.6 for now.
        reinplace "s|@MACOSX_DEPLOYMENT_TARGET@|10.6|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      path:lib/pkgconfig/libvncserver.pc: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  89dde3f04047f5a89fe985c0948ff8122775d2cc \
                            sha256  a12785fd1f202ff784da27ae75bd3fd43edaa824736b5b30499d9db993c1db64

    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  54e5748ee840376ca0b539e1956d496eb9c8dc9e \
                        sha256  82c112ab6003a92e6065a277077b2f3e1a9ca6871773ac3f4b1c771b8e699b42

    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 0755 ${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 0755 ${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}"
}
