---
- name: Install LM Studio dependencies
  ansible.builtin.dnf:
    name:
      - fuse
      - fuse-libs
      - curl
      - libatomic
      - python3
      - mesa-vulkan-drivers
      - vulkan-tools
      - socat
    state: present

- name: Create lmstudio system user
  ansible.builtin.user:
    name: "{{ lm_studio_user }}"
    home: "{{ lm_studio_home }}"
    shell: /bin/bash
    groups:
      - video
      - render
    append: true
    system: false
    create_home: true
    state: present

- name: Check if lms CLI is already installed
  ansible.builtin.stat:
    path: "{{ lm_studio_home }}/.lmstudio/bin/lms"
  register: lms_binary

- name: Copy patch-glibcxx-compat.py
  ansible.builtin.copy:
    src: patch-glibcxx-compat.py
    dest: /tmp/patch-glibcxx-compat.py
    mode: '0755'

- name: Copy LM Studio installer files
  ansible.builtin.copy:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    mode: "{{ item.mode }}"
  loop:
    - {src: install-lmstudio-compat.sh, dest: /tmp/install-lmstudio-compat.sh, mode: '0755'}
  when: not lms_binary.stat.exists

- name: Run LM Studio installer
  ansible.builtin.shell:
    cmd: /tmp/install-lmstudio-compat.sh
    executable: /bin/bash
  become: true
  become_user: "{{ lm_studio_user }}"
  environment:
    HOME: "{{ lm_studio_home }}"
  when: not lms_binary.stat.exists
  register: lms_install_result

- name: Show installer output
  ansible.builtin.debug:
    var: lms_install_result.stdout_lines
  when: lms_install_result is changed

- name: Deploy llmster wrapper script
  ansible.builtin.copy:
    src: run-llmster.sh
    dest: /opt/lmstudio/run-llmster.sh
    mode: '0755'

- name: Patch all GLIBCXX_3.4.30-dependent binaries (watcher.node, libllm_engine.so, ...)
  ansible.builtin.shell:
    cmd: |
      find "{{ lm_studio_home }}/.lmstudio" \( -name '*.node' -o -name 'lib*.so' \) \
        -exec python3 /tmp/patch-glibcxx-compat.py {} \;
    executable: /bin/bash
  register: patch_result
  changed_when: "'patched' in patch_result.stdout"

- name: Show patch output
  ansible.builtin.debug:
    var: patch_result.stdout_lines
  when: patch_result is changed

- name: Force Vulkan backend (AMD GPU — CPU backend selected by default)
  ansible.builtin.copy:
    dest: "{{ lm_studio_home }}/.lmstudio/.internal/backend-preferences-v1.json"
    owner: "{{ lm_studio_user }}"
    group: "{{ lm_studio_user }}"
    mode: '0644'
    content: |
      [
        {
          "model_format": "gguf",
          "name": "llama.cpp-linux-x86_64-vulkan-avx2",
          "version": "2.13.0"
        }
      ]
  notify:
    - Reload systemd
    - Restart lm-studio

- name: Deploy LM Studio systemd service
  ansible.builtin.template:
    src: lm-studio.service.j2
    dest: /etc/systemd/system/lm-studio.service
    mode: '0644'
  notify:
    - Reload systemd
    - Restart lm-studio

- name: Enable lm-studio service
  ansible.builtin.systemd:
    name: lm-studio
    enabled: true
    daemon_reload: true
    state: started

- name: Redirect models dir vers NFS (symlink)
  ansible.builtin.file:
    src: "{{ lm_studio_models_nfs_path }}"
    dest: "{{ lm_studio_home }}/.lmstudio/models"
    state: link
    force: true
    owner: "{{ lm_studio_user }}"
    group: "{{ lm_studio_user }}"
  when: lm_studio_models_nfs_path is defined and lm_studio_models_nfs_path != ""

- name: Deploy lms global wrapper (/usr/local/bin/lms)
  ansible.builtin.copy:
    dest: /usr/local/bin/lms
    mode: '0755'
    content: |
      #!/bin/bash
      exec sudo -u {{ lm_studio_user }} \
        HOME={{ lm_studio_home }} \
        HSA_OVERRIDE_GFX_VERSION={{ hsa_override_gfx_version }} \
        {{ lm_studio_home }}/.lmstudio/bin/lms "$@"

- name: Open LM Studio port in firewall
  ansible.posix.firewalld:
    port: "{{ lm_studio_port }}/tcp"
    permanent: true
    state: enabled
    immediate: true
