---
- name: Install build dependencies
  ansible.builtin.dnf:
    name:
      - cmake
      - gcc-c++
      - git
      - patchelf
      - hipblas-devel
      - rocblas-devel
      - hip-devel
      - hipcc
    state: present

- name: Clone llama.cpp
  ansible.builtin.git:
    repo: https://github.com/ggerganov/llama.cpp
    dest: /opt/llama.cpp
    depth: 1
    version: "{{ llama_server_commit | default('HEAD') }}"
  register: llama_clone

- name: Check if llama-server binary exists
  ansible.builtin.stat:
    path: /opt/llama.cpp/build/bin/llama-server
  register: llama_server_binary_stat

- name: Configure cmake build (ROCm HIP)
  ansible.builtin.command:
    cmd: >
      cmake -B build
      -DGGML_HIP=ON
      -DAMDGPU_TARGETS={{ llama_amdgpu_targets }}
      -DCMAKE_BUILD_TYPE=Release
      -DROCM_PATH={{ rocm_path }}
      -DCMAKE_PREFIX_PATH={{ rocm_path }}
      -DCMAKE_HIP_COMPILER={{ rocm_path }}/llvm/bin/clang++
    chdir: /opt/llama.cpp
  environment:
    PATH: "{{ rocm_path }}/bin:{{ ansible_env.PATH }}"
  when: llama_clone is changed or not llama_server_binary_stat.stat.exists

- name: Build llama-server
  ansible.builtin.command:
    cmd: cmake --build build --target llama-server -j{{ ansible_processor_nproc }}
    chdir: /opt/llama.cpp
  environment:
    PATH: "{{ rocm_path }}/bin:{{ ansible_env.PATH }}"
  when: llama_clone is changed or not llama_server_binary_stat.stat.exists

- name: Deploy systemd service
  ansible.builtin.template:
    src: llama-server.service.j2
    dest: /etc/systemd/system/llama-server.service
    mode: '0644'
  notify:
    - Reload systemd
    - Restart llama-server

- name: Enable llama-server service
  ansible.builtin.systemd:
    name: llama-server
    enabled: true
    daemon_reload: true
    state: started

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

- name: Disable lm-studio service (replaced by llama-server)
  ansible.builtin.systemd:
    name: lm-studio
    enabled: false
    state: stopped
  ignore_errors: true
