# -*- 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.4
github.tarball_from archive
revision            1

checksums           rmd160  63cfb4264a8eb90eaa14c1cdad80531eb9c86a07 \
                    sha256  25bb95b8705d8c79bde447e7c7019372eea7eaed9d0268510278e7fcdb1378a5 \
                    size    4827124

categories          multimedia audio
license             BSD
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer

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

homepage            https://musikcube.com

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

# Once ffmpeg-devel is switched to ffmpeg7, drop it from here:
conflicts_build     ffmpeg ffmpeg-devel

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

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