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

if {${os.platform} ne "darwin" || ${os.major} > 17} {
    github.setup    aardappel treesheets 9435739325
    version         20240609
    revision        0

    checksums       rmd160  9e0a59e955007cf7b05558323cc2eeee75e1f2ac \
                    sha256  e6497fac777caac8f55631ec63e93007d389d2e271939a73c34bd9a18d8ddc2a \
                    size    3088087

    compiler.cxx_standard \
                    2020
} else {
    # Pegged version for older systems.
    # https://github.com/aardappel/treesheets/issues/670
    github.setup    aardappel treesheets 4640240271
    version         20230408
    revision        0

    checksums       rmd160  9164ba2f3ff1678cbaeac267da4c87684eee1012 \
                    sha256  394b6ab25126a0b9fc680ec8dfd53385170664eb8fcb40b4e24489afda9ab770 \
                    size    3060894

    compiler.cxx_standard \
                    2017
}

homepage            https://strlen.com/treesheets

description         Free-Form Data Organizer (Hierarchical Spreadsheet)

long_description    {*}${description}

categories          editors
license             zlib

maintainers         {gmail.com:herby.gillot @herbygillot} \
                    openmaintainer

github.tarball_from archive

# https://github.com/aardappel/treesheets/pull/671
patchfiles-append   0001-lobster-tools.h-avoid-conflicting-define-for-ssize_t.patch

set ts_app_dir      ${applications_dir}/TreeSheets.app
set ts_libexec_dir  ${prefix}/libexec/${name}

cmake.install_prefix \
                    ${ts_libexec_dir}

if {${os.platform} eq "darwin" && ${os.major} > 17} {
    wxWidgets.use   wxWidgets-3.2
} elseif {${os.platform} eq "darwin" && ${os.major} > 11} {
    wxWidgets.use   wxWidgets-3.0-cxx11
} else {
    wxWidgets.use   wxGTK-3.0-cxx11
}

depends_lib-append  port:${wxWidgets.port}

configure.env-append \
                    PATH=$env(PATH):${wxWidgets.prefix}/bin

build.env-append    PATH=$env(PATH):${wxWidgets.prefix}/bin

configure.args-append \
                    -DGIT_WXWIDGETS_SUBMODULES=OFF \
                    -DwxWidgets_CONFIG_EXECUTABLE=${wxWidgets.wxconfig}

if {${os.platform} eq "darwin" && ${configure.build_arch} in [list ppc ppc64]} {
    depends_lib-append \
                    port:libdispatch-legacy
    configure.cppflags-append \
                    -I${prefix}/libexec/dispatch/usr/include
    configure.ldflags-append \
                    ${prefix}/libexec/dispatch/usr/lib/libdispatch.a
}

post-destroot {
    # Set up .app directory
    file mkdir ${destroot}${ts_app_dir}/Contents/MacOS
    file mkdir ${destroot}${ts_app_dir}/Contents/Resources

    file copy ${worksrcpath}/osx/TreeSheets/App.icns \
        ${destroot}${ts_app_dir}/Contents/Resources/

    file copy ${worksrcpath}/osx/TreeSheets/TreeSheets/TreeSheets-Info.plist \
        ${destroot}${ts_app_dir}/Contents/Info.plist

    reinplace -E {s|\$\{PRODUCT_NAME\}|TreeSheets|} \
        ${destroot}${ts_app_dir}/Contents/Info.plist

    # Create and install wrapper script
    set wrapper_script ${prefix}/bin/${name}

    set wrapper_sh ${destroot}${wrapper_script}
    set wsh [open ${wrapper_sh} w]
    puts ${wsh} "#!/bin/sh"
    puts ${wsh} "${ts_libexec_dir}/${name}"
    close ${wsh}

    file attributes ${destroot}${wrapper_script} -permissions 0755

    reinplace -E {s|\$\{EXECUTABLE_NAME\}|wrapper.sh|} \
        ${destroot}${ts_app_dir}/Contents/Info.plist

    file copy ${destroot}${wrapper_script} \
        ${destroot}${ts_app_dir}/Contents/MacOS/wrapper.sh
}
