#!/bin/bash

# Sacred 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

# Install NTFS support if missing
echo "📦 Ensuring NTFS-3G drivers are blessed by the Omnissiah..."
apt update -qq
apt install -y ntfs-3g

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

# Create mount directory
mkdir -p /mnt/omnissiah-vault

# Get dynamic user ID/GID for the aetos user
AETOS_UID=$(id -u aetos)
AETOS_GID=$(id -g aetos)

# Assign ownership
chown $AETOS_UID:$AETOS_GID /mnt/omnissiah-vault

echo "✅ Sacred directory created:"
echo "   🗂️  /mnt/omnissiah-vault (the great data hoard)"
echo

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

# Backup fstab
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..."

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

# Your UUID goes here — update if needed
VAULT_UUID="C678E33A78E3283F"

# Check existing entries
if grep -q "omnissiah-vault" /etc/fstab; then
    echo "⚠️  WARNING: Sacred bindings already detected in fstab!"
    echo "   Skipping fstab modification to prevent corruption..."
else
    cat >> /etc/fstab << EOF

# Sacred Drive Binding - Blessed by the Omnissiah
# Omnissiah's Vault - Mass Storage for Movies & Media
UUID=${VAULT_UUID} /mnt/omnissiah-vault ntfs-3g defaults,nofail,uid=${AETOS_UID},gid=${AETOS_GID},umask=022,x-systemd.automount,x-systemd.device-timeout=30s 0 0

EOF

    echo "✅ Sacred binding inscribed successfully!"
fi

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

# Reload systemd to acknowledge the sacred changes
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 resists binding!"
    echo "   Consult the sacred logs for heretical errors..."
    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 /mnt/omnissiah-vault; then
    mkdir -p /mnt/omnissiah-vault/{movies,tv-shows,documentaries,archives,media,downloads,configs}
    chown -R $AETOS_UID:$AETOS_GID /mnt/omnissiah-vault
    echo "✅ Omnissiah's Vault directory structure blessed"
else
    echo "❌ ERROR: Omnissiah's Vault is not mounted. Cannot bless structure."
fi

echo
echo "=========================================="
echo "     SACRED RITUAL COMPLETED!            "
echo "=========================================="
echo "🔱 The Machine Spirit is bound!"
echo "🗂️  Vault Path: /mnt/omnissiah-vault"
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"
