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

name                libreoffice
version             26.8.1.1
revision            0
categories          office aqua
license             {LGPL-3 MPL-2}
platforms           macosx
maintainers         {gmail.com:audvare @Tatsh} openmaintainer
description         Free, open source office suite.
long_description    LibreOffice is a free and powerful office suite, and a successor to \
                    OpenOffice.org (commonly known as OpenOffice). Its clean interface and \
                    feature-rich tools help you unleash your creativity and enhance your \
                    productivity.
homepage            https://www.libreoffice.org

set short_version   [string range $version 0 end-2]
set main_uri        https://download.documentfoundation.org/${name}/src/${short_version}/
set addons_uri      https://dev-www.libreoffice.org/src/
set extern_uri      https://dev-www.libreoffice.org/extern/
use_xz              yes
set main_distfile   ${name}-${version}${extract.suffix}

livecheck.type      regex
livecheck.url       https://downloadarchive.documentfoundation.org/${name}/old/latest/src/
livecheck.regex     "${name}-((?:\\d+\\.\\d+\\.\\d+)(?:\\.\\d+))?${extract.suffix}</a></td>"

extract.only        ${main_distfile} \
                    ${name}-dictionaries-${version}.tar.xz \
                    ${name}-translations-${version}.tar.xz
master_sites        ${main_uri}:main \
                    ${main_uri}:dicts \
                    ${main_uri}:translations \
                    ${extern_uri}:opens \
                    ${addons_uri}:addons

# MacPorts does not yet provide libeot, mdds 3.x, orcus 0.21, or zxcvbn-c.
# Keep bundled dependencies in sync with the release's download.lst.
distfiles           ${main_distfile}:main \
                    ${name}-dictionaries-${version}.tar.xz:dicts \
                    ${name}-translations-${version}.tar.xz:translations \
                    f543e6e2d7275557a839a164941c0a86e5f2c3f2a0042bfc434c88c6dde9e140-opens___.ttf:opens \
                    libcmis-0.6.3.tar.xz:addons \
                    libeot-0.01.tar.bz2:addons \
                    mdds-3.2.1.tar.xz:addons \
                    liborcus-0.21.0.tar.xz:addons \
                    skia-m147-ad8ecedbfdef9f4ae4b1e73347b6dd56e6637d38.tar.xz:addons \
                    zxcvbn-c-2.6.tar.gz:addons

patch.pre_args-replace \
                    -p0 -p1
patchfiles-append   patch-system-box2d3.diff \
                    patch-orcus-boost-suffix.diff \
                    patch-system-libxml-macos.diff

checksums           ${name}-${version}.tar.xz \
                    rmd160  63e7fd76aca844bf19531730bd4388f0d006b73e \
                    sha256  296ba78e7ab0d381b94c3d6342f4ebb2320ee5097ca58781b9b744cab90b9785 \
                    size    302435508 \
                    ${name}-dictionaries-${version}.tar.xz \
                    rmd160  e330e89c21d0d4472a14cbf06dcc14307788f598 \
                    sha256  ce366c50178f5bdf6df1c6df4a0146899cf27a0017734190d00d90f0c5acac61 \
                    size    62319860 \
                    ${name}-translations-${version}.tar.xz \
                    rmd160  91c82af7fc04ea7a4fc459bc2a64af1b6a3d1e75 \
                    sha256  789519befa409d7eec24043cea41ddd318d9f1212f555d8b28d4982e35693bfc \
                    size    236160656 \
                    f543e6e2d7275557a839a164941c0a86e5f2c3f2a0042bfc434c88c6dde9e140-opens___.ttf \
                    rmd160  b9e2cc0c836faa59eca3bfc0f24e1b790646b112 \
                    sha256  f543e6e2d7275557a839a164941c0a86e5f2c3f2a0042bfc434c88c6dde9e140 \
                    size    207992 \
                    libcmis-0.6.3.tar.xz \
                    rmd160  19005ddbd52b22fe2cdeed6ef9b5998e387d9553 \
                    sha256  5adb3781f374adf491103b2cb2dc290085150c9b7d097baf56d8ac35df84e792 \
                    size    504964 \
                    libeot-0.01.tar.bz2 \
                    rmd160  7e24a1158f9cb496c0c20f8254e22e4aa4eabbf3 \
                    sha256  cf5091fa8e7dcdbe667335eb90a2cfdd0a3fe8f8c7c8d1ece44d9d055736a06a \
                    size    260288 \
                    mdds-3.2.1.tar.xz \
                    rmd160  a25b37f20b9f275737f6ba5984e23f7fcf8661af \
                    sha256  673f5bb94612dbba581fc92b99b5e5dd1a53e29496a5dbc936432f6b0687c112 \
                    size    648256 \
                    liborcus-0.21.0.tar.xz \
                    rmd160  915cf7369aae4db6f5fba7bfca0ac065e6d72d5d \
                    sha256  1c6e473d6b8a63d61d0e6874a1762d6f0e0e78338d763e48ac93126ecde19b37 \
                    size    2267752 \
                    skia-m147-ad8ecedbfdef9f4ae4b1e73347b6dd56e6637d38.tar.xz \
                    rmd160  149772dc8646d4c91c7303bff1bd898ad1905b3e \
                    sha256  92f613b60126f9068af02c5eb29abef6931e343888f027d87da100ddf2a003a1 \
                    size    12137628 \
                    zxcvbn-c-2.6.tar.gz \
                    rmd160  02fdc62eae32118a0b6c6c64e8a65a1940d1cebd \
                    sha256  11e39f6776f9c82c68b2acb94336e32697d4ab6cdb4ac16f9583ccbdd735113a \
                    size    1405136

boost.version       1.91

depends_build-append \
                    port:autoconf \
                    port:automake \
                    port:bison \
                    port:cppunit \
                    port:dragonbox \
                    port:fast-float \
                    port:flex \
                    port:frozen \
                    port:gettext \
                    port:glm \
                    port:gmake \
                    port:gpatch \
                    port:gperf \
                    port:pkgconfig \
                    port:unixODBC
depends_lib-append  port:argon2 \
                    port:box2d \
                    port:clucene \
                    port:curl \
                    port:expat \
                    port:graphite2 \
                    path:lib/pkgconfig/harfbuzz.pc:harfbuzz \
                    path:lib/pkgconfig/harfbuzz-icu.pc:harfbuzz-icu \
                    port:hunspell \
                    port:hyphen \
                    path:lib/pkgconfig/icu-uc.pc:icu \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    port:lcms2 \
                    port:libabw \
                    port:libcdr-0.1 \
                    port:libe-book \
                    port:libepoxy \
                    port:libepubgen \
                    port:libetonyek \
                    port:libexttextcat \
                    port:libfreehand \
                    port:libiconv \
                    port:liblangtag \
                    port:libmspub \
                    port:libmwaw \
                    port:libnumbertext \
                    port:libodfgen \
                    port:libpagemaker \
                    port:libpng \
                    port:libqxp \
                    port:librevenge \
                    port:libstaroffice \
                    port:libvisio-0.1 \
                    port:libwpd-0.10 \
                    port:libwpg-0.3 \
                    port:libwps \
                    port:libxml2 \
                    port:libxslt \
                    port:libzmf \
                    port:md4c \
                    port:mythes \
                    port:nspr \
                    port:nss \
                    port:raptor2 \
                    port:redland \
                    port:tiff \
                    port:webp \
                    port:xmlsec \
                    port:zlib \
                    port:zstd
# Try to keep this in sync with the Python portgroup.
set pyver 3.14
set python_version [join [split ${pyver} "."] ""]
depends_lib-append  port:python${python_version}
# lxml is used by gla11y while building the user interface.
depends_build-append \
                    port:py${python_version}-lxml

if {${os.platform} eq "darwin" && ${os.major} < 20} {
    known_fail yes
    depends_build
    depends_lib
    pre-fetch {
        ui_error "Building ${name} @${version} requires macOS 11 or later."
        ui_error ""
        ui_error "Consider downloading a version from LibreOffice's website:"
        ui_error "https://www.libreoffice.org/download/download/"
        ui_error ""
        return -code error "incompatible OS X version"
    }
}

set product_name LibreOffice

pre-configure {
    system "cd ${worksrcpath} && env NOCONFIGURE=1 ./autogen.sh"
}

use_xcode               yes
compiler.cxx_standard   2020
# CMS_NO_REGISTER_KEYWORD required for C++17 or newer compiler.
# PYTHON_CFLAGS and PYTHON_LIBS have to be non-empty
configure.cxxflags-append -std=c++20
configure.env-append  \
                    "LCMS2_CFLAGS=-I${prefix}/include -DCMS_NO_REGISTER_KEYWORD=1" \
                    PYTHON=${prefix}/bin/python${pyver} \
                    PYTHON_CFLAGS=-I${frameworks_dir}/Python.framework/Versions/${pyver}/include/python${pyver} \
                    "PYTHON_LIBS=-L${frameworks_dir}/Python.framework/Versions/${pyver}/lib -lpython${pyver} -ldl -framework CoreFoundation"

# Most arguments are from
# https://wiki.documentfoundation.org/LibreOffice_Vanilla_for_Mac#LibreOffice_Vanilla
configure.args-append  \
                    --disable-breakpad \
                    --disable-bundle-mariadb \
                    --disable-ccache \
                    --disable-coinmp \
                    --disable-epm \
                    --disable-fetch-external \
                    --disable-firebird-sdbc \
                    --disable-ldap \
                    --disable-lotuswordpro \
                    --disable-lpsolve \
                    --disable-odk \
                    --disable-online-update \
                    --disable-openssl \
                    --disable-pdfimport \
                    --disable-pdfium \
                    --disable-postgresql-sdbc \
                    --disable-report-builder \
                    --disable-zxing \
                    --enable-bogus-pkg-config \
                    --enable-cups \
                    --enable-extension-integration \
                    --enable-mpl-subset \
                    --enable-python=system \
                    --enable-readonly-installset \
                    --enable-release-build \
                    --enable-symbols \
                    --with-boost=[boost::install_area] \
                    --with-external-tar=${distpath} \
                    --with-gnu-patch=${prefix}/bin/gpatch \
                    --with-myspell-dicts \
                    --with-macosx-version-min-required=${macosx_deployment_target} \
                    --with-package-version=${version} \
                    --with-parallelism=${build.jobs} \
                    --with-product-name=${product_name} \
                    --with-system-clucene \
                    --with-system-dragonbox \
                    --with-system-epoxy \
                    --with-system-fast-float \
                    --with-system-frozen \
                    --with-system-glm \
                    --with-system-headers \
                    --with-system-hunspell \
                    --with-system-jars \
                    --with-system-libabw \
                    --with-system-libebook \
                    --with-system-libepubgen \
                    --with-system-libexttextcat \
                    --with-system-libmwaw \
                    --with-system-libnumbertext \
                    --with-system-libqxp \
                    --with-system-libs \
                    --with-system-libstaroffice \
                    --with-system-libzmf \
                    --with-system-mythes \
                    --with-system-nss \
                    --with-system-xmlsec \
                    --with-theme=colibre \
                    --with-tls=nss \
                    --with-vendor=MacPorts \
                    --without-doxygen \
                    --without-fonts \
                    --without-java \
                    --without-package-format \
                    --without-system-dicts \
                    --without-system-libcmis \
                    --without-system-libeot \
                    --without-system-libfixmath \
                    --without-system-mdds \
                    --without-system-orcus \
                    --without-system-sane \
                    --without-system-zxcvbn \
                    --without-webdav \
                    \"--with-lang=bg br ca ca-valencia cy cs da de el en-US en-GB es et eu fi fr ga gd gl hr hu id is it ja km lt lv nb nl nn pl pt pt-BR ro ru sk sl sv ta tr uk zh-CN zh-TW\"
configure.env-append \
                    GNUMAKE=${prefix}/bin/gmake

variant pdfimport description {Enable editable PDF import using system Poppler} {
    license-append  GPL-2+
    depends_lib-append \
                    port:freetype \
                    port:poppler
    configure.args-delete --disable-pdfimport
    configure.args-append \
                    --enable-pdfimport \
                    --enable-poppler \
                    --with-system-freetype \
                    --with-system-poppler
}

build.target        build
build.cmd           ${prefix}/bin/gmake

# Info-ZIP from MacPorts can collide when creating temporary archives in
# parallel. Use Apple's zip for the build's resource packaging.
pre-build {
    file mkdir ${workpath}/build-tools
    ln -sf /usr/bin/zip ${workpath}/build-tools/zip
}
# config_host.mk exports configure's PATH, so override it as a make variable.
build.args-append PATH=${workpath}/build-tools:$env(PATH)

destroot {
    copy ${worksrcpath}/instdir/${product_name}.app ${destroot}${applications_dir}

    set app_bundle ${destroot}${applications_dir}/${product_name}.app
    # Manually create the keep-directory marker before sealing the bundle.
    #xinstall -d ${app_bundle}/Contents/Resources/en.lproj
    #xinstall -m 0644 /dev/null ${app_bundle}/Contents/Resources/en.lproj/.turd_${subport}
    close [open ${app_bundle}/Contents/Resources/en.lproj/.turd_${subport} w]
    system "/usr/bin/codesign --force --deep --sign - \"${app_bundle}\""

    file mkdir ${destroot}${prefix}/bin
    foreach command {soffice unopkg} {
        set wrapper ${destroot}${prefix}/bin/${command}
        set wrapper_file [open ${wrapper} w]
        puts ${wrapper_file} [format {#!/bin/sh
exec "%s/%s.app/Contents/MacOS/%s" "$@"} ${applications_dir} ${product_name} ${command}]
        close ${wrapper_file}
        file attributes ${wrapper} -permissions 0755
    }
}
