- name: Install or Update the Wyoming Service
  hosts: wyoming
  vars_files:
    - secrets/vars.yaml
  tasks:
  - name: Install and upgrade wyoming/wakeword
    when: install
    block:
    # Stop running services before install/upgrade
    - name: Stop wakeword service
      ansible.builtin.systemd_service:
        state: stopped
        name: wakeword.service
        scope: user
      ignore_errors: true
    - name: Stop wyoming service
      ansible.builtin.systemd_service:
        state: stopped
        name: wyoming.service
        scope: user
      ignore_errors: true
    # Wyoming Service
    - name: Checkout/pull the wyoming repo
      ansible.builtin.git:
        repo: '{{ wyoming.repo }}'
        dest: "{{ ansible_env.HOME }}/wyoming-satellite"
        update: yes
    - name: Run the script/setup in the wyoming repo
      command: "{{ ansible_env.HOME }}/wyoming-satellite/script/setup"
    - name: Install audio enhancements in wyoming repo
      command: "{{ ansible_env.HOME }}/wyoming-satellite/.venv/bin/pip3 install 'webrtc-noise-gain==1.2.3'"
    # Wake word service
    - name: Checkout/pull the wyoming repo
      ansible.builtin.git:
        repo: '{{ wakeword.repo }}'
        dest: "{{ ansible_env.HOME }}/wyoming-openwakeword"
        update: yes
    - name: Run the script/setup in the wakeword repo
      command: "{{ ansible_env.HOME }}/wyoming-openwakeword/script/setup"
  # Custom sound bites for start/stop listening
  - name: Copy sound bites
    copy:
      src: "{{ item }}"
      dest: "{{ ansible_env.HOME }}/wyoming-satellite/sounds/"
    with_items:
      - listening.wav
      - finished.wav
  # Custom wake word models
  - name: Copy custom models
    copy:
      src: "{{ item }}"
      dest: "{{ ansible_env.HOME }}/wyoming-openwakeword/wyoming_openwakeword/models/{{ item }}"
    with_items:
      - jarvis_v2.tflite
  # Copy and start systemd services
  - name: Create ~/.config/systemd/user dir
    ansible.builtin.file:
      path: "{{ ansible_env.HOME }}/.config/systemd/user"
      state: directory
      mode: '0755'
  - name: Copy wyoming.service
    template:
      src: wyoming.service
      dest: "{{ ansible_env.HOME }}/.config/systemd/user/wyoming.service"
      mode: '0644'
  - name: Copy wakeword.service
    template:
      src: wakeword.service
      dest: "{{ ansible_env.HOME }}/.config/systemd/user/wakeword.service"
      mode: '0644'
  - name: Reload wakeword service
    ansible.builtin.systemd_service:
      state: restarted
      name: wakeword.service
      enabled: true
      daemon_reload: true
      scope: user
  - name: Reload wyoming service
    ansible.builtin.systemd_service:
      state: restarted
      name: wyoming.service
      enabled: true
      daemon_reload: true
      scope: user
  - name: Add debug alias for wakeford
    lineinfile:
      path: "{{ ansible_env.HOME }}/.bashrc"
      line: alias debug-wakeword="journalctl --user -u wakeword -f | grep -E '0\.0[^0].*|0\.[^0].*'"
      regexp: '^alias debug-wakeword=.*$'
      state: present
      insertafter: EOF
      create: true
  - name: Add debug alias for wyoming
    lineinfile:
      path: "{{ ansible_env.HOME }}/.bashrc"
      line: alias debug-wyoming="journalctl --user -u wyoming -f | grep 'Stopped recording to'"
      regexp: '^alias debug-wyoming=.*$'
      state: present
      insertafter: EOF
      create: true