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

github.setup        Arthur-Ficial apfel 1.5.5 v
revision            1
github.tarball_from archive

homepage            https://apfel.franzai.com
description         The free AI already on your Mac

long_description    {*}{
    The free AI already on your Mac. CLI tool, OpenAI-compatible server, and interactive
    chat — all on-device via Apple Intelligence. No API keys, no cloud, no downloads.

    Depends on Apple FoundationModels available in macOS 26 or later.
}

categories          llm
installs_libs       no
license             MIT
maintainers         {macports.halostatue.ca:austin @halostatue} \
                    openmaintainer

checksums           rmd160  f9cde544cb1ec866dfc57565db38970a52816053 \
                    sha256  d00c0d99eb7b5bd3f6aafccfb937c12519ded88abc239d6237c540894d1b9e90 \
                    size    1300378

platforms           {darwin >= 26}
supported_archs     arm64

use_configure       no

build.cmd           swift
build.target        build
build.args          --configuration release --disable-sandbox

post-build {
    system -W ${worksrcpath} "make generate-man-page"
}

set config_path     ${prefix}/etc/${name}
set env_file        ${name}_env.conf
set wrapper_script  ${name}-wrapper.sh

destroot {
    xinstall -m 0755 ${worksrcpath}/.build/release/apfel ${destroot}${prefix}/bin/
    xinstall -m 0644 ${worksrcpath}/.build/release/apfel.1 ${destroot}${prefix}/share/man/man1

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

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}

notes-append "
    The example config file is copied to ${config_path}/${env_file} and its content\
    will be preserved across upgrades and reinstalls. The startup item will use this\
    config file by default.
    
    The service must run as a user with Apple Intelligence enabled. The wrapper script\
    reads APFEL_USER variable in the config file and runs the service as this user.\
    The default value 501 is usually (but not guaranteed) the primary user.\
    Change it as needed.
"
