# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2019-present Team LibreELEC (https://libreelec.tv)

THREADCOUNT=${THREADCOUNT:-$(nproc)}

start_multithread_build() {
  local buildopts result=0

  # init thread control folder
  rm -rf "${THREAD_CONTROL}"
  mkdir -p "${THREAD_CONTROL}/locks"
  echo -1 >"${THREAD_CONTROL}/progress.prev"
  echo 0 >"${THREAD_CONTROL}/progress"
  touch "${THREAD_CONTROL}/status"

  # Increase file descriptors if building one thread/package
  [ "${THREADCOUNT}" = "0" ] && ulimit -n ${ULIMITN:-10240}

  # create a single log file by default for a single threaded build (or the builder is a masochist)
  if [ ${THREADCOUNT} -eq 1 -a "${ONELOG,,}" != "no" ] || [ "${ONELOG,,}" = "yes" ]; then
    buildopts+=" --no-log-burst"
  else
    mkdir -p "${THREAD_CONTROL}/logs"
    buildopts+=" --log-burst"
  fi
  buildopts+=" --log-combine ${LOGCOMBINE:-always}"

  [ "${AUTOREMOVE}" = "yes" ] && buildopts+=" --auto-remove"

  # When building addons, don't halt on error - keep building all packages/addons
  [ "${MTADDONBUILD}" = "yes" ] && buildopts+=" --continue-on-error" || buildopts+=" --halt-on-error"

  [ "${MTPROGRESS}" = "yes" ] && buildopts+=" --progress"
  [ "${MTVERBOSE}" = "yes" ] && buildopts+=" --verbose"
  [ "${MTDEBUG}" = "yes" ] && buildopts+=" --debug"
  if [ "${DISABLE_COLORS}" = "yes" ]; then
    buildopts+=" --colors=never"
  else
    buildopts+=" --colors=${MTCOLORS:-auto}"
  fi
  [ "${MTBOOKENDS}" = "no" ] && buildopts+=" --without-bookends" || buildopts+=" --with-bookends"
  [ "${MTIMMEDIATE}" = "no" ] && buildopts+=" --fail-after-active" || buildopts+=" --fail-immediately"

  buildopts+=" --stats-interval ${MTINTERVAL:-60}"

  # pipefail: return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status
  set -o pipefail

  ${SCRIPTS}/pkgjson | ${SCRIPTS}/genbuildplan.py --show-wants --with-json "${THREAD_CONTROL}"/plan.json \
      --build ${@} > "${THREAD_CONTROL}"/plan || result=1

  if [ ${result} -eq 0 ]; then
    save_build_config

    # export the following vars so that they will be available to subprocesses of pkgbuilder.py
    export ROOT SCRIPTS THREAD_CONTROL

    MTBUILDSTART=$(date +%s) MTWITHLOCKS=yes ${SCRIPTS}/pkgbuilder.py \
        --plan "${THREAD_CONTROL}"/plan.json \
        --joblog "${THREAD_CONTROL}"/joblog \
        --loadstats "${THREAD_CONTROL}"/loadstats \
        --max-procs ${THREADCOUNT} ${buildopts} || result=1

    [ ${result} -eq 0 -a -f "${THREAD_CONTROL}"/history ] && echo && cat "${THREAD_CONTROL}"/history | ${ROOT}/tools/mtstats.py

    rm -f "${THREAD_CONTROL}/parallel.pid"
  fi

  set +o pipefail

  return ${result}
}
