# 2022-05-25 README.adoc, with screenshots:
# https://github.com/iiab/iiab/tree/master/roles/pbx#readme


# 2021-08-05: Asterisk's own install_prereq (below) handles essentially all of these
# - name: Asterisk - Install dependencies
#   include_tasks: asterisk_dependencies.yml

# BEWARE: 'systemctl is-active asterix' falsely reports 'inactive' even when systemd
# is compiled in below!   FWIW: /opt/iiab/asterisk/contrib/systemd/asterisk.service
# https://github.com/asterisk/asterisk/blob/master/contrib/systemd/asterisk.service

- name: Asterisk - Install package 'libsystemd-dev' so Asterisk compiles in imperfect-but-improving systemd support -- if ./configure below places '#define HAVE_SYSTEMD 1' in /opt/iiab/asterisk/include/asterisk/autoconfig.h -- please later confirm with 'ldd /usr/sbin/asterisk | grep systemd' -- per https://community.asterisk.org/t/systemctl-start-asterisk-is-fail-with-timeout/81123/3
  package:
    name: libsystemd-dev
    state: present

- name: Asterisk - Download {{ asterisk_url }}/{{ asterisk_src_file }} to {{ downloads_dir }}
  get_url:
    url: "{{ asterisk_url }}/{{ asterisk_src_file }}"
    dest: "{{ downloads_dir }}"    # e.g. /opt/iiab/downloads/asterisk-18-current.tar.gz
    timeout: "{{ download_timeout }}"

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

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

- name: Asterisk - Create source dir {{ asterisk_src_dir }}
  file:
    path: "{{ asterisk_src_dir }}"    # /opt/iiab/asterisk
    state: directory

- name: Asterisk - Extract to source dir (root:root by default)
  unarchive:
    src: "{{ downloads_dir }}/{{ asterisk_src_file }}"
    dest: "{{ asterisk_src_dir }}"
    extra_opts: [--strip-components=1]
    creates: "{{ asterisk_src_dir }}/Makefile"


# 2021-08-03: Asterisk's install_prereq script (stanza just below) installs
# 'aptitude' regardless, within handle_debian() here:
# https://github.com/asterisk/asterisk/blob/master/contrib/scripts/install_prereq#L262-L271
#
# - name: Asterisk - Install aptitude (otherwise install_prereq fails?)
#   package:
#     name: aptitude
#     state: latest

# https://github.com/iiab/iiab/issues/3489
- name: "Asterisk - Apply patch to {{ asterisk_src_dir }}/contrib/scripts/install_prereq for Raspberry Pi (#3489) if 'asterisk_rpi_patch: True'"
  ansible.posix.patch:
    src: install_prereq.diff
    dest: "{{ asterisk_src_dir }}/contrib/scripts/install_prereq"
  when: asterisk_rpi_patch

- name: Asterisk - Run 'install_prereq install' for dependencies - CAN TAKE 2-5 MIN OR LONGER!
  shell: export DEBIAN_FRONTEND=noninteractive && ./contrib/scripts/install_prereq install
  args:
    chdir: "{{ asterisk_src_dir }}"

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

- name: Asterisk - Run './configure --with-pjproject-bundled --with-jansson-bundled'
  command: ./configure --with-pjproject-bundled --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 menuselect.makeopts
  # 2021-08-06: Let's standardize (ABOVE) if 6 others (BELOW) aren't needed?
  # 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 4-30 MIN OR LONGER!
  command: make
  args:
    chdir: "{{ asterisk_src_dir }}"
    creates: defaults.h

- name: Asterisk - Run 'make install' - CAN TAKE 1-2 MIN W/ SLOW DISKS?
  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' - this creates /etc/asterisk/asterisk.conf used below
  command: make samples
  args:
    chdir: "{{ asterisk_src_dir }}"

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

# 2021-08-06: Most install recipes do 'update-rc.d -f asterisk remove' here.
# Can't hurt but we do that a bit later in freepbx.yml


# 2021-08-06: Taken care of just below
# - name: Asterisk - Ensure group 'asterisk' exists
#   group:
#     name: asterisk
#     state: present

#- name: Asterisk - Ensure system user 'asterisk' has primary group 'asterisk', groups 'audio,dialout', home '/var/lib/asterisk'
- name: Asterisk - Create Linux user 'asterisk'
  user:
    name: asterisk
    # group: asterisk            # 2021-08-06: Implicit
    groups: audio,dialout        # 2021-08-08: Plausibly might help?
    home: /var/lib/asterisk      # 2021-08-07: /home/asterisk (default) ok too
    system: yes                  # 2021-08-08: No evidence this is needed.  Hopefully it doesn't cause harm?  (FYI Ansible doesn't change pre-existing users)
    append: yes                  # 2021-08-08: Not essential.  So Ansible adds groups w/o deleting existing group memberships.

# 2021-08-17: Moved to enable-or-disable.yml
# - name: Asterisk - Add user 'www-data' to group 'asterisk' - if pbx_use_nginx
#   user:
#     name: www-data
#     groups: asterisk
#     # system: yes                # 2021-08-06: Ansible doesn't change pre-existing users
#     append: yes
#   when: pbx_use_nginx

- name: Asterisk - Set ownership for 6 directories (asterisk:asterisk, recurse)
  file:
    dest: "{{ item }}"
    owner: asterisk
    group: asterisk
    recurse: yes
  with_items:
    - /var/run/asterisk
    - /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'


- name: "Set 'asterisk_installed: True'"
  set_fact:
    asterisk_installed: True

- name: "Add 'asterisk_installed: True' to {{ iiab_state_file }}"
  lineinfile:
    path: "{{ iiab_state_file }}"    # /etc/iiab/iiab_state.yml
    regexp: '^asterisk_installed'
    line: 'asterisk_installed: True'
