---
- hosts: all
  vars:
    ###############################################################
    ##################### Golang Vars #############################
    ###############################################################
    # Go language SDK version number
    golang_version: '1.13'
    go_version_target: "go version go{{ golang_version }} linux/amd64"
    # Mirror to download the Go language SDK redistributable package from
    golang_mirror: 'https://storage.googleapis.com/golang'
    # Base installation directory the Go language SDK distribution
    golang_install_dir: '/usr/local/go'
    # Directory to store files downloaded for Go language SDK installation
    golang_download_dir: "{{ x_ansible_download_dir | default(ansible_env.HOME + '/.ansible/tmp/downloads') }}"
    # Location for GOPATH environment variable
    golang_gopath: "/home/{{ gouser }}/go"
    # Filename of Go language SDK redistributable package
    golang_redis_filename: 'go{{ golang_version }}.linux-amd64.tar.gz'
    ###############################################################
    # CGRateS location
    cgrates_dir: "{{ golang_gopath }}/src/github.com/cgrates/cgrates"

    rootUser : root

    freeswitch_packages:
      - freeswitch-meta-all
      - freeswitch-mod-json-cdr

    dependencies:
      - build-essential
      - git
      - redis-server
      - mariadb-server
      - postgresql
      - postgresql-contrib
      - python-dev
      - gcc
      - make
      - binutils
      - libasound2-dev

    customPath: "{{ lookup('env','PATH') }}:{{ golang_gopath }}/bin:/usr/local/go/bin:{{ ansible_env.PATH }}"

  remote_user: '{{ gouser }}'
  tasks:
###########################################################################################################################
# install dependencies
  - name: Install dependencies
    become: yes
    apt: name={{ dependencies }} state=present

  - name: Add key for freeswitch
    become: yes
    apt_key: url=https://files.freeswitch.org/repo/deb/debian-release/fsstretch-archive-keyring.asc state=present

  - name: Add FreeSwitch apt repository (1.8) (Debian 8/Jessie)
    become: yes
    apt_repository:
      repo: deb http://files.freeswitch.org/repo/deb/debian-release/ stretch main
      state: present
      filename: 'freeswitch'

  # Install FreeSwitch
  - name: Install FreeSwitch
    become: yes
    apt: name={{ freeswitch_packages }} state=latest

###########################################################################################################################
# Install Golang
  - name: install unarchive dependencies (zypper)
    become: yes
    zypper:
      name:
        - gzip
        - tar
      state: present
    when: ansible_pkg_mgr == 'zypper'

  - name: Install golang
    include: go.yaml

###########################################################################################################################
# Install and config CGRateS
  - name: Install and config CGRateS
    include: cgrates.yaml

# Configure FreeSwitch
  - name:  Unzip FreeSWITCH config
    become: yes
    shell: 'sudo tar -xvf freeswitch_conf.tar.gz'
    args:
      chdir: '{{ cgrates_dir }}/data/tutorial_tests/fs_evsock/freeswitch/etc'

  - name:  Remove FreeSWITCH default config from /etc/freesitch
    become: yes
    shell: 'sudo rm -rf *'
    args:
      chdir: '/etc/freeswitch'

  - name:  Copy our custom config for FreeSWITCH in /etc/freesitch
    become: yes
    shell: 'sudo cp -r {{ cgrates_dir }}/data/tutorial_tests/fs_evsock/freeswitch/etc/freeswitch/* /etc/freeswitch'
    args:
      chdir: '/etc/freeswitch'

# Configure PJSUA
  - name: Config PJSUA
    include: pjsua.yaml
