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

categories          net
platforms           macosx
license             BSD
maintainers         nomaintainer
# ppc64 is not support in the kernel:
# /System/Library/Frameworks/Kernel.framework/Headers/machine/locks.h:38:2: error: #error architecture not supported
supported_archs     ppc i386 x86_64 arm64
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

# TODO: verify that a newer version works correctly on 10.6,
# otherwise change the fallback threshold here.
if {${os.major} < 10} {
    version         20111101
    revision        1
    checksums       rmd160  d7ec7fbea29d05de3a1532737b74970d9bbcc2ed \
                    sha256  d5a21b326e95afc7db3c6683d3860f3192e3260fae12f74e8404a8fd0764558f \
                    size    80453

    patchfiles      patch-src__tap__Makefile-leopard.diff \
                    patch-src__tun__Makefile-leopard.diff
} else {
    version         20150118
    revision        1
    checksums       rmd160  bfdd394e10a982728c4867b2536b337ab78831e1 \
                    sha256  f01fe7a3dd0f2f64c13606ff0230696d3995f2eeae19f0cdeb778650db9f37ba \
                    size    53376

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

master_sites        sourceforge:project/tuntaposx/tuntap/${version}
distname            tuntap_${version}_src

worksrcdir          tuntap

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 could 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 \
                        queue.patch
    post-patch {
        foreach {d} {tun tap} {
            set f ${worksrcpath}/src/${d}/Makefile
            reinplace "s|@SDKROOT@|${configure.sdkroot}|" ${f}
        }
    }
}

if {${os.major} <= 9} {
    patchfiles-append   patch-leopard.diff
}

use_configure       no

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

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}
