# -*- 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
PortGroup           xcode 1.0

github.setup        machyve xhyve dfbe09b9db0ef9384c993db8e72fb3e96f376e7b
version             20211002
revision            0
categories          emulators
platforms           darwin
supported_archs     x86_64
license             BSD
maintainers         {jeremyhu @jeremyhu} {@catap korins.ky:kirill}
description         a lightweight OS X virtualization solution
long_description \
    The xhyve hypervisor is a port of bhyve to OS X. \
    It is built on top of Hypervisor.framework in OS X 10.10 Yosemite and higher, \
    runs entirely in userspace, and has no other dependencies. \
    It can run FreeBSD and vanilla Linux distributions and \
    may gain support for other guest operating systems in the future.

checksums           rmd160  5f535564dcbb3d463fd4128106636942056b280c \
                    sha256  efaf7bb059d18cd90ce524064a51a03a3c343709efcc415cce7e8e4bb1fb42c8 \
                    size    11718624

use_xcode           yes

depends_lib-append  port:zlib

post-patch {
    foreach script [glob -- ${worksrcpath}/${name}run-*.sh] {
        reinplace "s|test/|${prefix}/share/${name}/test/|" ${script}
        reinplace "s|build/${name}|${prefix}/bin/${name}|" ${script}
    }
}

pre-fetch {
    if {${os.platform} ne "darwin" || ${os.major} < 14} {
        ui_error "${name} requires Hypervisor.framework from OS X Yosemite or later"
            return -code error "incompatible OS X version"
    }
}

platform macosx {
    # Here a tricky part. xhyve needs xcode sdk and not CommandLineTools
    # see: https://github.com/macports/macports-ports/pull/11911
    set macosx_sdkpath ${configure.developer_dir}/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
    if { [file exists ${macosx_sdkpath}] } {
        configure.sdkroot ${macosx_sdkpath}
    }
}


xcode.destroot.path ${prefix}/bin
build.args-append INSTALL_PREFIX=${prefix}
destroot.args-append INSTALL_PREFIX=${prefix}

# Temporary workaround for <rdar://problem/30056438
# codesign fails with CSSMERR_CSP_INVALID_CONTEXT_HANDLE during destroot (but not build) when building through MacPorts
build.args-append CODE_SIGN_ENTITLEMENTS="" CODE_SIGN_IDENTITY=""
destroot.args-append CODE_SIGN_ENTITLEMENTS="" CODE_SIGN_IDENTITY=""
post-destroot {
    xinstall -d -m 755 ${destroot}${prefix}/share/${name}
    xinstall -m 644 -W ${worksrcpath} src/xhyve-entitlements.plist ${destroot}${prefix}/share/${name}/entitlements.plist
}

post-destroot {
    xinstall -d -m 755 ${destroot}${prefix}/libexec/${name}
    xinstall -m 755 -W ${worksrcpath} ${name}run-freebsd.sh ${destroot}${prefix}/libexec/${name}/${name}run-freebsd.sh.sample
    xinstall -m 755 -W ${worksrcpath} ${name}run-tinycorelinux.sh ${destroot}${prefix}/libexec/${name}/${name}run-tinycorelinux.sh.sample
    xinstall -m 755 -W ${worksrcpath} ${name}run-windows.sh ${destroot}${prefix}/libexec/${name}/${name}run-windows.sh.sample
    xinstall -d -m 755 ${destroot}${prefix}/share/${name}
    file copy ${worksrcpath}/test ${destroot}${prefix}/share/${name}
}

notes "
    Please codesign ${prefix}/bin/xhyve with an appropriate identity and entitlements.plist:
    sudo /usr/bin/codesign --force --sign <identity> -o library --entitlements ${prefix}/share/${name}/entitlements.plist ${xcode.destroot.path}/xhyve

    The sample shell scripts to run xhyve are installed as ${prefix}/libexec/${name}/${name}run-*.sh.sample.
    Copy the scripts for editing. They will be overwritten when the port is updated.
"
