# This task installs the PEAK PCAN driver.

---
- name: Create the opt directory if it doesn't exist
  ansible.builtin.file:
    path: "/opt"
    state: directory
    mode: 0755

- name: Download peak-linux drivers from Z3
  ansible.builtin.get_url:
    url: "{{ pcan_download_url }}"
    dest: "/opt/{{ pcan_tarball }}"
    mode: 0400

- name: Extract the peak-linux drivers
  ansible.builtin.unarchive:
    src: "/opt/{{ pcan_tarball }}"
    remote_src: yes
    dest: "/opt/"
  register: archive

- name: Build PCAN driver with NETDEV
  community.general.make:
    chdir: '/opt/{{ pcan_package_name }}/driver'
    params:
      NET: NETDEV_SUPPORT
  when: archive.changed

- name: Install PCAN driver
  community.general.make:
    chdir: '/opt/{{ pcan_package_name }}/driver'
    target: install
  when: archive.changed

- name: Load PCAN driver
  community.general.modprobe:
    name: pcan
    state: present

- name: Ensure pcan module is loaded on reboot
  ansible.builtin.lineinfile:
    path: /etc/modules
    line: pcan

- name: Turn off ASPE in /etc/default/grub
  ansible.builtin.replace:
    path: /etc/default/grub
    regexp: "console=tty1 console=tty0"
    replace: quiet splash pcie_aspm=off
    backup: true
  notify:
    - Reboot PC
