#!/usr/bin/env bash
# Clean ml110 - Remove old files before syncing
# This is a separate script for safety

set -euo pipefail

REMOTE_HOST="192.168.11.10"
REMOTE_USER="root"
REMOTE_BASE="/opt"

log_info() { echo "[INFO] $1"; }
log_warn() { echo "[WARN] $1"; }
log_error() { echo "[ERROR] $1"; }

log_warn "=== WARNING: This will DELETE all files in:"
log_warn "  - ${REMOTE_BASE}/smom-dbis-138-proxmox"
log_warn "  - ${REMOTE_BASE}/smom-dbis-138 (config and keys will be preserved if you run sync script)"
log_warn ""
read -p "Are you sure you want to delete these directories? (type 'DELETE' to confirm): " confirm

if [[ "$confirm" != "DELETE" ]]; then
    log_info "Aborted by user"
    exit 0
fi

# Create backup first
BACKUP_DIR="/opt/backup-$(date +%Y%m%d-%H%M%S)"
log_info "Creating backup at ${BACKUP_DIR}..."
sshpass -p 'L@kers2010' ssh -o StrictHostKeyChecking=no "${REMOTE_USER}@${REMOTE_HOST}" \
    "mkdir -p ${BACKUP_DIR} && \
     (test -d ${REMOTE_BASE}/smom-dbis-138 && cp -r ${REMOTE_BASE}/smom-dbis-138 ${BACKUP_DIR}/ || true) && \
     (test -d ${REMOTE_BASE}/smom-dbis-138-proxmox && cp -r ${REMOTE_BASE}/smom-dbis-138-proxmox ${BACKUP_DIR}/ || true) && \
     echo 'Backup created'"

log_info "Removing old directories..."
sshpass -p 'L@kers2010' ssh -o StrictHostKeyChecking=no "${REMOTE_USER}@${REMOTE_HOST}" \
    "rm -rf ${REMOTE_BASE}/smom-dbis-138-proxmox && \
     echo '✓ Removed smom-dbis-138-proxmox'"

log_info "✓ Cleanup complete"
log_info "Backup saved at: ${BACKUP_DIR}"

