#cloud-config
# OfficeDash TV kit for a Raspberry Pi (help article: /help/raspberry-pi).
# Copy this file, network-config, meta-data and the officedash folder to the card's
# "bootfs" after writing Raspberry Pi OS Lite (32-bit) to it.
#
# Replace TV_PASSWORD below (keep the quotes): the password for user "tv" over SSH.
# Setting up more than one TV? Give each card its own hostname (officedash-tv-2, ...).
# Not in the Netherlands? Change NL below and in network-config to your country code
# (BE, DE, FR, GB, ...) and the timezone to yours.
hostname: officedash-tv
manage_etc_hosts: true
timezone: Europe/Amsterdam

users:
  - name: tv
    groups: users,adm,audio,video,render,input,netdev,plugdev,sudo
    shell: /bin/bash
    sudo: ALL=(ALL) NOPASSWD:ALL
    lock_passwd: false
    plain_text_passwd: "TV_PASSWORD"

ssh_pwauth: true

bootcmd:
  - [rfkill, unblock, wifi]

write_files:
  # Log in on the TV screen automatically.
  - path: /etc/systemd/system/getty@tty1.service.d/autologin.conf
    content: |
      [Service]
      ExecStart=
      ExecStart=-/sbin/agetty --autologin tv --noclear %I $TERM

  # Keep the system log on the card (capped), so a failure can be read after a restart.
  - path: /etc/systemd/journald.conf.d/officedash.conf
    content: |
      [Journal]
      Storage=persistent
      SystemMaxUse=50M

  # While the setup network is up, every name a phone looks up points at the Pi,
  # so the phone opens the setup page by itself.
  - path: /etc/NetworkManager/dnsmasq-shared.d/officedash-captive.conf
    content: |
      address=/#/10.42.0.1

  # Installs the kiosk software. Runs at every boot until it has succeeded once, and
  # waits for the right time first: the Pi has no clock battery, and apt refuses package
  # signatures while the clock is still on the image's build date.
  - path: /usr/local/sbin/officedash-install
    permissions: "0755"
    content: |
      #!/bin/sh
      set -eu
      MARK=/var/lib/officedash/installed
      [ -e "$MARK" ] && exit 0
      mkdir -p /opt/officedash
      cp -r /boot/firmware/officedash/. /opt/officedash/
      echo "waiting for the network and the right time"
      i=0
      until [ "$(timedatectl show -p NTPSynchronized --value)" = yes ]; do
        i=$((i + 1))
        [ "$i" -gt 120 ] && { echo "no time sync after 10 min, retrying later"; exit 1; }
        sleep 5
      done
      dpkg --configure -a
      apt-get update
      DEBIAN_FRONTEND=noninteractive apt-get install -y \
        cage chromium wlrctl fonts-noto-color-emoji python3-qrcode
      mkdir -p /var/lib/officedash
      touch "$MARK"
      echo "installed"
      systemctl restart officedash-wifi-setup
      systemctl restart getty@tty1

  - path: /etc/systemd/system/officedash-install.service
    content: |
      [Unit]
      Description=OfficeDash TV kiosk install (until it succeeds once)
      After=NetworkManager.service time-set.target
      ConditionPathExists=!/var/lib/officedash/installed

      [Service]
      Type=oneshot
      ExecStart=/usr/local/sbin/officedash-install
      Restart=on-failure
      RestartSec=30

      [Install]
      WantedBy=multi-user.target

  - path: /etc/systemd/system/officedash-wifi-setup.service
    content: |
      [Unit]
      Description=OfficeDash TV wifi setup helper
      After=NetworkManager.service
      Wants=NetworkManager.service

      [Service]
      ExecStart=/usr/bin/python3 /opt/officedash/wifi-setup.py
      Restart=always
      RestartSec=5

      [Install]
      WantedBy=multi-user.target

  # The kiosk: cage runs Chromium full-screen on the wifi helper's local page, which
  # moves on to the dashboard once the Pi is online. wlrctl parks the pointer in the
  # bottom-right corner, off the screen.
  - path: /home/tv/.bash_profile
    owner: tv:tv
    defer: true
    content: |
      if [ -z "$WAYLAND_DISPLAY" ] && [ "$(tty)" = /dev/tty1 ]; then
        if ! command -v cage >/dev/null || ! command -v chromium >/dev/null; then
          clear
          echo "OfficeDash TV is installing. This takes 10 to 30 minutes and needs internet."
          echo "No wifi? Join the network OfficeDash-Setup with your phone."
          exec sleep infinity
        fi
        sed -i 's/"exited_cleanly":false/"exited_cleanly":true/; s/"exit_type":"Crashed"/"exit_type":"Normal"/' ~/.config/chromium/Default/Preferences 2>/dev/null
        exec cage -s -- sh -c '
          ( for i in 1 2 3 4 5 6; do sleep 5; wlrctl pointer move 5000 5000; done ) &
          exec chromium --kiosk --noerrdialogs --disable-infobars --no-first-run \
            --check-for-update-interval=31536000 http://127.0.0.1/
        '
      fi

runcmd:
  - raspi-config nonint do_wifi_country NL
  - mkdir -p /opt/officedash /var/log/journal
  - cp -r /boot/firmware/officedash/. /opt/officedash/
  - systemctl daemon-reload
  - systemctl restart systemd-journald
  - systemctl enable --now ssh
  - systemctl enable --now officedash-wifi-setup
  - systemctl enable officedash-install
  - systemctl start --no-block officedash-install
  - systemctl restart getty@tty1
