---
- name: download gitea binary
  ansible.builtin.get_url:
    url: "https://dl.gitea.io/gitea/{{ gitea.version }}/gitea-{{ gitea.version }}-linux-amd64"
    dest: /usr/local/sbin/gitea
    mode: 0755
  notify: systemctl restart gitea.service

- name: create user git
  ansible.builtin.user:
    name: git
    system: true
    shell: /bin/bash
    comment: "Git Version Control"
    home: "{{ gitea.home_path }}"

- name: create gitea directories
  ansible.builtin.file:
    path: '{{ item.path }}'
    mode: '{{ item.mode | default("0755") }}'
    group: git
    owner: '{{ item.owner | default("git") }}'
    state: directory
  loop:
    - { path: /etc/gitea/, mode: "0750", owner: root }
    - { path: "{{ gitea.data_path }}", mode: "0750" }
    - path: "{{ (gitea.data_path, 'custom') | path_join }}"
    - path: "{{ (gitea.data_path, 'data') | path_join }}"
    - path: "{{ (gitea.data_path, 'log') | path_join }}"
  notify: systemctl restart gitea.service

- name: deploy gitea configuration
  ansible.builtin.template:
    src: gitea/app.ini.j2
    dest: /etc/gitea/app.ini
    mode: '0640'
    group: git
  notify: systemctl restart gitea.service

- name: deploy systemd unit
  ansible.builtin.template:
    src: systemd/system/gitea.service.j2
    dest: /etc/systemd/system/gitea.service
  notify: systemctl daemon-reload

- name: systemctl enable gitea.service
  ansible.builtin.systemd:
    name: gitea
    enabled: true