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

name                1password-cli
version             2.30.3
revision            1

homepage            https://support.1password.com/command-line

description         Official 1Password command-line tool

long_description    View and manage 1password.com users, groups and objects \
                    from the command-line.

categories          security
installs_libs       no
license             Restrictive/Distributable
maintainers         {gmail.com:newtonne.github @newtonne} \
                    {gmail.com:herby.gillot @herbygillot} \
                    openmaintainer

set bin_name        op
set archive         ${bin_name}_apple_universal
set pkg_path        ${workpath}/pkg
set pkg_workpath    ${pkg_path}/op.pkg

extract.cmd         pkgutil
extract.pre_args    --expand
extract.post_args   ${pkg_path}
extract.mkdir       yes
extract.suffix      .pkg

master_sites        https://cache.agilebits.com/dist/1P/op2/pkg/v${version}/
distfiles           ${archive}_v${version}${extract.suffix}
supported_archs     arm64 x86_64

checksums           rmd160  c4ca4c5dd42507bf04640a07ce7d65341911e784 \
                    sha256  4559e0ee1b997d1451f7d4cb2b09a6ba7eb0a1288884ce589da11f5a074f26be \
                    size    16958893

# Pre-built binary
use_configure       no
build               {}

post-extract {
    system -W ${pkg_workpath} "cat ./Payload | gzip -d > ./Payload.cpio"
    system -W ${pkg_workpath} "cat ./Payload.cpio | cpio -i"
}

post-build {
    set op ${pkg_workpath}/${bin_name}
    set completions ${worksrcpath}/completions

    xinstall -m 0755 -d ${completions}

    foreach shell {bash fish zsh} {
        system -nodup -W ${worksrcpath} \
          "${op} completion ${shell} > ${completions}/${bin_name}.${shell}"
    }
}

destroot {
    xinstall -m 0755 ${pkg_workpath}/${bin_name} ${destroot}${prefix}/bin

    set completions ${worksrcpath}/completions

    set bash_comp_path ${destroot}${prefix}/share/bash-completion/completions
    xinstall -m 0755 -d ${bash_comp_path}
    xinstall -m 0644 ${completions}/${bin_name}.bash ${bash_comp_path}/${bin_name}

    set fish_comp_path ${destroot}${prefix}/share/fish/vendor_completions.d
    xinstall -m 0755 -d ${fish_comp_path}
    xinstall -m 0644 ${completions}/${bin_name}.fish ${fish_comp_path}

    set zsh_comp_path ${destroot}${prefix}/share/zsh/site-functions
    xinstall -m 0755 -d ${zsh_comp_path}
    xinstall -m 0644 ${completions}/${bin_name}.zsh ${zsh_comp_path}/_${bin_name}
}

livecheck.type      regex
livecheck.url       https://app-updates.agilebits.com/product_history/CLI2
livecheck.regex     ${archive}_v(\\d+(\\.\\d+)+)${extract.suffix}

notes {
  1Password CLI has been installed as ${bin_name}

  For instructions on getting started, see:
  https://support.1password.com/command-line-getting-started/#get-started-with-the-command-line-tool

  Completion scripts have been installed for Bash, Fish, and Zsh.
}