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

github.setup        clangen musikcube 3.0.5
revision            1
categories          multimedia audio
license             BSD
maintainers         {@barracuda156 macos-powerpc.org:barracuda} openmaintainer

description         Cross-platform terminal-based music player
long_description    {*}${description}, audio engine, metadata indexer and server in C++.

homepage            https://musikcube.com

checksums           rmd160  06b8a46414238446044c4d758b460c747d395afe \
                    sha256  708292a583bb5072a8dbb14e408c2a1f61de9b8c9786d4e53b3e69bef5dad8c5 \
                    size    4891353
github.tarball_from archive

compiler.c_standard 1999
compiler.cxx_standard \
                    2017

# There is no dependency on Boost anymore,
# instead a standalone Asio is used.
depends_build-append \
                    port:asio \
                    path:bin/pkg-config:pkgconfig

# Care should be taken with regard to FFMpeg.
# Its API changes, and musikcube source may fail
# to maintain compatibility with older or newer versions.
set ffmpg_ver       7

conflicts_build     ffmpeg

configure.pkg_config_path-prepend \
                    ${prefix}/libexec/ffmpeg${ffmpg_ver}/lib/pkgconfig

depends_lib-append  port:curl \
                    port:flac \
                    port:lame \
                    port:ffmpeg${ffmpg_ver} \
                    port:libev \
                    port:libmicrohttpd \
                    port:libogg \
                    port:libopenmpt \
                    port:libvorbis \
                    port:mpg123 \
                    port:ncurses \
                    port:portaudio \
                    port:pulseaudio \
                    port:taglib \
                    port:zlib

patchfiles-append   patch-CMakeLists.diff \
                    patch-fix-curses.diff

# Leopard does not support needed APIs, so disable macosmediakeys plug-in.
# https://trac.macports.org/ticket/72817
if {${os.platform} eq "darwin" && ${os.major} < 10} {
    patchfiles-append \
                    patch-leopard.diff
}

post-patch {
    reinplace "s,@PREFIX@,${prefix},g" \
                    ${worksrcpath}/src/plugins/ffmpegdecoder/CMakeLists.txt \
                    ${worksrcpath}/src/plugins/stockencoders/CMakeLists.txt
    reinplace "s,@FFMPG_VER@,${ffmpg_ver},g" \
                    ${worksrcpath}/src/plugins/ffmpegdecoder/CMakeLists.txt \
                    ${worksrcpath}/src/plugins/stockencoders/CMakeLists.txt
}

# Please do not change default settings without a good reason to
# and sufficient testing on all supported systems. In particular,
# everything may build with no errors but fail to work.
configure.args-append \
                    -DENABLE_BUNDLED_TAGLIB=false \
                    -DENABLE_PCH=false

# The following are just here for convenience:
# -DENABLE_MACOS_SYSTEM_NCURSES=true will link against dynamic libs.
# -DDISABLE_WIDE_NCURSES_LIB_SUFFIXES=true should be used if ncurses does not install w-postfixed libs.

# https://github.com/clangen/musikcube/issues/656
if {[string match *gcc* ${configure.compiler}]} {
    configure.cflags-append \
                    -std=c99 -fobjc-exceptions
}

# atomics check pretends to be there but does nothing:
# https://github.com/clangen/musikcube/issues/657
if {[string match *gcc* ${configure.compiler}] && ${configure.build_arch} in [list arm i386 ppc]} {
    configure.ldflags-append \
                    -latomic
}

# PowerPC-dedicated issue:
# https://github.com/clangen/musikcube/issues/658
platform darwin powerpc {
    # We do need custom optflags here, otherwise while the build succeeds,
    # the app does not work correctly.
    configure.optflags \
                    -O0 -g

    notes "
    Presently CoreAudio output may not work correctly on PowerPC systyems.
    Please use PortAudio or possibly PulseAudio instead.
    "
}

post-destroot {
    # Build system dumps everything into share. Fix installation:
    move ${destroot}${prefix}/share/musikcube/libmusikcore.dylib ${destroot}${prefix}/lib/libmusikcore.dylib

    # Same condition as in legacysupport:
    if {${os.platform} eq "darwin" && ${configure.cxx_stdlib} ne "libc++"} {
        # The port installs its wrappers, but they do not work correctly.
        # Replace with fixed ones. These are only wrappers, not actual binaries.
        foreach f {musikcube musikcubed} {
            delete ${destroot}${prefix}/bin/${f}
            reinplace "s,@PREFIX@,${prefix},g" ${filespath}/${f}
            xinstall -m 0755 ${filespath}/${f} ${destroot}${prefix}/bin
        }
    }
}
