# -*- 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           active_variants 1.1
PortGroup           perl5 1.0

name                lilypond

categories          textproc
maintainers         {snc @nerdling} openmaintainer

license             GPL-3+
# LilyPond does not link with Ghostscript.  However, at least one of
# Ghostscript's dependencies (libpaper) links with legacysupport, which
# is APSL-2 licensed, and which would cause an apparent conflict.
license_noconflict  ghostscript

description         An automated engraving system for typesetting sheet music.
long_description    Lilypond is a unix-based automated engraving system that \
                    generates beautiful sheet music from input files. Lilypond \
                    uses its own input format, .ly, which in many ways is \
                    similar to LaTeX. Lilypond can export sheet music to PDF, \
                    EPS, SVG, and PNG formats, and can also create MIDI files.

perl5.major         5.34

subport lilypond-devel {
    long_description  The development version of the LilyPond music typesetter.
}


variant mactex description {Use MacTeX or non-MacPorts TeX; see\
    "port notes" for more information} {}
variant docs description {Build documentation files} {}


notes-append "\
    Pre-installation note for 'mactex' variant:

    MacTeX or another external TeXLive distribution gets used for\
    installation instead of MacPorts's texlive packages; the path to\
    the TeX distribution's binary directory (for example\
    '/Library/TeX/texbin') must be added to 'binpath' in\
    'macports.conf' *before* installing this port.

    Note that TeX is not needed after installation."


compiler.cxx_standard 2014

if {${subport} eq ${name}} {
    version         2.24.4
    revision        1
    conflicts       lilypond-devel
    checksums       rmd160  5285ff3a441bfd2d936e73a22a8d75a9622c2632 \
                    sha256  e96fa03571c79f20e1979653afabdbe4ee42765a3d9fd14953f0cd9eea51781c \
                    size    19442316

    set livecheck_url "source.html"
} else {
    version         2.25.13
    revision        1
    conflicts       lilypond
    checksums       rmd160  ffb30994d5000ce591f93050bf51b6fbdac65128 \
                    sha256  a23a16de1abe0ef80696f59244d93d542a284738b9b29a7ca3ca0aa9bef8b667 \
                    size    19274596

    set livecheck_url "development.html"
}

set branch          [join [lrange [split ${version} .] 0 1] .]
homepage            https://lilypond.org
master_sites        https://lilypond.org/download/sources/v${branch}/

livecheck.type      regex
livecheck.regex     ${name}-(\\d+(\\.\\d+)+)
livecheck.url       ${homepage}/${livecheck_url}

platforms           darwin
universal_variant   no

depends_build-append \
                    port:autoconf \
                    port:bison \
                    port:flex \
                    port:fontforge \
                    port:gmake \
                    port:p${perl5.major}-encode \
                    port:p${perl5.major}-pod-escapes \
                    port:p${perl5.major}-pod-simple \
                    port:p${perl5.major}-podlators \
                    port:p${perl5.major}-scalar-list-utils \
                    port:pkgconfig \
                    port:t1utils \
                    port:texinfo \
                    port:urw-core35-fonts

if {![variant_isset mactex]} {
    depends_build-append \
                    port:texlive-fonts-recommended \
                    port:texlive-metapost \
}
if {[variant_isset docs] && ![variant_isset mactex]} {
    # For +docs, only bibtex and xetex are needed right now.
    depends_build-append \
                    port:texlive-xetex
}

set py_ver          3.11
set py_ver_nodot    [string map {. {}} ${py_ver}]

# Either 'pango' or 'pango-devel' will do, thus we depend on its
# pkgconfig file.
depends_lib-append  path:lib/pkgconfig/pango.pc:pango \
                    port:extractpdfmark \
                    port:ghostscript \
                    port:python${py_ver_nodot} \
                    port:guile-2.2

build.type          gnu
configure.cmd       autoconf -f && ./configure
configure.python    ${prefix}/bin/python${py_ver}
configure.perl      ${perl5.bin}

# These targets also build and install compiled Scheme files (with
# extension '.go').  This is essential to make LilyPond run with
# an acceptable speed while using Guile 2.2.
build.target        bytecode
destroot.target     install-bytecode

set lilypond.texgyredir \
    "${prefix}/share/texmf-texlive/fonts/opentype/public/tex-gyre"
set lilypond.mactex_bin   ""
set lilypond.temp         ""
set lilypond.have_texgyre false

if {[variant_isset mactex]} {
    # Find the binary directory of the external TeX distribution by
    # searching the kpsewhich program in the path.  We assume that all
    # other binaries of the distribution are in this directory, too.
    if {[auto_execok kpsewhich] ne ""} {
        set lilypond.temp {*}[auto_execok kpsewhich]
    }
    set lilypond.mactex_bin \
        [file dirname [file normalize ${lilypond.temp}]]
    if {${lilypond.mactex_bin} eq "."} {
        pre-fetch {
            return -code error "Cannot find MacTeX or external TeXLive\
                installation; aborting.  Have you added the path to\
                your TeX distribution's binary directory to 'binpath'\
                in 'macports.conf'?"
        }
    } elseif {${lilypond.mactex_bin} eq \
                  [file dirname [file normalize "${prefix}/bin/kpsewhich"]]} {
        pre-fetch {
            return -code error "Variant 'mactex' doesn't work with\
                installed MacPorts TeXLive packages.  Either\
                uninstall them or don't use the 'mactex' variant of\
                ${subport}."
        }
    } else {
        set lilypond.have_texgyre \
            [regexp -line {installed: *Yes} \
                 [exec ${lilypond.mactex_bin}/tlmgr info --only-installed tex-gyre]]
    }

    if {${lilypond.have_texgyre}} {
        set lilypond.texgyredir \
            [file dirname \
                [exec ${lilypond.mactex_bin}/kpsewhich texgyreschola-regular.otf]]
    } else {
        pre-fetch {
            return -code error "TeXLive package 'tex-gyre' not\
                installed; aborting.  Please install it, then\
                try again"
        }
    }
}

if {[variant_isset docs]} {
    # Currently, +docs only builds the info files.  For a complete
    # documentation build a texi2html 1.82 bundle is necessary,
    # which doesn't exist yet in MacPorts.
    configure.args-append   --disable-texi2html
} else {
    configure.args-append   --disable-documentation
}
if {${subport} eq ${name}} {
    configure.args-append   --with-urwotf-dir=${prefix}/share/fonts/urw-core35-fonts
    configure.args-append   --with-texgyre-dir=${lilypond.texgyredir}
} else {
    # This is a temporary fix until MacPorts provides guile 3, see
    # https://github.com/macports/macports-ports/pull/14600.
    configure.env-append    GUILE_FLAVOR=guile-2.2

    # Regarding font setup see https://trac.macports.org/ticket/67026,
    # which is still unresolved.
}

configure.env-append    PYTHON=${configure.python}
build.env-append        PYTHON=${configure.python}
destroot.env-append     PYTHON=${configure.python}

configure.env-append    LTDL_LIBRARY_PATH=${prefix}/lib
build.env-append        LTDL_LIBRARY_PATH=${prefix}/lib
destroot.env-append     LTDL_LIBRARY_PATH=${prefix}/lib

configure.args-append   --with-flexlexer-dir=${worksrcpath}/flex

pre-patch {
    file mkdir ${worksrcpath}/flex
    copy ${prefix}/include/FlexLexer.h ${worksrcpath}/flex/FlexLexer.h
}

patchfiles              patch-fix-missing-override.diff \
                        patch-add-flex-to-include.diff

post-patch {
    # Correct mf2pt1 binary location.
    reinplace -W ${worksrcpath} \
        s|"-mem=mf2pt1"|"-mem=../../mf2pt1"|g \
        scripts/build/mf2pt1.pl

    # Correct python shebang.
    reinplace -W ${worksrcpath} -E \
        s|/usr/bin/(env\\\ )?python3?|${configure.python}|g \
        scripts/auxiliar/check_texi_refs.py \
        scripts/auxiliar/check_translation.py \
        scripts/auxiliar/texi-langutils.py \
        scripts/auxiliar/update-snippets.py

    # ImageMagick and xelatex (or pdflatex) are only needed for
    # complete documentation builds, which +docs doesn't do currently.
    # Ditto for Cyrillic stuff (i.e., testing for 'fikparm.mf').
    if {[variant_isset docs]} {
        reinplace -W ${worksrcpath} \
            {s|\(IMAGEMAGICK.*\), $DOCUMENTATION_REQUIRED|\1, OPTIONAL|g} \
            configure.ac
        reinplace -W ${worksrcpath} \
            {s|\(PDFLATEX.*\), $DOCUMENTATION_REQUIRED|\1, OPTIONAL|g} \
            configure.ac
        reinplace -W ${worksrcpath} \
            {s|if test "$TEX_FIKPARM" = ""|if false|g} \
            configure.ac
    }
}

post-destroot {
    move \
        ${destroot}${prefix}/bin/lilypond \
        ${destroot}${prefix}/libexec/lilypond-bin
    xinstall -m 755 \
        ${filespath}/lilypond.in \
        ${destroot}${prefix}/bin/lilypond
    reinplace \
        "s|@@PREFIX@@|${prefix}|g" \
        ${destroot}${prefix}/bin/lilypond
}
