#!/bin/bash
# Format 6x 250GB SSDs and check if Proxmox recognizes them
# Run on R630-01 as root

set -e

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

DRIVES=("sdc" "sdd" "sde" "sdf" "sdg" "sdh")

echo "=========================================="
echo "Format 250GB SSDs and Check Proxmox Recognition"
echo "=========================================="
echo ""

# Check if running as root
if [ "$EUID" -ne 0 ]; then 
    echo -e "${RED}ERROR: Please run as root${NC}"
    exit 1
fi

echo -e "${BLUE}=== Step 1: Current Drive Status ===${NC}"
echo "-----------------------------------"
echo "Checking current status of 250GB drives:"
for drive in "${DRIVES[@]}"; do
    if [ -b "/dev/$drive" ]; then
        size=$(fdisk -l "/dev/$drive" 2>/dev/null | grep "^Disk /dev/$drive" | awk '{print $3, $4}')
        echo "  /dev/$drive: $size"
        # Check if has partitions
        partitions=$(lsblk -n /dev/$drive 2>/dev/null | grep -c "part" || echo "0")
        if [ "$partitions" -gt 0 ]; then
            echo -e "    ${YELLOW}Has $partitions partition(s)${NC}"
        else
            echo -e "    ${GREEN}No partitions (clean)${NC}"
        fi
    else
        echo -e "  ${YELLOW}/dev/$drive not found${NC}"
    fi
done
echo ""

echo -e "${BLUE}=== Step 2: Format Drives (Create Partition Table) ===${NC}"
echo "-----------------------------------"
echo -e "${YELLOW}This will create a new partition table on each drive${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}  Formatting /dev/$drive...${NC}"
    
    # Unmount any partitions
    umount /dev/${drive}* 2>/dev/null || true
    
    # Create new GPT partition table
    echo "    Creating GPT partition table..."
    parted -s "/dev/$drive" mklabel gpt 2>&1 || echo "    (Partition table may already be GPT)"
    
    # Optionally create a single partition (we'll leave it unformatted for now)
    # This makes the drive visible but doesn't format it with a filesystem
    echo "    Creating single partition..."
    parted -s "/dev/$drive" mkpart primary 0% 100% 2>&1 || echo "    (Partition may already exist)"
    
    # Re-read partition table
    partprobe "/dev/$drive" 2>/dev/null || true
    
    echo -e "${GREEN}    ✓ /dev/$drive formatted${NC}"
    echo ""
done

echo -e "${BLUE}=== Step 3: Check Block Devices ===${NC}"
echo "-----------------------------------"
echo "All block devices:"
lsblk -o NAME,SIZE,TYPE,MOUNTPOINT,FSTYPE,MODEL | grep -E "NAME|sd"
echo ""

echo -e "${BLUE}=== Step 4: Check Proxmox Storage ===${NC}"
echo "-----------------------------------"
echo "Proxmox storage status:"
if command -v pvesm &> /dev/null; then
    pvesm status 2>/dev/null || echo -e "${YELLOW}pvesm command failed${NC}"
else
    echo -e "${YELLOW}pvesm command not found${NC}"
fi
echo ""

echo -e "${BLUE}=== Step 5: Check Available Disks in Proxmox ===${NC}"
echo "-----------------------------------"
echo "Checking if Proxmox can see the drives..."

# Check via pvesm
if command -v pvesm &> /dev/null; then
    echo "Available storage:"
    pvesm status 2>/dev/null | grep -E "Name|Type|Status" || true
fi

# Check via lsblk (what Proxmox would see)
echo ""
echo "Drives visible to system:"
for drive in "${DRIVES[@]}"; do
    if [ -b "/dev/$drive" ]; then
        info=$(lsblk -n -o SIZE,TYPE,MOUNTPOINT,FSTYPE /dev/$drive 2>/dev/null | head -1)
        echo "  /dev/$drive: $info"
    fi
done
echo ""

echo -e "${BLUE}=== Step 6: Check via Proxmox API (if accessible) ===${NC}"
echo "-----------------------------------"
echo "Note: To check via Proxmox web UI:"
echo "  1. Go to Datacenter > Storage"
echo "  2. Click 'Add' > 'Disk' or 'Directory'"
echo "  3. Check if drives are listed"
echo ""

echo "=========================================="
echo -e "${GREEN}Formatting Complete!${NC}"
echo "=========================================="
echo ""
echo "Summary:"
echo "--------"
echo "All 6 drives have been formatted with GPT partition tables"
echo ""
echo "To check in Proxmox Web UI:"
echo "  1. Login to Proxmox: https://192.168.11.11:8006"
echo "  2. Go to: Datacenter > Storage"
echo "  3. Click 'Add' to see available disks"
echo ""
echo "To check via command line:"
echo "  pvesm status"
echo "  lsblk"
echo ""

