# -*- 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           cmake 1.1
PortGroup           github 1.0
PortGroup           legacysupport 1.1

# Only used for wrappers:
legacysupport.newest_darwin_requires_legacy 0

github.setup        dsw7 GPTifier 1.6.0 v
revision            0
categories          llm
license             MIT
maintainers         {@barracuda156 macos-powerpc.org:barracuda} openmaintainer
description         C++ OpenAI CLI interface
long_description    ${name} is a command-line tool designed to interact seamlessly \
                    with OpenAI API.
checksums           rmd160  0a65c21d07889b96a39a2385df4d509c30ef80ed \
                    sha256  50ceee9f01f28e71e3531793fd3fe0a5cab929202daf6058bcd67a3fd4329291 \
                    size    32318
github.tarball_from archive

# gpt(11666) malloc: *** error for object 0xa00a6754: pointer being freed was not allocated
legacysupport.redirect_bins gpt

cmake.source_dir    ${worksrcpath}/${name}

set sharedir        ${prefix}/share/gptifier

# Drop hardcoding of install prefix etc.
patchfiles          patch-CMakeLists.diff \
                    patch-setup.diff

post-patch {
    reinplace "s|@GPTIFIER_SHAREDIR@|${sharedir}|g" ${worksrcpath}/setup
}

set libfmt_v        12
cmake.prefix_path-append \
                    ${prefix}/lib/libfmt${libfmt_v}/cmake

depends_lib-append  port:curl \
                    port:libfmt${libfmt_v} \
                    port:nlohmann-json \
                    port:tomlplusplus

compiler.cxx_standard   2017

configure.args-append \
                    -DENABLE_COVERAGE=OFF \
                    -DENABLE_TESTING=OFF \
                    -DUSE_SYSTEM_NLOHMANN_JSON=ON \
                    -DUSE_SYSTEM_TOMLPLUSPLUS=ON

# https://github.com/dsw7/GPTifier/issues/175
configure.cppflags-append \
                    -I${prefix}/include/nlohmann \
                    -I${prefix}/include/toml++

# This does not work from within CMake at the moment.
# test.run            yes

post-destroot {
    xinstall -d ${destroot}${sharedir}
    xinstall -d ${destroot}${prefix}/share/doc/gptifier
    xinstall -m 0775 -W ${worksrcpath} setup \
        ${destroot}${sharedir}
    xinstall -m 0644 -W ${worksrcpath}/\.gptifier gptifier.toml README.txt \
        ${destroot}${sharedir}
    xinstall -m 0644 -W ${worksrcpath} LICENSE README.md \
        ${destroot}${prefix}/share/doc/gptifier
}

notes "
Initial set-up is needed prior to running the program for the first time.\
Please execute $prefix/share/gptifier/setup script from non-root user \
and edit the generated config file: ~/.gptifier as needed.
OPENAI_API_KEY is passed via the environment.
"
