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

github.setup        Itsusinn tuic 1.8.10 v
github.tarball_from archive
revision            0

categories          net security
license             GPL-3+
maintainers         {i0ntempest @i0ntempest} openmaintainer

description         [string toupper ${name}] proxy protocol client and server
long_description    [string toupper ${name}] is a low-latency proxy protocol built on QUIC. \
                    This port installs both the TUIC client and server implementations.

checksums           ${name}-${version}${extract.suffix} \
                    rmd160  01a52c573491311ec1029fea61998396b6ed753a \
                    sha256  ac47f9d8b4f820031236e255f8012ee6858ae50ab56550bf98ad8e0d07b0d977 \
                    size    174901

# cargo workspace incompatibility
cargo.offline_cmd

build.pre_args-prepend \
                    --package tuic-client \
                    --package tuic-server

destroot {
    foreach executable {tuic-client tuic-server} {
        xinstall -m 0755 \
            ${worksrcpath}/target/[cargo.rust_platform]/release/${executable} \
            ${destroot}${prefix}/bin/
    }

    xinstall -d ${destroot}${prefix}/etc/${name}
    xinstall -m 0640 ${filespath}/client.toml \
        ${destroot}${prefix}/etc/${name}/client.toml.example
    xinstall -m 0640 ${filespath}/server.toml \
        ${destroot}${prefix}/etc/${name}/server.toml.example
    reinplace "s|@PREFIX@|${prefix}|g" \
        ${destroot}${prefix}/etc/${name}/server.toml.example

    xinstall -d ${destroot}${prefix}/var/lib/${name}
    xinstall -d ${destroot}${prefix}/share/doc/${name}
    xinstall -m 0644 -W ${worksrcpath} LICENSE README.md \
        ${destroot}${prefix}/share/doc/${name}/
}

post-activate {
    foreach config {client server} {
        if {![file exists ${prefix}/etc/${name}/${config}.toml]} {
            file copy ${prefix}/etc/${name}/${config}.toml.example \
                ${prefix}/etc/${name}/${config}.toml
        }
    }
}

startupitem.create      yes
startupitem.executable  ${prefix}/bin/tuic-server \
                        --config ${prefix}/etc/${name}/server.toml

notes "
    Example client and server configurations have been installed in
    ${prefix}/etc/${name}. Existing configurations are preserved across
    upgrades and reinstalls.

    Before loading the server, replace the placeholder UUID and password in
    ${prefix}/etc/${name}/server.toml and adjust its listen address. The
    supplied configuration listens only on localhost by default.
"
