- name: Asterisk - Install dependencies
  include: asterisk_dependencies.yml

- name: Asterisk - Download software to /opt/iiab/downloads
  get_url:
    url: "{{ asterisk_url }}/{{ asterisk_src_file }}"
    dest: "{{ downloads_dir }}/{{ asterisk_src_file }}"
    timeout: "{{ download_timeout }}"
  when: internet_available | bool

- name: Asterisk - Check for /opt/iiab/downloads/{{ asterisk_src_file }}
  stat:
    path: "{{ downloads_dir }}/{{ asterisk_src_file }}"
  register: asterisk_src

- name: Asterisk - FAIL (force Ansible to exit) IF /opt/iiab/downloads/{{ asterisk_src_file }} doesn't exist
  fail:
    msg: "{ downloads_dir }}/{{ asterisk_src_file }} is REQUIRED in order to install."
  when: not asterisk_src.stat.exists

- name: Asterisk - Create install source directory
  file: 
    path: "{{ asterisk_src_dir }}"
    state: directory

- name: Asterisk - Extract source
  unarchive: 
    src: "{{ downloads_dir }}/{{ asterisk_src_file }}"
    dest: "{{ asterisk_src_dir }}"
    owner: root
    group: root
    extra_opts: [--strip-components=1]
    creates: "{{ asterisk_src_dir }}/Makefile"

- name: Asterisk - Download mp3 decoder library into source tree
  command: "./contrib/scripts/get_mp3_source.sh"
  args:
    chdir: "{{ asterisk_src_dir }}"
    creates: "addons/mp3/mpg123.h"

- name: Asterisk - Install aptitude (otherwise install_prereq fails?)
  package: 
    name: aptitude
    state: latest

- name: Asterisk - Ensure all dependencies are resolved - CAN TAKE 5 MIN OR LONGER!
  shell: export DEBIAN_FRONTEND=noninteractive && ./contrib/scripts/install_prereq install 
  args:
    chdir: "{{ asterisk_src_dir }}"

- name: Asterisk - Run the configure script
  command: "./configure --with-jansson-bundled"
  args:
    chdir: "{{ asterisk_src_dir }}"

- name: Asterisk - Run make menuselect.makeopts
  command: "make menuselect.makeopts"
  args:
    chdir: "{{ asterisk_src_dir }}"
    creates: "menuselect.makeopts"  

- name: Asterisk - Do a bit of menuselect configuration
  command: >
    menuselect/menuselect --enable app_macro --enable format_mp3
    --enable CORE-SOUNDS-EN-WAV --enable CORE-SOUNDS-EN-G722
    --enable EXTRA-SOUNDS-EN-WAV --enable EXTRA-SOUNDS-EN-G722 --enable EXTRA-SOUNDS-EN-GSM
    --disable-category MENUSELECT_MOH        
  args:
    chdir: "{{ asterisk_src_dir }}"

- name: Asterisk - Run 'make' - CAN TAKE 30 MIN OR LONGER!
  command: make 
  args:
    chdir: "{{ asterisk_src_dir }}"
    creates: "defaults.h"

- name: Asterisk - Run 'make install' - CAN TAKE 2 MIN OR LONGER!
  command: make install
  args:
    chdir: "{{ asterisk_src_dir }}"
    creates: "/usr/sbin/asterisk"

- name: Asterisk - Run 'make config'
  command: make config
  args:
    chdir: "{{ asterisk_src_dir }}"

- name: Asterisk - Run 'make samples'
  command: make samples
  args:
    chdir: "{{ asterisk_src_dir }}"

- name: Asterisk - Run 'ldconfig'
  shell: ldconfig
  args:
    chdir: "{{ asterisk_src_dir }}"

- name: Asterisk - Ensure group 'asterisk' exists
  group:
    name: asterisk
    state: present

- name: Asterisk - Ensure user 'asterisk' exists, and belongs to the required groups
  user:
    name: asterisk
    group: asterisk
    groups: audio,dialout
    home: "/var/lib/asterisk"
    system: yes
    append: yes

- name: 'Asterisk - Set ownership of 5 directories: /etc/asterisk, /var/lib/asterisk, /var/log/asterisk, /var/spool/asterisk, /usr/lib/asterisk'
  file: 
    dest: "{{ item }}" 
    owner: asterisk 
    group: asterisk
    recurse: yes
  with_items:
    - /etc/asterisk
    - /var/lib/asterisk
    - /var/log/asterisk
    - /var/spool/asterisk
    - /usr/lib/asterisk

- name: Asterisk - Set default user to 'asterisk' in /etc/default/asterisk
  lineinfile:
    path: /etc/default/asterisk
    regexp: 'AST_USER='
    line: 'AST_USER="asterisk"'

- name: Asterisk - Set default group to 'asterisk' in /etc/default/asterisk
  lineinfile:
    path: /etc/default/asterisk
    regexp: 'AST_GROUP='
    line: 'AST_GROUP="asterisk"'

- name: Asterisk - Set default user to 'asterisk' in /etc/asterisk/asterisk.conf
  lineinfile:
    path: /etc/asterisk/asterisk.conf
    regexp: 'runuser ='
    line: 'runuser = asterisk'

- name: Asterisk - Set default group to 'asterisk' in /etc/asterisk/asterisk.conf
  lineinfile:
    path: /etc/asterisk/asterisk.conf
    regexp: 'rungroup ='
    line: 'rungroup = asterisk'
