# -*- 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           boost 1.0
PortGroup           github 1.0
PortGroup           legacysupport 1.1
PortGroup           openssl 1.0

# error: 'ERR_GET_FUNC' was not declared in this scope; did you mean 'ERR_GET_LIB'?
openssl.branch      1.1

# Only wrappers:
legacysupport.newest_darwin_requires_legacy 0

github.setup        freelan-developers freelan 779a1421adbbfa35568cea9b212d1ba0635570e1
version             2020.03.24
revision            0

# Drop on next update:
if {${os.platform} eq "darwin" && ${os.major} < 10} {
    incr revision
}

categories          net security p2p
license             GPL-3
maintainers         {@barracuda156 macos-powerpc.org:barracuda}
description         Peer-to-peer, secure, easy-to-setup, multi-platform, \
                    open-source, highly-configurable VPN software
long_description    ${description}
homepage            http://www.freelan.org

checksums           rmd160  31444ccbed56434f463d510ca628265974461981 \
                    sha256  90951caf71327663db93096fdf3a78a8c14035e5a789b367a7aa971bb51a8b31 \
                    size    3073150
github.tarball_from archive

# freelan(11440) malloc: *** error for object 0xa00a6754: pointer being freed was not allocated
legacysupport.redirect_bins ${name}

patchfiles          0001-Fix-SConstruct.patch

# https://github.com/freelan-developers/freelan/pull/250
patchfiles-append   0002-Fix-for-miniupnpc-1.8.patch

if {${os.platform} eq "darwin" && ${configure.cxx_stdlib} ne "libc++"} {
    patchfiles-append \
                    0003-Old-C-ABI.patch
}

post-patch {
    reinplace "s,@PREFIX@,${prefix},g" ${worksrcpath}/SConstruct
    reinplace "s,@OPENSSL@,[openssl::install_area],g" ${worksrcpath}/SConstruct
    reinplace "s,@BOOST@,[boost::install_area],g" ${worksrcpath}/SConstruct
}

# Does not build with Python 3.x atm:
set py_ver          2.7
set py_ver_nodot    [string map {. {}} ${py_ver}]

# scons for old Python:
depends_build-append \
                    port:scons-3

depends_lib-append  port:curl \
                    port:libiconv \
                    port:miniupnpc \
                    path:lib/libldap.dylib:openldap \
                    port:tuntaposx \
                    port:zlib

compiler.cxx_standard   2011

# Build system is mildly idiotic. While compiler value is picked from env
# for build stage, it is ignored in destroot, which breaks everything.
# Seriously, why, when there is CMake?
configure {
    file mkdir ${workpath}/bins
    set scons_bin ${prefix}/Library/Frameworks/Python.framework/Versions/${py_ver}/bin/scons

    set gcc [open "${workpath}/bins/gcc" w 0755]
    puts ${gcc} "#!/bin/sh"
    puts ${gcc} "${configure.cc} -arch $\{BUILD_ARCH:-${build_arch}\} \"\$@\""
    close ${gcc}

    set gxx [open "${workpath}/bins/g++" w 0755]
    puts ${gxx} "#!/bin/sh"
    puts ${gxx} "${configure.cxx} -arch $\{BUILD_ARCH:-${build_arch}\} \"\$@\""
    close ${gxx}

    set scons [open "${workpath}/bins/scons" w 0755]
    puts ${scons} "#!/bin/sh"
    puts ${scons} "${scons_bin} \"\$@\""
    close ${scons}
}

pre-build {
    build.env-append \
        PATH=${workpath}/bin:$env(PATH) \
        CC=${workpath}/bins/gcc \
        CXX=${workpath}/bins/g++
}

build.cmd           ${workpath}/bins/scons
build.target        apps

pre-destroot {
    destroot.env    {*}${build.env}
}

destroot.cmd-append prefix=${destroot}${prefix}

post-destroot {
    set sharedir ${destroot}${prefix}/share/${name}
    xinstall -d ${sharedir}
    copy ${worksrcpath}/README.md ${sharedir}/
    copy ${worksrcpath}/scripts ${sharedir}/
}
