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

SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
TARGET=${DEPLOY_TARGET:-daniel@10.0.0.33}
SSH_KEY=${DEPLOY_SSH_KEY:-$HOME/.ssh/harbor_deploy}
ssh_opts=(-i "$SSH_KEY" -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes)

if [[ "${1:-}" != "--yes" ]]; then
  printf '%s\n' "usage: reset-data.sh --yes" >&2
  printf '%s\n' "this permanently deletes all Mongo, Redis, and MinIO data on $TARGET" >&2
  exit 1
fi

printf '%s\n' "uploading reset-data.sh"
rsync -az -e "ssh -i $SSH_KEY -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes" \
  "$SCRIPT_DIR/remote/reset-data.sh" "$TARGET:/tmp/harbor-reset-data.sh"

printf '%s\n' "The remote host may request the sudo password once."
ssh -t "${ssh_opts[@]}" "$TARGET" \
  "sudo install -o root -g root -m 0755 /tmp/harbor-reset-data.sh /opt/harbor/deploy/remote/reset-data.sh && rm -f /tmp/harbor-reset-data.sh && sudo /opt/harbor/deploy/remote/reset-data.sh --yes"
