#!/bin/bash
# MarkBase + Linux RAID 0 Integration
# Export RAID 0 via iSCSI for macOS WebDAV integration

set -e

echo "=== MarkBase + Linux RAID 0 Integration ==="
echo ""

# Step 1: Install iSCSI Target
echo "=== Step 1: Install iSCSI Target (tgt) ==="
apt-get install -y tgt tgt-admin

echo "✅ tgt installed"
echo ""

# Step 2: Configure iSCSI Target
echo "=== Step 2: Configure iSCSI Target ==="

TARGET_NAME="iqn.2026-05.com.markbase:raid0.media"
LUN_PATH="/dev/md0"

cat > /etc/tgt/conf.d/markbase_raid0.conf << EOF
<target ${TARGET_NAME}>
    # RAID 0 backend device
    backing-store ${LUN_PATH}
    
    # LUN parameters
    lun 1
    
    # Access control (allow all for testing)
    initiator-address ALL
    
    # Performance tuning
    max-xfer-length 1048576
    queue-depth 128
    
    # Write cache (dangerous for RAID 0, but faster)
    write-cache enabled
</target>
EOF

echo "✅ iSCSI target configured: $TARGET_NAME"
echo "   Backend: $LUN_PATH (RAID 0)"
echo ""

# Step 3: Start tgt service
echo "=== Step 3: Start tgt service ==="
systemctl enable tgt
systemctl start tgt

echo "✅ tgt service started"
echo ""

# Step 4: Verify iSCSI target
echo "=== Step 4: Verify iSCSI target ==="
tgt-admin --show

echo ""

# Step 5: Generate macOS connection script
echo "=== Step 5: Generate macOS connection script ==="

SERVER_IP=$(hostname -I | awk '{print $1}')

cat > /tmp/macos_iscsi_connect.sh << 'EOF'
#!/bin/bash
# macOS iSCSI Initiator Script
# Connect to Linux RAID 0 via iSCSI

set -e

echo "=== macOS iSCSI Connection Script ==="
echo ""

# Check for macOS
if [[ "$(uname)" != "Darwin" ]]; then
    echo "Error: This script is for macOS only"
    exit 1
fi

# Install xtend SAN iSCSI Initiator (free alternative)
echo "Step 1: Install iSCSI Initiator"
echo "Recommended: xtend SAN iSCSI Initiator (free)"
echo "Download: https://www.xtend.com/products/iscsi-initiator"
echo ""

SERVER_IP="${SERVER_IP}"
TARGET_NAME="${TARGET_NAME}"

echo "Step 2: Discover iSCSI targets"
echo "Run in xtend SAN console:"
echo "  Discovery Portal: $SERVER_IP:3260"
echo "  Target: $TARGET_NAME"
echo ""

echo "Step 3: Connect to target"
echo "Expected result:"
echo "  New disk appears: /dev/diskX"
echo "  Mount point: /Volumes/MarkBase_RAID0"
echo ""

echo "Step 4: Format disk (optional)"
echo "  diskutil eraseDisk XFS MarkBase_RAID0 /dev/diskX"
echo ""

echo "Performance expectations:"
echo "  Network: 10GbE required for full speed"
echo "  Expected: 28 GB/s read (limited by network: 10GbE ≈ 1.25 GB/s)"
echo "  For full speed: Use local Linux mount instead"
echo ""
EOF

echo "✅ macOS connection script generated: /tmp/macos_iscsi_connect.sh"
echo "   Server IP: $SERVER_IP"
echo "   Target: $TARGET_NAME"
echo ""

# Step 6: Generate MarkBase WebDAV config
echo "=== Step 6: Generate MarkBase WebDAV config ==="

cat > /tmp/markbase_webdav_config.toml << EOF
[webdav]
backend = "raid0_linux"
mount_point = "/mnt/raid0_media"

[raid0]
device = "/dev/md0"
stripe_size = 64KB
total_disks = 4
total_size = 8TB

[performance]
expected_read = 28000  # MB/s
expected_write = 20000  # MB/s
expected_iops = 2400000

[integration]
sqlite_db = "data/users/warren.sqlite"
file_tree_sync = true
EOF

echo "✅ MarkBase WebDAV config generated"
echo ""

# Step 7: Network tuning
echo "=== Step 7: Network tuning (10GbE) ==="

# Check for 10GbE interface
echo "Checking network interfaces:"
ip link show

echo ""
echo "Recommended network config for 10GbE:"
cat > /tmp/10gbe_network_tuning.sh << 'EOF'
#!/bin/bash
# 10GbE Performance Tuning

# Increase TCP buffer sizes
sysctl -w net.core.rmem_max=134217728
sysctl -w net.core.wmem_max=134217728
sysctl -w net.core.rmem_default=33554432
sysctl -w net.core.wmem_default=33554432
sysctl -w net.ipv4.tcp_rmem='4096 87380 134217728'
sysctl -w net.ipv4.tcp_wmem='4096 65536 134217728'

# Increase max connections
sysctl -w net.core.somaxconn=1024
sysctl -w net.ipv4.tcp_max_syn_backlog=2048

# Enable jumbo frames (MTU 9000)
# Note: Requires switch support
# ip link set eth0 mtu 9000

echo "✅ Network tuning applied"
EOF

echo "✅ Network tuning script generated: /tmp/10gbe_network_tuning.sh"
echo ""

# Summary
echo "=== Integration Complete ==="
echo ""
echo "Linux RAID 0 Configuration:"
echo "  Device: /dev/md0"
echo "  Mount: /mnt/raid0_media"
echo "  iSCSI Target: $TARGET_NAME"
echo "  Server IP: $SERVER_IP"
echo "  Port: 3260"
echo ""
echo "macOS Connection:"
echo "  1. Install xtend SAN iSCSI Initiator"
echo "  2. Discover: $SERVER_IP:3260"
echo "  3. Connect: $TARGET_NAME"
echo "  4. Mount: /Volumes/MarkBase_RAID0"
echo ""
echo "MarkBase Integration:"
echo "  1. WebDAV backend: /mnt/raid0_media"
echo "  2. SQLite: warren.sqlite (12658 nodes)"
echo "  3. File tree sync: enabled"
echo ""
echo "⚠️  Performance Notes:"
echo "  - Local Linux mount: 28 GB/s (full speed)"
echo "  - iSCSI (10GbE): 1.25 GB/s (network limit)"
echo "  - iSCSI (25GbE): 3.125 GB/s (better)"
echo "  - For production: Use local mount or 25GbE+"
echo ""
echo "Next Steps:"
echo "  1. Copy media files to /mnt/raid0_media"
echo "  2. Run fio performance test"
echo "  3. Configure MarkBase WebDAV (cargo run -- display)"
echo "  4. Test file tree + RAID 0 integration"
echo ""