---
- name: Ensure configuration file is present
  become: true
  ansible.builtin.template:
    src: "app.ini.j2"
    dest: "{{ forgejo_data_dir }}/gitea/conf/app.ini"
    mode: '0644'
    owner: 'debian'
    group: 'debian'
  notify: 'restart forgejo-app'

- name: Ensure forgejo.pod is present
  ansible.builtin.template:
    src: "forgejo.pod.j2"
    dest: "/home/debian/.config/containers/systemd/forgejo.pod"
    mode: '0755'
    owner: 'debian'
    group: 'debian'
  notify: 'restart forgejo-app'

- name: Ensure forgejo-app.container is present
  ansible.builtin.template:
    src: "forgejo-app.container.j2"
    dest: "/home/debian/.config/containers/systemd/forgejo-app.container"
    mode: '0755'
    owner: 'debian'
    group: 'debian'
  notify: 'restart forgejo-app'

- name: Ensure forgejo-postgres.container is present
  ansible.builtin.template:
    src: "forgejo-postgres.container.j2"
    dest: "/home/debian/.config/containers/systemd/forgejo-postgres.container"
    mode: '0755'
    owner: 'debian'
    group: 'debian'
  notify: 'restart forgejo-postgres'

- name: Ensure Caddyfile is present
  become: true
  ansible.builtin.template:
    src: 'forgejo.Caddyfile.j2'
    dest: '/etc/caddy/forgejo.Caddyfile'
    owner: root
    group: root
    mode: '0644'
  notify: reload caddy
  when: 'forgejo_domain is defined'

- name: Allow Forgejo SSH connections
  become: true
  community.general.ufw:
    port: "{{ forgejo_ssh_port }}"
    rule: 'allow'
  when: 'forgejo_ssh_port is defined'
