---
- name: Perform optional versionscheck
  ansible.builtin.include_tasks:
    file: 'versioncheck.yml'
  when: submodules_versioncheck|bool

- name: Gather installed packages for checks later on
  ansible.builtin.package_facts:
    manager: 'auto'

- name: Prepare gitea/forgejo variable import
  block:
    - name: Gather variables for gitea or forgejo
      ansible.builtin.include_vars:
        file: "{{ lookup('first_found', gitea_fork_variables) }}"
  rescue:
    - name: Gitea/Forejo import info
      ansible.builtin.fail:
        msg: "Currently only {{ gitea_supported_forks }} are supported."

- name: Gather variables for each operating system
  ansible.builtin.include_vars:
    file: "{{ lookup('first_found', gitea_variables) }}"

- name: Gather versioning information
  ansible.builtin.include_tasks:
    file: "set_{{ gitea_fork | lower }}_version.yml"

- name: Backup gitea before update
  ansible.builtin.include_tasks:
    file: 'backup.yml'
  when: gitea_backup_on_upgrade|bool

- name: Create gitea user and role
  ansible.builtin.include_tasks:
    file: 'create_user.yml'

- name: "Install or update {{ gitea_fork }}"
  ansible.builtin.include_tasks:
    file: "install_{{ gitea_fork | lower }}.yml"

- name: Create directories
  ansible.builtin.include_tasks:
    file: 'directory.yml'

- name: Setup gitea systemd service
  ansible.builtin.include_tasks:
    file: 'install_systemd.yml'
  when: ansible_service_mgr == "systemd"

- name: Generate JWT Secrets if undefined
  ansible.builtin.include_tasks:
    file: 'jwt_secrets.yml'

- name: Generate gitea secrets if undefined
  ansible.builtin.include_tasks:
    file: 'gitea_secrets.yml'

- name: Configure gitea
  ansible.builtin.include_tasks:
    file: 'configure.yml'

- name: Deploy optional fail2ban rules
  ansible.builtin.include_tasks:
    file: 'fail2ban.yml'
  when: gitea_fail2ban_enabled | bool

- name: Optionally customize gitea
  ansible.builtin.include_tasks:
    file: 'customize_logo.yml'
  when: gitea_customize_logo | bool

- name: Optionally customize footer
  ansible.builtin.include_tasks:
    file: 'customize_footer.yml'
  when: gitea_customize_footer | bool

- name: Optionally deploy public files
  ansible.builtin.include_tasks:
    file: 'customize_public_files.yml'
  when: gitea_customize_files | bool