---
- hosts: hobbes.n39.eu
  become: true

  vars:
    ansible_python_interpreter: /usr/bin/python3


  roles:


  tasks:
    - name: Install packages needed for the system
      # This is a list of all packages,
      # unless they are installed by a specific role
      ansible.builtin.apt:
        state: present
        name:
          # This is needed for the user-executed tasks
          - acl
          # Regular packages
          - lightdm
          - accountsservice
          - unclutter
          - lxde
          - chromium-browser
          - rng-tools

    - name: Remove the screensavers
      ansible.builtin.apt:
        state: absent
        name:
          - xscreensaver


    - name: Set MAC address for proper DHCP recognition
      # Uses mac variable from inventory
      ansible.builtin.template:
        src: templates/network-interfaces-dhcp-mac.j2
        dest: /etc/network/interfaces.d/hobbes-mac
        owner: root
        group: root
        mode: '0644'

    - name: Disable IPv6
      # Because it is not working....
      ansible.builtin.copy:
        src: files/sysctl-no-ipv6.conf
        dest: /etc/sysctl.d/99-systcl-no-ipv6.conf
        owner: root
        group: root
        mode: '0644'


    ### Kiosk setup
    #
    # https://github.com/grafana/grafana-kiosk

    - name: Ensure kiosk user is there
      ansible.builtin.user:
        name: "{{ kiosk_user }}"
        groups: audio,plugdev,input,netdev
        append: yes


    - name: Create bin directory
      file:
        path: "/home/{{ kiosk_user }}/bin"
        owner: "{{ kiosk_user }}"
        mode: '0755'
        state: directory

    - name: Download grafana-kiosk
      ansible.builtin.get_url:
        url: "https://github.com/grafana/grafana-kiosk/releases/download/{{ kiosk_software_version }}/grafana-kiosk.linux.{{ kiosk_software_arch }}"
        dest: "/home/{{ kiosk_user }}/bin/grafana-kiosk"
        mode: '0755'
        force: no


    - name: Setup autologin in lightdm
      ansible.builtin.blockinfile:
        path: /etc/lightdm/lightdm.conf
        block: |
          [Seat:seat0]
          autologin-user = pi
          autologin-user-timeout = 0
          autologin-in-background = False

    - name: Remove autostart
      # None of the things in autostart are needed or wanted
      ansible.builtin.file:
        path: /etc/xdg/lxsession/LXDE/autostart
        state: absent


    - name: Add systemd service
      ansible.builtin.template:
        src: templates/hobbes/grafana-kiosk.service.j2
        dest: /etc/systemd/system/grafana-kiosk.service
        owner: root
        group: root
        mode: '0644'

    - name: Enable and start kiosk service
      ansible.builtin.service:
        name: grafana-kiosk.service
        enabled: true
        state: started

    - name: Set default systemd target to graphical
      ansible.builtin.file:
        src: /lib/systemd/system/graphical.target
        dest: /etc/systemd/system/default.target
        state: link
        force: yes


  handlers: