#!/bin/bash -e

#---------------------------------------------------------------------
# File information.

# buildlibre-release - LibreOffice "build" script
# License: MIT/X (for this file only)
# Revision: 260325

#---------------------------------------------------------------------
# Overview.

# 1. This script builds LibreOffice for Laclin.

# 2. As of  upstream releases  4.4.X through 7.X,  LibreOffice release
# source tarballs have names of the form:
#
#     libreoffice-*.tar.xz
#
# plus a  set of  supplemental archives.  This script assumes that the
# tarballs and/or archives have already been downloaded.  It uses them
# to build a "release" version of the package.

#---------------------------------------------------------------------

# 3. To prepare the files discussed above, proceed as follows:
#
# 3a. Locate an  official LibreOffice source-tarball FTP or HTTP down-
# load site. The following site might be a good starting point:
#
#     http://download.documentfoundation.org/libreoffice/src/

#---------------------------------------------------------------------

# 3b. Go to the site. Select a release of LibreOffice to build. Locate
# the associated source directory or web-page and go there.  It should
# provide  tarballs  with names similar to the following.  The numbers
# will depend on the release selected:
#
#     libreoffice-26.2.2.2.tar.xz
#     libreoffice-dictionaries-26.2.2.2.tar.xz
#     libreoffice-help-26.2.2.2.tar.xz
#     libreoffice-translations-26.2.2.2.tar.xz

#---------------------------------------------------------------------

# 3c. Locally,  check the "libresrc" folder in the LibreOffice package
# resource tree. The tarballs in question may already be there.

#---------------------------------------------------------------------

# 3d. If not,  download the tarballs associated  with the release that
# you select.  Store them in the "libresrc" folder.
#
# As of mid-2018, if NUMBERS is set to the appropriate release string,
# the following  shell commands  might  produce the links needed,  but
# this isn't guaranteed:
#
# NUMBERS=26.2.2.2
# BACON=`echo $NUMBERS | sed 's/\.[0-9]$//'`
# TOAST=http://download.documentfoundation.org/libreoffice/src/$BACON
#
# echo $TOAST/libreoffice-dictionaries-$NUMBERS.tar.xz
# echo $TOAST/libreoffice-help-$NUMBERS.tar.xz
# echo $TOAST/libreoffice-translations-$NUMBERS.tar.xz
# echo $TOAST/libreoffice-$NUMBERS.tar.xz

#---------------------------------------------------------------------

# 3e. The tarballs  should  all  have the  same  NUMBERS part in their
# names.  This is the LibreOffice release  string.  Edit this  script.
# Set RELEASE  to the LibreOffice release  string.

#---------------------------------------------------------------------

# 3f. Edit the "cfg" file for this distro's "libreoffice" package. Set
# "revision" to the LibreOffice release string.

#---------------------------------------------------------------------

# 3g. You now need a number of other tarballs and/or archives.  Possi-
# bly 500 MB to 2 GB of files or more.
#
# If you're rebuilding the copy of LibreOffice that came with the dis-
# tro that this script is for,  the  required files may  already be in
# the "libresrc" directory.
#
# Otherwise,  if you don't do anything to obtain the files in advance,
# the build procedure will try to download them.  But this isn't guar-
# anteed to work.
#
# One way to obtain the files is as follows:
#
# (g1) Extract a file named  "download.lst"  from  the  source tarball
#      which has a name similar to "libreoffice-26.2.2.2.tar.xz".
#
# (g2) Execute a command similar to:
#      perl lodown2link.pl < download.lst > tempgetfiles.sh
#
#      You  should have  received the  ".pl" file  with  the file that
#      you're reading.  Copy the ".pl" file and "download.lst"  to the
#      "libresrc" directory mentioned before.
#
# The website used by the script may be missing some of the files that
# it tries to get. If this is the case,  you'll need to find  the mis-
# sing files at other websites.
#
# If you're doing all of this much after 2017,  things may have chang-
# ed.  In this case, you'll need to stay with the files that came with
# this script or rewrite this script.

#---------------------------------------------------------------------

# 3h. Check the directory  that contains  this script for  patch files
# with names of the form "*.spcpat". If such files are present, recon-
# cile them with the contents of the downloaded files.

#---------------------------------------------------------------------

# 4. Under Laclin, execute:
#
#     makelac libreoffice --force

# The preceding steps would  need significant changes to work in other
# distros. In theory, it should be possible.

#---------------------------------------------------------------------

# 5. Warnings.

# The directory tree specified by BUILDDIR  will be deleted unless the
# parameter KEEPBUILD is set to "yes".

#---------------------------------------------------------------------

# 6. Parameters.

# Set  BUILDDIR to an absolute path for the  temporary directory where
# LibreOffice  should be built.  The directory  doesn't need  to exist
# initially and, in fact, if it exists, it will be deleted.

# Don't set BUILDDIR to  "/tmp" or  "/var/tmp" or  any  directory that
# shouldn't be deleted! Instead,  set it to the pathname of an  appro-
# priate  subdirectory. The pathname  shouldn't contain spaces.  Note:
# The associated filesystem should  have a  large amount of  free disk
# space (at least 16 gigabytes).

# The TOPDIR and PREFIX settings used in  "runlibre" should  match the
# ones specified here.

# The BUILDDIR setting  shouldn't  specify  the  following tree or any
# subdirectory thereof, as this will conflict with  a tree that's used
# at a higher level:
#
#     /var/tmp/build/libreoffice

#---------------------------------------------------------------------

# 7. External vs. internal Python 3.

# In theory, "libreoffice"  can be built using either the  system copy
# of Python 3 or an internal copy. In practice, as of 2024, builds us-
# ing the internal copy are difficult. So, this script  presently uses
# the system copy.  One consequence is that "libreoffice" may not work
# as a headless server.

# To  switch from external- to internal- Python 3 mode,  the following
# changes to this script are required:
#
# 7a. In the section labeled as follows:
#
#     Force some "include" and "lib" paths.
#
# Remove these two names from the outer "for" loop:
#
#     python3 zlib

# 7b. Replace this configure-time option switch:
#
#     --enable-python=system              \
#
# with:
#
#     --enable-python=internal            \
#     --without-system-zlib               \

# To  switch from internal- to external- Python 3 mode,  the following
# reverse changes are required:
#
# 7c. In the section labeled as follows:
#
#     Force some "include" and "lib" paths.
#
# Add these two names to the outer "for" loop:
#
#     python3 zlib

# 7d. Replace these configure-time option switches:
#
#     --enable-python=internal            \
#     --without-system-zlib               \
#
# with:
#
#     --enable-python=system              \

#---------------------------------------------------------------------

# 8. For the  releases listed below,  the final sizes of the "install"
# tree were as follows:

#     libreoffice 4.0.3.3:
#     609 MB - Uncompressed size
#     261 MB - When stored in a squashfs-lzma filesystem

#     libreoffice 4.3.2.2:
#     743 MB - Uncompressed size
#     295 MB - When stored in a squashfs-lzma filesystem

#     libreoffice 4.3.4.1:
#     616 MB - Uncompressed size
#     270 MB - When stored in a squashfs-lzma filesystem

#     libreoffice 4.4.1.2:
#     666 MB - Uncompressed size
#     281 MB - When stored in a squashfs-lzma filesystem

#     libreoffice 4.4.2.2:
#     673 MB - Uncompressed size
#     282 MB - When stored in a squashfs-lzma filesystem

#     libreoffice 5.0.4.2:
#     810 MB - Uncompressed size
#     305 MB - When stored in a squashfs-lzma filesystem

#     libreoffice 5.3.3.2:
#     539 MB - Uncompressed size
#     139 MB - When stored in a squashfs-lzma filesystem

#     libreoffice 6.0.4.2:
#     504 MB - Uncompressed size
#     134 MB - When stored in a squashfs-lzma filesystem

# The drops for 5.3.3.2 and 6.0.4.2 are  probably related to moving to
# the use of more external (i.e., system) components.

#---------------------------------------------------------------------

# 9. Miscellaneous.

# 9a. If a LibreOffice "make" operation aborts, the following command,
# executed in the "libreoffice-NUMBER" folder, should repeat the oper-
# ation in verbose mode:
#
#     make GMAKE_OPTIONS='VERBOSE=1'

# This will only work if the  "build" procedure gets  far  enough that
# the original "make" operation starts before the abort occurs.

# 9b. Some LibreOffice "make" operations fail  in  such a way that the
# "build" system doesn't detect errors. When this happens, LibreOffice
# may appear to have been built successfully, but it will not run cor-
# rectly.  To determine if this has happened, review  the  LibreOffice
# "build" log file.

#---------------------------------------------------------------------

# RELEASE may specify:
#
# A normal release number of the form #.#.#.#
# A beta   release number of the form #.#.#.#.beta#
# A git snapshot string such as "git-709866a3b"

RELEASE=26.2.2.2

#---------------------------------------------------------------------

OK=no
if [ -f buildlibre-release ]; then
    if [ -f ../special/buildlibre-release ]; then
        if [ -d libresrc ]; then
             OK=yes
        fi
    fi
fi            

if [ "@$OK" == "@no" ]; then
    cat << END

Error: This script must be executed in the directory that contains it.
This must be a "/special/" subdirectory of the following form:

    /src/0620_libresuite/0025_libreoffice248/special/

The digits will vary.  The "/special/" subdirectory  must also contain
the "libresrc/" subdirectory that goes with this file.
END
    exit 1    
fi

#---------------------------------------------------------------------

if [ "@$LACBITS" == "@32" ]; then
    export NOJAVA=true
    BITS_RELATED="--without-galleries"
else
    export NOJAVA=false
    BITS_RELATED=
fi

export BUILDLO25=true
export NOJAVA=true

#---------------------------------------------------------------------

if [ "@$NOJAVA" == "@true" ]; then
    JAVAPKG=
    JAVA_HOME=
    JAVASW=--without-java
    ANT_HOME=
else
    JAVAPKG=openjdk15
    JAVA_HOME=$PRODTREE/$JAVAPKG
    JAVASW=
    ANT_HOME=$JAVA_HOME
fi

export ANT_HOME JAVA_HOME

#---------------------------------------------------------------------

BUILDDIR=/var/tmp/build/buildlibre
KEEPBUILD=no
TMPDIR=$BUILDDIR/tmp
VENDOR="Laclin"

export CC=clang
export CXX=clang++

if [ "@$CC" == "@clang" ]; then
    . srcpkgpaths llvmgcc
fi

if [ "@$LACARCH" == "@" ]; then
    echo Error: LACARCH not set
    exit 1
fi

DIR=`pwd`
PKGNAME=`echo $DIR | sed -e 's/.*_//' -e 's@/special$@@'`
echo PKGNAME=$PKGNAME

TOPDIR=/$LACARCH/pkg/$PKGNAME
BINDIR=$TOPDIR/bin
PREFIX=$TOPDIR
export LO_PREFIX=$PREFIX

PKGSPCDIR=`echo /src/0*_libresuite/0*_$PKGNAME/special`
RUNSCBASE=runlibre

THISFILEX=$PKGSPCDIR/buildlibre-release
RUNSCPATH=$PKGSPCDIR/$RUNSCBASE.wrapper.in
SBALLDIRX=$PKGSPCDIR/libresrc
TBALLDIRX=$BUILDDIR/libresrc
SBOOTBALL=`echo $SBALLDIRX/libreoffice-$RELEASE.tar.xz 2>&1`

# For a git snapshot,  the preceding "SBOOTBALL=" line might be repla-
# ced with something similar to:
#
# SBOOTBALL=`echo $SBALLDIRX/libreoffice-git-709866a3b.tgz 2>&1`

#---------------------------------------------------------------------

if [ \! -f "$THISFILEX" ]; then
    echo Error: PKGSPCDIR and/or THISFILEX settings are incorrect
    exit 1
fi

# if [ \! -f "$ANT_HOME/bin/ant" ]; then
#     echo Error: ANT_HOME setting is incorrect
#     exit 1
# fi

if [ \! -f "$RUNSCPATH" ]; then
    echo "Error: RUNSCPATH setting is incorrect"
    exit 1
fi

if [ \! -f "$SBOOTBALL" ]; then
    echo Error: SBOOTBALL setting is incorrect:
    echo $SBOOTBALL
    exit 1
fi

if [ \! -d "$SBALLDIRX" ]; then
    echo Error: SBALLDIRX setting is incorrect:
    echo $SBALLDIRX
    exit 1
fi

export TMPDIR

#---------------------------------------------------------------------

if [ -d "$BUILDDIR" ]; then
    DELETEDIR="$BUILDDIR.delete-1-$$"
    mv "$BUILDDIR" "$DELETEDIR"
     echo "rm -fr \"$DELETEDIR\" &"
    rm -fr  "$DELETEDIR" &
    DELETEDIR=
fi

rm -fr   $BUILDDIR
mkdir -p $BUILDDIR
cd       $BUILDDIR

mkdir -p $SBALLDIRX $SBOOTDIRX $TMPDIR
pushd    $SBALLDIRX
popd

rm -fr              $TBALLDIRX
ln -s    $SBALLDIRX $TBALLDIRX

#---------------------------------------------------------------------
# Terminate any running instances of LibreOffice.

killany program/soffice.bin

#---------------------------------------------------------------------
# Needed to use wrappers that we're about to create.

PATH=.:`pwd`:$PATH

#---------------------------------------------------------------------
# Create a "uname" wrapper to bypass a problem.

rm -f uname
hash  uname

cat > uname << 'END'
#!/usr/bin/env perl
use strict;
use Carp;
use warnings;
my $str =  `UNAME_EXE @ARGV`;
   $str =  "" unless defined $str;
   $str =~ s@\b3\.\d+\.\d+\b@2.6.39@g;
print $str;
END

UNAME_EXE=`which uname`
sed -e "s@UNAME_EXE@$UNAME_EXE@g" -i uname
chmod 755 uname
hash      uname
./uname -a

#---------------------------------------------------------------------
# Create a "wget" wrapper to bypass a problem.

rm -f wget
hash  wget

cat > wget << 'END'
#!/usr/bin/env perl
@ARGV = grep { !m@^-4\z@; } @ARGV;
exec 'WGET_EXE', @ARGV;
END

WGET_EXE=`which wget`
sed -e "s@WGET_EXE@$WGET_EXE@g" -i wget
chmod 755 wget
hash      wget

#---------------------------------------------------------------------

echo ====== `date` Bootstrap unpack started
# deco $SBOOTBALL
for x in $TBALLDIRX/libreoffice-*.tar.xz
do
    tar Jxf $x
done
echo ====== `date` Bootstrap unpack done

#---------------------------------------------------------------------

cd *-$RELEASE
mkdir -p .git

if [ \! -f sources.ver ]; then
    echo "lo_sources_ver=$RELEASE" > sources.ver
fi

#---------------------------------------------------------------------
# Additional setup.

NJ=`getconf _NPROCESSORS_ONLN 2>&1`
if [ $NJ -gt 32 ]; then NJ=32; fi

sed -e "s|TMPDIR=/tmp|TMPDIR=$TMPDIR|" \
    -i config.guess

BOOSTLIB=$PRODTREE/boost/lib

#---------------------------------------------------------------------

if [ "@$LACINCDIRS" == "@" ]; then LACINCDIRS=/; fi

if [ "@$LACLLVMSELF" \!= "@" ]; then
    LACINCDIRS=$PRODTREE/$LACLLVMSELF/include:$LACINCDIRS
fi

#---------------------------------------------------------------------
# Force some "include" and "lib" paths.

for x in \
    boost          bzip2          cairo          clucene        \
    freetype2      frozen         gdbm           glm            \
    gstreamer      gtk20          gtkcore        hunspell       \
    libabw         libcdr         libebook       \
    libetonyek     libexttextcat  libfreehand    libgcrypt      \
    libgltf        libgpgme       libicu         liblangtag     \
    libmspub       libmwaw        libodfgen      liborcus       \
    libpagemaker   librevenge     libstaroffice  libvisio       \
    libzmf         lpsolve        mariadb        mdds           \
    mpfr           mythes         nspr           nss            \
    opencollada    openldap       postgresql     tommath        \
    unixodbc       xmlsec1        xorg           brotli         \
    $JAVAPKG       python3        zlib           libwebp
do
    LACLDDIRS=$PRODTREE/$x/lib:$LACLDDIRS
    LACINCDIRS=$PRODTREE/$x/include:$LACINCDIRS

    for dir in bin sbin
    do
        XBINDIR=$PRODTREE/$x/$dir
        if [  -d $XBINDIR ]; then
            PATH=$XBINDIR:$PATH
        fi
    done

    PKG_CONFIG_PATH=$PRODTREE/$x/lib/pkgconfig:$PKG_CONFIG_PATH
done

#---------------------------------------------------------------------

LACINCDIRS=$PRODTREE/mariadb/include/mysql:$LACINCDIRS
LACINCDIRS=$PRODTREE/nss/include/nss:$LACINCDIRS

LDFLAGS=`echo "$LDFLAGS" | sed -e 's/^ *//'`
LDINSERTBEFORE="-ldl -lc"

export LACINCDIRS LACLDDIRS LDINSERTBEFORE
export LDFLAGS PATH PKG_CONFIG_PATH

#---------------------------------------------------------------------
# Notes on "--with-system-..." switches.

# WS1. The safest thing to do, when building LibreOffice,  is to build
# system libraries using releases reasonably compatible  with the ones
# that LibreOffice expects, and to specify "--with-system-..." switch-
# es at LibreOffice "build" time to use those libraries.

# WS2. This is probably required in the case of "libxml2":

# "--with-system-libxml"  is needed here or  LibreOffice may abort  at
# startup time.  The problem occurs because  LibreOffice links to lib-
# raries which link to the system's copy of  "libxml2", causing Libre-
# Office to link with that version when it expects to get its own ver-
# sion. Setting LD_LIBRARY_PATH to LibreOffice's  library  directories
# doesn't solve the problem.

# Using  LD_PRELOAD to preload  the  internal  copy of  "libxml2.so.2"
# *does* solve the problem, but  may lead to  other issues.  So, using
# "--with-system-libxml" is probably the best solution for now.

# WS3. If the system has "libepoxy", "--with-system-epoxy" is probably
# needed. Otherwise, the switch should be omitted.

#---------------------------------------------------------------------
# Apply source-code patches.

for x in $PKGSPCDIR/*.spcpat
do
    if [ -f "$x" ]; then
        patch -p1 < $x || exit 1
    fi
done

sed -e "/distro-install-file-lists/d" \
    -i Makefile.in

sed -e "/gzip -f/d"   \
    -e "s|.1.gz|.1|g" \
    -i bin/distro-install-desktop-integration

sed -e 's@#include <key.h>@#include <gpgme++/key.h>@' \
    -e 's@<context.h>@<gpgme++/context.h>@' \
    -e 's@<data.h>@<gpgme++/data.h>@' \
    -e 's@<decryptionresult.h>@<gpgme++/decryptionresult.h>@' \
    -e 's@<encryptionresult.h>@<gpgme++/encryptionresult.h>@' \
    -e 's@<global.h>@<gpgme++/global.h>@' \
    -e 's@<importresult.h>@<gpgme++/importresult.h>@' \
    -e 's@<keylistresult.h>@<gpgme++/keylistresult.h>@' \
    -e 's@<signingresult.h>@<gpgme++/signingresult.h>@' \
    -i comphelper/source/misc/*xx \
       xmlsecurity/source/gpg/*xx

#---------------------------------------------------------------------

export USECCACHE=yes

#---------------------------------------------------------------------
# Replace "box2d" with a patched version.

pushd $SBALLDIRX
PATCHED_DIR=box2d-patched
PATCHED_FILE=$PATCHED_DIR.tar.gz
rm -fr $PATCHED_DIR $PATCHED_FILE

ORIG_FILE=`ls box2d-[0-9.]*.tar.gz | tail -1`
ORIG_SUM=`sha256sum -b $ORIG_FILE | sed 's/ .*//'`
ORIG_DIR=`echo $ORIG_FILE | sed -e 's@\.tar.gz$@@'`

rm -fr  $ORIG_DIR
tar zxf $ORIG_FILE
ls      $ORIG_DIR > /dev/null || exit 1

sed -e  '/<math.h>/i #include <cmath>' \
    -e  's@return isfinite(@return std::isfinite(@' \
    -i  $ORIG_DIR/include/box2d/b2_math.h || exit 1

mv      $ORIG_DIR     $PATCHED_DIR
tar zcf $PATCHED_FILE $PATCHED_DIR
rm  -fr               $PATCHED_DIR
tardate $PATCHED_FILE || exit 1

PATCHED_SUM=`sha256sum -b $PATCHED_FILE | sed 's/ .*//'`
popd

sed -e "s@$ORIG_SUM@$PATCHED_SUM@" \
    -e "s@$ORIG_FILE@$PATCHED_FILE@" \
    -i download.lst

#---------------------------------------------------------------------
# Replace "pdfium" with a patched version.

pushd $SBALLDIRX
PATCHED_FILE=pdfium-patched.tar.bz2
rm -fr pdfium $PATCHED_FILE

ORIG_FILE=`ls pdfium-[0-9][0-9][0-9][0-9].tar.bz2 | tail -1`
ORIG_SUM=`sha256sum -b $ORIG_FILE | sed 's/ .*//'`

tar jxf $ORIG_FILE
ls pdfium > /dev/null || exit 1

TTS=`which treetextsub`
cd pdfium
$TTS --pattern=isfinite   --newtext=std::isfinite --keeptimes
$TTS --pattern=isnan      --newtext=std::isnan    --keeptimes
$TTS --pattern=std::std:: --newtext=std::         --keeptimes
cd ..

# sed -e 's@(isnan(f))@(std::isnan(f))@' \
#     -i pdfium/core/fpdfapi/page/cpdf_psengine.cpp || exit 1

tar jcf $PATCHED_FILE pdfium
tardate $PATCHED_FILE || exit 1
rm  -fr pdfium

PATCHED_SUM=`sha256sum -b $PATCHED_FILE | sed 's/ .*//'`
popd

sed -e "s@$ORIG_SUM@$PATCHED_SUM@" \
    -e "s@$ORIG_FILE@$PATCHED_FILE@" \
    -i download.lst

#---------------------------------------------------------------------
# Replace "sacjava" with a patched version.

pushd $SBALLDIRX
ORIG_FILE=39bb3fcea1514f1369fcfc87542390fd-sacjava-1.3.zip
ORIG_SUM=`sha256sum -b $ORIG_FILE | sed 's/ .*//'`
PATCHED_FILE=sacjava-patched.zip
PATCHED_SUM=`sha256sum -b $PATCHED_FILE | sed 's/ .*//'`
popd

sed -e "s@$ORIG_SUM@$PATCHED_SUM@" \
    -e "s@$ORIG_FILE@$PATCHED_FILE@" \
    -i download.lst

#---------------------------------------------------------------------
# Set environment variables related to OpenCOLLADA.

# This section may not be needed for LibreOffice 6 and above.

export OPENCOLLADA_CFLAGS="-I$PRODTREE/opencollada/include \
-I$PRODTREE/opencollada/include/opencollada"

export OPENCOLLADA_LIBS="-L$PRODTREE/opencollada/lib \
-lUTF -lOpenCOLLADABaseUtils -lOpenCOLLADAFramework \
-lOpenCOLLADASaxFrameworkLoader -lGeneratedSaxParser \
-lMathMLSolver -lpcre"

export COLLADA2GLTF_CFLAGS=$OPENCOLLADA_CFLAGS

export COLLADA2GLTF_LIBS="-lcollada2gltfConvert \
-lo3dgc_dec_lib -lo3dgc_enc_lib -lo3dgc_common_lib \
$OPENCOLLADA_LIBS"

#---------------------------------------------------------------------
# Make previously-downloaded archives accessible.

install -dm755               external/tarballs
ln -sv $PKGSPCDIR/libresrc/* external/tarballs/

#---------------------------------------------------------------------

# These "configure" switches were needed  for LO 5,  but they were ap-
# parently dropped for LO 6:
#
#     --with-system-opencollada
#     --with-system-libgltf
#     --with-system-collada2gltf

# Needed for LO 6.2 but apparently dropped for LO 6.3:
#
#     --disable-kde4

#---------------------------------------------------------------------
# Do the "configure" step.

sed -e 's/^\(bootstrap:.*\)check-if-root/\1/' \
    -i Makefile.in

echo ====== `date` configure start

( ./autogen.sh \
    --disable-ccache                    \
    --disable-dbus                      \
    --disable-dconf                     \
    --disable-dependency-tracking       \
    --disable-fetch-external            \
    --disable-firebird-sdbc             \
    --disable-ld                        \
    --disable-odk                       \
    --disable-postgresql-sdbc           \
\
    --enable-eot                        \
    --enable-ext-nlpsolver              \
    --enable-ext-wiki-publisher         \
    --enable-release-build=yes          \
    --enable-python=system              \
\
    --host=$LACARCHHOST                 \
    --mandir=$TOPDIR/man                \
    --prefix=$PREFIX                    \
    --sysconfdir=$TOPDIR/etc            \
\
    --with-boost-libdir=$BOOSTLIB       \
    --with-boost-date-time              \
    --with-boost-filesystem             \
    --with-external-tar=$TBALLDIRX      \
    --with-help                         \
    --with-myspell-dicts                \
    --with-parallelism=$NJ              \
    --with-vendor="$VENDOR"             \
\
    --without-doxygen                   \
    --without-export-validation         \
    --without-gssapi                    \
    --without-junit                     \
    --without-krb5                      \
    --without-system-dicts              \
\
    --with-system-boost                 \
    --with-system-bzip2                 \
    --with-system-cairo                 \
    --with-system-clucene               \
    --with-system-cppunit               \
    --with-system-curl                  \
    --with-system-epoxy                 \
    --with-system-expat                 \
    --with-system-frozen                \
    --with-system-glm                   \
    --with-system-gpgmepp               \
    --with-system-graphite              \
    --with-system-harfbuzz              \
    --with-system-hunspell              \
    --with-system-icu                   \
    --with-system-jpeg                  \
    --with-system-lcms2                 \
    --with-system-libabw                \
    --with-system-libatomic_ops         \
    --with-system-libcdr                \
    --with-system-libebook              \
    --with-system-libepubgen            \
    --with-system-libetonyek            \
    --with-system-libexttextcat         \
    --with-system-libfreehand           \
    --with-system-liblangtag            \
    --with-system-libmspub              \
    --with-system-libmwaw               \
    --with-system-libnumbertext         \
    --with-system-libodfgen             \
    --with-system-libpagemaker          \
    --with-system-libpng                \
    --with-system-libqxp                \
    --with-system-librevenge            \
    --with-system-libstaroffice         \
    --with-system-libtommath            \
    --with-system-libvisio              \
    --with-system-libwebp               \
    --with-system-libwpd                \
    --with-system-libwpg                \
    --with-system-libwps                \
    --with-system-libxml                \
    --with-system-libzmf                \
    --with-system-lpsolve               \
    --with-system-mariadb               \
    --with-system-mdds                  \
    --with-system-mythes                \
    --with-system-nss                   \
    --with-system-odbc                  \
    --with-system-openldap              \
    --with-system-openssl               \
    --with-system-orcus                 \
    --with-system-poppler               \
    --with-system-postgresql            \
    --with-system-redland               \
    --with-system-xmlsec                \
\
$JAVASW \
$BITS_RELATED \
\
2>&1 || exit 1) | tee configure.log
echo ====== `date` configure end

#---------------------------------------------------------------------

mkdir -p $PREFIX

# This is needed or the "moz" part of the build may fail:
mkdir -p $PREFIX/lib

#---------------------------------------------------------------------

if [ 0 == 1 ]; then
# Note:  The directories  that are being  linked here into the current
# directory may not exist yet.

    if [ \! -d dictionaries ]; then
        ln -nsf src/libreoffice-dictionaries-$RELEASE/dictionaries .
    fi
    if [ \! -d helpcontent2 ]; then
        ln -nsf src/libreoffice-help-$RELEASE/helpcontent2 .
    fi
fi

#---------------------------------------------------------------------

echo ====== `date` make start
((make build GMAKE_OPTIONS='VERBOSE=1' 2>&1  || \
  make build GMAKE_OPTIONS='VERBOSE=1' 2>&1  || \
  make build GMAKE_OPTIONS='VERBOSE=1' 2>&1) || exit 1) \
    | tee make.log
echo ====== `date` make end

echo ====== `date` make-install start
(make install 2>&1 || exit 1) | tee make-install.log
echo ====== `date` make-install end

#---------------------------------------------------------------------

cd $PREFIX
find . -type f -name \*.so -exec chmod 755 "{}" \; || true

#---------------------------------------------------------------------

mkdir -p  $BINDIR
cd        $BINDIR
ls ../lib/libreoffice/program/swriter || exit 1

if [ "@$NOJAVA" == "@true" ]; then
    JAVA_HOME=/tmp
fi

sed -e "s@__META_JAVA_HOME__@$JAVA_HOME@g" \
    -e "s@__META_TOPDIR__@$TOPDIR@g" \
        < $RUNSCPATH \
        > $RUNSCBASE
chmod 755 $RUNSCBASE

#---------------------------------------------------------------------

echo Initial contents of bin directory:
ls -als
echo Creating links in bin directory

for x in sbase scalc sdraw simpress smath soffice spadmin swriter
do
    echo ln -nsf $RUNSCBASE $x
         ln -nsf $RUNSCBASE $x || true
done

cat swriter > /dev/null || exit 1
echo Done creating links

#---------------------------------------------------------------------
# Delete the build tree, or not, as specified.

if  [ "x$KEEPBUILD" == "xyes" ]; then
    echo Keeping $BUILDDIR as specified
    echo You should delete that directory tree later
else
    DELETEDIR="$BUILDDIR.delete-2-$$"
    echo Starting delete of $DELETEDIR
    cd /
    mv "$BUILDDIR" "$DELETEDIR"
    rm -fr "$DELETEDIR" &
    DELETEDIR=
fi

#---------------------------------------------------------------------
# Wrap it up.

echo Done
exit 0
