#!/bin/bash

echo "🍓 Adhaan Raspberry Pi Kiosk Setup"
echo "=================================="

# Update systeem
echo "📦 Systeem bijwerken..."
sudo apt update -y
sudo apt upgrade -y

# Installeer Docker
echo "🐳 Docker installeren..."
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo usermod -aG docker $USER
rm get-docker.sh

# Installeer Docker Compose
echo "🔧 Docker Compose installeren..."
sudo apt install -y docker-compose

# Installeer GUI componenten voor kiosk mode
echo "🖥️  GUI componenten installeren..."
sudo apt install -y --no-install-recommends \
    xserver-xorg \
    x11-xserver-utils \
    xinit \
    openbox \
    chromium-browser \
    unclutter

# Maak kiosk script
echo "📝 Kiosk script aanmaken..."
cat > /home/$USER/kiosk.sh << 'EOF'
#!/bin/bash
sleep 5
/usr/bin/chromium-browser \
    --kiosk \
    --start-maximized \
    --noerrdialogs \
    --disable-infobars \
    --disable-session-crashed-bubble \
    --disable-restore-session-state \
    --disable-background-timer-throttling \
    --disable-backgrounding-occluded-windows \
    --disable-renderer-backgrounding \
    --disable-features=TranslateUI \
    --disable-ipc-flooding-protection \
    --enable-features=OverlayScrollbar \
    --no-first-run \
    --fast \
    --fast-start \
    --disable-default-apps \
    --disable-popup-blocking \
    --disable-prompt-on-repost \
    --disable-hang-monitor \
    --disable-background-networking \
    --aggressive-cache-discard \
    --memory-pressure-off \
    --max_old_space_size=100 \
    --force-device-scale-factor=1 \
    http://localhost:5090 &
EOF

chmod +x /home/$USER/kiosk.sh

# Configureer autostart
echo "⚙️  Autostart configureren..."
sudo mkdir -p /etc/xdg/openbox
cat > /tmp/autostart << 'EOF'
# Schakel power management uit
xset -dpms
xset s noblank
xset s off

# Verberg cursor na 3 seconden
unclutter -idle 3 -root &

# Verwijder crash berichten
sed -i 's/"exited_cleanly":false/"exited_cleanly":true/' ~/.config/chromium/'Local State' 2>/dev/null || true
sed -i 's/"exited_cleanly":false/"exited_cleanly":true/; s/"exit_type":"[^"]\+"/"exit_type":"Normal"/' ~/.config/chromium/Default/Preferences 2>/dev/null || true

# Start kiosk
/home/USER_PLACEHOLDER/kiosk.sh
EOF

sed "s/USER_PLACEHOLDER/$USER/g" /tmp/autostart | sudo tee /etc/xdg/openbox/autostart > /dev/null

# Configureer automatische X start
echo "🚀 Automatische X start configureren..."
if ! grep -q "startx" ~/.bash_profile 2>/dev/null; then
    echo '[[ -z $DISPLAY && $XDG_VTNR -eq 1 ]] && startx -- -nocursor' >> ~/.bash_profile
fi

# Configureer automatische login
echo "👤 Automatische login configureren..."
sudo raspi-config nonint do_boot_behaviour B4

# Maak systemd service voor Docker
echo "🔄 Docker service configureren..."
cat > /tmp/adhan-docker.service << 'EOF'
[Unit]
Description=Adhaan Docker Services
Requires=docker.service
After=docker.service

[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/home/USER_PLACEHOLDER/adhan
ExecStart=/usr/bin/docker-compose -f docker-compose-pi.yml up -d
ExecStop=/usr/bin/docker-compose -f docker-compose-pi.yml down
TimeoutStartSec=0

[Install]
WantedBy=multi-user.target
EOF

sed "s/USER_PLACEHOLDER/$USER/g" /tmp/adhan-docker.service | sudo tee /etc/systemd/system/adhan-docker.service > /dev/null
sudo systemctl enable adhan-docker.service

echo "✅ Setup voltooid!"
echo ""
echo "📋 Volgende stappen:"
echo "1. Herstart de Pi: sudo reboot"
echo "2. De Pi start automatisch in kiosk mode"
echo "3. Docker containers starten automatisch"
echo "4. Webpagina opent automatisch op http://localhost:5090"
echo ""
echo "🔧 Handmatige bediening:"
echo "- Alt+F4: Sluit browser"
echo "- Ctrl+Alt+T: Open terminal"
echo "- sudo systemctl status adhan-docker: Check Docker status" 