- name: Install dependencies
  become: true
  ansible.builtin.package:
    name: "{{ item }}"
    state: present
  loop:
    - sysstat
    - bc

- name: Enable sysstat service
  become: true
  ansible.builtin.systemd:
    name: sysstat
    enabled: true
    state: started

- name: Read existing GRUB_CMDLINE_LINUX
  ansible.builtin.shell: set -o pipefail && grep '^GRUB_CMDLINE_LINUX=' /etc/default/grub | cut -d= -f2- | tr -d '"'
  register: grub_line
  changed_when: false
  failed_when: grub_line.stdout == ""

- name: Set fact for existing_cmdline
  ansible.builtin.set_fact:
    existing_cmdline: "{{ grub_line.stdout }}"

- name: Set fedora grub file locations
  ansible.builtin.set_fact:
    grub_config_path_bios: /boot/grub2/grub.cfg
  when: ansible_distribution == "Fedora" and ansible_os_family == "RedHat"

- name: Show current GRUB_CMDLINE_LINUX
  ansible.builtin.debug:
    msg: "Current GRUB_CMDLINE_LINUX: {{ existing_cmdline }}"

- name: Updated GRUB_CMDLINE_LINUX
  ansible.builtin.debug:
    msg: "Current GRUB_CMDLINE_LINUX: {{ existing_cmdline | regex_replace(' *mem_sleep_default=\\S+', '') }} mem_sleep_default=deep"

- name: Ensure mem_sleep_default=deep is set in GRUB_CMDLINE_LINUX
  become: true
  vars:
    grub_cmdline_linux_line: >-
      GRUB_CMDLINE_LINUX="{{ existing_cmdline | regex_replace(' *mem_sleep_default=\\S+', '') }} mem_sleep_default=deep"
  ansible.builtin.lineinfile:
    path: /etc/default/grub
    regexp: '^GRUB_CMDLINE_LINUX='
    line: "{{ grub_cmdline_linux_line }}"

- name: Generate grub config for BIOS
  become: true
  changed_when: false
  ansible.builtin.command: grub2-mkconfig -o {{ grub_config_path_bios }}

- name: Create idle check and suspend script
  become: true
  ansible.builtin.copy:
    dest: /usr/local/bin/auto_suspend_script.sh
    mode: '0755'
    owner: root
    group: root
    content: "{{ lookup('file', 'scripts/auto_suspend.sh') }}"

- name: Create systemd service for auto suspend
  become: true
  ansible.builtin.copy:
    dest: /etc/systemd/system/auto-suspend.service
    content: |
      [Unit]
      Description=Check system idleness and suspend if idle
      After=multi-user.target

      [Service]
      Type=oneshot
      ExecStart=/usr/local/bin/auto_suspend_script.sh
    owner: root
    group: root
    mode: '0644'

- name: Create sleep.conf.d folder if it doesn't exist
  become: true
  ansible.builtin.file:
    path: /etc/systemd/sleep.conf.d
    state: directory
    owner: root
    group: root
    mode: '0755'

- name: Create sleep.conf.d for deep sleep
  become: true
  ansible.builtin.copy:
    dest: /etc/systemd/sleep.conf.d/mem-deep.conf
    content: |
      [Sleep]
      MemorySleepMode=deep
    owner: root
    group: root
    mode: '0644'

- name: Create systemd timer for auto suspend
  become: true
  ansible.builtin.copy:
    dest: /etc/systemd/system/auto-suspend.timer
    content: |
      [Unit]
      Description=Run idle suspend check every 10 minutes

      [Timer]
      OnBootSec=10min
      OnUnitActiveSec=10min
      Unit=auto-suspend.service

      [Install]
      WantedBy=timers.target
    owner: root
    group: root
    mode: '0644'

- name: Reload systemd daemon
  become: true
  ansible.builtin.systemd:
    daemon_reload: true

- name: Enable and start auto-suspend timer
  become: true
  ansible.builtin.systemd:
    name: auto-suspend.timer
    enabled: true
    state: started
