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

name                tunnelblick
set app_name        Tunnelblick
github.setup        Tunnelblick ${app_name} 3.5.26 v
revision            0
categories          security net aqua
platforms           {darwin < 14}
supported_archs     ppc
license             GPL-2
maintainers         {@barracuda156 macos-powerpc.org:barracuda} openmaintainer

description         free, open source graphic user interface for OpenVPN
long_description    Tunnelblick is a {*}${description} on macOS. The port is experimental at this stage.
homepage            https://tunnelblick.net

# Discussion: https://github.com/Tunnelblick/Tunnelblick/issues/876

# It is possible to avoid fetching two Tunnelblick versions, of course,
# and fetch openvpn2 and easy-rsa tarballs, but Tunnelblick applies its patches
# onto both, so it gonna become messy in that case.
set extra_source_v  8.0
set openvpn_v       2.6.14
distfiles-append    v${extra_source_v}${extract.suffix}:extra_source
master_sites-append https://github.com/Tunnelblick/${app_name}/archive/refs/tags/:extra_source
checksums           ${distname}${extract.suffix} \
                    rmd160  929e82807856794e12eb1a777d64205fd2ef7e2e \
                    sha256  2688a4211dce7d32766d7e17b1015f0867cbe32128af8f5ae3b7d878b764ebbe \
                    size    13609453 \
                    v${extra_source_v}${extract.suffix} \
                    rmd160  1f60357bd180d93809bf2b7b976ebda9d363a562 \
                    sha256  92492653116d9758d44ee169f001119fdc0ffbff16fdf590ee2272ed995963ca \
                    size    70452788
github.tarball_from archive

set main_src_path   ${workpath}/${app_name}-${version}

post-extract {
    # Delete old 3d-party sources. This must be adjusted when updating the port.
    delete -f ${main_src_path}/third_party/sources/easy-rsa
    delete -f ${main_src_path}/third_party/sources/openvpn/openvpn-2.3.18
    delete -f ${main_src_path}/third_party/sources/openvpn/openvpn-2.4.4

    copy ${workpath}/${app_name}-${extra_source_v}/third_party/sources/easy-rsa \
                    ${main_src_path}/third_party/sources/
    copy ${workpath}/${app_name}-${extra_source_v}/third_party/sources/openvpn/openvpn-${openvpn_v} \
                    ${main_src_path}/third_party/sources/openvpn/
}

patch.dir           ${workpath}/${app_name}-${version}
patch.pre_args-replace \
                    -p0 -p1
# Backport from https://github.com/Tunnelblick/Tunnelblick/tree/3.5
patchfiles          73ee4526e3375605102a9a611fe8c1544aa1cb05.patch

patchfiles-append   0001-Adjust-tuntap.patch \
                    0002-Adjust-build-system.patch
post-patch {
    reinplace "s|/usr/local|${prefix}|g" \
                    ${patch.dir}/tunnelblick/Tunnelblick.xcodeproj/project.pbxproj \
                    ${patch.dir}/third_party/BuildThirdPartyItems.sh
    reinplace "s|@TARGET@|${macosx_deployment_target}|g" \
                    ${patch.dir}/third_party/Makefile-Xcode3
}

worksrcdir          ${worksrcpath}/${name}
xcode.project       ./${app_name}.xcodeproj
xcode.configuration Release

# Use static legacy-support:
depends_build-append \
                    port:autoconf \
                    port:automake \
                    path:lib/MacportsLegacySupport.dylib:legacy-support \
                    port:libtool

# FIXME: upstream links all libs statically, but it does not work from existing build script.
# Not that dynamic linking is wrong, but it is desirable to minimize dependencies.
depends_lib-append  port:lz4 \
                    port:lzo2 \
                    path:lib/libssl.dylib:openssl \
                    port:pkcs11-helper

if {${configure.sdkroot} eq ""} {
    configure.sdkroot \
                    ${developer_dir}/SDKs/MacOSX${macosx_deployment_target}.sdk
}

xcode.build.settings-append \
                    ARCHS=${configure.build_arch} \
                    ONLY_ACTIVE_ARCH=yes \
                    PREFIX=${prefix} \
                    SDKROOT=${configure.sdkroot} \
                    TUNTAP_ARCHS=${configure.build_arch}

# Xcode 3.2.6 lies about its version, posing as 4.x, which leads to incorrect
# choices during the build. This is a workaround.
if {${os.major} < 11} {
    xcode.build.settings-append \
                    XCODE_BRANCH=0300
}

# Tunnelblick expects to be in /Applications:
destroot.violate_mtree yes

destroot {
    copy ${worksrcpath}/build/Release/${app_name}.app ${destroot}/Applications/
}

# FIXME: as of now, easy-rsa does not install correctly and will nag a user on start-up.
# Those are just helper scripts, however; the VPN works fine without them.

# https://github.com/Tunnelblick/Tunnelblick/issues/876#issuecomment-3773039291
notes "
To complete the installation, please run the following command (as root):

`sudo /Applications/${app_name}.app/Contents/Resources/installer 5`
"
