---
- name: Acquiring home of current user
  when: ansible_facts["system"] == "Linux"
  ansible.builtin.shell:
    cmd: "echo ~{{ ansible_user }}"
  register: ansible_user_home
- name: Acquiring home of current user
  when: ansible_facts["system"] == "Linux"
  ansible.builtin.shell:
    cmd: "echo {{ ansible_user }}"
  register: current_user
- name: Informing user of requirement of two main domains 
  when: (certbot.domains | length) < 2 or (certbot.domains | length) > 2
  ansible.builtin.fail:
    msg: Only two domains allowed and required
- name: Informing user of requirement at least one wildcard
  when: (certbot.domains | select("regex", "^\\*\\.") | list | length) == 0
  ansible.builtin.fail:
    msg: At least one of the FQDNs must have a wildcard
# - name: Setting the FQDN for development
#   when: compose.mode == "dev"
#   ansible.builtin.set_fact:
#     web_fqdn: "{{ (certbot.domains | map('regex_replace', '\\.([^\\.]*)$', '.test') | reject('regex', '^\\*\\.') | list)[0] }}"
- name: Setting the FQDN
  # when: compose.mode == "prod"
  ansible.builtin.set_fact:
    web_fqdn: "{{ (certbot.domains | reject('regex', '^\\*\\.') | list)[0] }}"
- name: Moving git repository from initial path
  block:
    - name: Recursively copying directory and its contents to elsewhere
      ansible.builtin.copy:
        src: "{{ ansible_user_home.stdout }}/repos/.foreign/{{ source_code.repos.blog.name }}"
        remote_src: true
        dest: "{{ ansible_user_home.stdout }}/repos/"
        owner: "{{ ansible_user }}"
        group: "{{ ansible_user }}"
      register: new_blog_path
    - name: Deleting directory at previous path
      ansible.builtin.file:
        path: "{{ ansible_user_home.stdout }}/repos/.foreign/{{ source_code.repos.blog.name }}"
        state: absent
    - name: Creating directory for new bare repository
      ansible.builtin.file:
        path: "{{ ansible_user_home.stdout }}/src"
        recurse: true
        owner: "{{ ansible_user }}"
        group: "{{ ansible_user }}"
        state: directory
    - name: Creating bare repository
      ansible.builtin.command:
        cmd: "git init --bare {{ source_code.repos.blog.name }}.git"
        chdir: "{{ ansible_user_home.stdout }}/src"
        creates: "{{ ansible_user_home.stdout }}/src/{{ source_code.repos.blog.name }}.git"
- name: Running Hugo blog
  when: source_code.repos.blog.run
  block:
    - name: Creating path for Hugo content files
      when: caddy.containerized and (compose.containers.webserver is defined or compose.containers.webserver != None)
      ansible.builtin.file:
        path: "{{ ansible_user_home.stdout }}/journal/blog"
        recurse: true
        owner: "{{ ansible_user }}"
        group: "{{ ansible_user }}"
        mode: "755"
        state: directory
    - name: Creating some initial text content for Hugo
      become: true
      become_user: "{{ current_user.stdout }}"
      when: caddy.containerized and (compose.containers.webserver is defined or compose.containers.webserver != None)
      ansible.builtin.template:
        src: "user/journal/blog/_index.md.j2"
        dest: "{{ ansible_user_home.stdout }}/journal/blog/index.md"
        owner: "{{ ansible_user }}"
        group: "{{ ansible_user }}"
        mode: "644"
        backup: true
    - name: Creating some initial image content for Hugo
      become: true
      become_user: "{{ current_user.stdout }}"
      when: caddy.containerized and (compose.containers.webserver is defined or compose.containers.webserver != None)
      ansible.builtin.copy:
        src: "user/journal/mythe-sisyphus-klein.png"
        dest: "{{ ansible_user_home.stdout }}/journal/blog/mythe-sisyphus-klein.png"
        owner: "{{ ansible_user }}"
        group: "{{ ansible_user }}"
        mode: "644"
        backup: true
    - name: Creating subdirectory for blog website root
      when: caddy.containerized and (compose.containers.webserver is defined or compose.containers.webserver != None)
      ansible.builtin.file:
        path: "{{ ansible_user_home.stdout }}/srv/blog.{{ web_fqdn }}"
        owner: "{{ ansible_user }}"
        group: "{{ ansible_user }}"
        mode: "755"
        state: directory
    - name: Build Hugo blog at additional webroot
      when: caddy.containerized and (compose.containers.webserver is defined or compose.containers.webserver != None)
      ansible.builtin.command:
        argv:
          - hugo
          - "--quiet"
          - "-b"
          - "https://blog.{{ web_fqdn }}/"
          - "-c"
          - "{{ ansible_user_home.stdout }}/journal/blog"
          - "-d"
          - "{{ ansible_user_home.stdout }}/srv/blog.{{ web_fqdn }}"
          - "--cleanDestinationDir"
        chdir: "{{ new_blog_path.dest }}"
        creates: "{{ ansible_user_home.stdout }}/srv/blog.{{ web_fqdn }}/index.html"
    # @TODO write error Caddy template HTML file at the blog's web root
    - name: Adding Caddyfile subdomain entry
      when: caddy.containerized and mode == 'prod' and (compose.containers.webserver is defined or compose.containers.webserver != None)
      ansible.builtin.blockinfile:
        path: "{{ ansible_user_home.stdout }}/.config/caddy/Caddyfile"
        block: |
          blog.{{ web_fqdn }} {
            respond 503
            # root /srv/blog.{{ web_fqdn }}
            file_server
            encode gzip

            handle_errors {
              root /srv/{{ web_fqdn }}
              rewrite /error/{err.status_code}.html
              templates
            }
          }
        prepend_newline: true
        marker: "# <-- {mark} ANSIBLE MANAGED BLOG DOMAIN -->"
        owner: "{{ ansible_user }}"
        group: "{{ ansible_user }}"
        mode: "644"
        state: present
    - name: Adding Caddyfile subdomain entry
      when: caddy.containerized and (mode == 'dev' or caddy.scheme == 'http') and (compose.containers.webserver is defined or compose.containers.webserver != None)
      ansible.builtin.blockinfile:
        path: "{{ ansible_user_home.stdout }}/.config/caddy/Caddyfile"
        block: |
          http://blog.{{ web_fqdn }} {
            respond 503
            # root /srv/blog.{{ web_fqdn }}
            file_server
            encode gzip

            handle_errors {
              root /srv/{{ web_fqdn }}
              rewrite /error/{err.status_code}.html
              templates
            }
          }
        prepend_newline: true
        marker: "# <-- {mark} ANSIBLE MANAGED BLOG DOMAIN -->"
        owner: "{{ ansible_user }}"
        group: "{{ ansible_user }}"
        mode: "644"
        state: present
- name: Checking the status of podman containers
  ansible.builtin.command:
    argv:
      - podman
      - ps
  register: podman_status
- name: Restarting webserver / reverse proxy container
  become: true
  become_user: "{{ current_user.stdout }}"
  when: "'revproxy0' in podman_status.stdout and (caddy is defined and caddy != None) and caddy.containerized"
  containers.podman.podman_container:
    name: "{{ compose.containers.webserver.name }}"
    state: started
    force_restart: true