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

name                tuntaposx
version             20150118
categories          net
platforms           darwin
license             BSD
maintainers         nomaintainer

description         Tun and tap virtual devices.

long_description    Unix-style tun and tap virtual network interfaces for Mac \
                    OS X. You typically use these for VPN and OS virtualization.

homepage            http://tuntaposx.sourceforge.net/
master_sites        sourceforge:project/tuntaposx/tuntap/${version}
distname            tuntap_${version}_src
worksrcdir          tuntap

checksums           rmd160  bfdd394e10a982728c4867b2536b337ab78831e1 \
                    sha256  f01fe7a3dd0f2f64c13606ff0230696d3995f2eeae19f0cdeb778650db9f37ba

destroot.violate_mtree  yes
destroot.args       BASE=${destroot}${prefix}

patchfiles          patch-src-tap-Makefile.diff \
                    patch-src-tun-Makefile.diff \
                    queue.patch

post-patch {
    foreach {d} {tun tap} {
        set f ${worksrcpath}/src/${d}/Makefile
        reinplace "s|@CC@|${configure.cc}|g" ${f}
        reinplace "s|@CXX@|${configure.cxx}|g" ${f}
        reinplace "s|@ARCHFLAGS@|[get_canonical_archflags cc]|" ${f}
    }
}

# This patch and reinplace should be integrated with the above patches
# once it can be confirmed that all the different systems correctly use the SDK
# path, and that the <= 9 patches also still apply cleanly as well
if {${os.major} >= 18} {
    patchfiles-append patch-mojave.diff
    post-patch {
        foreach {d} {tun tap} {
            set f ${worksrcpath}/src/${d}/Makefile
            reinplace "s|@SDKROOT@|${configure.sdkroot}|" ${f}
        }
    }
}

if { ${os.platform} eq "darwin" && ${os.major} <= 9} {
    known_fail  yes
    pre-fetch {
        ui_error "${name} ${version} does not presently install on MacOSX 10.5 or earlier."
        return -code error "Unsupported platform"
    }
}

platform darwin 10 {
    # Kernel can be 32-bit or 64-bit, so build both
    default_variants +universal
    configure.universal_archs i386 x86_64
}

use_configure       no

startupitem.create  yes
startupitem.pidfile none
set kext_dir        ${prefix}/Library/Extensions
startupitem.start   "kextload ${kext_dir}/tap.kext ; kextload  ${kext_dir}/tun.kext"
startupitem.stop    "kextunload ${kext_dir}/tap.kext ; kextunload  ${kext_dir}/tun.kext"

post-destroot {
    set docs ${destroot}${prefix}/share/doc/${name}
    xinstall -m 755 -d ${docs}
    xinstall -m 644 -W ${worksrcpath} README Changelog ${docs}
}

post-activate {
    set tap_loaded [expr [string length [exec kextstat -lb foo.tap 2>/dev/null]] > 0]
    set tun_loaded [expr [string length [exec kextstat -lb foo.tun 2>/dev/null]] > 0]
    
    if {${tap_loaded} || ${tun_loaded}} {
        ui_msg "**********************************************************************"
        ui_msg "* Warning: tuntaposx virtual device kernel extension loaded already. *"
        ui_msg "* Maybe you have installed the tuntaposx package already without     *"
        ui_msg "* using MacPorts, e.g. by installing Tunnelblick openvpn GUI. See    *"
        ui_msg "* kextstat(8), kextload(8) and kextunload(8) for manual handling.    *"
        ui_msg "**********************************************************************"
    }
}


livecheck.regex     {/tuntap_([0-9.]+)_src}
