---
- debug:
    msg: "Performing installation of Xray..."

- name: create group for xray
  group:
    name: "{{ xray_group }}"
    state: present
  become: yes

- name: create user for xray
  user:
    name: "{{ xray_user }}"
    group: "{{ xray_group }}"
    system: yes
  become: yes

- name: ensure jfrog_home_directory exists
  file:
    path: "{{ jfrog_home_directory }}"
    state: directory
  become: yes

- name: download xray
  unarchive:
    src: "{{ xray_tar }}"
    dest: "{{ jfrog_home_directory }}"
    remote_src: yes
    owner: "{{ xray_user }}"
    group: "{{ xray_group }}"
    creates: "{{ xray_untar_home }}"
  become: yes
  register: downloadxray
  until: downloadxray is succeeded
  retries: 3

- name: MV untar directory to xray home
  command: "mv {{ xray_untar_home }} {{ xray_home }}"
  become: yes

- debug:
    msg: "Running dependency installation for {{ ansible_os_family }}"

- name: perform dependency installation
  include_tasks: "{{ ansible_os_family }}.yml"

- name: ensure etc exists
  file:
    path: "{{ xray_home }}/var/etc"
    state: directory
    owner: "{{ xray_user }}"
    group: "{{ xray_group }}"
  become: yes

- name: configure system yaml
  template:
    src: system.yaml.j2
    dest: "{{ xray_home }}/var/etc/system.yaml"
  become: yes

- name: ensure {{ xray_home }}/var/etc/security/ exists
  file:
    path: "{{ xray_home }}/var/etc/security/"
    state: directory
    owner: "{{ xray_user }}"
    group: "{{ xray_group }}"
  become: yes

- name: configure master key
  template:
    src: master.key.j2
    dest: "{{ xray_home }}/var/etc/security/master.key"
  become: yes

- name: configure join key
  template:
    src: join.key.j2
    dest: "{{ xray_home }}/var/etc/security/join.key"
  become: yes

- name: ensure {{ xray_home }}/var/etc/info/ exists
  file:
    path: "{{ xray_home }}/var/etc/info/"
    state: directory
    owner: "{{ xray_user }}"
    group: "{{ xray_group }}"
  become: yes

- name: configure installer info
  template:
    src: installer-info.json.j2
    dest: "{{ xray_home }}/var/etc/info/installer-info.json"
  become: yes

- name: create xray service
  shell: "{{ xray_home }}/app/bin/installService.sh"
  become: yes

- name: start and enable xray
  service:
    name: xray
    state: restarted
  become: yes