- name: Setup mqtt_srv
  become: "{{ 'no' if inventory_hostname == 'nas.home.lan' else 'yes' }}"
  block:
    - name: Include vault
      ansible.builtin.include_vars:
        file: jaydee.yml
    - name: Delete content & directory
      ansible.builtin.file:
        state: absent
        path: "{{ dest_folder }}"
    
    - name: Check if git binary exists
      stat:
        path: /usr/bin/git
      register: git_stat
    
    - name: Include git
      ansible.builtin.include_role:
        name: git
      when: not git_stat.stat.exists

    - name: GIT pull
      tags:
        - git_pull
      ansible.builtin.git:
        repo: "https://{{ git_user | urlencode }}:{{ git_password_mqtt | urlencode }}@gitlab.sectorq.eu/jaydee/mqtt_srv.git"
        dest: "{{ dest_folder }}"
        update: true
        clone: true
        version: main
    - name: Print message
      ansible.builtin.debug:
        msg: "{{ inventory_hostname }}"
    - name: Create dir
      ansible.builtin.file:
        path: /etc/mqtt_srv/
        state: directory
        mode: '0755'
        owner: root
        group: root
    - name: Create dir
      ansible.builtin.file:
        path: /myapps/mqtt_srv/
        recurse: true
        state: directory
        mode: '0755'
        owner: root
        group: root
    - name: Upload service config
      ansible.builtin.copy:
        src: "{{ dest_folder }}/mqtt_srv.service"
        dest: /etc/systemd/system/mqtt_srv.service
        remote_src: true
        mode: '0755'
        owner: root
        group: root
      when: inventory_hostname != 'nas.home.lan'

    - name: Upload service script
      ansible.builtin.copy:
        src: "{{ dest_folder }}/mqtt_srv.py"
        dest: /myapps/mqtt_srv/mqtt_srv.py
        mode: '0755'
        owner: root
        group: root
        remote_src: true
      when: inventory_hostname != 'nas.home.lan'
    - name: Upload monitors script
      ansible.builtin.copy:
        src: "get_monitors.sh"
        dest: /myapps/mqtt_srv/get_monitors.sh
        mode: '0755'
        owner: root
        group: root
      when: inventory_hostname == 'morefine.home.lan'      
    - name: Upload service req
      ansible.builtin.copy:
        src: "{{ dest_folder }}/requirements.txt"
        dest: /myapps/mqtt_srv/requirements.txt
        mode: '0755'
        owner: root
        group: root
        remote_src: true
      when: inventory_hostname != 'nas.home.lan'


    - name: Upload service script config
      ansible.builtin.copy:
        src: "{{ dest_folder }}/mqtt_srv.cfg"
        dest: /etc/mqtt_srv/mqtt_srv.cfg
        mode: '755'
        owner: root
        remote_src: true
      when: inventory_hostname != 'nas.home.lan'

    # - name: Upload service script1
    #   ansible.builtin.copy:
    #     src: scripts/mqtt_srv.sh
    #     dest: /jffs/scripts/mqtt_srv/
    #     mode: '755'
    #     owner: admin
    #   when: inventory_hostname in groups['router']
    #   become: false


    # - name: Upload service script
    #   ansible.builtin.copy:
    #     src: scripts/mqtt_srv.py
    #     dest: /jffs/scripts/mqtt_srv/
    #     mode: '755'
    #     owner: admin
    #   when: inventory_hostname in groups['router']
    #   become: false

    # - name: Upload service script1
    #   ansible.builtin.copy:
    #     src: "{{ dest_folder }}/mqtt_srv.sh"
    #     dest: /etc/init.d/
    #     mode: '755'
    #     owner: admin
    #     remote_src: true
    #   when: inventory_hostname == 'nas.home.lan'

    - name: Print message
      ansible.builtin.debug:
        msg: "{{ dest_folder }}"

    - name: Upload service script2
      ansible.builtin.copy:
        src: "{{ dest_folder }}/mqtt_srv.py"
        dest: /myapps/mqtt_srv/mqtt_srv.py
        mode: '755'
        owner: admin
        remote_src: true
      when: inventory_hostname == 'nas.home.lan'

    - name: Install venv
      ansible.builtin.apt:
        name:
          - python3-virtualenv

    - name: Install specified python requirements in indicated (virtualenv)
      ansible.builtin.pip:
        requirements: /myapps/mqtt_srv/requirements.txt
        virtualenv: /myapps/mqtt_srv/venv
      when: inventory_hostname != 'nas.home.lan'

    - name: Just force systemd to reread configs (2.4 and above)
      ansible.builtin.systemd:
        daemon_reload: true
      when: inventory_hostname != 'nas.home.lan'

    - name: Restart mqtt_srv service
      ansible.builtin.service:
        name: mqtt_srv.service
        state: restarted
        enabled: true
      when: inventory_hostname != 'nas.home.lan'

    - name: Restart mqtt service
      ansible.builtin.shell: "(/etc/init.d/mqtt_srv.sh restart >/dev/null 2>&1 &)"
      async: 10
      poll: 0
      when: inventory_hostname == 'nas.home.lan'
      changed_when: my_output.rc != 0
