#!/usr/bin/env bash
# Read-only baseline download monitor status.
set -u -o pipefail

project_root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
log_dir="$project_root/outputs/logs"
pid_file="$log_dir/ecmwf_baseline_monitor.pid"

printf 'Monitor process:\n'
if [[ -f "$pid_file" ]]; then
  pid=$(cat "$pid_file")
  if [[ "$pid" =~ ^[1-9][0-9]*$ ]]; then
    ps -o pid,ppid,pgid,sid,etime,stat,args -p "$pid" || true
  else
    printf 'invalid PID file: %s\n' "$pid"
  fi
else
  printf 'PID file absent\n'
fi

printf '\nRecent monitor log:\n'
tail -20 "$log_dir/ecmwf_baseline_monitor.log" 2>/dev/null || true

printf '\nDeferred damaged-tape gaps:\n'
if [[ -f "$project_root/config/ecmwf_known_damaged_tape_gaps.tsv" ]]; then
  cat "$project_root/config/ecmwf_known_damaged_tape_gaps.tsv"
fi
if [[ -f "$log_dir/ecmwf_baseline_deferred_gaps.tsv" ]]; then
  cat "$log_dir/ecmwf_baseline_deferred_gaps.tsv"
elif [[ ! -f "$project_root/config/ecmwf_known_damaged_tape_gaps.tsv" ]]; then
  printf 'none recorded\n'
fi
