# -*- 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 118446 2014-04-02 14:56:36Z michaelld@macports.org $

PortSystem      1.0
PortGroup       qt4 1.0
PortGroup       python 1.0

name            py-pyqt4
python.versions 24 25 26 27 31 32 33 34
python.default_version 27
categories-append     devel
platforms       macosx
maintainers     michaelld openmaintainer
license         {GPL-2 GPL-3}
description     PyQt4 is a set of Python bindings for the Qt4 toolkit
long_description \
    PyQt4 is a set of Python bindings for the Qt4 toolkit. The \
    bindings are implemented as a set of Python modules: qt, \
    qtcanvas, qtgl, qtnetwork, qtsql, qttable, qtui and qtxml, \
    and contains 300 classes and over 5,750 functions and methods.
homepage        http://www.riverbankcomputing.co.uk/software/pyqt/intro

# pre-declare provided subports

foreach py_ver ${python.versions} {
    subport py${py_ver}-pyqt4-devel {}
    subport py${py_ver}-pyqt4 {}
}

# keep 'port' happy, by defining a global version which is for the
# primary release

set patch 4.10
version ${patch}.4

if {${name} ne ${subport}} {

    if {[string last devel ${subport}] != -1} {

        # devel port

        # replaced by py*-pyqt4 (non -devel), hopefully permanently

        replaced_by py${python.version}-pyqt4

        pre-configure {
            ui_error "This port has been temporarily replaced by 'py${python.version}-pyqt4'."
            return -code error
        }

        distfiles

    } else {

        # release port

        revision        1

        master_sites    sourceforge:pyqt
        distname        PyQt-mac-gpl-${version}

        checksums       rmd160 e3db64a1535a774a99a9a3a8b2deb406fc31a922 \
                        sha256 43ca99e80911ac82a6840335a6681f3193ce5257a50f1a31eb8ce4097330d023

        patchfiles-append patch-configure.py.diff

    }

    dist_subdir        python

    depends_lib-append port:py${python.version}-sip \
                       port:qt4-mac

    # patch-configure.py* file alters configure.py so that the
    # qtdesigner plugin can link.  It ensures that both LFLAGS are
    # included and that the relevant version framework is linked.  It
    # also adds a "framework" directory concept to make the split in
    # qt4-mac as of 4.8.0, and uses it where appropriate.

    # fix location of MacPorts'-installed Qt .apps, step 1

    patchfiles-append patch-fix-qt_apps_dir.diff

    post-patch {
        reinplace "s|@@MACPORTS_PYTHON_FRAMEWORK@@|${frameworks_dir}/Python.framework/Versions/${python.branch}/Python|" ${worksrcpath}/configure.py

        # fix location of MacPorts'-installed Qt .apps, step 2

        reinplace "s|@QT_APPS_DIR@|${qt_apps_dir}|" \
            ${worksrcpath}/examples/demos/qtdemo/menumanager.py \
            examples/designer/plugins/plugins.py
    }

    use_configure yes

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

        foreach arch [get_canonical_archs] {
            configure.args-append --use-arch=$arch
        }
    }

    configure.cmd       "${python.bin} configure.py -q ${qt_qmake_cmd} --verbose --confirm-license"
    configure.pre_args
    configure.universal_args-delete --disable-dependency-tracking

    configure.args-append   -e QtGui \
                            -e QtHelp \
                            -e QtMultimedia \
                            -e QtNetwork \
                            -e QtDeclarative \
                            -e QtOpenGL \
                            -e QtScript \
                            -e QtScriptTools \
                            -e QtSql \
                            -e QtSvg \
                            -e QtTest \
                            -e QtWebKit \
                            -e QtXml \
                            -e QtXmlPatterns \
                            -e QAxContainer \
                            --no-qsci-api

    # SIP's configure tools ignore environment variables, so have to
    # get important once in in other ways if/as possible; SIP will, by
    # default, use what was found by QMake when qt4-mac was installed.

    set cflags   "${configure.cflags}"
    set cxxflags "${configure.cxxflags}"
    if {[tbool configure.pipe]} {
        set cflags   "${cflags} -pipe"
        set cxxflags "${cxxflags} -pipe"
    }

    configure.post_args \
        CC=${configure.cc} \
        CXX=${configure.cxx} \
        LFLAGS="-F${frameworks_dir} ${configure.ldflags}" \
        CFLAGS="${cflags}" \
        CXXFLAGS="${cxxflags}"

    # subport for Python 2.4 or 2.5 installs into non-Framework locations
    if {${python.version} <= 25} {
        configure.cmd-append " -d ${python.pkgd} -b ${prefix}/bin"
    }

    # Don't build designer plugin for 2.4; not built as a framework
    if {${python.version} != 24 } {
        configure.args-append   -e QtDesigner
    }

    variant phonon description {Add phonon support} {
        configure.args-append   -e phonon
        depends_lib-append      port:phonon
    }


    variant scintilla description {Add QScintilla API file } {
        configure.args-delete   --no-qsci-api
        configure.args-append   --qsci-api
        depends_lib-append      port:qscintilla
    }

    # dbus-python does not yet work correctly with 3.0+

    if {${python.version} <= 27} {

        depends_lib-append port:dbus-python${python.version}
        configure.args-append --dbus=${python.include}/dbus-1.0

    }

    post-configure {
        if {[variant_isset universal]} {
            set conflags ""
            foreach arch ${configure.universal_archs} {
                if {${arch} eq "i386"} {append conflags "x86 "} else {
                    if {${arch} eq "ppc64"} {append conflags "ppc_64 "} else {
                        append conflags ${arch} " "
                    }
                }
            }

            set profiles [exec find ${worksrcpath} -name "*.pro"]
            foreach profile ${profiles} {
                reinplace -E "s|^(CONFIG\[ \\t].*)|\\1 ${conflags}|" ${profile}
            }

            # This file exists if Qt was installed non-universal.
            # Easiest just to check if it was created.
            if {[file exists ${worksrcpath}/designer/python.pro ]} {
                system -W ${worksrcpath}/designer "${qt_qmake_cmd} \
                    -spec ${qt_mkspecs_dir}/macx-g++ \
                    -o Makefile python.pro"
            }
        }
    }

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

    build.cmd           make
    build.target        all
    use_parallel_build  yes

    destroot.cmd        ${build.cmd}
    destroot.destdir    DESTDIR=${destroot}

    test.run            yes
    test.cmd            cd qt && ${python.bin} -c 'import PyQt4'

    post-destroot {
        # Clean out empty dirs first
        set docdir ${destroot}${prefix}/share/doc
        file delete ${docdir}/${subport}/examples
        file delete ${docdir}/${subport}/
        file copy ${worksrcpath}/doc ${docdir}/${subport}
        file copy ${worksrcpath}/examples \
            ${docdir}/${subport}
        xinstall -m 644 -W ${worksrcpath} \
            GPL_EXCEPTION.TXT GPL_EXCEPTION_ADDENDUM.TXT \
            LICENSE.GPL2 LICENSE.GPL3 OPENSOURCE-NOTICE.TXT NEWS README \
            THANKS \
            ${docdir}/${subport}

        # Rename designer plugin to include Python version; easiest
        # just to check if it was created, instead of patching.
        if {[file exists ${destroot}${qt_plugins_dir}/designer/libpyqt4.dylib]} {
            # rename Qt4 specific files
            # (a) designer plugin
            set old_name libpyqt4.dylib
            set new_name libpy${python.version}qt4.dylib

            move ${destroot}${qt_plugins_dir}/designer/${old_name} \
                ${destroot}${qt_plugins_dir}/designer/${new_name}
            system "install_name_tool -id ${new_name} \
                ${destroot}${qt_plugins_dir}/designer/${new_name}"
        }

        # (b) Qsci API file, if it exists (if Qsci is installed at build time)
        if {[variant_isset scintilla]} {
            move ${destroot}${qt_data_dir}/qsci/api/python/PyQt4.api \
                ${destroot}${qt_data_dir}/qsci/api/python/PyQt4-Python${python.branch}.api
        }

        # delete python3 code from python2 installs and vice versa,
        # if each exists
        if {${python.version} >= 30} {
            if {[file exists ${destroot}${python.pkgd}/PyQt4/uic/port_v2]} {
                delete ${destroot}${python.pkgd}/PyQt4/uic/port_v2
            }
        } else {
            if {[file exists ${destroot}${python.pkgd}/PyQt4/uic/port_v3]} {
                delete ${destroot}${python.pkgd}/PyQt4/uic/port_v3
            }
        }
    }

    variant debug description "Build debug libraries" {
        configure.cmd-append --debug
    }

}

if {[string last devel ${subport}] != -1} {

    livecheck.type   none

} else {

    livecheck.type   regex
    livecheck.url    http://www.riverbankcomputing.co.uk/software/pyqt/download
    livecheck.regex  >PyQt-mac-gpl-(\[0-9.\]*).tar.gz<

}
