if ab_match_arch powerpc || \
   ab_match_arch ppc64 || \
   ab_match_arch loongson3; then
    export FIREBIRD="--disable-firebird-sdbc"
else
    export FIREBIRD="--enable-firebird-sdbc"
fi

if ! bool $USECLANG; then
  # HACK: when not using clang, we also need to avoid LibO
  # force clang usage on Skia
  export LO_CLANG_CC=gcc
  export LO_CLANG_CXX=g++
fi

abinfo "Autogen-ing ..."
# FIXME: Use bundled GpgME as our version is too new for 7.3.
# Check again with 7.5.
./autogen.sh \
             --with-vendor="Anthon Open Source Community" \
             --with-parallelism="${ABTHREADS}" \
             --enable-split-app-modules \
             --enable-release-build \
             --prefix=/usr --exec-prefix=/usr --sysconfdir=/etc \
             --libdir=/usr/lib --mandir=/usr/share/man \
             --with-jdk-home="/usr/lib/java" \
             --with-ant-home="/usr/share/apache-ant" \
             --with-sac-jar="/usr/share/java/sacjava/sac.jar" \
             --with-external-dict-dir=/usr/share/hunspell \
             --with-external-hyph-dir=/usr/share/hyphen \
             --with-external-thes-dir=/usr/share/mythes \
             --disable-avahi \
             --enable-dbus \
             --enable-evolution2 \
             --enable-gio \
             --enable-gtk3 \
             --enable-kf5 \
             --enable-introspection \
             --enable-openssl \
             --disable-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-libcdr \
             --with-system-mdds\
             --with-system-libvisio \
             --with-system-libmspub \
             --with-system-libexttextcat \
             --with-system-orcus \
             --with-system-liblangtag \
             --with-system-libodfgen \
             --with-system-libmwaw \
             --with-system-libetonyek \
             --with-system-libfreehand \
             --with-system-libtommath \
             --with-system-libatomic-ops \
             --with-system-libebook \
             --with-system-libabw \
             --with-system-coinmp \
             --with-system-dicts \
             --with-system-boost \
             --with-system-beanshell \
             --with-system-cppunit \
             --with-system-graphite\
             --with-system-glm \
             --with-system-libwpg \
             --with-system-libwps \
             --with-system-redland\
             --with-system-libzmf \
             --with-system-libstaroffice \
             --with-system-icu \
             --with-system-cairo \
             --with-system-libs \
             --with-system-mythes \
             --with-system-headers \
             --with-system-clucene \
             --with-system-gpgmepp \
             --with-system-mariadb \
             --with-system-poppler \
             --without-system-libcmis \
             --without-system-firebird ${FIREBIRD} \
             --without-system-hsqldb \
             --without-myspell-dicts \
             --without-system-libeot \
             --without-system-frozen \
             --without-system-zxcvbn \
             --without-junit \
             --without-system-box2d \
             --without-system-dragonbox \
             --without-system-libfixmath \
             --without-system-rhino \
             --without-system-java-websocket \
             --with-gdrive-client-id="1006183841565.apps.googleusercontent.com" \
             --with-gdrive-client-secret="XN6oYWBv7O7w_heXB8TVuldr" \
             --disable-dependency-tracking \
             --enable-pch=full \
             --with-lang="ALL" \
             --enable-skia="no"

if ab_match_arch loongarch64 || \
   ab_match_arch loongarch64_nosimd || \
   ab_match_arch riscv64; then
    abinfo "Fetching Firebird ..."
    make UnpackedTarball_firebird

    abinfo "Applying patches for Firebird ..."
    ab_apply_patches "$SRCDIR"/autobuild/patches/*.firebird-deferred."${CROSS:-$ARCH}"
fi

for i in $(find "$SRCDIR" -name config.guess -o -name config.sub); do \
    abinfo "Copying replacement $i ..."
    cp -v /usr/bin/$(basename $i) $i ; \
done

abinfo "Building LibreOffice"
make build

abinfo "Deploying files"
make distro-pack-install DESTDIR="$PKGDIR"
