# -*- coding: utf-8; mode: tcl; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4; truncate-lines: t -*- vim:fenc=utf-8:et:sw=4:ts=4:sts=4
# $Id: Portfile 68459 2010-06-03 23:43:56Z mcalhoun@macports.org $

PortSystem          1.0

name                qt4-mac
conflicts           qt4-mac-devel kdelibs3 kdelibs4 libevent xmlrpcxx
version             4.6.2
revision            2
categories          aqua
platforms           macosx
maintainers         nomaintainer

homepage            http://qt.nokia.com/
description         Qt Tool Kit (Native Aqua Version)
long_description    Qt is a cross-platform application and UI framework for writing cross-platform GUI applications. \
                    This version uses the native Aqua UI. For a X11 version, see qt4-x11.
master_sites        trolltech
distname            qt-everywhere-opensource-src-${version}

checksums           md5     eb651ee4b157c01696aa56777fc6e0e5 \
                    sha1    977c10b88a2230e96868edc78a9e3789c0fcbf70 \
                    rmd160  5f3fea120f2dba274c4150b02162bba40b65a872

depends_build       port:pkgconfig

depends_lib         port:zlib port:dbus port:openssl port:sqlite3 \
                    port:tiff port:libpng port:libmng port:jpeg

# Test for Precompiled-headers support requires a C++ compiler, but configure calls it, by default, with a C compiler.
patchfiles          patch-precomp.test.diff

# The build process uses -fconstant-cfstrings.
# configure.compiler should therefore not be one of the MacPorts compilers.

# --prefix is not recognized.
configure.pre_args-delete  --prefix=${prefix}

################################################################################
# We do not want flags of any kind -- QT is smart enough to set up its own.    #
################################################################################

configure.cflags
configure.cppflags
configure.cxxflags
configure.ldflags

configure.universal_cflags
configure.universal_cppflags
configure.universal_cxxflags
configure.universal_ldflags

configure.cc_archflags
configure.cxx_archflags
if {[info exists configure.ld_archflags]} {
    configure.ld_archflags
}

set dirname qt4-mac
set qt_dir ${prefix}/libexec/${dirname}

#  CPATH is used instead of -I to avoid conflicts with other ports.
#  See http://trac.macports.org/ticket/15219 and http://trac.macports.org/ticket/18723.
#
# -system-sqlite ensures the use SQLite provided by MacPorts instead of Qt.
#
# -dbus-linked prevends qt4 from trying to dynamically load libdbus-1,
#     which it is not able to find in ${prefix}.
# -openssl-linked ensures that the MacPorts openssl is used.
#
configure.args                                            \
    -v                                                    \
    -debug-and-release                                    \
    -confirm-license                                      \
    -opensource                                           \
    -prefix          ${qt_dir}                            \
    -docdir          ${prefix}/share/doc/${dirname}       \
    -examplesdir     ${prefix}/share/${dirname}/examples  \
    -demosdir        ${prefix}/share/${dirname}/demos     \
    -cocoa                                                \
    -system-sqlite                                        \
    -openssl-linked                                       \
    -dbus-linked                                          \
    -L${prefix}/lib                                       \
    -arch            \"${build_arch}\"

lappend cpath        ${prefix}/include
lappend library_path ${prefix}/lib

pre-configure {
    configure.env-append \
        CPATH=[join ${cpath} :] \
        LIBRARY_PATH=[join ${library_path} :]
}

pre-build {
    build.env-append \
        CPATH=[join ${cpath} :] \
        LIBRARY_PATH=[join ${library_path} :]
}

# Stop configure script from searching for SQL Drivers
#    not available from MacPorts.
foreach driver {db2 ibase oci tds} {
    configure.args-append -no-sql-${driver}
}

# By default, disable most SQL Drivers.
# There must be at least one enabled (http://trac.macports.org/ticket/15627).
# SQLite is chosen to be always enabled because a copy of it is distributed
#    with Qt, so it is reasonable for the user to expect it.
foreach driver {mysql odbc psql sqlite2} {
    configure.args-append -no-sql-${driver}
}

platform darwin 8 {
    configure.args-delete -cocoa
    post-configure {
        fs-traverse item ${worksrcpath} {
            if {"Makefile" == [file tail ${item}]} {
                reinplace "s|-framework QtHelp|-framework QtHelp -lQtCLucene|" ${item}
            }
        }
    }
}

variant mysql description {Enable MySQL SQL Driver} {
    depends_lib-append path:lib/mysql5:mysql5

    lappend cpath        ${prefix}/include/mysql5/mysql
    lappend library_path ${prefix}/lib/mysql5/mysql

    configure.args-delete -no-sql-mysql
}

variant odbc description {Enable iODBC SQL Driver} {
    depends_lib-append port:libiodbc
    configure.args-delete -no-sql-odbc
}

variant psql description {Enable PostgreSQL SQL Driver} {
    depends_lib-append port:postgresql83

    lappend cpath        ${prefix}/include/postgresql83
    lappend library_path ${prefix}/lib/postgresql83

    configure.args-delete -no-sql-psql
}

variant sqlite2 description {Enable SQLite version 2 SQL Driver} {
    depends_lib-append port:sqlite2
    configure.args-delete -no-sql-sqlite2
}

build.target        first
use_parallel_build  yes

destroot.destdir    INSTALL_ROOT="${destroot}"

post-patch {
    # Ensure that correct compilers are used.
    reinplace "s| g++\$| ${configure.cxx}|" ${worksrcpath}/mkspecs/common/g++.conf
    reinplace "s| gcc\$| ${configure.cc}|"  ${worksrcpath}/mkspecs/common/g++.conf
    reinplace "s| g++\$| ${configure.cxx}|" ${worksrcpath}/mkspecs/macx-g++/qmake.conf
    reinplace "s| gcc\$| ${configure.cc}|"  ${worksrcpath}/mkspecs/macx-g++/qmake.conf
}

post-destroot {
    # Install documentation.
    foreach doc { INSTALL LGPL_EXCEPTION.txt LICENSE.GPL3 LICENSE.LGPL README } {
        xinstall -c -m 644 ${worksrcpath}/$doc ${destroot}${prefix}/share/doc/${name}
    }

    # Create link in ${prefix}/bin to executable files in ${qt_dir}/bin.
    foreach bin [glob -type f -directory ${destroot}${qt_dir}/bin *] {
        ln -s ${qt_dir}/bin/[file tail ${bin}] ${destroot}${prefix}/bin
    }

    xinstall -d -m 0755 ${destroot}${applications_dir}/Qt
    # Create link in ${applications_dir}/Qt to .app directories in ${qt_dir}/bin/.
    # Create link in ${prefix}/bin to all *.app/Contents/MacOS/*.
    foreach app [glob -type d -directory ${destroot}${qt_dir}/bin *.app] {
        ln -s ${qt_dir}/bin/[file tail ${app}] ${destroot}${applications_dir}/Qt
        foreach bin [glob -tails -type f -directory ${app}/Contents/MacOS *] {
            ln -s ${qt_dir}/bin/[file tail ${app}]/Contents/MacOS/${bin} ${destroot}${prefix}/bin
        }
    }

    # Avoid conflict with other qt packages.
    # Ensure lower case names.
    foreach bin [glob ${destroot}${prefix}/bin/*] {
        file rename ${bin} ${destroot}${prefix}/bin/[string tolower [file tail ${bin}]]-mac
    }

    # Fix the .pc and .prl files by removing ${destroot}.
    foreach fixfile [glob -nocomplain -directory ${destroot} ${qt_dir}/lib/pkgconfig/*.pc ${qt_dir}/lib/*.prl ${qt_dir}/lib/*/*.prl ${prefix}/share/${dirname}/demos/shared/*.prl] {
        reinplace  \
            "s|-L${worksrcpath}/lib|-L${qt_dir}/lib|g" \
            ${fixfile}
        reinplace  \
            "s|-F${worksrcpath}/lib|-F${qt_dir}/lib|g" \
            ${fixfile}
    }

    # Create link to Frameworks in Frameworks directory.
    xinstall -d -m 0755 ${destroot}${frameworks_dir}
    foreach fdir [glob -tails -directory ${destroot}${qt_dir}/lib -nocomplain *.framework] {
        ln -s ${qt_dir}/lib/${fdir} ${destroot}${frameworks_dir}
    }

    # The debug .pc files have no counterpart in qt4-x11, so set a link to them in the pkgconfig directory.
    xinstall -d -m 0755 ${destroot}${prefix}/lib/pkgconfig
    foreach fl [glob -tails -directory ${destroot}${qt_dir}/lib/pkgconfig *_debug.pc] {
        ln -s ${qt_dir}/lib/pkgconfig/${fl} ${destroot}${prefix}/lib/pkgconfig/
    }

    # Install select file for qt4_select.
    xinstall -m 755 -d ${destroot}${prefix}/etc/select/qt4
    xinstall -m 644 ${filespath}/${dirname} ${destroot}${prefix}/etc/select/qt4/
}

variant raster description {Use raster graphics system by default} {
    configure.args-append   -graphicssystem  raster
}

variant no_demos description {Do not build demo programs} {
   configure.args-append -nomake demos
}

variant no_examples description {Do not build example programs} {
   configure.args-append -nomake examples
}

variant universal {
    configure.universal_args-delete --disable-dependency-tracking
    configure.args-delete -arch            \"${build_arch}\"
    configure.args-append -arch            \"${universal_archs}\"
    if {[info exists universal_sysroot]} {
        configure.args-append -sdk ${universal_sysroot}
    }
}

livecheck.type      regex
livecheck.url       http://qt.nokia.com/downloads/mac-os-cpp
livecheck.regex     "qt-everywhere-opensource-src-(4(?:\\.\\d+)*)${extract.suffix}"
