#!/usr/bin/env bash
# Resume only after confirming any earlier manual build has exited.
set -euo pipefail
office_build_root="${OFFICE_BUILD_ROOT:-$HOME/.cache/tabmorph/office-build}"
office_source="$office_build_root/core-fc03117d3f8f2d7137d973e5db3b2514e8eba990"
office_log="${OFFICE_BUILD_LOG:-/tmp/tabmorph-office-build.log}"
office_lock="$office_build_root/.tabmorph-build-lock"

if ! mkdir "$office_lock"; then
  echo "Another recipe owns $office_lock; inspect it before starting a build." >&2
  exit 1
fi
printf '%s\n' "$$" > "$office_lock/owner.pid"
trap 'rm -f "$office_lock/owner.pid"; rmdir "$office_lock"' EXIT
export EMSDK_QUIET=1
source "$office_build_root/emsdk/emsdk_env.sh"
export PATH="$office_build_root/native-tools/bin:$office_build_root/native-tools/pyvenv/bin:$PATH"
export PKG_CONFIG="$office_build_root/native-tools/bin/pkg-config"
export PKG_CONFIG_FOR_BUILD="$PKG_CONFIG"
export PKG_CONFIG_LIBDIR="$office_build_root/native-tools/empty-pkgconfig"
export PKG_CONFIG_PATH=
cd "$office_source"
test -s config_host.mk
test -s sources.ver
test -x "$office_build_root/native-tools/bin/make"
grep -q '^export MPL_SUBSET=TRUE$' config_host.mk
grep -q '^export DISABLE_GUI=TRUE$' config_host.mk
grep -q -- '-sDYNAMIC_EXECUTION=0' desktop/Executable_soffice_bin.mk
nice -n 10 "$office_build_root/native-tools/bin/make" -j4 > "$office_log" 2>&1
