---
- name: Install CGRateS dependencies
  become: true
  ansible.builtin.package:
    name: "{{ cgrates_dependencies }}"
    state: present
    update_cache: true

- name: Create cgrates directory
  ansible.builtin.file:
    state: directory
    mode: u=rwx,go=rx
    owner: "{{ ansible_user }}"
    dest: "{{ cgrates_dir }}"
  when: cgrates_clone_repo | bool

- name: Git clone cgrates
  ansible.builtin.git:
    repo: https://github.com/cgrates/cgrates.git
    dest: "{{ cgrates_dir }}"
    update: true
    force: true
    version: "{{ cgrates_branch }}"
  when: cgrates_clone_repo | bool

- name: Install cgrates binaries
  ansible.builtin.command:
    cmd: bash -c './build.sh'
    chdir: "{{ cgrates_dir }}"
  environment:
    PATH: "{{ ansible_env.PATH }}:/usr/local/go/bin"

- name: Create cgrates data folder symlink
  ansible.builtin.file:
    src: "{{ cgrates_dir }}/data"
    dest: "{{ cgrates_data_path }}"
    state: link
  become: true
  when: cgrates_data_path | length > 0

- name: Create cgrates binary symlinks
  ansible.builtin.file:
    src: "{{ golang_gopath }}/bin/{{ item }}"
    dest: "{{ cgrates_bin_path }}/{{ item }}"
    state: link
  become: true
  when: cgrates_bin_path | length > 0
  loop:
    - cgr-engine
    - cgr-loader
    - cgr-migrator
    - cgr-console
    - cgr-tester

- name: Run post install scripts
  become: true
  ansible.builtin.command:
    cmd: "{{ item.cmd }}"
    chdir: "{{ cgrates_dir }}/data/storage/{{ item.db }}"
  when: item.db in cgrates_dbs
  loop:
    - db: mysql
      cmd: sh setup_ers_db.sh root CGRateS.org localhost
    - db: mysql
      cmd: sh setup_cgr_db.sh root CGRateS.org localhost
    - db: postgres
      cmd: sh create_db_with_users.sh
    - db: postgres
      cmd: sh create_ers_db.sh
    - db: mongo
      cmd: sh setup_cgr_db.sh
