#!/bin/bash
# Helper script to run Ceph OSD setup on r630-01 remotely
# Usage: ./run-ceph-osd-setup-remote.sh [password] [--include-sdb]

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SETUP_SCRIPT="$SCRIPT_DIR/setup-ceph-osds-r630.sh"
PROXMOX_HOST="192.168.11.11"
PROXMOX_USER="root"

# Get password from argument or environment
if [ -n "$1" ] && [ "$1" != "--include-sdb" ]; then
    PASSWORD="$1"
    shift
elif [ -n "$PROXMOX_PASSWORD" ]; then
    PASSWORD="$PROXMOX_PASSWORD"
fi

# Check for --include-sdb flag
INCLUDE_SDB="false"
if [[ "$@" == *"--include-sdb"* ]]; then
    INCLUDE_SDB="true"
fi

# Check if sshpass is installed
if ! command -v sshpass &>/dev/null; then
    echo "Error: sshpass not installed. Install with: sudo apt-get install sshpass"
    exit 1
fi

if [ -z "$PASSWORD" ]; then
    echo "Usage: $0 <password> [--include-sdb]"
    echo "Or: PROXMOX_PASSWORD=yourpass $0 [--include-sdb]"
    exit 1
fi

echo "=========================================="
echo "Running Ceph OSD Setup on R630-01"
echo "=========================================="
echo ""

# Copy script to remote server
echo "Copying setup script to $PROXMOX_USER@$PROXMOX_HOST..."
sshpass -p "$PASSWORD" scp -o StrictHostKeyChecking=no "$SETUP_SCRIPT" "$PROXMOX_USER@$PROXMOX_HOST:/tmp/setup-ceph-osds-r630.sh" || {
    echo "Error: Failed to copy script"
    exit 1
}

# Make it executable
sshpass -p "$PASSWORD" ssh -o StrictHostKeyChecking=no "$PROXMOX_USER@$PROXMOX_HOST" "chmod +x /tmp/setup-ceph-osds-r630.sh"

# Run the script
echo ""
echo "Running setup script..."
echo ""

if [ "$INCLUDE_SDB" = "true" ]; then
    sshpass -p "$PASSWORD" ssh -o StrictHostKeyChecking=no "$PROXMOX_USER@$PROXMOX_HOST" "INCLUDE_SDB=true /tmp/setup-ceph-osds-r630.sh"
else
    sshpass -p "$PASSWORD" ssh -o StrictHostKeyChecking=no "$PROXMOX_USER@$PROXMOX_HOST" "/tmp/setup-ceph-osds-r630.sh"
fi

