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

PortSystem                  1.0
PortGroup                   github 1.0
PortGroup                   active_variants 1.1

if {${os.platform} eq "darwin" && ${os.major} < 11} {
    PortGroup               qmake 1.0

    github.setup            CsoundQt CsoundQt 0.9.7
    revision                0
    checksums               rmd160  de2e910e66ae1f9290091b11168a5a6515c183f8 \
                            sha256  c12242d4e1f745ac2c1ae23d1ef72661d68cb51baf110c17140deb1c994cfa1f \
                            size    66170886
    github.tarball_from     archive

    patchfiles-append       patch-qcs-macx.diff \
                            patch-fix-includes.diff \
                            patch-qt4-compat.diff
} else {
    PortGroup               qmake5 1.0

    github.setup            CsoundQt CsoundQt 1.1.1 v
    revision                0
    checksums               rmd160  c274b03cccda9092311a477d1f12058391c0cbbe \
                            sha256  652fa51dc19acae031919216ec8043cb3767763008a62a134854940e2d065326 \
                            size    67621515
    github.tarball_from     archive

    qt5.depends_component   qtxmlpatterns

    patchfiles-append       patch-qcs-macx-qt5.diff
}

categories                  aqua audio
license                     LGPL-2.1+
maintainers                 {@barracuda156 gmail.com:vital.had} openmaintainer

description                 ${name} is a frontend for Csound
long_description            {*}${description} featuring a highlighting editor \
                            with autocomplete, interactive widgets and integrated help. \
                            It is cross-platform and aims to be a simple yet powerful \
                            and complete development environment for Csound.

homepage                    https://csoundqt.github.io

depends_lib-append          port:csound \
                            port:libsndfile

compiler.cxx_standard       2011

post-patch {
    reinplace "s,@PREFIX@,${prefix},g" ${worksrcpath}/qcs-macx.pro ${worksrcpath}/qcs.pro ${worksrcpath}/config.pri
    reinplace "s,@CC@,${configure.cc},g" ${worksrcpath}/qcs.pro
    reinplace "s,@CXX@,${configure.cxx},g" ${worksrcpath}/qcs.pro
    reinplace "s,@DESTDIR@,${worksrcpath}," ${worksrcpath}/qcs.pro
    reinplace "s,@CFLAGS@,${configure.cppflags} ${configure.cflags} [get_canonical_archflags cc]," ${worksrcpath}/qcs.pro
    reinplace "s,@CXXFLAGS@,${configure.cppflags} ${configure.cxxflags} [get_canonical_archflags cxx]," ${worksrcpath}/qcs.pro
    reinplace "s,@LFLAGS@,[get_canonical_archflags ld]," ${worksrcpath}/qcs.pro
}

if {${configure.build_arch} ni [list arm i386 ppc]} {
    default_variants-append +double
}

if [variant_isset double] {
    set cs_conf             CONFIG+=build64
    set libname             CsoundLib64
    set appname             ${name}-d-cs6
} else {
    set cs_conf             CONFIG+=build32
    set libname             CsoundLib
    set appname             ${name}-f-cs6
}

configure {
    system -W ${worksrcpath} "${qt_qmake_cmd} qcs.pro ${cs_conf}"
}

build.cmd                   make
build.target

set cs_framework_path       ${libname}.framework/Versions/6.0
set csoundlib               ${cs_framework_path}/${libname}

destroot {
    system -W ${worksrcpath} "install_name_tool -change ${prefix}/lib/${csoundlib} ${frameworks_dir}/${csoundlib} ./${appname}.app/Contents/MacOS/${appname}"
    move ${worksrcpath}/${appname}.app ${destroot}${applications_dir}/${name}.app
}

variant double description "Use double precision" {
    require_active_variants port:csound double
}

if {![catch {set result [active_variants csound double]}]} {
    if {$result} {
        if {![variant_isset double]} {
            return -code error "csound has been built with double precision, please install ${name} with +double."
        }
    }
}
