- name: chan_dongle - Download software to /opt/iiab/downloads
  get_url:
    url: "{{ chan_dongle_url }}/{{ chan_dongle_src_file }}"
    dest: "{{ downloads_dir }}/{{ chan_dongle_src_file }}"
    timeout: "{{ download_timeout }}"
  when: internet_available

- name: chan_dongle - Check for /opt/iiab/downloads/{{ chan_dongle_src_file }}
  stat:
    path: "{{ downloads_dir }}/{{ chan_dongle_src_file }}"
  register: chan_dongle_src

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

- name: chan_dongle - Create install source directory
  file: 
    path: "{{ chan_dongle_src_dir }}"
    state: directory

- name: chan_dongle - Extract source
  unarchive: 
    src: "{{ downloads_dir }}/{{ chan_dongle_src_file }}"
    dest: "{{ downloads_dir }}"
    owner: root
    group: root

- name: chan_dongle - move to {{ chan_dongle_src_dir }}
  command: rsync -av {{ downloads_dir }}/asterisk-chan-dongle-master/ {{ chan_dongle_src_dir }}
  args:
    chdir: "{{ downloads_dir }}"

- name: chan_dongle - Run the bootstrap script
  command: "./bootstrap"
  args:
    chdir: "{{ chan_dongle_src_dir }}"
    creates: "{{ chan_dongle_src_dir }}/configure"

- name: chan_dongle - Find out asterisk version
  shell: asterisk -V |cut -d " " -f 2
  register: asterisk_ver

- name: chan_dongle - Run the configure script
  command: "./configure --with-astversion={{asterisk_ver.stdout}}"
  args:
    chdir: "{{ chan_dongle_src_dir }}"
    creates: "{{ chan_dongle_src_dir }}/Makefile"

- name: chan_dongle - Run 'make'
  command: make 
  args:
    chdir: "{{ chan_dongle_src_dir }}"
    creates: "{{ chan_dongle_src_dir }}/chan_dongle.o"

- name: chan_dongle - Run 'make install'
  command: make install
  args:
    chdir: "{{ chan_dongle_src_dir }}"
    creates: "/usr/lib/asterisk/modules/chan_dongle.so"

- name: chan_dongle - Copy dongle.conf over
  command: cp {{ chan_dongle_src_dir }}/etc/dongle.conf /etc/asterisk/
  args:
    chdir: "{{ chan_dongle_src_dir }}"
    creates: "/etc/asterisk/dongle.conf"

