# -*- 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
# $Id: Portfile 90460 2012-03-06 07:27:41Z ryandesign@macports.org $

PortSystem          1.0
PortGroup           qt4 1.0

name                phonon
version             4.4.2
revision            3
categories          audio kde kde4
license             {LGPL-2.1 LGPL-3}
maintainers         nomaintainer
description         Cross Platform Multimedia API used by KDE4
long_description    Cross Platform Multimedia API used by KDE4
platforms           darwin
homepage            http://phonon.kde.org
master_sites        kde:stable/${name}/${version}/
use_bzip2           yes
distname            phonon-${version}
checksums           md5 d9eab28383783261254f1cef3b92a3fa \
                    sha1 c329d924fb75a89a9de8a4d799ff4dcd7ee3302e \
                    rmd160 aa37903f166904dd09401541df8fc268a2858606

depends_build-append  port:automoc

# after extracting both qt4-mac (latest) and phonon, do:
# $ cd `port dir phonon`/phonon*/qt7
# $ for tf in `ls -1`; do \
# > diff -u ${tf} `port dir qt4-mac`/work/qt-*/src/3rdparty/phonon/qt7/${tf};
# > done > `port dir phonon`/files/patch-qt7.diff
patchfiles            patch-qt7.diff

set PHONON_MAJOR_VERSION [lindex [split ${version} "."] 0]
set PHONON_MINOR_VERSION [lindex [split ${version} "."] 1]
set PHONON_PATCH_VERSION [lindex [split ${version} "."] 2]

set PHONON_BUILD_DIR ${worksrcpath}/build

post-patch {
    # fix phonon/audiodataoutput.cpp to not use the moc file
    reinplace "/audiodataoutput\.moc/d" \
        ${worksrcpath}/phonon/audiodataoutput.cpp

    # install qt 4.7.0 QMake files into phonon source
    system "cd ${filespath}/qmake && tar cf - . | \
        (cd ${worksrcpath}; tar xf -)"

    # create the 'phononnamespace.h' file
    system "cd ${worksrcpath}/phonon && \
        sed -e 's,@PHONON_LIB_MAJOR_VERSION@,${PHONON_MAJOR_VERSION},g' \
            -e 's,@PHONON_LIB_MINOR_VERSION@,${PHONON_MINOR_VERSION},g' \
            -e 's,@PHONON_LIB_PATCH_VERSION@,${PHONON_PATCH_VERSION},g' \
            phononnamespace.h.in > phononnamespace.h"

    # create the 'phonon.pc' files (release and debug)
    system "cd ${worksrcpath} && \
        mkdir -p ${PHONON_BUILD_DIR}/lib/pkgconfig && \
        sed -e 's,@PHONON_LIB_MAJOR_VERSION@,${PHONON_MAJOR_VERSION},g' \
            -e 's,@PHONON_LIB_MINOR_VERSION@,${PHONON_MINOR_VERSION},g' \
            -e 's,@PHONON_LIB_PATCH_VERSION@,${PHONON_PATCH_VERSION},g' \
            -e 's,@QT_MOC_CMD@,${qt_moc_cmd},g' \
            -e 's,@QT_UIC_CMD@,${qt_uic_cmd},g' \
            -e 's,@QT_LIBS_DIR@,${qt_libs_dir},g' \
            -e 's,@QT_INCLUDES_DIR@,${qt_includes_dir},g' \
            -e 's,@PHONON_PREFIX@,${prefix},g' < phonon_debug.pc.in > \
            ${PHONON_BUILD_DIR}/lib/pkgconfig/phonon_debug.pc"
    system "cd ${PHONON_BUILD_DIR}/lib/pkgconfig && \
        sed -e 's,_debug,,g' \
            -e 's,debug,release,g' \
            -e 's,Debug,Release,g' \
            phonon_debug.pc > phonon.pc"

    # create the 'headers.pri' and 'phonon' files
    set public_headers [exec find ${worksrcpath}/phonon \
                                 -name "*.h" -maxdepth 1 -mindepth 1 | \
                             sed -e "s@${worksrcpath}/@@g" | \
                             grep -v "_p"]
    set private_headers [exec find ${worksrcpath}/phonon \
                                 -name "*.h" -maxdepth 1 -mindepth 1 | \
                             sed -e "s@${worksrcpath}/@@g" | \
                             grep "_p"]
    set class_headers [exec find ${worksrcpath}/includes/phonon \
                               -name "\[A-Z\]*" | \
                           sed -e "s@${worksrcpath}/includes/@@g"]

    # create 'phonon' file first
    set INCLUDE_HEADERS ""
    foreach header ${public_headers} {
        set INCLUDE_HEADERS "${INCLUDE_HEADERS}\#include \"${header}\"@"
    }
    system "cd ${worksrcpath} && \
              sed -e 's,@INCLUDE_HEADERS@,${INCLUDE_HEADERS},' phonon.in | \
              tr '@' '\\n' > includes/phonon/phonon"
    unset INCLUDE_HEADERS

    # add new phonon to list of headers
    lappend public_headers "includes/phonon/phonon"

    # create 'headers.pri' file second
    set PUBLIC_HEADERS ""
    foreach header ${public_headers} {
        lappend PUBLIC_HEADERS "../${header}"
    }
    set CLASS_HEADERS ""
    foreach header ${class_headers} {
        lappend CLASS_HEADERS "../includes/${header}"
    }
    set PRIVATE_HEADERS ""
    foreach header ${private_headers} {
        lappend PRIVATE_HEADERS "../${header}"
    }
    system "cd ${worksrcpath} && \
              sed -e 's,@PHONON_PUBLIC_HEADERS@,${PUBLIC_HEADERS},' \
                  -e 's,@PHONON_CLASS_HEADERS@,${CLASS_HEADERS},' \
                  -e 's,@PHONON_PRIVATE_HEADERS@,${PRIVATE_HEADERS},' \
                headers.pri.in > includes/phonon/headers.pri"
    unset PUBLIC_HEADERS
    unset CLASS_HEADERS
    unset PRIVATE_HEADERS
}

pre-configure {
    # set arch type(s); done is a stage to make sure the 'options
    # qt_arch_types' is evaluated -after- +universal (if selected).
    configure.pre_args-append CONFIG+="${qt_arch_types}"

    # set build type
    set build_type "release"
    if {[variant_isset debug]} {
        set build_type "debug_and_release build_all"
    }
    configure.pre_args-append CONFIG+=\"${build_type}\"
}

# set up to use the installed QMake files
configure.cmd       ${qt_qmake_cmd}
configure.pre_args  \
    CONFIG+="phonon phonon-backend" \
    PHONON_MAJOR_VERSION=${PHONON_MAJOR_VERSION} \
    PHONON_MINOR_VERSION=${PHONON_MINOR_VERSION} \
    PHONON_PATCH_VERSION=${PHONON_PATCH_VERSION} \
    WORKSRCPATH=${worksrcpath} \
    LOCAL_BUILD_TREE=${PHONON_BUILD_DIR}
configure.args      "-o Makefile phonon.pro"
configure.universal_args

# allow ccache, if specified by the user
pre-build {
    if {[tbool configure.ccache]} {
        build.post_args "CCACHE=ccache"
    }
}

# Install cmake files (taken from port 'kdelibs4'). To use this file,
# include '-DCMAKE_MODULE_PATH=...' in configure.args during the
# initial 'configure' stage (which uses CMake), to the full path of
# where the CMake files are installed.
post-destroot {
    xinstall -m 755 -d ${destroot}${qt_cmake_module_dir}
    xinstall -m 644 -W ${filespath} \
        FindPhonon.cmake \
        ${destroot}${qt_cmake_module_dir}
}

universal_variant   yes

variant debug description "Build release and debug versions" {}

livecheck.type		regex
livecheck.url		http://www.gtlib.gatech.edu/pub/kde/stable/${name}/
livecheck.regex		"\(\\d+(?:\\.\\d+)*)\/"
