# -*- 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

PortSystem          1.0
PortGroup           python 1.0
PortGroup           github 1.0

github.setup        wbsoft frescobaldi 3.3.0 v
conflicts           ${name}-devel ${name}2
categories          editors python
maintainers         {gmail.com:davide.liessi @dliessi} openmaintainer
description         A LilyPond sheet music text editor
long_description    Frescobaldi is an advanced text editor \
                    to edit LilyPond sheet music files. \
                    Features include an integrated PDF preview \
                    and a powerful Score Wizard.
homepage            http://www.frescobaldi.org/
platforms           darwin
license             GPL-2+

github.tarball_from releases

checksums           rmd160  8d66f81b24fd7cdf352bd9d5e5ce6e210716aca6 \
                    sha256  9fb90934fbc08f6c7df0a4d999e89a9a82fe9b4a0f21c0d40a1474e3ac7bc0d8 \
                    size    6580339

subport ${name}-devel {
    set devel_version   ${github.version}
    # When frescobaldi-devel is different from frescobaldi and points
    # to a commit hash, append '-dev' to devel_version above, then use
    # github.setup with the commit hash (before setting version below).
    conflicts           ${name} ${name}2
    version             20230327

    # When using a commit hash, uncomment the following line.
    # github.tarball_from archive
}

subport ${name}2 {
    github.setup        wbsoft frescobaldi 2.20.0 v
    revision            1
    conflicts           ${name} ${name}-devel

    checksums           rmd160  27f121545cc7efe6a2b10fa69490a5714655f729 \
                        sha256  c3d7faab7b21ddbab98749751a94d0a848138a800b0ae744f0ec94000b459ae4 \
                        size    4478977
}

depends_run-append  port:portmidi

if {"${name}2" eq ${subport}} {
    python.default_version  37
    python.pep517           no
} else {
    python.default_version  310
    python.pep517           no
}

depends_build-append    port:py${python.version}-setuptools
depends_lib-append      port:py${python.version}-ly

if {"${name}-devel" eq ${subport} && ${devel_version} ne ${github.version}} {
    depends_build-append    port:gettext \
                            port:appstream-glib
}

if {"${name}2" eq ${subport}} {
    depends_build-append    port:py${python.version}-importlib-metadata
    depends_run-append      port:py${python.version}-pyqt4 \
                            port:py${python.version}-poppler-qt4
} else {
    depends_run-append      port:py${python.version}-pyqt5 \
                            port:py${python.version}-poppler-qt5 \
                            port:py${python.version}-qpageview
    if {${os.platform} eq "darwin" && ${os.major} < 13} {
        # QtWebEngine appeared after Qt 5.3, which is the latest version
        # available on Mac OS X 10.6 (Darwin 10);
        # none of the supported versions (5.5, 5.6) builds on Mac OS X 10.7
        # (Darwin 11);
        # version 5.5 may build on OS X 10.8 (Darwin 12), where the
        # default version is 5.7, but I am not sure and I don't want to
        # take care of this case in the Portfile: I will give instructions
        # on manually installing Qt 5.5 instead of 5.7 to individual users
        # if needed.
        long_description    ${long_description} \
                            \nSome of Frescobaldi's tools \
                            will not work on this system \
                            (see port notes for details).
        notes-append        "Some of Frescobaldi's tools, namely the\
                            Documentation Browser and the SVG View,\
                            require QtWebEngine,\
                            which cannot be installed on your system.\
                            You may use Frescobaldi 3 without those \
                            tools or install Frescobaldi 2\
                            (i.e., the port ${name}2) instead."
        notes-append        {}
    } else {
        depends_run-append  port:py${python.version}-pyqt5-webengine
    }
}

variant app description {Make application bundle} {
    depends_build-append    port:py${python.version}-py2app
}
default_variants    +app

if {![variant_isset app]} {
    supported_archs     noarch
}

if {"${name}-devel" eq ${subport} && ${devel_version} ne ${github.version}} {
    pre-build {
        system -W ${worksrcpath} "make PYTHON=${python.bin} -C i18n"
        system -W ${worksrcpath} "make -C linux"
        reinplace "s:version = \"\[^\"\]*\":version = \"${devel_version}\":g" \
            ${worksrcpath}/frescobaldi_app/appinfo.py
    }
}

build {
    if {[variant_isset app]} {
        if {[info exists devel_version]} {
            set bundle_version    ${devel_version}-${version}
        } else {
            set bundle_version    ${version}
        }

        system -W ${worksrcpath} "${prefix}/bin/python${python.branch} \
            macosx/mac-app.py -f -v ${bundle_version} -s ${prefix}/bin/frescobaldi"
    }
}

post-destroot {
    set share_destroot ${destroot}${prefix}/share
    set share_python ${python.prefix}/share

    set man_dir /man/man1
    xinstall -d ${share_destroot}${man_dir}
    ln -s ${share_python}${man_dir}/frescobaldi.1 ${share_destroot}${man_dir}

    set desktop_dir /applications
    xinstall -d ${share_destroot}${desktop_dir}
    ln -s ${share_python}${desktop_dir}/frescobaldi.desktop ${share_destroot}${desktop_dir}

    set icons_dir /icons/hicolor/scalable/apps
    xinstall -d ${share_destroot}${icons_dir}
    ln -s ${share_python}${icons_dir}/frescobaldi.svg ${share_destroot}${icons_dir}

    set doc_dir ${share_destroot}/doc/${subport}
    xinstall -d ${doc_dir}
    xinstall -m 644 -W ${worksrcpath} \
        COPYING \
        README.md \
        THANKS \
        ${doc_dir}
    if {"${name}2" eq ${subport}} {
        xinstall -m 644 -W ${worksrcpath} ChangeLog ${doc_dir}
    } else {
        xinstall -m 644 -W ${worksrcpath} CHANGELOG.md ${doc_dir}
    }

    if {[variant_isset app]} {
        file copy ${worksrcpath}/dist/Frescobaldi.app ${destroot}${applications_dir}
    }
}

if {"${name}2" eq ${subport}} {
    github.livecheck.regex {(2[^"]+)}
}

notes-append \
"You may want to install FluidSynth and obtain a SoundFont\
in order to have audio output from Frescobaldi's MIDI output.\
You will need to select FluidSynth's MIDI input port\
in Frescobaldi's MIDI settings (in Preferences)\
while FluidSynth is running."
