# Prefer booster to dracut so make sure to never install it
- name: Ignore dracut
  ansible.builtin.lineinfile:
    path: /etc/xbps.d/ignore-dracut.conf
    line: ignorepkg=dracut
    state: "present"
    create: true # create file if absent
  tags:
    - packages
    - dracut

- name: Update xbps and system
  community.general.xbps:
    name:
      - xbps
    state: latest
    update_cache: true
    upgrade: true
  tags:
    - packages
    - update

- name: Install Base system
  community.general.xbps:
    name:
      - base-system
    state: "{{ desired_package_state | default('present') }}"
  tags:
    - packages

- name: Get rid of temporary container metapackage
  community.general.xbps:
    name:
      - base-container-full
    state: absent
  tags:
    - packages

- name: Install booster
  community.general.xbps:
    name: booster
    state: "{{ desired_package_state | default('present') }}"
  notify: installed-booster
  tags:
    - packages
    - booster

- name: Set hostname
  ansible.builtin.template:
    src: hostname.j2
    dest: /etc/hostname
  tags: hostname

- name: Set timezone
  ansible.builtin.file:
    path: /etc/localtime
    src: /usr/share/zoneinfo/{{ timezone }}
    state: link
  tags: timezone

- name: Install glibc
  tags: glibc
  block:
  - name: Check if glibc locales exist
    ansible.builtin.stat:
      path: /etc/default/libc-locales
    register: libc_locales_file

  - name: Set correct glibc locales
    ansible.builtin.lineinfile:
      path: /etc/default/libc-locales
      regexp: "^{{ item }}"
      line: "{{ item }}"
      state: "present"
      create: true
    loop: "{{ locales_enabled }}"
    when: libc_locales_file.stat.exists
    notify: glibc-locales-changed

- name: Set up chrony for NTP management
  community.general.xbps:
    name:
      - chrony
    state: "{{ desired_package_state | default('present') }}"
  tags: chrony

- name: Activate chronyd service
  ansible.builtin.file:
    src: "/etc/sv/chronyd"
    dest: "/etc/runit/runsvdir/default/chronyd"
    state: link
  tags: chrony

- name: Activate acpid service
  ansible.builtin.file:
    src: "/etc/sv/acpid"
    dest: "/etc/runit/runsvdir/default/acpid"
    state: link
  tags: acpid

- name: Set up snooze as cron daemon
  tags:
    - cron
    - snooze
  block:
    - name: Install snooze
      community.general.xbps:
        name:
          - snooze
        state: "{{ desired_package_state | default('present') }}"

    - name: Activate snooze cron services
      ansible.builtin.file:
        force: "yes"
        src: "/etc/sv/{{ item }}"
        dest: "/etc/runit/runsvdir/default/{{ item }}"
        state: link
      loop:
        - snooze-hourly
        - snooze-daily
        - snooze-weekly
        - snooze-monthly

- name: Set up socklog as syslog daemon
  tags:
    - syslog
    - socklog
  block:
    - name: Install socklog
      community.general.xbps:
        name:
          - socklog-void
        state: "{{ desired_package_state | default('present') }}"

    - name: Activate snooze cron services
      ansible.builtin.file:
        force: "yes"
        src: "/etc/sv/{{ item }}"
        dest: "/etc/runit/runsvdir/default/{{ item }}"
        state: link
      loop:
        - socklog-unix
        - nanoklogd

    - name: Put user in socklog group
      ansible.builtin.user:
        name: "{{ user_name }}"
        groups: [socklog]
        append: true