---
# tasks/main.yml
- name: Überprüfen, ob die Installation schon durchgeführt wurde
  stat:
    path: "/home/{{ raspi_user }}/mqttDisplayClient/mqttDisplayClient.ini"
  register: setup_complete
- name: Debug raspi_user vor dem Shell-Task
  debug:
    msg: "raspi_user: {{ raspi_user }}"

- name: Klone das mqttDisplayClient Git-Repository
  become: yes
  become_user: "{{ raspi_user }}"
  git:
    repo: "https://github.com/olialb/mqttDisplayClient"
    dest: "/home/{{ raspi_user }}/mqttDisplayClient"
    update: yes
    version: main  # Stelle sicher, dass du den richtigen Branch verwendest (standardmäßig 'master')
  when: not setup_complete.stat.exists and enableMQTTFeature

- name: Installiere pyautogui und führe setup.sh aus
  become: yes
  become_user: "{{ raspi_user }}"
  shell: |
    cd "/home/{{ raspi_user }}/mqttDisplayClient"
    bash setup.sh -f pyautogui
  args:
    creates: "/home/{{ raspi_user }}/mqttDisplayClient/mqttDisplayClient.ini"
  when: enableMQTTFeature

- name: Template the config.ini file
  become: yes
  become_user: "{{ raspi_user }}"
  template:
    src: templates/mqttDisplayClient.ini.j2
    dest: "/home/{{ raspi_user }}/mqttDisplayClient/mqttDisplayClient.ini" # Der Pfad zur INI-Datei
  notify:
    - Restart Service
  when: enableMQTTFeature

- name: cron-Job für mqtt-befehl jede minute Mausklick und dann verstecken
  cron:
    name: "Autogui-Befehl ausführen"
    minute: "*"
    user: "{{ raspi_user }}"
    job: '/usr/bin/mosquitto_pub -h "{{ mqtt_broker }}" -t "kiosk/01/{{ deviceName }}/autogui/set" -u "{{ mqtt_username }}" -P "{{ password }}" -m "click(1895,52)"; sleep 1; /usr/bin/mosquitto_pub -h "{{ mqtt_broker }}" -t "kiosk/01/{{ deviceName }}/autogui/set" -u "{{ mqtt_username }}" -P "{{ password }}" -m "click(1920,1080)"'
    state: present
  when: enableMQTTFeature and enableMQTTcronjob
