---

- name: Install Asterisk and its dependencies
  yum: name={{ fpbx_packages }}
  tags: fpbx

- name: Install music on hold and languages packages
  yum: name={{ fpbx_moh_pkg + fpbx_snd_pkg }}
  tags: fpbx

- name: Link MariaDB ODBC connector to emulate MySQL connector
  file: src=/usr/lib64/libmaodbc.so dest=/usr/lib64/libmyodbc5.so state=link
  tags: fpbx

- name: Create a user for provisioning
  user:
    name: phone
    home: "{{ fpbx_root_dir }}/provisioning"
    shell: /bin/rbash
    password: "{{ fpbx_phone_pass | password_hash('sha256', 65535 | random(seed=inventory_hostname) | string) }}"
  tags: fpbx

- when: fpbx_install_mode != 'none'
  block:
    - name: Download FreePBX
      get_url:
        url: "{{ fpbx_archive_url }}"
        dest: "{{ fpbx_root_dir }}/tmp/"
        checksum: "sha256:{{ fpbx_archive_sha256 }}"
    
    - name: Extract fpbx archive
      unarchive:
        src: "{{ fpbx_root_dir }}/tmp/freepbx-{{ fpbx_version }}-latest.tgz"
        dest: "{{ fpbx_root_dir }}/tmp"
        remote_src: True

  tags: fpbx

# Create the FreePBX database
- import_tasks: ../includes/webapps_create_mysql_db.yml
  vars:
    - db_name: "{{ fpbx_db_name }}"
    - db_user: "{{ fpbx_db_user }}"
    - db_server: "{{ fpbx_db_server }}"
    - db_pass: "{{ fpbx_db_pass }}"
    - append_privs: True
  tags: fpbx

# Create the CDR database
- import_tasks: ../includes/webapps_create_mysql_db.yml
  vars:
    - db_name: "{{ fpbx_cdr_db_name }}"
    - db_user: "{{ fpbx_db_user }}"
    - db_server: "{{ fpbx_db_server }}"
    - db_pass: "{{ fpbx_db_pass }}"
    - append_privs: True
  tags: fpbx

- when: fpbx_install_mode == 'install'
  block:
    - name: Ensure asterisk is running
      service: name=asterisk state=started
    
    - name: Remove config file before installation
      file: path={{ item }} state=absent
      loop:
        - /etc/freepbx.conf
        - /etc/amportal.conf
    
    - name: Install base framework
      command: >
        scl enable php{{ fpbx_php_version }} -- ./install
          -n --webroot={{ fpbx_root_dir }}/web --dbengine=mysql
          --dbuser={{ fpbx_db_user }} --dbname={{ fpbx_db_name }}
          --cdrdbname={{ fpbx_cdr_db_name }} --dbpass={{ fpbx_db_pass | quote }}
          --astmoddir=/usr/lib64/asterisk/modules/
          --astagidir=/usr/share/asterisk/agi-bin/
          --ampsbin=/usr/local/bin
          --ampcgibin=/opt/freepbx/cgi-bin
      args:
        chdir: "{{ fpbx_root_dir }}/tmp/freepbx"

  tags: fpbx

- name: Check for wrapper symlinks
  stat: path=/usr/local/bin/{{ item }}
  register: fpbx_wrapper_links
  loop:
    - fwconsole
    - amportal
  tags: fpbx

- name: Remove symlinks
  file: path=/usr/local/bin/{{ item.item }} state=absent
  when: item.stat.islnk is defined and item.stat.islnk
  loop: "{{ fpbx_wrapper_links.results }}"
  tags: fpbx

- name: Install wrappers
  template: src={{ item }}.j2 dest=/usr/local/bin/{{ item }} mode=755
  loop:
    - fwconsole
    - amportal
  tags: fpbx

- name: Install safe_asterisk
  copy: src=safe_asterisk dest=/usr/local/bin/safe_asterisk mode=755
  tags: fpbx

- name: Ensure asterisk service is stopped and disabled
  service: name=asterisk state=stopped enabled=False
  tags: fpbx

- name: Deploy FreePBX service unit
  template: src=freepbx.service.j2 dest=/etc/systemd/system/freepbx.service
  register: fpbx_unit
  notify: restart freepbx
  tags: fpbx

- name: Reload systemd
  systemd: daemon_reload=True
  when: fpbx_unit.changed
  tags: fpbx

- name: Deploy pre/post backup scripts
  template: src={{ item }}_backup.sh.j2 dest=/etc/backup/{{ item }}.d/freepbx.sh mode=750
  loop:
    - pre
    - post
  tags: fpbx

- name: Install agi scripts
  copy: src=agi/{{ item }} dest=/usr/share/asterisk/agi-bin/{{ item }} mode=750 group=asterisk
  loop:
    - jitsi_conf_pin
  tags: fpbx

- name: Install dehydrated hook
  template: src=dehydrated_hook.j2 dest=/etc/dehydrated/hooks_deploy_cert.d/freepbx.sh mode=755
  tags: fpbx

# PM2 requires the php command available in PATH
- name: Add a link to php
  file: src=/bin/php{{ fpbx_php_version }} dest=/usr/local/bin/php state=link
  tags: fpbx
