# -*- 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.18.0 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  561a506b69fe2c01521fb319c28337dc2872687c \
                    sha256  fc068d7fdd80dd6a968b57128d736b8c6147aa23bcba584c925eb73832f6523e \
                    size    24095281

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

set pyver           3.12

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}
    }
}
