# -*- 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/ollama/ollama 0.12.2 v
github.tarball_from archive
revision            0

categories          llm
installs_libs       no
maintainers         {@rdallman reed.pizza:github} {i0ntempest @i0ntempest} \
                    openmaintainer
license             MIT

description         ${name} runs and manages LLMs
long_description    Get up and running with large language models easily
homepage            https://ollama.com

checksums           rmd160  638ddf7ff6bb683b3a2da50307d799c5cc0cfd42 \
                    sha256  33d0fc50a0564a8ba9792f845994148e1cb509b0380575cef1ee124a34784b0b \
                    size    11017197

# See https://github.com/ollama/ollama/pull/6854
patchfiles-append   patch-6854.diff
patch.args          -p1

go.offline_build    no
build.post_args     -ldflags \"-s -w \
                    -X 'github.com/${github.author}/${github.project}/version.Version=${version}' \
                    -X 'github.com/${github.author}/${github.project}/server.mode=release'\"

set ollama_user     ${name}
set ol_data_path    ${prefix}/var/${name}
set ol_models_path  ${ol_data_path}/models
set ol_log_file     ${prefix}/var/log/${name}.log
set config_path     ${prefix}/etc/${name}
set env_file        ${name}_env.conf
set wrapper_script  ${name}-wrapper.sh

add_users           ${ollama_user} \
                    group=${ollama_user} \
                    realname=Ollama

destroot.keepdirs-append \
                    ${destroot}${ol_models_path}

destroot {
    xinstall -m 0755 ${worksrcpath}/${name} ${destroot}${prefix}/bin/${name}
    xinstall -d -m 0755 ${destroot}${ol_models_path}
    file attributes ${destroot}${ol_models_path} -owner ${ollama_user} -group ${ollama_user}
    file attributes ${destroot}${ol_data_path} -owner ${ollama_user} -group ${ollama_user}

    file mkdir ${destroot}${prefix}/libexec/${name}
    copy ${filespath}/${wrapper_script} ${destroot}${prefix}/libexec/${name}/${wrapper_script}
    file mkdir ${destroot}${config_path}
    copy ${filespath}/${env_file} ${destroot}${config_path}/${env_file}.example
    reinplace "s|@@PREFIX@@|${prefix}|g" ${destroot}${prefix}/libexec/${name}/${wrapper_script}
    reinplace "s|@@BIN@@|${name}|g" ${destroot}${prefix}/libexec/${name}/${wrapper_script}
    reinplace "s|@@CONFIG_PATH@@|${config_path}|g" ${destroot}${prefix}/libexec/${name}/${wrapper_script}
    reinplace "s|@@ENV_FILE@@|${env_file}|g" ${destroot}${prefix}/libexec/${name}/${wrapper_script}
    reinplace "s|@@HOME@@|${ol_data_path}|g" ${destroot}${config_path}/${env_file}.example
    reinplace "s|@@MODELS_PATH@@|${ol_models_path}|g" ${destroot}${config_path}/${env_file}.example

    system -W ${destroot}${prefix}/etc/LaunchDaemons/${startupitem.uniquename} \
         "/usr/libexec/PlistBuddy -c \"Add :SessionCreate bool true\" ${startupitem.plist} &&\
          /usr/libexec/PlistBuddy -c \"Add :ProcessType string Interactive\" ${startupitem.plist}"
}

post-activate {
    if {![file exists ${config_path}/${env_file}]} {
        copy ${config_path}/${env_file}.example ${config_path}/${env_file}
    }
}

startupitem.create  yes
startupitem.executable \
                    ${prefix}/libexec/${name}/${wrapper_script}
startupitem.user    ${ollama_user}
startupitem.group   ${ollama_user}

variant logging description {Enable logging for startup item} {
    startupitem.logfile \
                    ${ol_log_file}
    startupitem.logfile.stderr \
                    ${ol_log_file}
    startupitem.logevents \
                    yes
}

variant completion description "Install bash completion for ${name}" {
    # Use this until upstream has implemented proper shell completions
    depends_run-append \
                    port:ollama-bash-completion-plugin
}

default_variants-append \
                    +completion

notes-append "
    The example config file is copied to ${config_path}/${env_file} and its content\
    will be preserved across upgrades and reinstalls. This config file configures ${name}\
    to fetch models to ${ol_models_path}. The startup item will use this config file by default.
"

github.livecheck.regex \
                    {([0-9.]+)}
