# -*- 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           golang 1.0
PortGroup           git_archive 1.0

go.setup            github.com/apernet/hysteria 2.14.0 app/v
revision            0
categories          net
maintainers         {i0ntempest @i0ntempest} openmaintainer
license             MIT

description         network utility and proxy optimized for networks of poor quality
long_description    ${name} is a feature-packed network utility optimized for networks of poor quality \
                    (e.g. satellite connections, congested public Wi-Fi, cross-country connection) \
                    powered by a custom version of QUIC protocol.

checksums           rmd160  d5eee8974601a01525ef58ea6a7bbb79eb0421fd \
                    sha256  2808da83ef2f4f76be91f7ae0df7518f1cc185d5410cd6d174e738527cf5545e \
                    size    4809286

pre-build {
    set cmd_pkg ${go.package}/app/v2/cmd
    set build_date [clock format ${source_date_epoch} -gmt 1 -format {%Y-%m-%dT%H:%M:%SZ}]
    set toolchain [string range [exec ${go.bin} version] 11 end]

    # Match the library version reported by upstream's build script.
    set core_mod [open ${worksrcpath}/core/go.mod]
    try {
        if {![regexp -line {^\s*github\.com/apernet/quic-go\s+(\S+)} [read ${core_mod}] -> lib_version]} {
            error "Cannot determine the quic-go version from core/go.mod"
        }
    } finally {
        close ${core_mod}
    }

    # Go uses only the last -ldflags argument, so keep all metadata together.
    build.pre_args-append \
                    -ldflags=\" \
                    -X '${cmd_pkg}.appVersion=v${version}' \
                    -X '${cmd_pkg}.appDate=${build_date}' \
                    -X '${cmd_pkg}.appType=release' \
                    -X '${cmd_pkg}.appToolchain=${toolchain}' \
                    -X '${cmd_pkg}.appCommit=[git_archive::commit_id]' \
                    -X '${cmd_pkg}.appPlatform=${goos}' \
                    -X '${cmd_pkg}.appArch=${goarch}' \
                    -X '${cmd_pkg}.libVersion=${lib_version}' \
                    \"
}

build.args-append   -o ../${name} .
# go2port no longer works since 1.3.1 as there's no go.mod at project root
go.offline_build    no
build.dir           ${worksrcpath}/app

destroot {
    xinstall -m 0755 ${worksrcpath}/${name} ${destroot}${prefix}/bin/
    file mkdir ${destroot}${prefix}/etc/${name}
    copy ${filespath}/server.json ${filespath}/client.json ${destroot}${prefix}/etc/${name}/
}

post-activate {
    if {![file exists ${prefix}/etc/${name}/config.json]} {
        copy ${prefix}/etc/${name}/client.json ${prefix}/etc/${name}/config.json
    }
}

notes-append "
    The default config file is copied to ${prefix}/etc/${name}/config.json and its content\
    will be preserved across upgrades and reinstalls. The startup item will use this config\
    file by default.
"

startupitem.create          yes
startupitem.executable      ${prefix}/bin/${name} --config ${prefix}/etc/${name}/config.json
