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

usage() {
  cat <<'EOF'
Usage:
  ./scripts/cir/fetch_cir_sequence_from_remote.sh \
    --ssh-target <user@host> \
    --remote-path <path> \
    --local-path <path>
EOF
}

SSH_TARGET=""
REMOTE_PATH=""
LOCAL_PATH=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --ssh-target) SSH_TARGET="$2"; shift 2 ;;
    --remote-path) REMOTE_PATH="$2"; shift 2 ;;
    --local-path) LOCAL_PATH="$2"; shift 2 ;;
    -h|--help) usage; exit 0 ;;
    *) echo "unknown argument: $1" >&2; usage; exit 2 ;;
  esac
done

[[ -n "$SSH_TARGET" && -n "$REMOTE_PATH" && -n "$LOCAL_PATH" ]] || { usage >&2; exit 2; }

mkdir -p "$(dirname "$LOCAL_PATH")"
rsync -a "$SSH_TARGET:$REMOTE_PATH/" "$LOCAL_PATH/"
echo "done: $LOCAL_PATH"
