---
- 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 for production
  # when: compose.mode == "prod"
  ansible.builtin.set_fact:
    web_fqdn: "{{ (certbot.domains | reject('regex', '^\\*\\.') | list)[0] }}"
- name: Linking repository to another path
  ignore_errors: true
  ansible.builtin.file:
    src: "{{ ansible_user_home.stdout }}/repos/.foreign/quartz"
    dest: "{{ ansible_user_home.stdout }}/repos/skato-quartz"
    state: link
- name: Installing NodeJS dependencies of quartz software
  become: true
  become_user: "{{ current_user.stdout }}"
  community.general.npm:
    path: "{{ ansible_user_home.stdout }}/repos/.foreign/quartz"
    state: latest
- name: Configuring quartz software
  block:
    - name: Creating path for Quartz content files (path for Obsidian vaults)
      ansible.builtin.file:
        path: "{{ ansible_user_home.stdout }}/journal/notes"
        recurse: true
        owner: "{{ ansible_user }}"
        group: "{{ ansible_user }}"
        mode: "755"
        state: directory
    - name: Creating some initial text content for Quartz
      become: true
      become_user: "{{ current_user.stdout }}"
      ansible.builtin.template:
        src: "user/journal/notes/index.md.j2"
        dest: "{{ ansible_user_home.stdout }}/journal/notes/index.md"
        owner: "{{ ansible_user }}"
        group: "{{ ansible_user }}"
        mode: "644"
        backup: true
    - name: Creating some initial image content for Quartz
      become: true
      become_user: "{{ current_user.stdout }}"
      ansible.builtin.copy:
        src: user/journal/mythe-sisyphus-klein.png
        dest: "{{ ansible_user_home.stdout }}/journal/notes/mythe-sisyphus-klein.png"
        owner: "{{ ansible_user }}"
        group: "{{ ansible_user }}"
        mode: "644"
        backup: true
    - name: Initializing quartz website
      become: true
      become_user: "{{ current_user.stdout }}"
      ansible.builtin.command:
        argv:
          - npx 
          - "-y"
          - quartz
          - create
          - "-b"
          - notes.{{ web_fqdn }}"
          - "-t"
          - obsidian
          - "-s"
          - "{{ ansible_user_home.stdout }}/journal/notes"
          - "-X"
          - symlink
        chdir: "{{ ansible_user_home.stdout }}/repos/.foreign/quartz"
        creates: "{{ ansible_user_home.stdout }}/repos/.foreign/quartz/content/index.md"
    - name: Installing quartz plugins referenced in website template
      become: true
      become_user: "{{ current_user.stdout }}"
      ansible.builtin.command:
        argv:
          - npx
          - "-y"
          - quartz
          - plugin
          - install
        chdir: "{{ ansible_user_home.stdout }}/repos/.foreign/quartz"
- name: Starting quartz site web server
  become: true
  become_user: "{{ current_user.stdout }}"
  ansible.builtin.command:
    argv:
      - npx
      - "-y"
      - quartz
      - build
      - "-o"
      - "{{ ansible_user_home.stdout }}/srv/notes.{{ web_fqdn }}"
      - "--concurrency"
      - "3"
    chdir: "{{ ansible_user_home.stdout }}/repos/.foreign/quartz"
    creates: "{{ ansible_user_home.stdout }}/srv/notes.{{ web_fqdn }}/index.html"
- name: Creating directory for caddy configuration
  ansible.builtin.file:
    path: "{{ ansible_user_home.stdout }}/.config/caddy"
    recurse: true
    owner: "{{ ansible_user }}"
    group: "{{ ansible_user }}"
    state: directory
- name: Adding Caddyfile subdomain entry
  when: "(caddy is defined and caddy != None) and caddy.containerized and mode == 'prod'"
  ansible.builtin.blockinfile:
    path: "{{ ansible_user_home.stdout }}/.config/caddy/Caddyfile"
    block: |
      notes.{{ web_fqdn }} {
        respond 503
        # root /srv/notes.{{ 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 NOTES DOMAIN -->"
    owner: "{{ ansible_user }}"
    group: "{{ ansible_user }}"
    mode: "644"
    create: true
    state: present
- name: Adding Caddyfile subdomain entry
  when: "(caddy is defined and caddy != None) and caddy.containerized and (mode == 'dev' or caddy.scheme == 'http')"
  ansible.builtin.blockinfile:
    path: "{{ ansible_user_home.stdout }}/.config/caddy/Caddyfile"
    block: |
      http://notes.{{ web_fqdn }} {
        respond 503
        # root /srv/notes.{{ 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 NOTES DOMAIN -->"
    owner: "{{ ansible_user }}"
    group: "{{ ansible_user }}"
    mode: "644"
    create: true
    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