# -*- 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           compiler_blacklist_versions 1.0
PortGroup           boost 1.0

name                LyX
conflicts           LyX1

# 2.4.x has expermintal support for Qt6 - we are ignoring ATM
if {${os.platform} eq "darwin" && ${os.major} < 11} {
    PortGroup qt4 1.0

    version         2.3.8
    revision        1

    checksums       rmd160  80b2c53d12b04dd64a6326b4b1ea505dcb352120 \
                    sha256  7e8f56d148cb459eb00abbe9c6371744e4ff077b842c0024153a3f094b10d4ad \
                    size    16412700

    set branch      [join [lrange [split ${version} .] 0 1] .]

    configure.args-append \
                    --with-version-suffix=-${branch} \
                    --with-qt-dir=${prefix}/libexec/qt4 \
                    --disable-qt5

    # QRawFont is supposed to work, but does not:
    # GuiFontMetrics.cpp:127:9: error: 'QRawFont' was not declared in this scope; did you mean 'QFont'?
    # Anyway, it seems to have issues in Qt4:
    # https://stackoverflow.com/questions/12597595/qrawfontfromfont-not-working-correctly
    patchfiles-append   patch-GuiFontMetrics.cpp.diff
} elseif {${os.platform} eq "darwin" && ${os.major} < 17} {
    PortGroup qt5 1.0

    version         2.3.8
    revision        1

    checksums       rmd160  80b2c53d12b04dd64a6326b4b1ea505dcb352120 \
                    sha256  7e8f56d148cb459eb00abbe9c6371744e4ff077b842c0024153a3f094b10d4ad \
                    size    16412700

    qt5.depends_component   qtmacextras \
                            qtsvg

    set branch      [join [lrange [split ${version} .] 0 1] .]

    configure.args-append \
                    --with-version-suffix=-${branch} \
                    --with-qt-dir=${prefix}/libexec/qt5 \
                    --with-qt-includes=${prefix}/libexec/qt5/include \
                    --with-qt-libraries=${prefix}/libexec/qt5/lib
} else {
    PortGroup qt5 1.0

    version         2.4.2
    revision        1

    checksums       rmd160  686630c5fe6d98bb29625e0818d9ad027bdb3814 \
                    sha256  0d5a75dec9a169ab23bfa47da53ec65c62432de57014de315023d620ebccfe68 \
                    size    17871168

    qt5.depends_component   qtmacextras \
                            qtsvg

    set branch      [join [lrange [split ${version} .] 0 1] .]

    configure.args-append \
                    --with-version-suffix=-${branch} \
                    --with-macos-deployment-target=10.13 \
                    --with-qt-dir=${prefix}/libexec/qt5 \
                    --with-qt-includes=${prefix}/libexec/qt5/include \
                    --with-qt-libraries=${prefix}/libexec/qt5/lib
}

categories          aqua
license             GPL-2+
maintainers         {khindenburg @kurthindenburg} openmaintainer
description         WYSIWYM document processor
homepage            https://www.lyx.org
long_description    LyX is an advanced open source document processor \
                    that encourages an approach to writing based on \
                    the structure of your documents, not their \
                    appearance. LyX lets you concentrate on writing, \
                    leaving details of visual layout to the software.

depends_build-append    port:bison port:gawk port:cctools
depends_lib-append      port:enchant \
                        bin:latex:texlive \
                        port:ImageMagick \
                        port:hunspell \
                        port:libmagic \
                        port:aspell

use_xz              yes
distname            lyx-${version}
worksrcdir          lyx-${version}

master_sites        http://lyx.cybermirror.org/stable/${branch}.x/ \
                    http://sunsite.icm.edu.pl/pub/unix/editors/lyx/stable/${branch}.x/ \
                    ftp://ftp.ntua.gr/pub/X11/LyX/stable/${branch}.x/ \
                    ftp://ftp.lyx.org/pub/lyx/stable/${branch}.x/

compiler.cxx_standard 2014
compiler.blacklist-append { clang < 800 }
patchfiles-append   patch-lyx-applescriptproxy-add-stringh.diff
patchfiles-append   patch-lyx-linkbackserver-older-nsalert.diff

# error: '-fobjc-exceptions' is required to enable Objective-C exception syntax
if {[string match *gcc* ${configure.compiler}]} {
    # This must be set in pre-configure:
    pre-configure {
        configure.objcflags-append \
                    -fobjc-exceptions
    }
}

post-configure {
    reinplace \
        "s|\$(MKDIR_P) \$(pkgdatadir)|\
           \$(MKDIR_P) \$(DESTDIR)\$(pkgdatadir)|" \
        ${worksrcpath}/development/MacOSX/Makefile
}

configure.pre_args  --prefix=${applications_dir}/LyX.app
configure.args-append \
                    --without-x \
                    --without-included-boost \
                    --enable-optimization=${configure.optflags} \
                    --disable-silent-rules

set py_versions [list 3.9 3.10 3.11 3.12 3.13]

foreach py_ver ${py_versions} {
    set py_ver_nodot($py_ver) [string map {. ""} ${py_ver}]
    lappend py_variants python$py_ver_nodot($py_ver)
}

foreach py_ver ${py_versions} {
    set py_variant python$py_ver_nodot($py_ver)
    variant $py_variant conflicts {*}[ldelete $py_variants $py_variant] description "Use Python $py_ver" "
        depends_lib-append port:${py_variant}
        global python_framework
        set python_framework    ${frameworks_dir}/Python.framework/Versions/${py_ver}
        configure.python        ${prefix}/bin/python${py_ver}
    "
    if {[variant_isset $py_variant]} {
        set py_variant_set 1
    }
}

# Make the default match boost's stub boostXXX's default.
if {![info exists py_variant_set]} {
    default_variants +python311
}

variant debug description {Build with debugging information} {
    configure.args-append   --enable-debug
}

post-destroot {
    file delete  -force ${destroot}${applications_dir}/LyX.app/Contents/Library/Spotlight
}

notes "
You may want to install texlive-latex-extra if you get errors
about missing tex files.  It is a very large package.
It is advisible to not delete the previous version so that it can be
re-activated if there are issues with the new version.
https://guide.macports.org/#using.port.upgrade
"

livecheck.type      regex
livecheck.url       ${homepage}
livecheck.regex     "${name} (\\d+\\.\\d+\\.\\d+(\\.\\d+)?) released"
