---
- name: Tinc secret directory
  file:
    path: "{{ secretdir_tinc }}/{{ tinc_name }}"
    state: directory
    mode: 0700
  delegate_to: localhost

- name: Tinc secret directory / hosts subdirectory
  file:
    path: "{{ secretdir_tinc }}/{{ tinc_name }}/hosts"
    state: directory
    mode: 0700
  delegate_to: localhost

- include: keypair.yaml
  vars:
    _destdir: "{{ secretdir_tinc }}/{{ tinc_name }}"

- name: Tinc work directory
  file:
    path: "{{ workdir_tinc }}/{{ tinc_name }}"
    state: directory
    mode: 0750
  delegate_to: localhost

- name: Tinc work directory / hosts subdirectory
  file:
    path: "{{ workdir_tinc }}/{{ tinc_name }}/hosts"
    state: directory
    mode: 0750
  delegate_to: localhost

- include: host.yaml
  vars:
    _tinc_host_name: "{{ item['tinc_hostname'] }}"
    _tinc_host_addresses: "{{ item['tinc_addresses'] | default([]) }}"
    _destdir: "{{ workdir_tinc }}/{{ tinc_name }}/hosts"
    _key: "{{ lookup('file', secretdir_tinc + '/' + tinc_name + '/hosts/' + _tinc_host_name) }}"
  with_items: "{{ tinc_hosts }}"

- name: Tinc create staging directory
  file:
    path: "{{ stagedir }}/{{ inventory_hostname }}{{ basedir_conf_tinc }}/{{ tinc_name }}"
    state: directory
    mode: 0750
  delegate_to: localhost

- include: tinc-conf.yaml
  vars:
    _destdir: "{{ stagedir }}/{{ inventory_hostname }}{{ basedir_conf_tinc }}/{{ tinc_name }}"
    _connecthosts: "{{ tinc_hosts | selectattr('tinc_addresses', 'defined') | map(attribute='tinc_hostname') | list }}"

- include: tinc-up.yaml
  vars:
    _destdir: "{{ stagedir }}/{{ inventory_hostname }}{{ basedir_conf_tinc }}/{{ tinc_name }}"
    _hostnumber: "{{ tinc_hosts | selectattr('tinc_hostname', 'equalto', tinc_hostname) | map(attribute='tinc_hostnumber') | first }}"

- name: Tinc stage hosts directory
  copy:
    src: "{{ workdir_tinc }}/{{ tinc_name }}/hosts"
    dest: "{{ stagedir }}/{{ inventory_hostname }}{{ basedir_conf_tinc }}/{{ tinc_name }}"
    mode: 0640
    directory_mode: 0750
  delegate_to: localhost
 
- name: Tinc stage private key
  copy:
    src: "{{ secretdir_tinc }}/{{ tinc_name }}/{{ tinc_hostname }}.priv"
    dest: "{{ stagedir }}/{{ inventory_hostname }}{{ basedir_conf_tinc }}/{{ tinc_name }}/rsa_key.priv"
    mode: 0600
  delegate_to: localhost

- name: Tinc rc.d directory (FreeBSD)
  file:
    path: "{{ stagedir }}/{{ inventory_hostname }}{{ os_dir_etc }}/rc.d"
    state: directory
    mode: 0755
  delegate_to: localhost

- name: Tinc rc.conf.d directory (FreeBSD)
  file:
    path: "{{ stagedir }}/{{ inventory_hostname }}{{ os_dir_etc }}/rc.conf.d"
    state: directory
    mode: 0755
  delegate_to: localhost

- name: Tinc rc.conf.d/tincd directory (FreeBSD)
  file:
    path: "{{ stagedir }}/{{ inventory_hostname }}{{ os_dir_etc }}/rc.conf.d/tincd"
    state: directory
    mode: 0755
  delegate_to: localhost

- include: rc-freebsd.yaml
  vars:
    _rcddir: "{{ stagedir }}/{{ inventory_hostname }}{{ os_dir_etc }}/rc.d"
    _rcconfdir: "{{ stagedir }}/{{ inventory_hostname }}{{ os_dir_etc }}/rc.conf.d/tincd"
    _tinc_fib: "{{ tinc_hosts | selectattr('tinc_hostname', 'equalto', tinc_hostname) | map(attribute='tinc_fib') | first | default(omit) }}"
  when: tinc_name is defined

