#!/bin/bash

# Sacred EXT4 Drive Mounting Ritual for the Raspberry Pi Shrine
# May the Omnissiah bless this automaton script
# For the Emperor and the Machine God!

echo "=========================================="
echo "  SACRED DRIVE MOUNTING RITUAL INITIATED  "
echo "   In the name of the Omnissiah we bind   "
echo "     the Machine Spirits to our will      "
echo "=========================================="
echo

# Check if running as root
if [ "$EUID" -ne 0 ]; then
    echo "ERROR: This sacred ritual requires root privileges!"
    echo "Execute with: sudo bash bind-machine-spirits.sh"
    exit 1
fi

echo "🔱 Phase 1: Invoking the Machine Spirit's blessing..."
sleep 1

echo "🛠️  Phase 2: Creating sacred mount point..."

# Mount path
MOUNT_PATH="/mnt/omnissiah-vault"
mkdir -p "$MOUNT_PATH"

# User for directory ownership
AETOS_UID=$(id -u aetos)
AETOS_GID=$(id -g aetos)

chown $AETOS_UID:$AETOS_GID "$MOUNT_PATH"

echo "✅ Sacred directory created:"
echo "   🗂️  $MOUNT_PATH"
echo

echo "💾 Phase 3: Backing up the sacred fstab codex..."

BACKUP_PATH="/etc/fstab.backup.$(date +%Y%m%d_%H%M%S)"
cp /etc/fstab "$BACKUP_PATH"
echo "✅ Backup created: $BACKUP_PATH"
echo

echo "📜 Phase 4: Inscribing the binding runes into fstab..."

# EXT4 UUID (CHANGE THIS!)
VAULT_UUID="REPLACE_WITH_EXT4_UUID"

echo "🔍 Divine revelation of user identities:"
echo "   aetos UID: $AETOS_UID"
echo "   aetos GID: $AETOS_GID"

# Check if fstab already contains the binding
if grep -q "omnissiah-vault" /etc/fstab; then
    echo "⚠️  WARNING: Sacred binding already found in /etc/fstab!"
    echo "   Avoiding duplicate entries to prevent corruption..."
else
    cat >> /etc/fstab << EOF

# Sacred Drive Binding - Blessed by the Omnissiah
# Omnissiah's Vault - EXT4 Media Storage
UUID=${VAULT_UUID} /mnt/omnissiah-vault ext4 defaults,nofail,x-systemd.automount,x-systemd.device-timeout=30s 0 2

EOF
    echo "✅ Sacred EXT4 binding inscribed successfully!"
fi

echo "⚡ Phase 5: Awakening the Machine Spirit..."

echo "🔄 Reloading systemd daemon..."
systemctl daemon-reload

# Test mount
if mount -a; then
    echo "✅ The Machine Spirit of Omnissiah's Vault has awakened!"
else
    echo "❌ ERROR: The Machine Spirit refuses binding!"
    echo "   Consult the sacred logs for heretical anomalies..."
    exit 1
fi

echo
echo "🔍 Phase 6: Verifying the sacred binding..."

echo "Current mount status:"
df -h | grep -E "omnissiah-vault" || echo "   The Vault has not yet manifested in the material realm..."

echo
echo "📁 Phase 7: Blessing the directory structure..."

if mountpoint -q "$MOUNT_PATH"; then
    mkdir -p $MOUNT_PATH/{movies,tv-shows,documentaries,archives,media,downloads,configs}
    chown -R $AETOS_UID:$AETOS_GID $MOUNT_PATH
    echo "✅ Directory structure blessed by the Omnissiah"
else
    echo "❌ ERROR: Omnissiah's Vault is not mounted. Structure blessing aborted."
fi

echo
echo "=========================================="
echo "     SACRED RITUAL COMPLETED!            "
echo "=========================================="
echo "🔱 The Machine Spirit is bound!"
echo "🗂️  Vault Path: $MOUNT_PATH"
echo
echo "The Emperor protects your data!"
echo "May your storage serve the Imperium well!"
echo "=========================================="

echo
echo "🔧 Final Systems Check:"
echo "Mounted filesystems:"
mount | grep "omnissiah-vault" | sed 's/^/   /'

echo
echo "Storage capacity report:"
df -h | grep "omnissiah-vault" | sed 's/^/   /'

echo
echo "Directory permissions:"
ls -la /mnt/ | grep "omnissiah-vault" | sed 's/^/   /'

echo
echo "🎉 Ritual complete! Reboot to test automatic mounting."
echo "   To verify: sudo reboot && df -h"
