#!/bin/bash
# Script to wipe all 6x 250GB drives (sdc, sdd, sde, sdf, sdg, sdh)
# Run on R630-01 as root
# WARNING: This will destroy all data on these drives!

set -e

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# Drives to wipe
DRIVES=("sdc" "sdd" "sde" "sdf" "sdg" "sdh")

echo "=========================================="
echo "Wipe All 250GB Drives"
echo "=========================================="
echo ""
echo -e "${RED}WARNING: This will destroy ALL data on these drives!${NC}"
echo "Drives to wipe: ${DRIVES[@]}"
echo ""
read -p "Are you sure you want to continue? (type 'yes' to confirm): " confirm

if [ "$confirm" != "yes" ]; then
    echo "Aborted"
    exit 0
fi

echo ""
echo -e "${BLUE}=== Step 1: Removing from ubuntu-vg ===${NC}"
echo "-----------------------------------"

# Check if ubuntu-vg exists and has mounted volumes
if vgs ubuntu-vg &>/dev/null; then
    echo "Checking ubuntu-vg status..."
    
    # Unmount any mounted logical volumes
    echo "  Unmounting logical volumes..."
    umount /dev/ubuntu-vg/* 2>/dev/null || true
    
    # Deactivate volume group
    echo "  Deactivating ubuntu-vg..."
    vgchange -a n ubuntu-vg 2>/dev/null || true
    
    # Remove physical volumes from ubuntu-vg
    echo "  Removing physical volumes from ubuntu-vg..."
    for drive in "${DRIVES[@]}"; do
        if pvs 2>/dev/null | grep -q "/dev/${drive}3.*ubuntu-vg"; then
            echo "    Removing /dev/${drive}3 from ubuntu-vg..."
            pvremove "/dev/${drive}3" -y -ff 2>/dev/null || echo "    (Already removed or not in VG)"
        fi
    done
    
    echo -e "${GREEN}  ✓ Removed from ubuntu-vg${NC}"
else
    echo -e "${YELLOW}  ubuntu-vg not found or already removed${NC}"
fi
echo ""

echo -e "${BLUE}=== Step 2: Wiping All Drives ===${NC}"
echo "-----------------------------------"

for drive in "${DRIVES[@]}"; do
    if [ ! -b "/dev/$drive" ]; then
        echo -e "${YELLOW}  /dev/$drive not found, skipping...${NC}"
        continue
    fi
    
    echo -e "${BLUE}  Wiping /dev/$drive...${NC}"
    
    # Unmount any partitions
    umount /dev/${drive}* 2>/dev/null || true
    
    # Wipe filesystem signatures
    echo "    Removing filesystem signatures..."
    wipefs -a "/dev/$drive" 2>/dev/null || true
    
    # Zero out first 100MB
    echo "    Zeroing first 100MB..."
    dd if=/dev/zero of="/dev/$drive" bs=1M count=100 status=progress 2>/dev/null || true
    
    echo -e "${GREEN}    ✓ /dev/$drive wiped${NC}"
    echo ""
done

echo "=========================================="
echo -e "${GREEN}All Drives Wiped Successfully!${NC}"
echo "=========================================="
echo ""
echo "Next steps:"
echo "1. Create Ceph OSD on one drive (for third OSD):"
echo "   ceph-volume lvm create --data /dev/sdc"
echo ""
echo "2. Or create OSDs on multiple drives (for better performance):"
echo "   ceph-volume lvm create --data /dev/sdc"
echo "   ceph-volume lvm create --data /dev/sdd"
echo "   ceph-volume lvm create --data /dev/sde"
echo "   # ... etc"
echo ""
echo "3. Verify:"
echo "   ceph osd tree"
echo "   ceph health"
echo ""

