#!/usr/bin/env bash
set -euo pipefail

MOSAIC_HOME="${MOSAIC_HOME:-$HOME/.config/mosaic}"
sync_cmd="$MOSAIC_HOME/tools/_scripts/mosaic-orchestrator-sync-tasks"
run_cmd="$MOSAIC_HOME/tools/_scripts/mosaic-orchestrator-run"

do_sync=1
poll_sec=15
extra_args=()

while [[ $# -gt 0 ]]; do
  case "$1" in
    --no-sync)
      do_sync=0
      shift
      ;;
    --poll-sec)
      poll_sec="${2:-15}"
      shift 2
      ;;
    *)
      extra_args+=("$1")
      shift
      ;;
  esac
done

if [[ $do_sync -eq 1 ]]; then
  "$sync_cmd" --apply
fi

exec "$run_cmd" --until-drained --poll-sec "$poll_sec" "${extra_args[@]}"
