# -*- 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 89264 2012-01-22 19:48:30Z michaelld@macports.org $

PortSystem          1.0
PortGroup           qt4 1.0

name                liblastfm
version             0.3.3
revision            1
categories          audio
license             GPL-3
maintainers         michaelld openmaintainer

description         A set of libraries allowing use of the Last.fm site services.
long_description    liblastfm is a collection of libraries to help you \
integrate Last.fm services into your rich desktop software. It is \
officially supported software developed by Last.fm staff.

platforms           darwin

homepage            https://github.com/mxcl/liblastfm
master_sites        https://github.com/mxcl/liblastfm/tarball/${version}

checksums           md5     fe339bf46aefc515c251200d10262f79 \
                    sha1    f2e9705c9c2cbeaa14f46da9bd35ab36fe710392 \
                    rmd160  690931075f38fda78c69015ca5088c166f6e1013

worksrcdir          mxcl-${name}-1c739eb

depends_build-append port:ruby

depends_lib-append  port:libsamplerate port:fftw-3-single

universal_variant   yes

use_parallel_build  no

post-patch {
    # fix library install directory
    reinplace "/target\\.path/s@\\/lib@\\\$\\\$QMAKE_LIBDIR_QT@" \
        ${worksrcpath}/src/lastfm.pro
    reinplace "/target\\.path/s@\\/lib@\\\$\\\$QMAKE_LIBDIR_QT@" \
        ${worksrcpath}/src/fingerprint/fingerprint.pro

    # swap -l -L ordering (to be -L -l)
    reinplace "/LIBS/s@+= \\(-l\[^ \]*\\) \\(-L\[^ \]*\\)@+= \\2 \\1@" \
        ${worksrcpath}/demos/demos.pro
    reinplace "/LIBS/s@+= \\(-l\[^ \]*\\) \\(-L\[^ \]*\\)@+= \\2 \\1@" \
        ${worksrcpath}/tests/tests.pro
}

configure.pre_args  --prefix ${prefix}
configure.args      --release
configure.universal_args

# when Qt is installed without +universal, and qmake is called with
# "QMAKE_LDFLAGS += -arch foo" (or this is in the .qmake.cache, or
# some other location that is used by QMake), and "foo" is the native
# arch, then, at least sometimes, qmake will erroneously remove the
# second "foo" but leave the "-arch" flag -- generating an error at
# linking.  Not sure if this is the case with +debug; does not matter
# for this port, since it has no debug variant.
post-configure {
    if {![variant_isset universal]} {
        # (1) find subdir Makefile names from top-level
        # Makefile, and have 'make' create each in turn.
        foreach fixfile [exec grep -e "Makefile\[\^ \]\*:" \
                ${worksrcpath}/Makefile | \
                sed -e "s@\\(\[^ \]*/Makefile\[^ \]*\\):\[^ \]*@\\1@g"] {
            system "cd ${worksrcpath} && \
                    PATH=${qt_bins_dir}:$env(PATH) \
                    make ${fixfile}"
        }

        # (2) find all of the just-created Makefile*'s, and reinplace
        # the offending QMake flaw
        foreach fixfile [exec find ${worksrcpath} -name "Makefile*"] {
            reinplace "s@\\(-arch \[^ \]*\\) -arch@\\1@" ${fixfile}
        }
    }
}

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

post-destroot {
    # fix DYLD self-id for both libraries
    system "install_name_tool -id ${prefix}/lib/liblastfm.0.dylib ${destroot}${prefix}/lib/liblastfm.0.3.3.dylib"
    system "install_name_tool -id ${prefix}/lib/liblastfm_fingerprint.0.dylib ${destroot}${prefix}/lib/liblastfm_fingerprint.0.3.3.dylib"
}

livecheck.type      regex
livecheck.url       https://github.com/mxcl/liblastfm/tags
livecheck.regex     {tarball/([0-9.]+)"}
