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

script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=scripts/cluster/common.sh
source "$script_dir/common.sh"
load_cluster_env

cluster_scp "$script_dir/prune-legacy-generated.sh" \
  "$CLUSTER_NINGBO_HOST:/usr/local/sbin/easyai-prune-legacy-generated"
cluster_scp "$cluster_root/deploy/systemd/easyai-legacy-generated-cleanup.service" \
  "$CLUSTER_NINGBO_HOST:/etc/systemd/system/easyai-legacy-generated-cleanup.service"
cluster_scp "$cluster_root/deploy/systemd/easyai-legacy-generated-cleanup.timer" \
  "$CLUSTER_NINGBO_HOST:/etc/systemd/system/easyai-legacy-generated-cleanup.timer"
cluster_ssh "$CLUSTER_NINGBO_HOST" 'bash -s' <<'REMOTE'
set -euo pipefail
chmod 0750 /usr/local/sbin/easyai-prune-legacy-generated
systemctl daemon-reload
/usr/local/sbin/easyai-prune-legacy-generated --dry-run
systemctl enable --now easyai-legacy-generated-cleanup.timer
systemctl is-active --quiet easyai-legacy-generated-cleanup.timer
REMOTE
echo 'legacy_cleanup_timer_install=PASS interval=hourly strict_age_seconds=86400'
