- name: FreePBX - Install dependencies
  include: freepbx_dependencies.yml

- name: FreePBX - Download software to /opt/iiab/downloads
  get_url:
    url: "{{ freepbx_url }}/{{ freepbx_src_file }}"
    dest: "{{ downloads_dir }}/{{ freepbx_src_file }}"
    timeout: "{{ download_timeout }}"
  when: internet_available

- name: FreePBX - Check for {{ downloads_dir }}/{{ freepbx_src_file }}
  stat:
    path: "{{ downloads_dir }}/{{ freepbx_src_file }}"
  register: freepbx_src

- name: FreePBX - FAIL (force Ansible to exit) IF {{ downloads_dir }}/{{ freepbx_src_file }} doesn't exist
  fail:
    msg: "{ downloads_dir }}/{{ freepbx_src_file }} is REQUIRED in order to install."
  when: not freepbx_src.stat.exists

- name: FreePBX - Create install source directory
  file: 
    path: "{{ freepbx_src_dir }}"
    state: directory

- name: FreePBX - Extract source
  unarchive: 
    src: "{{ downloads_dir }}/{{ freepbx_src_file }}"
    dest: "{{ freepbx_src_dir }}"
    owner: root
    group: root
    extra_opts: [--strip-components=1]
    creates: "{{ freepbx_src_dir }}/install"

- name: FreePBX - Patch FreePBX source - IIAB Bug 1685
  patch:
    src: "roles/pbx/templates/71-freepbx-framework.patch"
    dest: "{{ freepbx_src_dir }}/amp_conf/htdocs/admin/libraries/BMO/Framework.class.php"

- name: FreePBX - Patch FreePBX source - wants [] not {}
  patch:
    src: "roles/pbx/templates/pbx.patch"
    dest: "{{ freepbx_src_dir }}/amp_conf/htdocs/admin/libraries/Composer/vendor/neitanod/forceutf8/src/ForceUTF8/Encoding.php"

- name: FreePBX - Disable & Stop asterisk service
  systemd:
    daemon_reload: yes
    name: asterisk
    enabled: no
    state: stopped

- name: FreePBX - Add mysql user
  mysql_user:
    name: "{{ asterisk_db_user }}"
    password: "{{ asterisk_db_password }}"
    priv: "{{ asterisk_db_dbname }}.*:ALL/{{ asterisk_db_cdrdbname }}.*:ALL"
#    login_host: "{{ asterisk_db_host }}"
#    login_user: "root"
#    login_password: "{{ mysql_root_password }}"
    host: "{{ (asterisk_db_host == 'localhost') | ternary('localhost', ansible_default_ipv4.address) }}"
    state: present

- name: FreePBX - Add mysql db
  mysql_db:
    name: "{{ asterisk_db_dbname }}"
    encoding: utf8
    collation: utf8_general_ci
#    login_host: "{{ asterisk_db_host }}"
#    login_user: "root"
#    login_password: "{{ mysql_root_password }}"
    state: present

- name: FreePBX - Add cdr mysql db
  mysql_db:
    name: "{{ asterisk_db_cdrdbname }}"
    encoding: utf8
    collation: utf8_general_ci
    login_host: "{{ asterisk_db_host }}"
    state: present

- name: FreePBX - Don't let freepbx take over the php sessions dir
  blockinfile:
    content: |
      [blacklist]
      directory = /var/lib/php/sessions
    marker: "; {mark} ANSIBLE MANAGED BLOCK"
    dest: /etc/asterisk/freepbx_chown.conf
    owner: asterisk
    group: asterisk
    create: yes

- name: FreePBX - Create php sessions directory
  file: 
    path: "/var/lib/php/asterisk_sessions/"
    state: directory

- name: FreePBX - Set ownership for php sessions directory
  file: 
    dest: "/var/lib/php/asterisk_sessions/" 
    owner: asterisk 
    group: asterisk
    recurse: yes
 
- name: FreePBX - Install (just run once) - CAN TAKE 12 MIN OR LONGER!
  command: ./start_asterisk start
  args:
    chdir: "{{ freepbx_src_dir }}"
    creates: "{{ freepbx_install_dir }}"

- name: FreePBX - Install (just run once) - CAN TAKE 12 MIN OR LONGER!
  command: ./install -n --webroot {{ freepbx_install_dir }} --dbuser {{ asterisk_db_user }} --dbpass {{ asterisk_db_password }} --dbname {{ asterisk_db_dbname }} --cdrdbname {{ asterisk_db_cdrdbname }}
  args:
    chdir: "{{ freepbx_src_dir }}"
    creates: "{{ freepbx_install_dir }}"

- name: FreePBX - Patch FreePBX source - disable get_magic_quotes_gpc()
  patch:
    src: "roles/pbx/templates/pbx2.patch"
    dest: "{{ freepbx_install_dir }}/freepbx/admin/libraries/view.functions.php"

- name: FreePBX - Create /etc/odbc.ini
  template:
    src: odbc.ini.j2
    dest: /etc/odbc.ini
    # owner: root
    # group: root
    # mode: '0644'

- name: FreePBX - Install Apache's sites-available/freepbx.conf from template
  template:
    src: freepbx.conf.j2
    dest: /etc/apache2/sites-available/freepbx.conf
    owner: "{{ apache_user }}"
    group: "{{ apache_user }}"
    mode: '0644'

- name: FreePBX - Add {{ pbx_http_port }} as a Listen directive to apache ports.conf
  lineinfile:
    path: /etc/apache2/ports.conf
    line: "Listen {{ pbx_http_port }}"
    insertafter: Listen 80  

- name: FreePBX - Install /etc/systemd/system/freepbx.service systemd unit file from template
  template:
    src: freepbx.service.j2
    dest: /etc/systemd/system/freepbx.service
    # mode: '0755'    # Let's go with '0644' per unit file norms
