Files
kiosk-display-setup/kiosk-display-setup.sh
T

145 lines
3.7 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
die() { echo "ERROR: $*" >&2; exit 1; }
warn() { echo "WARNING: $*" >&2; }
[[ "${EUID}" -ne 0 ]] || die "Run as your regular user, not sudo."
command -v firefox >/dev/null 2>&1 || die "Firefox is not installed or not in PATH."
command -v systemctl >/dev/null 2>&1 || die "systemctl is not available."
read -r -p "Enter the display location (e.g. Lobby): " LOCATION
LOCATION="$(
printf '%s' "$LOCATION" |
tr '[:lower:]' '[:upper:]' |
sed -E 's/[ _]+/-/g; s/[^A-Z0-9-]//g; s/-+/-/g; s/^-|-$//g'
)"
[[ -n "$LOCATION" ]] || die "Location can't be empty."
HOSTNAME="DISPLAY-NVR-$LOCATION"
[[ ${#HOSTNAME} -le 63 ]] || die "Hostname too long: $HOSTNAME"
read -r -p "Enter the NVR server URL (default: http://192.168.25.248): " URL_INPUT
URL="${URL_INPUT:-http://192.168.25.248}"
[[ "$URL" =~ ^https?:// ]] || die "URL must start with http:// or https://"
read -r -p "Enter daily reboot time in HH:MM format (default: 06:00): " REBOOT_TIME
REBOOT_TIME="${REBOOT_TIME:-06:00}"
[[ "$REBOOT_TIME" =~ ^([01][0-9]|2[0-3]):[0-5][0-9]$ ]] || die "Invalid time format."
APP_DIR="$HOME/.local/bin"
AUTOSTART_DIR="$HOME/.config/autostart"
XORG_CONF_DIR="/etc/X11/xorg.conf.d"
LAUNCHER="$APP_DIR/launch-nvr.sh"
DESKTOP_FILE="$AUTOSTART_DIR/launch-nvr.desktop"
CONFIG_FILE="$HOME/.config/nvr-display.conf"
mkdir -p "$APP_DIR" "$AUTOSTART_DIR" "$HOME/.config"
cat > "$CONFIG_FILE" <<EOF
HOSTNAME=$HOSTNAME
URL=$URL
REBOOT_TIME=$REBOOT_TIME
SETUP_DATE=$(date -u +%Y-%m-%dT%H:%M:%SZ)
EOF
chmod 600 "$CONFIG_FILE"
cat > "$LAUNCHER" <<EOF
#!/usr/bin/env bash
set -euo pipefail
URL="$URL"
cleanup() {
rc=\$?
if [[ \$rc -ne 0 ]]; then
echo "Firefox launcher exited with code \$rc" >&2
fi
}
trap cleanup EXIT
if command -v xset >/dev/null 2>&1 && [[ -n "\${DISPLAY:-}" ]]; then
xset s off || true
xset -dpms || true
xset s noblank || true
fi
exec firefox --kiosk "\$URL"
EOF
chmod 700 "$LAUNCHER"
cat > "$DESKTOP_FILE" <<EOF
[Desktop Entry]
Type=Application
Name=Launch NVR Display
Exec=$LAUNCHER
Terminal=false
X-GNOME-Autostart-enabled=true
X-MATE-Autostart-enabled=true
StartupNotify=false
NoDisplay=false
Comment=Automatically launch NVR display in kiosk mode
EOF
chmod 644 "$DESKTOP_FILE"
echo
echo "User-level setup complete:"
echo " Launcher: $LAUNCHER"
echo " Autostart: $DESKTOP_FILE"
echo " Config: $CONFIG_FILE"
echo
read -r -p "Continue with system-level changes (hostname, reboot timer)? [y/N] " -n 1 CONFIRM
echo
[[ "$CONFIRM" =~ ^[Yy]$ ]] || exit 0
echo "Setting hostname to: $HOSTNAME"
sudo hostnamectl set-hostname "$HOSTNAME" || warn "Failed to set hostname."
echo "Installing daily reboot timer for $REBOOT_TIME..."
sudo tee /etc/systemd/system/nvr-daily-reboot.service >/dev/null <<'EOF'
[Unit]
Description=Reboot NVR display computer
[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl reboot
EOF
sudo tee /etc/systemd/system/nvr-daily-reboot.timer >/dev/null <<EOF
[Unit]
Description=Reboot NVR display computer daily at $REBOOT_TIME
[Timer]
OnCalendar=*-*-* $REBOOT_TIME:00
Persistent=true
Unit=nvr-daily-reboot.service
AccuracySec=1s
[Install]
WantedBy=timers.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now nvr-daily-reboot.timer || warn "Failed to enable reboot timer."
if sudo systemctl is-active --quiet nvr-daily-reboot.timer; then
echo "Reboot timer is active."
else
warn "Reboot timer may not be active."
fi
echo
echo "=== Setup Complete ==="
echo "Hostname: $HOSTNAME"
echo "NVR URL: $URL"
echo "Daily reboot: $REBOOT_TIME"
echo
echo "Next steps:"
echo "1. Reboot to test autostart: sudo reboot"
echo "2. Verify Firefox opens in kiosk mode"
echo "3. Check timer: systemctl list-timers nvr-daily-reboot.timer"
echo "4. View logs: journalctl -u nvr-daily-reboot.service -f"