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

name                phonon

version             4.10.3
checksums           rmd160 7a1934b45a6d54d0803edf0a3cd6d7b5b4ecc1e7 \
                    sha256 2e8b145669afa0e93833e4064b657677abc9413e4007fa5ddc91397c9bddc295 \
                    size   336724
revision            0

license             {LGPL-2.1 LGPL-3}
maintainers         {michaelld @michaelld} openmaintainer
description         Cross Platform Multimedia API used by KDE4
platforms           darwin
homepage            https://phonon.kde.org
master_sites        kde:stable/${name}/${version}
use_xz              yes
distname            phonon-${version}

variant demos description {Build the demos} {
    configure.args-append   -DPHONON_BUILD_DEMOS:BOOL=ON
}

subport ${name}-qt5 {
    PortGroup           qt5 1.0
    PortGroup           cmake 1.1
    categories          audio kde kf5
    long_description    Phonon is a multimedia, multi-platform sound \
        framework for the application developer. This is the  Phonon 4\
        transitional library for Qt5 (API compatible with regular Phonon4)

    qt5.depends_component \
                            qtdeclarative qttools

    depends_build-append    port:kde-extra-cmake-modules
    configure.args-append   -DPHONON_BUILD_PHONON4QT5:BOOL=ON

    set LPH libphonon4qt5
    post-destroot {

        if {[variant_isset demos]} {
            set demos_dest_dir ${destroot}${prefix}/share/examples/phonon4qt5
            xinstall -m 775 -d ${demos_dest_dir}
            copy ${workpath}/build/demos/simpleplayer/simpleplayer.app ${demos_dest_dir}
            copy ${workpath}/build/demos/metadatareader/metadatareader.app ${demos_dest_dir}
            copy ${workpath}/build/demos/phota/phota.app ${demos_dest_dir}
            copy ${workpath}/build/demos/simplecapture/simplecapture.app ${demos_dest_dir}
        }

        # fix library and plugin self-names
        system "install_name_tool -id ${prefix}/lib/${LPH}.4.dylib ${destroot}${prefix}/lib/${LPH}.dylib"
        system "install_name_tool -id ${prefix}/lib/${LPH}experimental.4.dylib ${destroot}${prefix}/lib/${LPH}experimental.dylib"

        # fix use of libphonon4qt5
        system "install_name_tool -change lib/${LPH}.4.dylib ${prefix}/lib/${LPH}.4.dylib \
            ${destroot}${prefix}/lib/${LPH}experimental.dylib"
        system "install_name_tool -change lib/${LPH}.4.dylib ${prefix}/lib/${LPH}.4.dylib \
            ${destroot}${qt_plugins_dir}/designer/phononwidgets.dylib"
        system "install_name_tool -id phononwidgets.dylib ${destroot}${qt_plugins_dir}/designer/phononwidgets.dylib"
        if {[variant_isset demos]} {
            system "install_name_tool -change lib/${LPH}.4.dylib ${prefix}/lib/${LPH}.4.dylib \
                ${demos_dest_dir}/simpleplayer.app/Contents/MacOS/simpleplayer"
            system "install_name_tool -change lib/${LPH}.4.dylib ${prefix}/lib/${LPH}.4.dylib \
                ${demos_dest_dir}/metadatareader.app/Contents/MacOS/metadatareader"
            system "install_name_tool -change lib/${LPH}.4.dylib ${prefix}/lib/${LPH}.4.dylib \
                ${demos_dest_dir}/phota.app/Contents/MacOS/phota"
            system "install_name_tool -change lib/${LPH}.4.dylib ${prefix}/lib/${LPH}.4.dylib \
                ${demos_dest_dir}/simplecapture.app/Contents/MacOS/simplecapture"
        }
    }
}

default_variants    +compversion440

# fix use of CMAKE_INSTALL_NAME to be an absolute path
# (RJVB: why is this necessary?)
patchfiles-append   patch-cmake_FindPhononInternal.cmake.diff

# fix MODULE names to end in .dylib instead of .so
patchfiles-append   patch-cmake_PhononMacros.cmake.diff

variant pulseaudio description "Build Phonon with additional support for PulseAudio" {
    depends_lib-append port:pulseaudio
    configure.args-append \
        -DPULSEAUDIO_INCLUDE_DIR=${prefix}/include \
        -DPULSEAUDIO_LIBRARY=${prefix}/lib/libpulse.dylib \
        -DPULSEAUDIO_MAINLOOP_LIBRARY=${prefix}/lib/libpulse-mainloop-glib.dylib \
        -DWITH_PulseAudio=ON \
        -DPHONON_PULSESUPPORT=YES
}

if {![variant_isset pulseaudio]} {
    configure.args-append \
        -DWITH_PulseAudio=OFF \
        -DPHONON_PULSESUPPORT=NO \
        -DPULSEAUDIO_INCLUDE_DIR= \
        -DPULSEAUDIO_LIBRARY= \
        -DPULSEAUDIO_MAINLOOP_LIBRARY=
}

if {${subport} ne "${name}-qt5"} {
    PortGroup           qt4 1.0
    PortGroup           cmake 1.1
    categories          audio kde kde4
    long_description    Phonon is a multimedia, multi-platform sound \
        framework for the application developer.
    # do we really depend on port:automoc, because cmake prints
    # -- Using CMake automoc builtin
    depends_build-append    port:automoc

    variant compversion440 description {Build libraries with compatibility version 4.4.0 for existing Qt4 installs expecting that} {
        # fix the library compatibility version to be 4.4.0
        patchfiles-append   patch-CMakeLists.txt.diff
    }

    set LPH libphonon
    post-destroot {

        global demos_dest_dir
        if {[variant_isset demos]} {
            set demos_dest_dir ${destroot}${prefix}/share/examples/phonon
            xinstall -m 775 -d ${demos_dest_dir}
            copy ${workpath}/build/demos/simpleplayer/simpleplayer ${demos_dest_dir}
            copy ${workpath}/build/demos/metadatareader/metadatareader ${demos_dest_dir}
            copy ${workpath}/build/demos/phota/phota ${demos_dest_dir}
            copy ${workpath}/build/demos/simplecapture/simplecapture ${demos_dest_dir}
        }

        if {[variant_isset compversion440]} {
            # link major library version name
            ln -s ${prefix}/lib/${LPH}.dylib ${destroot}${prefix}/lib/${LPH}.4.dylib
            ln -s ${prefix}/lib/${LPH}experimental.dylib ${destroot}${prefix}/lib/${LPH}experimental.4.dylib

            set DLVERS  4.4.0
        } else {
            set DLVERS  4
        }

        # fix library and plugin self-names
        system "install_name_tool -id ${prefix}/lib/${LPH}.4.dylib ${destroot}${prefix}/lib/${LPH}.dylib"
        system "install_name_tool -id ${prefix}/lib/${LPH}experimental.4.dylib ${destroot}${prefix}/lib/${LPH}experimental.dylib"

        # fix use of libphonon
        system "install_name_tool -change lib/${LPH}.${DLVERS}.dylib ${prefix}/lib/${LPH}.4.dylib \
            ${destroot}${prefix}/lib/${LPH}experimental.dylib"
        system "install_name_tool -change lib/${LPH}.${DLVERS}.dylib ${prefix}/lib/${LPH}.4.dylib \
            ${destroot}${qt_plugins_dir}/designer/libphononwidgets.dylib"
        if {[variant_isset demos]} {
            system "install_name_tool -change lib/${LPH}.${DLVERS}.dylib ${prefix}/lib/${LPH}.4.dylib \
                ${demos_dest_dir}/simpleplayer"
            system "install_name_tool -change lib/${LPH}.${DLVERS}.dylib ${prefix}/lib/${LPH}.4.dylib \
                ${demos_dest_dir}/metadatareader"
            system "install_name_tool -change lib/${LPH}.${DLVERS}.dylib ${prefix}/lib/${LPH}.4.dylib \
                ${demos_dest_dir}/phota"
            system "install_name_tool -change lib/${LPH}.${DLVERS}.dylib ${prefix}/lib/${LPH}.4.dylib \
                ${demos_dest_dir}/simplecapture"
        }
    }
}

cmake.out_of_source         yes

post-patch {
    set rel_mp_qt_cmake_dir [strsed ${qt_cmake_module_dir} "g@${prefix}/@@"]
    reinplace "s|@MACPORTS_CMAKE_DIR@|${rel_mp_qt_cmake_dir}|" \
        ${worksrcpath}/CMakeLists.txt
}

if {[info exists qt_cmake_defines]} {
    configure.args-append   \
        ${qt_cmake_defines}
}
# nb: there is no WITH_QZeitgeist option, keeping it out of nostalgia
configure.args-append   \
    -DPHONON_INSTALL_QT_EXTENSIONS_INTO_SYSTEM_QT=true \
    -DWITH_QZeitgeist=OFF

configure.ldflags-append -F${qt_frameworks_dir}

livecheck.type      regex
livecheck.url       http://mirrors.mit.edu/kde/stable/${name}/
livecheck.regex     ">(\[0-9.\]+)/<"
