- name: Define Debian/Ubuntu prerequisite packages
  set_fact:
    courseware_linux_prereq_packages:
      - build-essential
      - ca-certificates
      - cmake
      - curl
      - git
      - git-lfs
      - libcap2-bin
      - libcurl4-openssl-dev
      - nodejs
      - npm
      - pkg-config
      - python3
      - python3-pip
      - python3-venv
      - unzip
      - zstd

- name: Install Debian/Ubuntu prerequisites
  block:
    - name: Install Debian/Ubuntu prerequisite packages
      become: true
      apt:
        name: "{{ courseware_linux_prereq_packages }}"
        state: present
        update_cache: true
  rescue:
    - name: Repair interrupted dpkg state before retrying prerequisites
      become: true
      command:
        argv:
          - dpkg
          - --configure
          - -a
      register: courseware_dpkg_repair
      changed_when: false
      failed_when: false

    - name: Repair broken apt dependencies before retrying prerequisites
      become: true
      command:
        argv:
          - apt-get
          - -y
          - --fix-broken
          - install
      environment:
        DEBIAN_FRONTEND: noninteractive
      register: courseware_apt_fix_broken
      changed_when: >-
        '0 upgraded, 0 newly installed, 0 to remove' not in courseware_apt_fix_broken.stdout

    - name: Retry Debian/Ubuntu prerequisite package install
      become: true
      apt:
        name: "{{ courseware_linux_prereq_packages }}"
        state: present
        update_cache: true

- name: Query CUDA toolkit apt candidate
  command: apt-cache policy nvidia-cuda-toolkit
  register: courseware_cuda_toolkit_policy
  changed_when: false
  failed_when: false

- name: Check for nvcc
  command: which nvcc
  register: courseware_nvcc_check
  changed_when: false
  failed_when: false

- name: Set CUDA toolkit package availability
  set_fact:
    courseware_cuda_toolkit_package_available: >-
      {{
        courseware_cuda_toolkit_policy.rc == 0
        and 'Candidate: (none)' not in courseware_cuda_toolkit_policy.stdout
      }}

- name: Install distro CUDA toolkit when available
  block:
    - name: Install nvidia-cuda-toolkit
      become: true
      apt:
        name: nvidia-cuda-toolkit
        state: present
  rescue:
    - name: Fail with CUDA toolkit guidance after apt install error
      fail:
        msg: |
          CUDA Toolkit could not be installed from the distro package manager.

          This installer needs the Linux-side CUDA toolkit for llama.cpp, not just a working `nvidia-smi`.

          Try this first:
            sudo apt update
            sudo apt install -y nvidia-cuda-toolkit

          If that still fails, add NVIDIA's CUDA repository for your Debian/Ubuntu release and install the toolkit from there.

          Verify with:
            nvcc --version
            ls /usr/local/cuda/include/cuda_runtime.h
  when:
    - courseware_host_profile == "native-debian-ubuntu"
    - courseware_cuda_toolkit_package_available
    - not courseware_cuda_toolkit_ready

- name: Fail when native CUDA network repository bootstrap is unsupported
  fail:
    msg: |
      This native {{ ansible_distribution }} {{ ansible_distribution_version }} host does not match the CUDA network-repository variants this installer knows how to bootstrap automatically.

      Supported native CUDA repo bootstrap targets currently match NVIDIA's documented Debian/Ubuntu paths:
      - Debian 12 and Debian 13 on x86_64
      - Ubuntu 22.04 and Ubuntu 24.04 on x86_64 or arm64

      Install the Linux-side CUDA toolkit manually for this host, then rerun:
        ./labctl up

      Verify with:
        nvcc --version
        ls /usr/local/cuda/include/cuda_runtime.h
  when:
    - courseware_host_profile == "native-debian-ubuntu"
    - not courseware_cuda_toolkit_package_available
    - not courseware_cuda_toolkit_ready
    - not courseware_native_cuda_repo_supported

- name: Bootstrap NVIDIA CUDA network repository for native Debian/Ubuntu
  block:
    - name: Download NVIDIA CUDA keyring package
      get_url:
        url: "{{ courseware_native_cuda_keyring_url }}"
        dest: "{{ courseware_native_cuda_keyring_local_path }}"
        mode: "0644"
        force: false

    - name: Install NVIDIA CUDA keyring package
      become: true
      apt:
        deb: "{{ courseware_native_cuda_keyring_local_path }}"
        state: present

    - name: Install CUDA toolkit from NVIDIA network repository
      become: true
      apt:
        name: "{{ courseware_native_cuda_toolkit_package }}"
        state: present
        update_cache: true
  rescue:
    - name: Repair interrupted dpkg state before retrying CUDA repository install
      become: true
      command:
        argv:
          - dpkg
          - --configure
          - -a
      changed_when: false
      failed_when: false

    - name: Repair broken apt dependencies before retrying CUDA repository install
      become: true
      command:
        argv:
          - apt-get
          - -y
          - --fix-broken
          - install
      environment:
        DEBIAN_FRONTEND: noninteractive
      register: courseware_cuda_repo_fix_broken
      changed_when: >-
        '0 upgraded, 0 newly installed, 0 to remove' not in courseware_cuda_repo_fix_broken.stdout

    - name: Retry CUDA toolkit install from NVIDIA network repository
      become: true
      apt:
        name: "{{ courseware_native_cuda_toolkit_package }}"
        state: present
        update_cache: true
  when:
    - courseware_host_profile == "native-debian-ubuntu"
    - not courseware_cuda_toolkit_package_available
    - not courseware_cuda_toolkit_ready
    - courseware_native_cuda_repo_supported

- name: Check for Ollama binary
  command: which ollama
  register: courseware_ollama_check
  changed_when: false
  failed_when: false

- name: Install Ollama
  become: true
  shell: curl -fsSL https://ollama.com/install.sh | sh
  args:
    creates: /usr/local/bin/ollama
  when: courseware_ollama_check.rc != 0

- name: Mark Ollama as installed by courseware
  file:
    path: "{{ courseware_ollama_install_marker }}"
    state: touch
    mode: "0644"
  when: courseware_ollama_check.rc != 0

- name: Check for courseware-managed Ollama install marker
  stat:
    path: "{{ courseware_ollama_install_marker }}"
  register: courseware_ollama_install_marker_before

- name: Adopt existing local Ollama install into courseware management
  file:
    path: "{{ courseware_ollama_install_marker }}"
    state: touch
    mode: "0644"
  when:
    - not courseware_ollama_install_marker_before.stat.exists
    - courseware_ollama_check.rc == 0
    - ansible_system == "Linux"
    - ansible_service_mgr == "systemd"
    - courseware_is_wsl | bool
    - ansible_user_id != "ollama"
    - ansible_env.HOME is search('/home/')
    - ansible_env.HOME != '/usr/share/ollama'
    - ansible_env.HOME != '/var/lib/ollama'

- name: Refresh courseware-managed Ollama install marker
  stat:
    path: "{{ courseware_ollama_install_marker }}"
  register: courseware_ollama_install_marker_stat

- name: Check for Ollama systemd unit
  stat:
    path: /etc/systemd/system/ollama.service
  register: courseware_ollama_systemd_unit

- name: Stop and disable courseware-managed Ollama systemd service
  become: true
  systemd:
    name: ollama
    state: stopped
    enabled: false
  when:
    - ansible_service_mgr == "systemd"
    - courseware_ollama_install_marker_stat.stat.exists
    - courseware_ollama_systemd_unit.stat.exists
