#!/usr/bin/env bash

tee /etc/resolv.conf > /dev/null <<'EOF'
nameserver 1.1.1.1
nameserver 1.0.0.1
EOF

# Get SmartBox GPG key
wget -qO - https://repo.smartbox.ba/ubuntu/smartbox.gpg | gpg --dearmor -o /usr/share/keyrings/smartbox.gpg

# Add SmartBox repository
echo "deb [signed-by=/usr/share/keyrings/smartbox.gpg] https://repo.smartbox.ba/ubuntu noble main" | tee /etc/apt/sources.list.d/smartbox.list

# Update and upgrade
apt-get update && apt-get upgrade -y

# Install necessary packages
apt-get install -y --no-install-recommends xfce4 xfce4-goodies xorg openbox nano bash-completion qemu-kvm libvirt-daemon-system  

# Install DemoDesktop package
apt install ba.smartbox -y || apt-get install -f -y

# Create symbolic link
ln -s /opt/ba.smartbox/bin/ba.smartbox /usr/bin/ba.smartbox

# Create kiosk script
tee /opt/kiosk.sh > /dev/null <<'EOF'
#!/bin/bash

xset -dpms
xset s off

openbox-session &

while true; do
  ba.smartbox
done
EOF

chmod a+x /opt/kiosk.sh

# Create systemd service
tee /etc/systemd/system/kiosk.service > /dev/null <<'EOF'
[Unit]
Description=Launch Demo Kiosk app
After=network-online.target
After=display-manager.service
Wants=network-online.target

[Service]
ExecStart=startx /etc/X11/Xsession /opt/kiosk.sh

[Install]
WantedBy=multi-user.target
EOF

# Enable kiosk service 
systemctl enable kiosk.service

# Reboot the system
reboot
