#!/usr/bin/env bash
# Print a command for the user's Windows machine to pull a path over Tailscale SSH.
set -euo pipefail
DELIVERY_HOST="vasily@msi.tail0b014a.ts.net"
if [[ $# -ne 1 ]]; then
  echo "usage: t3-deliver <file-or-directory>" >&2
  exit 1
fi
if [[ ! -e "$1" ]]; then
  echo "t3-deliver: no such path: $1" >&2
  exit 1
fi
DELIVERY_PATH="$(realpath -- "$1")"
# Reject characters that cannot be shared safely between cmd and PowerShell.
# OpenSSH SFTP mode handles the quoted remote path without a remote shell.
if [[ "$DELIVERY_PATH" == *[\"\$\`\%\!]* || "$DELIVERY_PATH" == *$'\n'* || "$DELIVERY_PATH" == *$'\r'* ]]; then
  echo 't3-deliver: rename or archive this path with a simple name before delivery' >&2
  exit 1
fi
DELIVERY_FLAGS=""
if [[ -d "$DELIVERY_PATH" ]]; then
  DELIVERY_FLAGS="-r "
fi
printf 'Paste in cmd or PowerShell on your Windows machine (OpenSSH scp with SFTP support):\n\n'
printf 'scp %s"%s:%s" .\n' "$DELIVERY_FLAGS" "$DELIVERY_HOST" "$DELIVERY_PATH"
