#!/usr/bin/env bash
# vim: ft=sh
# —————————————————————————————————————————————————————————————————————————————————————
# NOTE: These setup scripts rely on an open source BASH framework BASHMATIC_HOME.
#       https://github.com/kigster/bashmatic
#
# The framework is pretty light-weight, and is installed in your $HOME/.bashmatic folder.
# You can safely remove that folder after the setup if you wish, although re-running the
# setup will re-install it.
# —————————————————————————————————————————————————————————————————————————————————————

[[ -z ${BASHMATIC_HOME} ]] && export BASHMATIC_HOME="${HOME}/.bashmatic"
[[ -d ${BASHMATIC_HOME} ]] || bash -c "$(curl -fsSL https://bashmatic.re1.re); bashmatic-install -q"

# shellcheck disable=SC1090
source "${BASHMATIC_HOME}/init.sh" 1>/dev/null 2>&1

export BAZEL_OPTS="--max_idle_secs=10800 --noshutdown_on_low_sys_mem --connect_timeout_secs=30"

main() {
  h1 "Cleaning Bazel directories... Please wait."
  # shellcheck disable=SC2207
  local -a du_before=($(du -hs .))
  run "bazel clean --expunge"
  # shellcheck disable=SC2207
  local -a examples=($(ls -1 examples))
  for dir in "${examples[@]}"; do
    run "cd examples/${dir}"
    run "bazel clean --expunge"
    run "cd -"
  done

  # shellcheck disable=SC2207
  local -a du_after=($(du -hs .))
  h2 "Space before cleaning: ${bldylw}${du_before[0]}" \
     "Space after  cleaning: ${bldgrn}${du_after[0]}"
}

main "$@"
