#!/bin/bash
set -e

# 0. Verify username
echo "=== Setting up DivineAcademix Kiosk for user: $(whoami) ==="
if [ "$(whoami)" != "rchs1911" ]; then
  echo "⚠️ Warning: Current user is $(whoami), not rchs1911. Proceeding with $(whoami)..."
fi

# 1. Install System Prerequisites (FUSE 2 for AppImage + Udev tools)
echo "--- 1. Installing Debian 13 AppImage Prerequisites ---"
sudo apt update
sudo apt install -y libfuse2t64 || sudo apt install -y libfuse2

# 2. Create Required Directories
echo "--- 2. Creating Application & Autostart Folders ---"
mkdir -p ~/Applications
mkdir -p ~/Documents
mkdir -p ~/.config/autostart

# 3. Copy AppImage to ~/Applications
echo "--- 3. Setting up DivineAcademix AppImage ---"
# Checks Downloads or USB drive
if ls ~/Downloads/*.AppImage 1> /dev/null 2>&1; then
  cp ~/Downloads/*.AppImage ~/Applications/DivineAcademix.AppImage
elif ls /media/rchs1911/*/*.AppImage 1> /dev/null 2>&1; then
  cp /media/rchs1911/*/*.AppImage ~/Applications/DivineAcademix.AppImage
else
  echo "⚠️ No AppImage found in ~/Downloads or USB. Please ensure DivineAcademix.AppImage is in ~/Applications/"
fi

# 4. Make AppImage Executable
chmod +x ~/Applications/DivineAcademix.AppImage

# 5. Configure Autostart at Boot
echo "--- 4. Creating Desktop Autostart Entry ---"
cat > ~/.config/autostart/divine.desktop << 'EOF'
[Desktop Entry]
Type=Application
Version=1.0
Name=DivineAcademix Kiosk
Comment=DivineAcademix RFID Attendance Kiosk
Exec=/home/rchs1911/Applications/DivineAcademix.AppImage --no-sandbox
StartupNotify=false
Terminal=false
X-GNOME-Autostart-enabled=true
EOF

chmod +x ~/.config/autostart/divine.desktop

# 6. Allow Passwordless Safe Poweroff (For Auto-Shutdown & MicroSD Protection)
echo "--- 5. Configuring Sudoers for Safe Poweroff ---"
echo "rchs1911 ALL=(ALL) NOPASSWD: /sbin/poweroff, /sbin/shutdown, /bin/systemctl" | sudo tee /etc/sudoers.d/010_rchs1911-poweroff
sudo chmod 0440 /etc/sudoers.d/010_rchs1911-poweroff
sudo visudo -c

# 7. Install USB RFID Reader Udev Rules (Prevents USB 3.0 / Hub reader lockup)
echo "--- 6. Installing USB RFID Port Rules ---"
sudo tee /etc/udev/rules.d/99-rfid-readers.rules << 'EOF'
# Grant full non-root access to USB RFID and HID readers
SUBSYSTEM=="input", GROUP="input", MODE="0666"
SUBSYSTEM=="usb", ATTRS{idVendor}!="", MODE="0666", GROUP="plugdev"
KERNEL=="event*", MODE="0666"
EOF

sudo udevadm control --reload-rules && sudo udevadm trigger
sudo usermod -a -G input,plugdev rchs1911

# 8. Disable Screen Blanking (Keeps display awake all day for students)
echo "--- 7. Disabling Display Sleep / Screen Blanking ---"
sudo raspi-config nonint do_blanking 1 2>/dev/null || true

echo "===================================================="
echo "✅ Setup Complete! DivineAcademix is ready for school."
echo "You can test launching now with:"
echo "  ~/Applications/DivineAcademix.AppImage --no-sandbox"
echo "Or simply reboot to verify automatic fullscreen startup:"
echo "  sudo reboot"
echo "===================================================="
