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

go.setup            github.com/restic/restic 0.19.1 v
go.offline_build    no
revision            0

homepage            https://restic.net

description         Fast, secure, efficient backup program.

long_description    Restic is a program that does backups right. Its design \
                    goals are: Easy, Fast, Verifiable, Secure, Efficient and \
                    Free.

categories          sysutils
installs_libs       no
license             BSD
maintainers         {gmail.com:herby.gillot @herbygillot} \
                    openmaintainer

checksums           rmd160  50be9eb98310dc5d8db5ca7bb368ce91c161a6de \
                    sha256  bb9b1a19040744d26d8a79be029d4e6b189c45ccc9d8831d7fe367d3c33df725 \
                    size    24166058

dist_subdir         ${name}/${version}_${revision}

set pyver           3.14

variant             docs description {Build the documentation} {
    set pyver_nodot [string map {. {}} ${pyver}]

    depends_build-append \
                    port:python${pyver_nodot} \
                    port:py${pyver_nodot}-sphinx \
                    port:py${pyver_nodot}-sphinx_rtd_theme
}

variant             debug description {Enable debug options} {
    build.post_args-append \
                    -tags debug
}

build.cmd           ${go.bin} run
build.target        build.go

post-build {
    if {[variant_isset docs]} {
        reinplace -W ${worksrcpath}/doc "s|sphinx-build|sphinx-build-${pyver}|g" Makefile
        system -W ${worksrcpath}/doc "make html"
    }
}

test.run            yes

test {
    set repository_path ${workpath}/repo
    set restore_path    ${workpath}/restore
    set file_name       build.go

    system -W ${worksrcpath} \
        "RESTIC_PASSWORD=\"foo\" ./restic -r ${repository_path} init"
    system -W ${worksrcpath} \
        "RESTIC_PASSWORD=\"foo\" ./restic -r ${repository_path} backup ${file_name} --no-cache"
    system -W ${worksrcpath} \
        "RESTIC_PASSWORD=\"foo\" ./restic -r ${repository_path} restore latest -t ${restore_path} --no-cache"
    system -W ${worksrcpath} \
        "diff -q ${file_name} ${restore_path}/${file_name}"
}

destroot {
    xinstall -m 0755 ${worksrcpath}/${name} ${destroot}${prefix}/bin/

    set mandir ${prefix}/share/man/man1
    xinstall -m 0755 -d ${destroot}${mandir}
    xinstall -m 0644 {*}[glob ${worksrcpath}/doc/man/*.1 ] \
        ${destroot}${mandir}/

    set bash_compl ${prefix}/share/bash-completion/completions
    xinstall -m 0755 -d ${destroot}${bash_compl}
    xinstall -m 0644 ${worksrcpath}/doc/bash-completion.sh \
        ${destroot}${bash_compl}/${name}

    set fish_compl ${prefix}/share/fish/vendor_completions.d
    xinstall -m 0755 -d ${destroot}${fish_compl}
    xinstall -m 0644 ${worksrcpath}/doc/fish-completion.fish \
        ${destroot}${fish_compl}/${name}.fish

    set zsh_compl ${prefix}/share/zsh/site-functions
    xinstall -m 0755 -d ${destroot}${zsh_compl}
    xinstall -m 0644 ${worksrcpath}/doc/zsh-completion.zsh \
        ${destroot}${zsh_compl}/_${name}

    set docdir ${prefix}/share/doc/${name}
    xinstall -m 0755 -d ${destroot}${docdir}
    xinstall -m 0644 -W ${worksrcpath} LICENSE README.md ${destroot}${docdir}

    if {[variant_isset docs]} {
        file copy ${worksrcpath}/doc/_build/html ${destroot}${docdir}
    }
}
