#!/usr/bin/env bash
# Sync provisioning/ to /root/provisioning on Chacha and Disco.
#
#   ./deploy.sh                                    # both hosts
#   HOSTS=chacha.hevs.ch ./deploy.sh                # just one
#   SSH_USER=jean.machin ./deploy.sh                # non-default login
#   PRUNE_LEGACY=1 ./deploy.sh                      # also see below
#
# SSH access to Chacha/Disco is a personal account with sudo, not root, so
# rsync can't write to /root directly. This stages the files under the ssh
# user's home first, then uses `sudo cp` on the remote host to place them
# into /root/provisioning.
#
# Additive only, like provision.py's default mode: files removed locally are
# not removed from /root/provisioning. Prune stale files by hand if needed,
# or see PRUNE_LEGACY below for the one-time flat-layout cleanup.
#
# Also ensures root's crontab still calls rm_vscode.sh, now at its
# provisioning/ path rather than flat in /root — that job runs hourly and
# would otherwise go dark the day someone deletes the pre-migration flat
# copy (see PRUNE_LEGACY).
set -euo pipefail

HOSTS="${HOSTS:-chacha.hevs.ch disco.hevs.ch}"
SSH_USER="${SSH_USER:-$USER}"
REMOTE_STAGE=".cache/provisioning-deploy"
REMOTE_TARGET="/root/provisioning"
CRON_LINE="5 * * * * $REMOTE_TARGET/nodes/scripts/rm_vscode.sh >/dev/null 2>&1"

# Scripts that used to live flat in /root and are now versioned under
# provisioning/nodes/scripts/, deployed above — safe to delete from /root
# once this script has copied their replacement into place. Set
# PRUNE_LEGACY=1 to remove them after deploying. Deliberately NOT included:
# dance_config.cfg, slurm_disco.conf, users_quota.list, fastfetch.tar.gz —
# none of those have a versioned replacement (see ops/scripts.md), deleting
# them would lose live-only data.
LEGACY_FLAT_SCRIPTS=(
    add_group.sh add_quota.sh backup_slurm_data.sh
    check_apptainer_instances.sh check_vscode.sh install_fastfetch.sh
    migrate_apptainer_caches.sh remove_sshuser.sh rm_vscode.sh
    add_sshuser.sh add_cifs_mount.sh
)

HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

for host in $HOSTS; do
    dest="$SSH_USER@$host"

    echo ">>> $host: staging under ~/$REMOTE_STAGE"
    ssh "$dest" "mkdir -p $REMOTE_STAGE"
    rsync -az --delete \
        --exclude '.venv/' --exclude '__pycache__/' --exclude '.git/' \
        "$HERE/" "$dest:$REMOTE_STAGE/"

    echo ">>> $host: sudo cp into $REMOTE_TARGET"
    ssh -t "$dest" "sudo mkdir -p $REMOTE_TARGET && sudo cp -a $REMOTE_STAGE/. $REMOTE_TARGET/"

    echo ">>> $host: ensuring rm_vscode.sh cron entry points at $REMOTE_TARGET"
    ssh -t "$dest" "sudo crontab -u root -l 2>/dev/null | grep -qxF '$CRON_LINE' || { sudo crontab -u root -l 2>/dev/null; echo '$CRON_LINE'; } | sudo crontab -u root -"

    if [[ "${PRUNE_LEGACY:-0}" == 1 ]]; then
        echo ">>> $host: removing legacy flat /root/*.sh scripts"
        legacy_paths=()
        for f in "${LEGACY_FLAT_SCRIPTS[@]}"; do
            legacy_paths+=("/root/$f")
        done
        ssh -t "$dest" "sudo rm -fv -- ${legacy_paths[*]}"
    fi
done
