#!/bin/bash
# Linux mdadm RAID 0 Deployment Script
# Target: 4+ NVMe disks, Stripe size: 64KB, Expected: 28 GB/s read

set -e

echo "=== mdadm RAID 0 Deployment Script ==="
echo "Target: 4 NVMe disks in RAID 0"
echo "Stripe size: 64KB (optimal for media files)"
echo ""

# Check root privileges
if [ "$EUID" -ne 0 ]; then
    echo "Error: This script requires root privileges"
    echo "Run with: sudo bash $0"
    exit 1
fi

# Step 1: Install mdadm
echo "=== Step 1: Install mdadm ==="
apt-get update -qq
apt-get install -y mdadm fio sysstat nvme-cli

echo "✅ mdadm installed"
echo ""

# Step 2: Check NVMe disks
echo "=== Step 2: Check NVMe disks ==="
echo "Listing all NVMe devices:"
nvme list

echo ""
echo "Checking disk health:"
for disk in /dev/nvme*n1; do
    echo "Device: $disk"
    nvme smart-log $disk | grep -E "(temperature|available_spare|percentage_used)"
    echo ""
done

echo ""
echo "⚠️  WARNING: All data on these disks will be DESTROYED!"
echo "Press Ctrl+C to cancel, or Enter to continue..."
read -r

# Step 3: Wipe disk metadata
echo "=== Step 3: Wipe disk metadata ==="
DISKS=$(ls /dev/nvme*n1 | head -n 4)
echo "Target disks: $DISKS"

for disk in $DISKS; do
    echo "Wiping $disk..."
    wipefs -a $disk
    mdadm --zero-superblock $disk
    echo "✅ $disk wiped"
done

echo ""

# Step 4: Create RAID 0 array
echo "=== Step 4: Create RAID 0 array ==="
echo "RAID parameters:"
echo "  Level: 0 (stripe)"
echo "  Stripe size: 64KB (optimal for 4K video editing)"
echo "  Disks: 4 NVMe SSDs"
echo ""

# Create RAID 0 with 64KB stripe size
mdadm --create /dev/md0 \
    --level=0 \
    --raid-devices=4 \
    --chunk=64 \
    $DISKS

echo "✅ RAID 0 array created: /dev/md0"
echo ""

# Step 5: Check RAID status
echo "=== Step 5: Check RAID status ==="
mdadm --detail /dev/md0

echo ""
echo "RAID configuration:"
cat /proc/mdstat

echo ""

# Step 6: Create filesystem
echo "=== Step 6: Create filesystem ==="
echo "Creating XFS filesystem (optimized for large files)..."

mkfs.xfs -f \
    -d su=64k,sw=4 \
    -L "raid0_media" \
    /dev/md0

echo "✅ XFS filesystem created"
echo ""

# Step 7: Mount RAID array
echo "=== Step 7: Mount RAID array ==="
mkdir -p /mnt/raid0_media

mount -t xfs \
    -o noatime,nodiratime,largeio,inode64 \
    /dev/md0 /mnt/raid0_media

echo "✅ RAID 0 mounted at /mnt/raid0_media"
echo ""

# Step 8: Save RAID configuration
echo "=== Step 8: Save RAID configuration ==="
mdadm --detail --scan >> /etc/mdadm/mdadm.conf
update-initramfs -u -k all

echo "✅ RAID configuration saved to /etc/mdadm/mdadm.conf"
echo "   (Will auto-start on reboot)"
echo ""

# Step 9: Add to fstab (optional)
echo "=== Step 9: Add to fstab ==="
UUID=$(blkid /dev/md0 | grep -o 'UUID="[a-f0-9-]*"' | cut -d'"' -f2)
echo "UUID: $UUID"

echo "Add this line to /etc/fstab for auto-mount:"
echo "UUID=$UUID /mnt/raid0_media xfs noatime,nodiratime,largeio,inode64 0 0"
echo ""

# Step 10: Performance test
echo "=== Step 10: Performance test ==="
echo "Running fio benchmark..."
echo ""

# Sequential read test
echo "Test 1: Sequential read (1GB file, 4K block)"
fio --name=seq_read \
    --filename=/mnt/raid0_media/test_1g.dat \
    --size=1G \
    --bs=4k \
    --rw=read \
    --direct=1 \
    --numjobs=1 \
    --group_reporting

echo ""

# Sequential write test
echo "Test 2: Sequential write (1GB file, 64K block)"
fio --name=seq_write \
    --filename=/mnt/raid0_media/test_1g_write.dat \
    --size=1G \
    --bs=64k \
    --rw=write \
    --direct=1 \
    --numjobs=1 \
    --group_reporting

echo ""

# Random IOPS test
echo "Test 3: Random IOPS (4K blocks)"
fio --name=rand_iops \
    --filename=/mnt/raid0_media/test_rand.dat \
    --size=1G \
    --bs=4k \
    --rw=randrw \
    --rwmixread=70 \
    --direct=1 \
    --numjobs=4 \
    --group_reporting

echo ""

echo "=== Deployment Complete ==="
echo ""
echo "RAID 0 Summary:"
echo "  Array: /dev/md0"
echo "  Mount: /mnt/raid0_media"
echo "  Stripe size: 64KB"
echo "  Total disks: 4"
echo "  Expected read: 28 GB/s (7000 × 4)"
echo "  Expected write: 20 GB/s (5000 × 4)"
echo ""
echo "⚠️  RAID 0 Warning:"
echo "  - No redundancy (single disk failure = total data loss)"
echo "  - Use for scratch space, cache, or non-critical data"
echo "  - Consider RAID 10 or RAID 5 for critical data"
echo ""
echo "Next steps:"
echo "  1. Copy test files to /mnt/raid0_media"
echo "  2. Run AJA System Test or dd benchmarks"
echo "  3. Configure iSCSI/NFS export for remote access"
echo "  4. Integrate with MarkBase WebDAV"
echo ""