#!/bin/bash
# Interactive script to create third Ceph OSD from ubuntu-vg drive
# 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'

echo "=========================================="
echo "Create Third Ceph OSD from ubuntu-vg Drive"
echo "=========================================="
echo ""

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

# Available 250GB drives
AVAILABLE_DRIVES=("sdc" "sdd" "sde" "sdf" "sdg" "sdh")

echo -e "${BLUE}Available 250GB drives:${NC}"
for i in "${!AVAILABLE_DRIVES[@]}"; do
    drive="${AVAILABLE_DRIVES[$i]}"
    if [ -b "/dev/$drive" ]; then
        size=$(fdisk -l "/dev/$drive" 2>/dev/null | grep "^Disk /dev/$drive" | awk '{print $3, $4}')
        in_vg=$(pvs 2>/dev/null | grep "/dev/${drive}3" | grep ubuntu-vg | wc -l)
        if [ "$in_vg" -gt 0 ]; then
            echo -e "  $((i+1)). ${YELLOW}/dev/$drive: ${size} - In ubuntu-vg${NC}"
        else
            echo -e "  $((i+1)). ${GREEN}/dev/$drive: ${size} - Available${NC}"
        fi
    fi
done
echo ""

# Get drive selection
read -p "Enter drive number (1-6) or device name (e.g., sdc): " selection

# Parse selection
if [[ "$selection" =~ ^[1-6]$ ]]; then
    DRIVE="${AVAILABLE_DRIVES[$((selection-1))]}"
elif [[ "$selection" =~ ^sd[a-z]$ ]]; then
    DRIVE="$selection"
else
    echo -e "${RED}Invalid selection${NC}"
    exit 1
fi

if [ ! -b "/dev/$DRIVE" ]; then
    echo -e "${RED}Drive /dev/$DRIVE not found${NC}"
    exit 1
fi

echo ""
echo -e "${YELLOW}Selected drive: /dev/$DRIVE${NC}"
echo ""

# Check if in ubuntu-vg
in_vg=$(pvs 2>/dev/null | grep "/dev/${DRIVE}3" | grep ubuntu-vg | wc -l)

if [ "$in_vg" -gt 0 ]; then
    echo -e "${YELLOW}WARNING: /dev/${DRIVE}3 is in ubuntu-vg volume group${NC}"
    echo -e "${RED}Removing it will destroy data on that logical volume!${NC}"
    echo ""
    read -p "Continue? (yes/no): " confirm
    if [ "$confirm" != "yes" ]; then
        echo "Aborted"
        exit 0
    fi
    
    echo ""
    echo -e "${BLUE}Step 1: Removing from ubuntu-vg...${NC}"
    
    # 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
    
    # Remove physical volume
    echo "  Removing /dev/${DRIVE}3 from ubuntu-vg..."
    pvremove "/dev/${DRIVE}3" -y
    
    echo -e "${GREEN}  ✓ Removed from ubuntu-vg${NC}"
    echo ""
fi

# Wipe the drive
echo -e "${BLUE}Step 2: Wiping /dev/$DRIVE...${NC}"
echo "  Wiping filesystem signatures..."
wipefs -a "/dev/$DRIVE"
echo "  Zeroing first 100MB..."
dd if=/dev/zero of="/dev/$DRIVE" bs=1M count=100 status=progress
echo -e "${GREEN}  ✓ Drive wiped${NC}"
echo ""

# Create Ceph OSD
echo -e "${BLUE}Step 3: Creating Ceph OSD on /dev/$DRIVE...${NC}"
ceph-volume lvm create --data "/dev/$DRIVE"
echo ""

# Verify
echo -e "${BLUE}Step 4: Verifying OSD creation...${NC}"
echo "OSD Tree:"
ceph osd tree
echo ""
echo "Ceph Health:"
ceph health
echo ""
echo "OSD Details:"
ceph osd df
echo ""

echo "=========================================="
echo -e "${GREEN}Third OSD Created Successfully!${NC}"
echo "=========================================="
echo ""
echo "You should now have 3 OSDs, which should fix the TOO_FEW_OSDS error."
echo "Check 'ceph health detail' for remaining issues."
echo ""

