- name: Initialize homeserver
  hosts: "{{ chosen_host | default('staging1.test') }}"
  remote_user: "{{ chosen_user | default('senpai') }}"
  vars_files:
    - vars/email_settings.yml
    - vars/podpose_settings.yml # REQUIRED
    - vars/source_code.yml # REQUIRED
    - vars/caddy_settings.yml
    - vars/certbot_settings@homeserver.yml # REQUIRED
    - vars/mysql_settings.yml
    - vars/redis_settings.yml
    - vars/nextcloud_settings.yml
    - vars/gitea_settings.yml
    - vars/opengist_settings.yml
    - vars/vpn_settings.yml
    - vars/headscale_settings.yml
    - vars/tailscale_settings.yml
    - vars/glance_settings.yml
    - vars/surge_settings.yml
    - vars/aria_settings.yml
    - vars/flexget_settings.yml # REQUIRED
    - vars/git_aliases.yml # REQUIRED
    - vars/vim_settings.yml # REQUIRED
    - vars/tor_settings.yml # REQUIRED
    - vars/config@{{ inventory_hostname | default('homeserver') }}.yml
  tasks:
    - name: Saving HTTP/(S)FTP credentials
      ansible.builtin.include_role:
        name: init-server
        tasks_from: netrc.yml
        handlers_from: userspace
    - name: Installing requisite packages
      ansible.builtin.include_role:
        name: init-server
        tasks_from: userspace@install-pkgs
        handlers_from: userspace
    - name: Acquiring home of current user
      when: ansible_facts["system"] == "Linux"
      ansible.builtin.shell:
        cmd: "echo ~{{ ansible_user }}"
      register: ansible_user_home
    - name: Acquiring current user
      when: ansible_facts["system"] == "Linux"
      ansible.builtin.shell:
        cmd: "echo {{ ansible_user }}"
      register: current_user
    - name: Disabling root user shell login
      become: true
      ansible.builtin.user:
        name: root
        shell: /sbin/nologin
      tags:
        - disable_root_shell
    - name: Disable login for root user altogether
      become: true
      ansible.builtin.user:
        name: root
        password: "'*'"
      tags:
        - disable_root_login
    - name: Reorganizing userspace package groups into single list
      ansible.builtin.set_fact:
        all_userspace_pkgs: "{{ pkgs | dict2items(key_name='pkg_group', value_name='pkgs') | map(attribute='pkgs') | list |  map(attribute='userspace', default='no_userspace') | list | flatten | reject('search', 'no_userspace') | list }}"
    - name: Reorganizing core package groups into single list
      ansible.builtin.set_fact:
        all_core_pkgs: "{{ pkgs | dict2items(key_name='pkg_group', value_name='pkgs') | map(attribute='pkgs') | list | map(attribute='core', default='no_core') | list | flatten | reject('search', 'no_core') | list }}"
    - name: Configuring aliases for using git
      when: "'git' in all_core_pkgs or 'git' in all_userspace_pkgs"
      community.general.git_config:
        name: "alias.{{ item[0] }}"
        scope: global
        value: "{{ item[1] }}"
      loop: "{{ git_aliases }}"
    - name: Checking presence of dependency for flexget
      when: "'uv' in all_core_pkgs and flexget.enabled"
      block:
        - name: Installing flexget
          become: true
          become_user: "{{ current_user.stdout }}"
          ansible.builtin.command:
            argv:
              - uv
              - tool
              - install
              - "flexget[locked,ftp,sftp]"
              - "--with"
              - pysocks
        - name: Creating requisite download paths
          ansible.builtin.file:
            path: "{{ ansible_user_home.stdout }}/downloads/flexget/vids/{{ item }}/torrents"
            recurse: true
            owner: "{{ ansible_user }}"
            group: "{{ ansible_user }}"
            state: directory
            mode: "755"
          loop:
            - series
        - name: Creating requisite download paths
          ansible.builtin.file:
            path: "{{ ansible_user_home.stdout }}/downloads/flexget/vids/torrents"
            recurse: true
            owner: "{{ ansible_user }}"
            group: "{{ ansible_user }}"
            state: directory
            mode: "755"
        - name: Creating requisite hidden download paths
          ansible.builtin.file:
            path: "{{ ansible_user_home.stdout }}/downloads/.xxx/flexget/vids/torrents"
            recurse: true
            owner: "{{ ansible_user }}"
            group: "{{ ansible_user }}"
            state: directory
            mode: "755"
        - name: Creating requisite hidden download paths
          ansible.builtin.file:
            path: "{{ ansible_user_home.stdout }}/downloads/media/vids/{{ item }}"
            recurse: true
            owner: "{{ ansible_user }}"
            group: "{{ ansible_user }}"
            state: directory
            mode: "755"
          loop:
            - series
            - features
        - name: Creating requisite hidden file for film torrent links
          ansible.builtin.file:
            path: "{{ ansible_user_home.stdout }}/media/vids/{{ item }}.csv"
            owner: "{{ ansible_user }}"
            group: "{{ ansible_user }}"
            state: touch
            mode: "644"
          loop:
            - features/.films
        - name: Creating requisite hidden download paths
          ansible.builtin.file:
            path: "{{ ansible_user_home.stdout }}/downloads/.xxx/media/vids"
            recurse: true
            owner: "{{ ansible_user }}"
            group: "{{ ansible_user }}"
            state: directory
            mode: "755"
        - name: Creating requisite hidden file for NSFW video torrent links
          ansible.builtin.file:
            path: "{{ ansible_user_home.stdout }}/.xxx/.{{ item }}.csv"
            owner: "{{ ansible_user }}"
            group: "{{ ansible_user }}"
            state: touch
            mode: "644"
          loop:
            - vids
        - name: Configuring flexget
          become: true
          become_user: "{{ current_user.stdout }}"
          ansible.builtin.template:
            src: user/flexget/config.yml.j2
            variable_start_string: "<<"
            variable_end_string: ">>"
            dest: "{{ ansible_user_home.stdout }}/.flexget/config.yml"
            owner: "{{ ansible_user }}"
            group: "{{ ansible_user }}"
            force: true
            backup: true
            # validate: "flexget check"
        - name: Creating a CRON job for flexget
          ansible.builtin.cron:
            name: Run flexget
            minute: "*/60"
            hour: "4-15"
            weekday: "1-5"
            job: "{{ ansible_user_home.stdout }}/.local/bin/flexget --cron execute"
    - name: Checking presence of dependency
      when: "'nvm' in all_core_pkgs or 'nvm' in all_userspace_pkgs"
      block:
        - name: Installing SoulSeek CLI
          community.general.npm:
            name: "soulseek-cli"
            global: true
            state: present
    - name: Opening port 51820
      become: true
      ansible.builtin.iptables:
        chain: INPUT
        protocol: "{{ item }}"
        destination_port: 51820
        jump: ACCEPT
        comment: Open up port 51820
      loop:
        - udp
        - tcp
    - name: Opening port 443
      become: true
      ansible.builtin.iptables:
        chain: INPUT
        protocol: "{{ item }}"
        destination_port: 443
        jump: ACCEPT
        comment: Open up port 443
      loop:
        - udp
        - tcp
    - name: Opening ports
      become: true
      ansible.builtin.iptables:
        chain: INPUT
        protocol: tcp
        destination_port: "{{ item }}"
        jump: ACCEPT
        comment: "Open up port {{ (item | string) }}"
      loop:
        - 80
        - 465
        - 587
        - 995
        - 993