---

#   # The subvol collection seems to be misbehaving?
#   # https://github.com/ansible-collections/community.general/issues/7098
# # (and btrfs_subvol module working)
# - name: Create void btrfs subvol
#   hosts: host
#   vars:
#     btrfs_target: 441a90a5-3da2-46ce-9e32-987569b746c9
#     mount_dir: /mnt/void
#   become: true
#   tasks:
#     - name: Create @voidroot subvool under root
#       community.general.btrfs_subvolume:
#         state: present
#         name: "/@"
#         filesystem_uuid: "{{ btrfs_target }}"


- name: Create voidlinux guest
  hosts: host
  become: true
  vars:
    mount_dir: /mnt/void
    tarball_url: "https://repo-default.voidlinux.org/live/current/void-x86_64-ROOTFS-20250202.tar.xz"
  tasks:
    - name: Unpack rootfs
      ansible.builtin.unarchive:
        remote_src: yes # we already downloaded it to the 'remote' system
        src: "{{ tarball_url }}"
        dest: "{{ mount_dir }}"
        # ONLY run if this file does not exist (could use any rootfs file to check)
        creates: "{{ mount_dir }}/etc/os-release"


# Get python onto void - otherwise we can not interact through ansible
- name: Bootstrap void python
  hosts: guest
  gather_facts: false
  tasks:
    - name: install python
      raw: test -e /usr/bin/python || xbps-install -y python
      register: python_install
      changed_when: "'installed successfully' in python_install.stdout"


- name: Void prep
  hosts: guest
  become: true
  vars:
    ansible_chroot_exe: arch-chroot
    disable_root_check: true
  tasks:
    # 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

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

    - name: Install Base system
      community.general.xbps:
        name:
          - base-system
        state: present

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

    - name: Install booster
      community.general.xbps:
        name: booster
        state: present