- hosts: rhasspy
  name: Install rhasspy
  become: true
  tasks:
    - name: Set a hostname
      ansible.builtin.hostname:
        name: rasp3-2
      when: ansible_eth0.macaddress == "b8:27:eb:29:cb:d4"

    - name: Set a hostname
      ansible.builtin.hostname:
        name: rasp3-1
      when: ansible_eth0.macaddress == "b8:27:eb:53:79:11" or ansible_wlan0.macaddress == "B8:27:EB:06:2C:44"

#    - name: Download package
#      ansible.builtin.get_url:
#        url: https://sectorq.eu/install/rhasspy_amd64.deb
#        dest: /tmp/rhasspy_amd64.deb
#        mode: '0777'
    - name: Get keys for raspotify
      ansible.builtin.shell:
        curl -sSL https://dtcooper.github.io/raspotify/key.asc | sudo tee /usr/share/keyrings/raspotify_key.asc  > /dev/null
      changed_when: false
    - name: Get keys for raspotify
      ansible.builtin.file:
        path: /usr/share/keyrings/raspotify_key.asc
        mode: "0644"

    - name: Get repo
      ansible.builtin.shell: >-
        echo 'deb [signed-by=/usr/share/keyrings/raspotify_key.asc] https://dtcooper.github.io/raspotify raspotify main' |
         sudo tee /etc/apt/sources.list.d/raspotify.list
      changed_when: false

    - name: Update apt cache
      ansible.builtin.apt:
        update_cache: true
        upgrade: true
      ignore_errors: true
    - name: Install curl,mosquitto ...
      ansible.builtin.apt:
        name:
          - curl
          # - libffi7
          - apt-transport-https
          - raspotify
          - mosquitto
          - python3-pip
        state: present
    - name: Reconfigure /etc/raspotify/conf
      ansible.builtin.lineinfile:
        path: /etc/raspotify/conf
        regexp: "{{ item.regexp }}"
        line: "{{ item.line }}"
      loop:
        - { regexp: '(^|^#)LIBRESPOT_NAME=', line: 'LIBRESPOT_NAME="rasp3-2"' }
        - { regexp: '(^|^#)LIBRESPOT_BITRATE=', line: 'LIBRESPOT_BITRATE="160"' }
        - { regexp: '(^|^#)LIBRESPOT_DEVICE=', line: 'LIBRESPOT_DEVICE="hw:CARD=Device,DEV=0"' }
        - { regexp: '(^|^#)LIBRESPOT_PASSWORD=', line: 'LIBRESPOT_PASSWORD="l4c1j4yd33Du5lo"' }
        - { regexp: '(^|^#)LIBRESPOT_USERNAME=', line: 'LIBRESPOT_USERNAME="jaydee67@sectorq.eu"' }
      when: ansible_eth0.macaddress == "b8:27:eb:29:cb:d4"
    - name: Reconfigure /etc/raspotify/conf
      ansible.builtin.lineinfile:
        path: /etc/raspotify/conf
        regexp: "{{ item.regexp }}"
        line: "{{ item.line }}"
      loop:
        - { regexp: '(^|^#)LIBRESPOT_NAME=', line: 'LIBRESPOT_NAME="rasp3-1"' }
        - { regexp: '(^|^#)LIBRESPOT_BITRATE=', line: 'LIBRESPOT_BITRATE="160"' }
        - { regexp: '(^|^#)LIBRESPOT_DEVICE=', line: 'LIBRESPOT_DEVICE="hw:CARD=Device,DEV=0"' }
        - { regexp: '(^|^#)LIBRESPOT_PASSWORD=', line: 'LIBRESPOT_PASSWORD="l4c1j4yd33Du5lo"' }
        - { regexp: '(^|^#)LIBRESPOT_USERNAME=', line: 'LIBRESPOT_USERNAME="jaydee67@sectorq.eu"' }
      when: ansible_eth0.macaddress == "b8:27:eb:53:79:11"

#    - name: Creating a symlink for libs
#      ansible.builtin.file:
#        src: "/usr/lib/aarch64-linux-gnu/libffi.so.7"
#        dest: "/usr/lib/aarch64-linux-gnu/libffi.so.6"
#        state: link
#    - name: Install a rhasspy_amd64.deb
#      ansible.builtin.apt:
#        deb: /tmp/rhasspy_amd64.deb
 
#    - name: Upload config
#      ansible.builtin.copy:
#        src: /etc/ansible/playbooks/files/services/rhasspy.service
#        dest: /etc/systemd/system/rhasspy.service
#        owner: root
#        group: root
#        mode: u=rwx,g=rx,o=rx
#    - name: Just force systemd to reread configs (2.4 and above)
#      ansible.builtin.systemd:
#        daemon_reload: true
#    - name: Restart service rhasspy, in all cases
#      ansible.builtin.service:
#        name: rhasspy
#        state: restarted
#    - name: Ensure rhasspy  is in a running state
#      ansible.builtin.service:
#        name: rhasspy
#        state: started
#      register: myservice_details
#      until: myservice_details.status.ActiveState == "active"
#      retries: 15
#      delay: 20
    - name: Make sure destination dir exists
      ansible.builtin.file:
        path: "/home/jd/.config/rhasspy/profiles/en/"
        state: directory
        owner: jd
        group: jd
        mode: '0755'
    - name: Upload config
      ansible.builtin.copy:
        src: conf/rhasspy/profile.json
        dest: /home/jd/.config/rhasspy/profiles/en/profile.json
        owner: jd
        group: jd
        mode: u=rw,g=r,o=r
    - name: Restart service raspotify, in all cases
      ansible.builtin.service:
        name: raspotify
        state: restarted
    - name: Install docker
      ansible.builtin.shell:
        curl -sSL https://get.docker.com | sh
      ignore_errors: true
      changed_when: false
    - name: Add user to docker group
      ansible.builtin.user:
        name: jd
        append: true
        groups: docker
    # - name: Install pip modules
    #   ansible.builtin.pip:
    #     name: docker

    - name: Pull image
      community.docker.docker_image:
        name: rhasspy/rhasspy
        source: pull
    - name: Create container
      community.docker.docker_container:
        name: rhasspy
        image: rhasspy/rhasspy
        command: --user-profiles /profiles --profile en
        devices:
          - "/dev/snd:/dev/snd"
        restart_policy: unless-stopped
        volumes:
          - "/etc/localtime:/etc/localtime:ro"
          - "/home/jd/.config/rhasspy/profiles:/profiles"
        ports:
          - "12101:12101"
