# Maintainer: AndyRTR <andyrtr@archlinux.org>
# Contributor: Bartłomiej Piotrowski <bpiotrowski@archlinux.org>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - disable firebird until libfbclient is updated
#  - specify a US mirror
#  - remove --enable-lto
#  - add -D_FILE_OFFSET_BITS=64 to CPPFLAGS for v7

highmem=1

# Google API keys (see http://www.chromium.org/developers/how-tos/api-keys)
# Note: These are for Arch Linux use ONLY. For your own distribution, please
# get your own set of keys. Feel free to contact foutrelis@archlinux.org for
# more information.
_google_default_client_id=413772536636.apps.googleusercontent.com
_google_default_client_secret=0ZChLK6AxeA3Isu96MkwqDR4

pkgbase=libreoffice-fresh
pkgname=('libreoffice-fresh-sdk' 'libreoffice-fresh')
_LOver=24.8.2.1
pkgver=24.8.2
pkgrel=2
arch=('x86_64')
license=('MPL-2.0' 'LGPL-3.0-or-later')
url="https://www.libreoffice.org/"
makedepends=('curl>=7.20.0' 'hunspell>=1.2.8' 'python' 'libwpd>=0.9.2' 'libwps'
    'neon>=0.28.6' 'pango' 'nspr' 'libjpeg-turbo' 'libxrandr' 'libgl' 'dbus-glib'
    'redland' 'hyphen' 'lpsolve' 'gcc-libs' 'sh' 'graphite' 'icu' 'libxslt'
    'lcms2' 'poppler' 'libvisio' 'libetonyek' 'libodfgen' 'libcdr'
    'libmspub' 'harfbuzz-icu' 'nss' 'hicolor-icon-theme'
    'gst-plugins-base-libs' 'sane' 'perl-archive-zip' 'zip' 'unzip' 'unixodbc' 'ant'
    'gperf' 'gtk3' 'gtk4' 'cppunit' 'beanshell' 'clucene'
    'qt5-base' 'qt5-x11extras'
    'qt6-base' 'qt6-multimedia' 'kio' 'kconfig' 'kcoreaddons' 'kwindowsystem' 'ki18n' 'kcoreaddons'
    'libmythes' 'libwpg'  'java-environment=17' 'postgresql-libs' 'mariadb-libs' 'libgl'
    'bluez-libs' 'gdb' 'doxygen'  'libatomic_ops'  'mdds'
    'ttf-liberation' 'ttf-dejavu' 'ttf-carlito' 'libxinerama' 'libpagemaker' 'glm'
    'libabw' 'libmwaw' 'libe-book' 'coin-or-mp' 'liblangtag' 'liborcus'
    'libexttextcat' 'gobject-introspection' 'libcmis' # 'libfbclient'
    'libtommath' 'libzmf' 'xmlsec' 'rxvt-unicode' 'gpgme' 'libwebp'
    'libepubgen' 'libfreehand' 'libqxp' 'libstaroffice'  'boost' 'libnumbertext'
    'abseil-cpp' 'clang' 'zxing-cpp'
    'libffi' 'git' 'python-setuptools' 'rhino'
)
options=('!lto') # using --enable-lto (for parallel link-time optimization)
_mirror="https://ftp.osuosl.org/pub/tdf/libreoffice/src/${pkgver}"
#_mirror="https://download.documentfoundation.org/libreoffice/src/${pkgver}"
#_mirror="https://dev-builds.libreoffice.org/pre-releases/src"
_additional_source_url="https://dev-www.libreoffice.org/src"
_additional_source_url2="https://dev-www.libreoffice.org/extern"
source=(${_mirror}/libreoffice{,-help,-translations}-${_LOver}.tar.xz{,.asc}
    ${_additional_source_url}/a7983f859eafb2677d7ff386a023bc40-xsltml_2.1.2.zip
    ${_additional_source_url}/0168229624cfac409e766913506961a8-ucpp-1.3.2.tar.gz
    ${_additional_source_url}/language-subtag-registry-2021-03-05.tar.bz2
    ${_additional_source_url}/17410483b5b5f267aa18b7e00b65e6e0-hsqldb_1_8_0.zip
    ${_additional_source_url}/d8bd5eed178db6e2b18eeed243f85aa8-flute-1.1.6.zip
    ${_additional_source_url}/ba2930200c9f019c2d93a8c88c651a0f-flow-engine-0.9.4.zip
    ${_additional_source_url}/pdfium-6425.tar.bz2
    ${_additional_source_url}/dtoa-20180411.tgz
    ${_additional_source_url}/lxml-4.1.1.tgz
    ${_additional_source_url}/Firebird-3.0.7.33374-0.tar.bz2
    ${_additional_source_url}/skia-m116-2ddcf183eb260f63698aa74d1bb380f247ad7ccd.tar.xz
    ${_additional_source_url}/box2d-2.4.1.tar.gz
    ${_additional_source_url}/dragonbox-1.1.3.tar.gz
    ${_additional_source_url}/frozen-1.1.1.tar.gz
    ${_additional_source_url}/zxcvbn-c-2.5.tar.gz
    ${_additional_source_url}/Java-WebSocket-1.5.6.tar.gz
    ${_additional_source_url2}/8249374c274932a21846fa7629c2aa9b-officeotron-0.7.4-master.jar  # for test suite
    ${_additional_source_url2}/odfvalidator-1.2.0-incubating-SNAPSHOT-jar-with-dependencies-971c54fd38a968f5860014b44301872706f9e540.jar # for test suite
    ${_additional_source_url2}/f543e6e2d7275557a839a164941c0a86e5f2c3f2a0042bfc434c88c6dde9e140-opens___.ttf
    soffice-template.desktop.in
    libreoffice-fresh.sh libreoffice-fresh.csh)
noextract=(a7983f859eafb2677d7ff386a023bc40-xsltml_2.1.2.zip
           0168229624cfac409e766913506961a8-ucpp-1.3.2.tar.gz
           language-subtag-registry-2021-03-05.tar.bz2
           17410483b5b5f267aa18b7e00b65e6e0-hsqldb_1_8_0.zip
           d8bd5eed178db6e2b18eeed243f85aa8-flute-1.1.6.zip
           ba2930200c9f019c2d93a8c88c651a0f-flow-engine-0.9.4.zip
           pdfium-6425.tar.bz2
           dtoa-20180411.tgz
           lxml-4.1.1.tgz
           Firebird-3.0.7.33374-0.tar.bz2
           skia-m116-2ddcf183eb260f63698aa74d1bb380f247ad7ccd.tar.xz
           box2d-2.4.1.tar.gz
           dragonbox-1.1.3.tar.gz
           frozen-1.1.1.tar.gz
           zxcvbn-c-2.5.tar.gz
           Java-WebSocket-1.5.6.tar.gz
           8249374c274932a21846fa7629c2aa9b-officeotron-0.7.4-master.jar  # for test suite
           odfvalidator-1.2.0-incubating-SNAPSHOT-jar-with-dependencies-971c54fd38a968f5860014b44301872706f9e540.jar  # for test suite
           f543e6e2d7275557a839a164941c0a86e5f2c3f2a0042bfc434c88c6dde9e140-opens___.ttf
)
validpgpkeys=('C2839ECAD9408FBE9531C3E9F434A1EFAFEEAEA3') # LibreOffice Build Team (CODE SIGNING KEY) <build@documentfoundation.org>
sha256sums=('ab301baeb64114a9a1bce5dcb914ee0bf3e2ac7b8d74d32f3233f5f312bcc4cf'
            'SKIP'
            '4ff77d50dfa77c45dff7ca8c5033028ff24530b7e9ce333aee2a418046d08ef6'
            'SKIP'
            '72382c0fb00d83511be9197bf1e4e0947899084a287a97960942e49624676efb'
            'SKIP'
            '75823776fb51a9c526af904f1503a7afaaab900fba83eda64f8a41073724c870'
            '983941d31ee8d366085cadf28db75eb1f5cb03ba1e5853b98f12f7f51c63b776'
            'ce80e8face06bf2ada363e0c159e3f990c4116fdae9232ca43e6369aa82bf16a'
            'd30b13f4ba2e3b6a2d4f020c0dee0a9fb9fc6fbcc2d561f36b78da4bf3802370'
            '1b5b24f7bc543c0362b667692f78db8bab4ed6dafc6172f104d0bd3757d8a133'
            '233f66e8d25c5dd971716d4200203a612a407649686ef3b52075d04b4c9df0dd'
            'fe0291b96d7352bac530d13ef2e5fd63ad9980e0128911f88b957b5992508f1c'
            '0082d0684f7db6f62361b76c4b7faba19e0c7ce5cb8e36c4b65fea8281e711b4'
            '940caef1ec7c78e0c34b0f6b94fe42d0f2022915ffc78643d28538a5cfd0f40e'
            'acb85cedafa10ce106b1823fb236b1b3e5d942a5741e8f8435cc8ccfec0afe76'
            '2223ebce534458a37826e8fe4f24635b0712cde7ed1bd3208f089f6fdd796e01'
            'd6b4650ff897ee1ead27cf77a5933ea197cbeef6705638dd181adc2e816b23c2'
            '09d63b05e9c594ec423778ab59b7a5aa1d76fdd71d25c7048b0258c4ec9c3384'
            'f7c7075750e8fceeac081e9ef01944f221b36d9725beac8681cbd2838d26be45'
            '77d6c6ecb35952a8d8ce7f736b7a2bf466275c48210e309b73782d6b7e84dffd'
            '167e86561cd7b5ed21b67d7543536134edcb14b373892739b28c417566a3832f'
            'f2443f27561af52324eee03a1892d9f569adc8db9e7bca55614898bc2a13a770'
            '984f2a479df79e27e7b01a5815ac53ae64e07746b882262d8a64566494515504'
            'f543e6e2d7275557a839a164941c0a86e5f2c3f2a0042bfc434c88c6dde9e140'
            'd0be8099cbee3c9dfda694a828149b881c345b204ab68826f317580aafb50879'
            '9fb33dbc3ffeb6af2ea7341b58242142d4eca903584c21c5f4b44ff27fbf5b5f'
            '512355b5ef646791089da8c2b35cf8b019673c0c8a58685962af3b5d9eb5d6bb')

prepare() {

    cd libreoffice-$_LOver

    # move external sources into place
    mkdir "${srcdir}"/ext_sources && pushd "${srcdir}"/ext_sources
    for source in "${noextract[@]}"; do
    ln -s "${srcdir}"/$source .
    done
    popd

    #use the CFLAGS but remove the LibO overridden ones
    for i in $CFLAGS; do
        case "$i" in
            -pipe|-Wall|-Werror*|-fexceptions) continue;;
        esac
        ARCH_FLAGS="$ARCH_FLAGS $i"
    done
    export ARCH_FLAGS
    export CFLAGS="$ARCH_FLAGS"
    export CXXFLAGS="$ARCH_FLAGS"
    export CPPFLAGS+=" -D_FILE_OFFSET_BITS=64"
}

build() {
    cd libreoffice-$_LOver

    # strip -s from Makeflags in case you use it to shorten build logs
    _MAKEFLAGS=${MAKEFLAGS/-s/}

    # this uses malloc_usable_size, which is incompatible with fortification level 3
    # /usr/lib/libreoffice/program/libskialo.so uses malloc_usable_size
    export CFLAGS="${CFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"
    export CXXFLAGS="${CXXFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"

    # http://site.icu-project.org/download/61#TOC-Migration-Issues
    CPPFLAGS+=' -DU_USING_ICU_NAMESPACE=1'

    # fix build with qt6 expecting libexec path
    export qt6_libexec_dirs="/usr/lib/qt6"

    ./autogen.sh --with-extra-buildid="${pkgver}-${pkgrel}" \
        --with-vendor="Arch Linux" \
        --enable-split-app-modules \
        --with-parallelism=${_MAKEFLAGS/-j/} \
        --with-external-tar="${srcdir}/ext_sources" \
        --disable-fetch-external \
        --enable-release-build \
        --prefix=/usr --exec-prefix=/usr --sysconfdir=/etc \
        --libdir=/usr/lib --mandir=/usr/share/man \
        --with-lang="" \
        --with-help=html \
        --disable-avahi \
        --enable-dbus \
        --enable-evolution2\
        --enable-gio\
        --disable-gtk3-kde5 \
        --disable-kf5 \
        --enable-kf6 \
        --enable-qt5 \
        --enable-qt6 \
        --enable-gtk3 \
        --enable-gtk4 \
        --enable-introspection \
        --enable-openssl \
        --enable-odk\
        --enable-python=system \
        --enable-scripting-beanshell \
        --enable-scripting-javascript \
        --disable-dconf \
        --disable-report-builder \
        --enable-ext-wiki-publisher \
        --enable-ext-nlpsolver \
        --without-fonts\
        --with-system-libxml \
        --with-system-libcdr \
        --with-system-mdds\
        --without-myspell-dicts \
        --with-system-libvisio \
        --with-system-libcmis \
        --with-system-libmspub \
        --with-system-libexttextcat \
        --with-system-orcus \
        --with-system-liblangtag \
        --with-system-libodfgen \
        --with-system-libmwaw \
        --with-system-libetonyek \
        --with-system-libfreehand \
        --disable-firebird-sdbc \
        --with-system-zxing \
        --with-system-libtommath \
        --with-system-libatomic-ops \
        --with-system-libebook \
        --with-system-libabw \
        --with-system-coinmp \
        --with-system-dicts \
        --with-external-dict-dir=/usr/share/hunspell \
        --with-external-hyph-dir=/usr/share/hyphen \
        --with-external-thes-dir=/usr/share/mythes \
        --with-system-beanshell \
        --with-system-cppunit\
        --with-system-graphite\
        --with-system-glm \
        --with-system-libnumbertext \
        --with-system-libwpg \
        --with-system-libwps \
        --with-system-redland\
        --with-system-libzmf \
        --with-system-gpgmepp \
        --with-system-libstaroffice \
        --with-jdk-home="/usr/lib/jvm/default" \
        --with-ant-home="/usr/share/ant"\
        --with-system-boost\
        --with-system-icu \
        --with-system-cairo \
        --with-system-libs \
        --with-system-mythes \
        --with-system-headers \
        --without-system-hsqldb \
        --without-junit \
        --with-system-clucene \
        --without-system-box2d \
        --without-system-dragonbox \
        --without-system-libfixmath \
        --without-system-frozen \
        --without-system-zxcvbn \
        --with-system-rhino \
        --with-gdrive-client-id=${_google_default_client_id} \
        --with-gdrive-client-secret=${_google_default_client_secret} \
        --disable-dependency-tracking

    touch src.downloaded
    make build

    mkdir "${srcdir}"/fakeinstall
    make DESTDIR="${srcdir}"/fakeinstall distro-pack-install
}

#check() {
#   cd "${srcdir}"/build
#   make check
#}

package_libreoffice-fresh-sdk() {
    pkgdesc="Software Development Kit for LibreOffice Fresh"
    depends=('libreoffice-fresh' 'gcc-libs' 'sh' 'make' 'zip' 'gcc' 'java-environment' 'glibc')
    provides=('libreoffice-sdk')
    conflicts=('libreoffice-still-sdk')

    for dir in $(grep -h ^%dir libreoffice-$_LOver/file-lists/sdk{,_doc}_list.txt); do
        install -dm755 "${pkgdir}"/${dir/\%dir/}
    done

    for file in $(grep -h -v ^%dir libreoffice-$_LOver/file-lists/sdk{,_doc}_list.txt); do
        dirname=$(dirname $file)
        [[ -d "${pkgdir}"/$dirname ]] || install -dm755 "${pkgdir}"/$dirname
        mv "${srcdir}"/fakeinstall/${file} "${pkgdir}"/$file
    done

    # fix permissions
    find "${pkgdir}"/usr/lib/libreoffice/sdk/examples -type f -exec chmod -x {} +

    # add LibreOfficeKit headers; FS#48066
    install -dm755 "${pkgdir}"/usr/include/LibreOfficeKit
    install -v -m644 "${srcdir}"/libreoffice-$_LOver/include/LibreOfficeKit/* "${pkgdir}"/usr/include/LibreOfficeKit
}

package_libreoffice-fresh() {
    pkgdesc="LibreOffice branch which contains new features and program enhancements"
    depends=('curl>=7.20.0' 'hunspell>=1.2.8' 'python' 'libwpd>=0.9.2' 'libwps'
        'neon>=0.28.6' 'pango' 'nspr' 'libjpeg-turbo' 'libxrandr' 'libgl'
        'redland' 'hyphen' 'lpsolve' 'gcc-libs' 'sh' 'graphite' 'icu' 'libxslt'
        'lcms2' 'poppler' 'libvisio' 'libetonyek' 'libodfgen' 'libcdr'
        'libmspub' 'harfbuzz-icu' 'nss' 'clucene' 'hicolor-icon-theme'
        'desktop-file-utils' 'shared-mime-info' 'libpagemaker'
        'libxinerama' 'libabw' 'libmwaw' 'libe-book' 'libcups'
        'liblangtag' 'libexttextcat' 'liborcus' 'libwebp' 'libcmis' # 'libfbclient'
        'libtommath' 'libzmf' 'libatomic_ops' 'xmlsec' 'libnumbertext' 'gpgme'
        'libfreehand' 'libstaroffice' 'libepubgen' 'libqxp' 'libepoxy'
        'zxing-cpp' 'xdg-utils'
        'libldap' 'fontconfig' 'zlib' 'libpng' 'freetype2' 'raptor' 'libxml2'
        'cairo' 'libx11' 'expat' 'glib2' 'boost-libs' 'libtiff' 'dbus' 'glibc'
        'librevenge' 'libxext' 'openjpeg2' 'argon2')
    optdepends=('java-runtime:     adds java support'
        'java-environment>=17: required by extension-wiki-publisher and extension-nlpsolver'
        'pstoedit:         translates PostScript and PDF graphics into other vector formats'
        'libmythes:        for use in thesaurus'
        'beanshell:        interactive java -- good for prototyping/macros'
        'libwpg:           library for importing and converting WordPerfect Graphics format'
        'sane:             for scanner access'
        'unixodbc:         adds ODBC database support'
        'gst-plugins-base-libs: for multimedia content, e.g. in Impress'
        'libpaper:         takes care of papersize'
        'postgresql-libs:  for postgresql-connector'
        'mariadb-libs:     for mysql-connector'
        'coin-or-mp:       required by the Calc solver'
        'gtk3:             for GTK3 integration'
        'gtk4:             for GTK4 integration (experimental)'
        'qt5-x11extras:    for Qt5 desktop integration'
        'kio:              for KF6 KDE desktop integration'
        'qt6-multimedia:   for Qt6 desktop integration'
        'rhino:            for JavaScript support')
    backup=(etc/libreoffice/sofficerc
        etc/libreoffice/bootstraprc
        etc/libreoffice/psprint.conf
        etc/profile.d/libreoffice-fresh.sh
        etc/profile.d/libreoffice-fresh.csh)
    provides=('libreoffice' 'libreoffice-en-US')
    conflicts=('libreoffice-still')

    mv fakeinstall/* "${pkgdir}"/

    # put configuration files into place
    install -dm755 "${pkgdir}"/etc/libreoffice
    install -m644 "${pkgdir}"/usr/lib/libreoffice/program/{bootstraprc,sofficerc} \
        "${pkgdir}"/etc/libreoffice/
    install -m644 "${pkgdir}"/usr/lib/libreoffice/share/psprint/psprint.conf \
        "${pkgdir}"/etc/libreoffice/

    # install dummy links to make them found by LibO
    cd "${pkgdir}"/usr/lib/libreoffice/program/
    ln -vsf /etc/libreoffice/{bootstraprc,sofficerc} .
    cd "${pkgdir}"/usr/lib/libreoffice/share/psprint/
    ln -vsf /etc/libreoffice/psprint.conf .

    # allow to preset desired VLC
    install -dm755 "${pkgdir}"/etc/profile.d
    install -m644 "${srcdir}"/libreoffice-fresh.{sh,csh} "${pkgdir}"/etc/profile.d/


    # move python bits into site-packages
    local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")
    install -dm755 "${pkgdir}"/$site_packages

    pushd "${pkgdir}"/$site_packages
        echo "import sys, os" > uno.py
        echo "sys.path.append('/usr/lib/libreoffice/program/')" >> uno.py
        echo "os.putenv('URE_BOOTSTRAP', 'vnd.sun.star.pathname:/usr/lib/libreoffice/program/fundamentalrc')" >> uno.py
        cat "${pkgdir}"/usr/lib/libreoffice/program/uno.py >> uno.py
        rm -f "${pkgdir}"/usr/lib/libreoffice/program/uno.py*
        mv -f "${pkgdir}"/usr/lib/libreoffice/program/unohelper.py* .
        mv -f "${pkgdir}"/usr/lib/libreoffice/program/officehelper.py* .
    popd

    # make pyuno find its modules
#     local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")
#
#     install -dm755 "${pkgdir}"/$site_packages
#     ln -svf /usr/lib/libreoffice/program/uno.py \
#         "${pkgdir}"/$site_packages/uno.py
#     ln -svf /usr/lib/libreoffice/program/unohelper.py \
#         "${pkgdir}"/$site_packages/unohelper.py


    # add a symlink required for gnome-documents; FS#51887
    # https://lists.freedesktop.org/archives/libreoffice/2016-March/073787.html
    ln -svf /usr/lib/libreoffice/program/liblibreofficekitgtk.so \
        "${pkgdir}"/usr/lib/liblibreofficekitgtk.so

    # cleanup
    rm -rf "${pkgdir}"/usr/share/libreoffice/sdk

    # add application descriptions
    install -dm755 "${pkgdir}"/usr/share/metainfo
    install -v -m644 "${srcdir}"/libreoffice-$_LOver/sysui/desktop/appstream-appdata/*.xml \
    "${pkgdir}"/usr/share/metainfo

    # add kde filemanager templates; FS#61662 - file taken from Debian
    install -dm755 "${pkgdir}"/usr/share/templates/.source
    install -v -m644 "${srcdir}"/libreoffice-$_LOver/extras/source/shellnew/soffice.* \
    "${pkgdir}"/usr/share/templates/.source

    cat "${srcdir}"/soffice-template.desktop.in \
                | sed -e "s/@APP@/Writer/" \
                | sed -e "s/@EXT@/odt/" \
                | sed -e "s/@TYPE@/text/" \
                > "${pkgdir}"/usr/share/templates/soffice.odt.desktop
        cat "${srcdir}"/soffice-template.desktop.in \
                | sed -e "s/@APP@/Calc/" \
                | sed -e "s/@EXT@/ods/" \
                | sed -e "s/@TYPE@/spreadsheet/" \
                > "${pkgdir}"/usr/share/templates/soffice.ods.desktop
        cat "${srcdir}"/soffice-template.desktop.in \
                | sed -e "s/@APP@/Impress/" \
                | sed -e "s/@EXT@/odp/" \
                | sed -e "s/@TYPE@/presentation/" \
                > "${pkgdir}"/usr/share/templates/soffice.odp.desktop
        cat "${srcdir}"/soffice-template.desktop.in \
                | sed -e "s/@APP@/Draw/" \
                | sed -e "s/@EXT@/odg/" \
                | sed -e "s/@TYPE@/drawing/" \
                > "${pkgdir}"/usr/share/templates/soffice.odg.desktop

    # make all i18n lang packages with help section ('1') available to
    # fix "F1" not opening translated offline help opening in browser
    # see also /usr/lib/libreoffice/help/en-US/langnames.js
    echo "var languagesSet = new Set(['en-US','am','ar','ast','bg','bn',\
    'bn-IN','bo','bs','ca','ca-valencia','cs','da','de','dz','el',\
    'en-GB','en-ZA','eo','es','et','eu','fi','fr','gl','gu','he',\
    'hi','hr','hu','id','is','it','ja','ka','km','ko','lo','lt','lv',\
    'mk','nb','ne','nl','nn','om','pl','pt','pt-BR','ro','ru','si',\
    'sid','sk','sl','sq','sv','ta','tg','tr','ug','uk','vi','zh-CN','zh-TW'])" \
        > "${pkgdir}"/usr/lib/libreoffice/help/languages.js
}