#cloud-config
# Example: create user (pi) with password, enable SSH, install KDE Plasma with touch options,
# set KDE as default GUI, and deploy Chromium kiosk autostart.
# Uses start-chromium.sh and chromium-kiosk.desktop from this project (emmc-provisioning/cloud-init/).
#
# 1. Generate a password hash on a Linux host:
#      mkpasswd -m sha-512 'YourPassword'
#    or: openssl passwd -6 'YourPassword'
#    Paste the full output (e.g. $6$...) into the passwd: line below.
# 2. To use a different username than "pi", replace every "pi" in this file.
# 3. To change the kiosk URL, edit the --app=... line in the start-chromium.sh content below.

package_update: true
package_upgrade: false

packages:
  - chromium-browser
  - wmctrl
  - openssh-server
  # KDE Plasma + touchscreen
  - kde-plasma-desktop
  - maliit-keyboard
  - xinput-calibrator

# Create user and set password (use hash from mkpasswd -m sha-512 or openssl passwd -6)
users:
  - name: pi
    groups: [adm, sudo, video]
    lock_passwd: false
    passwd: "$6$7xWGhGc6d1lJx1dU$4E8r1mkzVj51bjEbfzdP8wPxso..C36LbXkqU/X4oBGq94aGFMSrZb0PVI8zs/Om1Jm97/D..Apy2HTdCn3FV1"
    shell: /bin/bash

# Enable SSH (allow password auth so you can log in with the user above)
write_files:
  - path: /etc/ssh/sshd_config.d/99-cloud-init.conf
    content: |
      PasswordAuthentication yes
      PermitRootLogin no
  - path: /home/pi/start-chromium.sh
    content: |
      #!/bin/bash
      export GNOME_KEYRING_CONTROL=""
      export DISPLAY=:0
      export GDK_BACKEND=x11
      unset WAYLAND_DISPLAY
      for i in {1..60}; do
        if xset q >/dev/null 2>&1 || [ -n "$DISPLAY" ]; then
          if pgrep -x plasma_session >/dev/null 2>&1 || pgrep -x kwin_x11 >/dev/null 2>&1 || pgrep -x pcmanfm >/dev/null 2>&1 || pgrep -x lxsession >/dev/null 2>&1; then
            break
          fi
        fi
        sleep 0.5
      done
      sleep 5
      /usr/bin/chromium --start-fullscreen --noerrdialogs --disable-infobars --disable-session-crashed-bubble --disable-restore-session-state --no-first-run --password-store=basic --use-mock-keychain --ozone-platform=x11 --disable-features=UseChromeOSDirectVideoDecoder --app=http://127.0.0.1:8080 &
      sleep 3
      for i in {1..10}; do
        WINDOW_ID=$(wmctrl -l 2>/dev/null | grep -i chromium | head -1 | awk '{print $1}')
        if [ -n "$WINDOW_ID" ]; then
          wmctrl -i -r "$WINDOW_ID" -b add,fullscreen 2>/dev/null
          break
        fi
        sleep 0.5
      done
      wait
    owner: pi:pi
    permissions: "0755"
  - path: /home/pi/.config/autostart/chromium-kiosk.desktop
    content: |
      [Desktop Entry]
      Type=Application
      Name=Chromium Fullscreen
      Exec=/home/pi/start-chromium.sh
      Hidden=false
      NoDisplay=false
      X-GNOME-Autostart-enabled=true
    owner: pi:pi
    permissions: "0644"
  # KDE Plasma: switch to KDE as default session (X11 for Chromium compatibility)
  - path: /etc/lightdm/lightdm.conf.d/99-default-session.conf
    content: |
      [Seat:*]
      user-session=plasmax11
    permissions: "0644"
  # KDE touch-friendly: UI scale and input (for pi after first login)
  - path: /home/pi/.config/kdeglobals
    content: |
      [General]
      ForceFontDPI=120
    owner: pi:pi
    permissions: "0644"
  - path: /home/pi/.config/kwinrc
    content: |
      [Windows]
      BorderlessMaximizedWindows=true
      [Plugins]
      touchpointsEnabled=true
    owner: pi:pi
    permissions: "0644"
  # Start on-screen keyboard (maliit) with KDE for touch input
  - path: /home/pi/.config/autostart/maliit-keyboard.desktop
    content: |
      [Desktop Entry]
      Type=Application
      Name=Maliit Keyboard
      Exec=maliit-keyboard -r
      X-GNOME-Autostart-enabled=true
    owner: pi:pi
    permissions: "0644"

runcmd:
  - mkdir -p /home/pi/.config/autostart
  - chown -R pi:pi /home/pi/.config
  # Set KDE Plasma (X11) as default session so next boot uses KDE
  - update-alternatives --set x-session-manager /usr/bin/startplasma-x11 2>/dev/null || true
  - systemctl enable ssh
  - systemctl start ssh
  - cloud-init single --name cc_final_message
