# -*- 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/openai/openai-cli 1.40.2 v
go.offline_build    no
go.toolchain_min    1.26
revision            0

description         Official CLI for the OpenAI API

long_description    ${name} provides the openai command, a resource-based \
                    command-line interface to the OpenAI REST API. It covers \
                    responses, files, images, audio, batches, models and \
                    the admin endpoints, with readable output by default, \
                    JSON and JSONL output for scripting, and an interactive \
                    image picker.

categories          llm
installs_libs       no
license             Apache-2
maintainers         {gmail.com:herby.gillot @herbygillot} \
                    openmaintainer

checksums           rmd160  d73d6167ba94daa6ac97f652c14ee70bc0f1595d \
                    sha256  07a8395ce5a467696729af6690bee72729c70ffaccd8d5e7654369b5bdea8214 \
                    size    2654305

build.args-append   -ldflags \"-s -w\" \
                    -o openai
build.post_args-append \
                    ./cmd/openai

post-build {
    system -W ${worksrcpath} "./openai @manpages -o man"
    foreach shell {bash zsh fish} {
        system -W ${worksrcpath} "./openai @completion ${shell} > openai.${shell}"
    }
}

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

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

    set completions [dict create \
        bash ${prefix}/share/bash-completion/completions/openai \
        zsh  ${prefix}/share/zsh/site-functions/_openai \
        fish ${prefix}/share/fish/vendor_completions.d/openai.fish]
    foreach {shell dest} ${completions} {
        xinstall -d ${destroot}[file dirname ${dest}]
        xinstall -m 0644 ${worksrcpath}/openai.${shell} ${destroot}${dest}
    }

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

test.run            yes
test {
    set output [exec ${worksrcpath}/openai --version]
    if {![string match "* ${version}" ${output}]} {
        return -code error "unexpected version output: ${output}"
    }
}

notes "
The first interactive run of openai adds image picker shortcuts to your bash,
zsh or fish startup files. To remove them and stop this, run e.g.:

    openai @completion bash --uninstall-picker
"
