---
- name: Gitea Playbook (K8s)
  hosts: gitea
  gather_facts: true

# Specific tasks for this playbook
  tasks:
    - name: Set required facts
      ansible.builtin.set_fact:
        gitea_home: "{{ persistent_data | default('/var/lib') }}/gitea"
        gitea_mysql_home: "{{ database_path | default('/var/lib') }}/mysql.gitea"
        gitea_user_id: 1000
        gitea_group_id: 1000
        mysql_user_id: 999
        mysql_group_id: 107
        gitea_sshd_config: "/etc/ssh/sshd_config.d/03-gitea.conf"
        gitea_scripts:
          - "scripts/gitea-shell"
      tags: always

    - name: Copy local scripts to remote
      become: true
      ansible.builtin.copy:
        src: "{{ item }}"
        dest: "/usr/local/bin/{{ item | basename }}"
        owner: root
        group: root
        mode: "0755"
      loop: "{{ gitea_scripts }}"
      tags:
        - tasks
        - tasks::scripts

    - name: Copy sshd_config.d gitea files to remote
      become: true
      ansible.builtin.copy:
        src: "configs/sshd_config.d/gitea.conf"
        dest: "{{ gitea_sshd_config }}"
        owner: root
        group: root
        mode: "0600"
      tags:
        - tasks
        - tasks::sshd_config
        - tasks::configs

    - name: Create required gitea data folder
      become: true
      ansible.builtin.file:
        state: directory
        path: "{{ gitea_home }}"
        mode: "0750"
        owner: "{{ gitea_user_id }}"
        group: "{{ gitea_group_id }}"
      tags:
        - tasks
        - tasks::folders

    - name: Create required mysql data folder
      become: true
      ansible.builtin.file:
        state: directory
        path: "{{ gitea_mysql_home }}"
        mode: "0750"
        owner: "{{ mysql_user_id }}"
        group: "{{ mysql_group_id }}"
      tags:
        - tasks
        - tasks::folders
