#!/usr/bin/env bash
# Sync microbench/fresh_setup/ to /home/admin/cpfs/wjh/agentic-kv-fresh/scripts/
# so dash1 / dash2 see the same scripts. cpfs is mounted at the same path on
# both, so one rsync from any host with cpfs access is enough.
#
# Run from the agentic-kv repo root (this directory contains microbench/).

set -eo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SRC="${SCRIPT_DIR}/"
DEST_HOST="${1:-dash1}"
DEST="/home/admin/cpfs/wjh/agentic-kv-fresh/scripts/"

REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
REPLAYER_SRC="${REPO_ROOT}/replayer"
REPLAYER_DEST="/home/admin/cpfs/wjh/agentic-kv-fresh/replayer/"

echo "[deploy] syncing ${SRC} -> ${DEST_HOST}:${DEST}"
ssh "${DEST_HOST}" "mkdir -p ${DEST} ${REPLAYER_DEST}"
# dash1/2 don't have rsync; use tar over ssh.
tar -C "${SCRIPT_DIR}" --exclude='__pycache__' --exclude='*.pyc' \
        --exclude='deploy.sh' -czf - . \
    | ssh "${DEST_HOST}" "cd ${DEST} && tar -xzf -"

if [ -d "${REPLAYER_SRC}" ]; then
    echo "[deploy] syncing ${REPLAYER_SRC}/ -> ${DEST_HOST}:${REPLAYER_DEST}"
    tar -C "${REPLAYER_SRC}" --exclude='__pycache__' --exclude='*.pyc' -czf - . \
        | ssh "${DEST_HOST}" "cd ${REPLAYER_DEST} && tar -xzf -"
fi

echo "[deploy] done"
ssh "${DEST_HOST}" "ls -la ${DEST}; echo '---replayer---'; ls -la ${REPLAYER_DEST}"
