#!/usr/bin/env nix-shell
#!nix-shell -i bash -p bash -p nettools -p openssh -p rsync -p sane-scripts.vpn

# secret should include RN_USER
source /run/secrets/rsync-net-env
RN_ID=/run/secrets/rsync-net-id_ed25519

rc=0
fail() {
  printf "%b\n" "$1" >&2
  rc=1
}

set -x
ssh -i "$RN_ID" "$RN_USER@$RN_USER.rsync.net" ls -al
set +x

for host in desko lappy moby servo; do
  dir=nix/persist/private
  now=$(date +'%s')
  last_attempted=$(ssh -i "$RN_ID" "$RN_USER@$RN_USER.rsync.net" -- cat "$host/$dir/zzz-rsync-net/last-attempted")
  last_completed=$(ssh -i "$RN_ID" "$RN_USER@$RN_USER.rsync.net" -- cat "$host/$dir/zzz-rsync-net/last-completed")
  age_attempted_s=$(( "$now" - "${last_attempted:-0}" ))
  age_completed_s=$(( "$now" - "${last_completed:-0}" ))
  age_attempted_hr=$(( "$age_attempted_s" / 3600 ))
  age_completed_hr=$(( "$age_completed_s" / 3600 ))
  if ! [[ "$age_completed_hr" -lt 120 ]]; then
    fail "$host wasn't backed up within the last 5 days\n  completed: $age_completed_hr hours ago\n  tried: $age_attempted_hr hours ago"
  elif ! [[ "$age_completed_hr" -gt -2 ]]; then
    fail "$host was backed up in the future??"
  else
    echo "$host last backed up $age_completed_hr hours ago"
    if [ "$age_completed_hr" -gt 25 ]; then
      echo "- last attempt was $age_attempted_hr hours ago"
      echo "- attempts are made every 12hr, and 'completion' tracking is pessimistic:"
      echo "- normal operation will have the last completion always between 12hr and 24hr old"
      echo "- check host's own view with:"
      echo "  ssh $host cat /nix/persist/private/zzz-rsync-net/last-completed | sed 's/^/@/' | date -f /dev/stdin"
    fi
  fi
done

exit $rc
