# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; truncate-lines: t -*- vim:fenc=utf-8:et:sw=4:ts=4:sts=4

PortSystem          1.0

name                qwt
categories          graphics science
# actually a little more permissive than the standard LGPL, but the
# distinction doesn't matter to us.
license             LGPL-2.1
maintainers         {michaelld @michaelld} openmaintainer
homepage            http://qwt.sourceforge.net

master_sites        sourceforge:qwt
use_bzip2           yes
use_parallel_build  no

platforms           darwin

if {${subport} eq ${name}} {

    version         5.1.2
    distname        qwt-${version}
    revision        3
    conflicts       qwt52 qwt60 qwt61
    checksums       sha1    2fc04b78c438635a78e41a22fea6f9e016f7ca17 \
                    rmd160  a5a94aa3870a631f9605e6370c4d04fb1c79347e
    patchfiles      patch-qwtconfig.pri.51.diff
    livecheck.url   http://sourceforge.net/api/file/index/project-id/13693/mtime/desc/rss
    livecheck.regex \\/qwt\\/(5\\.1\\.\\d+)\\/

}

subport qwt52 {

    version         5.2.3
    revision        1
    distname        qwt-${version}
    conflicts       qwt qwt60 qwt61
    checksums       sha1    ff81595a1641a8b431f98d6091bb134bc94e0003 \
                    rmd160  9b2128ad2b53a65678f9ea57d34cdc73a3674d21
    patchfiles      patch-qwtconfig.pri.52.diff
    livecheck.url   http://sourceforge.net/api/file/index/project-id/13693/mtime/desc/rss
    livecheck.regex \\/qwt\\/(5\\.2\\.\\d+)\\/

}

subport qwt60 {

    version         6.0.2
    revision        1
    distname        qwt-${version}
    conflicts       qwt qwt52 qwt61
    checksums       sha1    cbdd00b29521987c9e7bc6aa51092f0474b9428d \
                    rmd160  c9d31f40b6001f09c59dbf4c0628ddd860474821
    patchfiles      patch-qwtconfig.pri.60.diff \
                    patch-qwtbuild.pri.60.diff \
                    patch-qwtfunctions.pri.60.diff \
                    patch-designer_designer.pro.60.diff
    livecheck.url   http://sourceforge.net/api/file/index/project-id/13693/mtime/desc/rss
    livecheck.regex \\/qwt\\/(6\\.0\\.\\d+)\\/

}

subport qwt61 {

    version         6.1.3
    distname        qwt-${version}
    conflicts       qwt qwt52 qwt60
    checksums       rmd160 1e36e79dab0ecc4df89d9fb20607925fe261c6b3 \
                    sha256 f3ecd34e72a9a2b08422fb6c8e909ca76f4ce5fa77acad7a2883b701f4309733

    use_parallel_build  yes

    if {![variant_isset qt4] && ![variant_isset qt5]} {
        default_variants    +qt4
    }

    patchfiles      patch-designer_designer.pro.61.diff \
                    patch-qwtconfig.pri.61.diff \
                    patch-qwtbuild.pri.61.diff
    livecheck.url   http://sourceforge.net/api/file/index/project-id/13693/mtime/desc/rss
    livecheck.regex \\/qwt\\/(6\\.1\\.\\d+)\\/
    post-patch {
        reinplace "s|@INSTALL_DOCS@@|${qt_docs_dir}/${subport}|" \
                    ${worksrcpath}/qwtconfig.pri
        reinplace "s|@INSTALL_HEADERS@@|${qt_includes_dir}|" \
                    ${worksrcpath}/qwtconfig.pri
    }

}

# get qwt's version numbers

global qwt_major, qwt_minor, qwt_patch
set qwt_vers [split ${version} "."]
set qwt_major [lindex ${qwt_vers} 0]
set qwt_minor [lindex ${qwt_vers} 1]
set qwt_patch [lindex ${qwt_vers} 2]
set qwt_maj_min ${qwt_major}.${qwt_minor}

description         QWT: Qt Widgets for Technical Applications, \
    Version ${qwt_maj_min}

long_description ${description}\nThe Qwt library contains GUI \
    Components and utility classes which are primarily useful for \
    programs with a technical background. Beside a 2D plot widget it \
    provides scales, sliders, dials, compasses, thermometers, wheels \
    and knobs to control or display values, arrays, or ranges of type \
    double. QWT version ${qwt_maj_min} contains source incompatible \
    changes versus other versions of QWT.

post-patch {

    # setup for debug, if selected

    if {[variant_isset debug]} {
        if {${qwt_major} == 5} {
            reinplace "/CONFIG/s@release@debug_and_release build_all@" \
                ${worksrcpath}/qwtconfig.pri
        } else {
            reinplace "/CONFIG/s@release@debug_and_release build_all@" \
                ${worksrcpath}/qwtbuild.pri
        }
    }

    # set arch type(s)

    if {${qwt_major} == 5} {
        reinplace "s/@ARCHES@/${qt_arch_types}/" \
            ${worksrcpath}/qwtconfig.pri
    } else {
        reinplace "s/@ARCHES@/${qt_arch_types}/" \
            ${worksrcpath}/qwtbuild.pri
    }

    # setup for examples, if selected

    if {![variant_isset examples]} {
        # I presume this worked with qwt < 6.1
        reinplace "s@QwtExamples@@" \
            ${worksrcpath}/qwtconfig.pri
    } else {
        # qwt 6.1.2 disables the examples by default, so reenable them when requested
        reinplace "s@#QWT_CONFIG     += QwtExamples@QWT_CONFIG     += QwtExamples@" \
            ${worksrcpath}/qwtconfig.pri
    }
}

if {${qwt_major} < 6} {

    # QWT version 5 can work with Qt3 or Qt4

    variant qt3 conflicts qt4 description {Use qt3-mac} {}

    variant qt4 conflicts qt3 description {Use qt4-mac} {}

    # move setting of 'qt_qmake_cmd' outside variants, since 'if'
    # statements are processed in-order while variants are processed after
    # everything else.

    set qt_qmake_cmd ""

    if {[variant_isset qt3]} {

        depends_lib-append  port:qt3-mac
        set qt_qmake_cmd    ${prefix}/libexec/qt3-mac/bin/qmake

    } else {

        # when variant 'qt3' is not set, use qt4 whether by default or via
        # the user's variant choice

        if {![variant_isset qt4]} {
            default_variants    +qt4
        }
        PortGroup           qmake 1.0
        qt4.debug_variant   no

    }
} else {

    variant qt5 conflicts qt4 description "Build with Qt5" {}
    variant qt4 conflicts qt5 description "Build with Qt4" {}

    # QWT 6+ uses Qt4+ only
    if {![variant_isset qt5]} {
        PortGroup qt4 1.0
        PortGroup qmake 1.0
    } else {
        #PortGroup           qt5 1.0
        PortGroup           qmake5 1.0
        depends_lib-append  port:qt5-qtbase port:qt5-qtsvg
        set qt_arch_types   "x86_64"
        post-patch {
            reinplace "s|QT_STATIC_CONST|static const|" \
                ${worksrcpath}/src/qwt_scale_map.cpp \
                ${worksrcpath}/src/qwt_scale_map.h
            reinplace "s/QT_INSTALL_FRAMEWORKS/QT_INSTALL_LIBS/" \
                ${worksrcpath}/qwtconfig.pri
        }
        configure.args-append \
                -config absolute_library_soname
    }
}

# configure.cmd       ${qt_qmake_cmd}
# configure.pre_args-delete  --prefix=${prefix}
configure.pre_args-append  INSTALLBASE="${prefix}"

# qmake does not handle these flags
configure.universal_args-delete --disable-dependency-tracking
configure.args-delete           --disable-dependency-tracking

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

post-destroot {

    # if qwt is installed as a framework,
    # link in as libraries and headers too

    if {[file exists ${destroot}${qt_frameworks_dir}/qwt.framework/qwt]} {

        global qwt_major, qwt_minor, qwt_patch
        foreach fixfile [exec find ${destroot}${qt_frameworks_dir} \
                             -name "*.framework" | \
                             sed -e "s@${destroot}@@g"] {

            set tf_full [strsed ${fixfile} {s@\\.framework@@}]
            set tf [strsed ${tf_full} {g@.*\/@@}]

            # link headers into ${qt_includes_dir}, removing
            # directories if they are already there first

            file mkdir ${destroot}${qt_includes_dir}
            file mkdir ${destroot}${qt_libs_dir}
            set inc_file ${destroot}${qt_includes_dir}/${tf}
            if {[file exists ${inc_file}]} {
                file delete -force ${inc_file}
            }
            ln -s ${tf_full}.framework/Headers ${inc_file}

            # link libraries into ${qt_libs_dir}

            set libs_dir ${destroot}${qt_libs_dir}

            ln -s ${tf_full}.framework/${tf} \
                ${libs_dir}/lib${tf}.dylib
            ln -s ${tf_full}.framework/${tf} \
                ${libs_dir}/lib${tf}.${qwt_major}.dylib
            ln -s ${tf_full}.framework/${tf} \
                ${libs_dir}/lib${tf}.${qwt_major}.${qwt_minor}.dylib
            ln -s ${tf_full}.framework/${tf} \
                ${libs_dir}/lib${tf}.${qwt_major}.${qwt_minor}.${qwt_patch}.dylib

            if {[variant_isset debug]} {

                # link debug libraries into ${qt_libs_dir}

                ln -s ${tf_full}.framework/${tf}_debug \
                    ${libs_dir}/lib${tf}_debug.dylib
                ln -s ${tf_full}.framework/${tf}_debug \
                    ${libs_dir}/lib${tf}_debug.${qwt_major}.dylib
                ln -s ${tf_full}.framework/${tf}_debug \
                    ${libs_dir}/lib${tf}_debug.${qwt_major}.${qwt_minor}.dylib
                ln -s ${tf_full}.framework/${tf}_debug \
                    ${libs_dir}/lib${tf}_debug.${qwt_major}.${qwt_minor}.${qwt_patch}.dylib

            }
        }
    }
    if {[variant_isset examples]} {
        set exdir ${destroot}${qt_examples_dir}/${subport}
        xinstall -d -m 755 ${exdir}
        foreach bin [glob -nocomplain ${build.dir}/examples/bin/*] {
            copy ${bin} ${exdir}
        }
    }
}

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

variant examples description "Build examples" {}
