---
- name: 'Install required software'
  become: true
  ansible.builtin.apt:
    name: 'podman'
    state: 'present'

# Build image to run uffd in
- name: 'Create temporary build directory'
  become: true
  ansible.builtin.file:
    path: '{{ build_uffd_tempdir }}'
    state: 'directory'
    mode: 'u=rwx,g=rx,o=rx'

- name: 'Copy static files for image'
  become: true
  ansible.builtin.copy:
    src: '{{ item.file }}'
    dest: '{{ build_uffd_tempdir }}/{{ item.file }}'
    owner: 'root'
    group: 'root'
    mode: '{{ item.mode }}'
  loop:
    - file: 'cccv-archive-key.asc'
      mode: 'u=rw,g=r,o=r'
    - file: 'entrypoint.sh'
      mode: 'u=rwx,g=rx,o=rx'
    - file: '{{ build_uffd_package_name }}'
      mode: 'u=rw,g=r,o=r'

- name: 'Copy templates for image'
  become: true
  ansible.builtin.template:
    src: '{{ item.file }}'
    dest: '{{ build_uffd_tempdir }}/{{ item.file }}'
    owner: 'root'
    group: 'root'
    mode: '{{ item.mode }}'
  loop:
    - file: 'cccv-archive.list'
      mode: 'u=rw,g=r,o=r'
    - file: 'Containerfile'
      mode: 'u=rw,g=r,o=r'

- name: 'Create recent uffd image'
  become: true
  containers.podman.podman_image:
    build:
      cache: false
      force_rm: true
      rm: true
    pull: false
    push: false
    force: '{{ build_uffd_force_rebuild }}'
    name: 'uffd'
    path: '{{ build_uffd_tempdir }}'
    tag: '{{ ansible_date_time.iso8601_basic_short }}'
  register: 'created_image_data'

- name: 'Tag image as latest'
  when: 'build_uffd_tag_latest'
  become: true
  containers.podman.podman_tag:
    image: '{{ created_image_data.image[0].NamesHistory[0] }}'
    target_names:
      - 'uffd:latest'
  register: 'created_image_data'

- name: 'Cleanup temporary build directory'
  become: true
  ansible.builtin.file:
    state: 'absent'
    path: '{{ build_uffd_tempdir }}'
