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

codeberg.setup          tenacityteam tenacity 1.3.4 v
set libnyquist_commit   d4fe08b079538a2fd79277ef1a83434663562f04
revision                0

set tenacity_distfile   ${name}-${version}${extract.suffix}
set libnyquist_distfile libnyquist-${libnyquist_commit}${extract.suffix}

checksums               ${tenacity_distfile} \
                        rmd160  acdf7e447f9e7ac21f3bf7141f4484327e5f2860 \
                        sha256  75329d2ecdee1a48c1a13f2727bc10b6fd2fe1057d5a4f80f989b67e33240f86 \
                        size    48863846 \
                        ${libnyquist_distfile} \
                        rmd160  da26633dcaae3b4e8885d17742b1b0a55858def4 \
                        sha256  27630884fa1c9302cd7b7c0252c2e8bcd52dfde481b3f464ed1ccc75c313827d \
                        size    706367


description             an easy-to-use, cross-platform multi-track audio editor/recorder

long_description        Tenacity is {*}${description} forked from Audacity.

# The automatically-generated tarball does not contain the submodules so we have
# to fetch them manually.
# https://codeberg.org/tenacityteam/tenacity/issues/315
# But there is also a suggestion to replace the libnyquist submodule with an
# external library at which point we won't need any submodules.
# https://codeberg.org/tenacityteam/tenacity/issues/365
master_sites            ${codeberg.master_sites}/[join ${codeberg.tag_prefix}]${codeberg.version}[join ${codeberg.tag_suffix}]${extract.suffix}?dummy=:tenacity \
                        [string map [list /${codeberg.project}/ /libnyquist/] ${codeberg.master_sites}]/${libnyquist_commit}${extract.suffix}?dummy=:libnyquist

distfiles               ${tenacity_distfile}:tenacity \
                        ${libnyquist_distfile}:libnyquist

categories              audio
maintainers             {ryandesign @ryandesign} openmaintainer
license                 GPL-2+

# CMakeLists.txt sets macOS deployment target to 10.15 and src/CMakeLists.txt
# sets the SDK to 10.13 when linking to fix high CPU use on 10.14 and later.
# I haven't tried removing that yet.
platforms               {darwin >= 19}

# CMakeLists.txt checks CMAKE_SIZEOF_VOID_P and WORDS_BIGENDIAN. I haven't tried
# using the muniversal portgroup yet.
universal_variant       no

patchfiles              FFmpeg.patch \
                        GIT_DESCRIBE.patch \
                        TenacityFunctions.cmake.patch

post-patch {
    # https://codeberg.org/tenacityteam/tenacity/issues/397
    reinplace "s|@GIT_DESCRIBE@|[join ${codeberg.tag_prefix}]${codeberg.version}[join ${codeberg.tag_suffix}]|g" ${worksrcpath}/CMakeLists.txt
    # https://codeberg.org/tenacityteam/tenacity/issues/401
    reinplace "s|@PREFIX@|${prefix}|g" ${worksrcpath}/libraries/lib-ffmpeg-support/FFmpegFunctions.cpp
}

# Python might not actually be required anymore.
# https://codeberg.org/tenacityteam/tenacity/issues/393#issuecomment-1653304
set python_version      3.13
wxWidgets.use           wxWidgets-3.2

depends_build-append    port:gettext \
                        port:nasm \
                        path:bin/pkg-config:pkgconfig \
                        port:python[string map {. {}} ${python_version}]

depends_lib-append      port:ffmpeg \
                        port:flac \
                        port:expat \
                        port:lame \
                        port:libebml \
                        port:libid3tag \
                        port:libmad \
                        port:libmatroska \
                        port:libogg \
                        port:libsndfile \
                        port:libvorbis \
                        port:portaudio \
                        port:soundtouch \
                        port:soxr \
                        port:sqlite3 \
                        port:twolame \
                        port:${wxWidgets.port} \
                        port:zlib

# The codeberg portgroup turns this on which doesn't work when using multiple
# distfiles and it's better to set an accurate worksrcdir in the first place.
extract.rename          no

worksrcdir              ${name}

post-extract {
    # Move the manually-downloaded git submodules where they need to go.
    foreach lib [list libnyquist] {
        delete ${worksrcpath}/lib-src/${lib}
        move ${workpath}/${lib} ${worksrcpath}/lib-src
    }
}

compiler.cxx_standard   2017

cmake.generator         Ninja

configure.python        ${prefix}/bin/python${python_version}

configure.args-append   -DCCACHE=OFF \
                        -DPython3_EXECUTABLE=${configure.python} \
                        -DSCCACHE=OFF \
                        -DVCPKG=OFF \
                        -DwxWidgets_CONFIG_EXECUTABLE=${wxWidgets.wxconfig}

# This option does not prevent Tenacity from needing to dynamically load the
# FFmpeg libraries, it just hides the section of the Preferences window that
# allows the user to choose a different location for the dynamically-loaded
# FFmpeg libraries. We set the correct location using a patchfile.
# https://codeberg.org/tenacityteam/tenacity/issues/401
configure.args-append   -DDISABLE_DYNAMIC_LOADING_FFMPEG=ON

# LV2 support requires lilv and lv2 (for which we have ports) and suil (for
# which we don't).
# https://github.com/lv2/suil
configure.args-append   -DLV2=OFF

# MIDI support requires PortMidi (for which we have a port), PortTime (which is
# part of PortMidi) and PortSMF (for which we don't have a port).
# https://codeberg.org/tenacityteam/portsmf
configure.args-append   -DMIDI=OFF

# sbsms support requires libsmsms for which we don't have a port.
# https://github.com/claytonotey/libsbsms
# However there is also an open suggestion to replace libsbsms with rubberband
# for which we do already have a port.
# https://codeberg.org/tenacityteam/tenacity/issues/377
configure.args-append   -DSBSMS=OFF

# Vamp plugin support requires vamp-plugin-sdk? for which we don't have a port.
# https://github.com/vamp-plugins/vamp-plugin-sdk
configure.args-append   -DVAMP=OFF

# The program's native localization is English so there aren't any separate
# English localization resources, but if the English localization folder doesn't
# exist then the interface will be in some other language. The build system does
# create this folder but doesn't put anything in it, and MacPorts post destroot
# processing and archiving does not preserve empty folders.
destroot.keepdirs       ${destroot}${applications_dir}/Tenacity.app/Contents/Resources/en.lproj

post-destroot {
    # TODO: Figure out if some CMake flag can direct the app here.
    move ${destroot}${prefix}/Tenacity.app ${destroot}${applications_dir}

    # The installation process installs this library which is not needed because
    # the program has already been linked with it statically at build time.
    # https://codeberg.org/tenacityteam/tenacity/issues/394
    delete ${destroot}${prefix}/lib/liblibnyquist.a
}
