---
- name : Install IceCast-KH-AC Dependencies
  apt :
    name : "{{ packages }}"
    state : latest
    install_recommends : no
  vars :
    packages :
      - libxml2
      - libxslt1-dev
      - libvorbis-dev
      - libssl-dev
      - libcurl4-openssl-dev
      - pkg-config

- name : Download IceCast-KH-AC Source
  get_url :
    url : https://github.com/AzuraCast/icecast-kh-ac/archive/2.4.0-kh13-ac2.tar.gz
    dest : "{{ app_base }}/servers/icecast2/icecast2.tar.gz"
    force : yes

- name : Extract IceCast-KH-AC Source
  unarchive :
    src : "{{ app_base }}/servers/icecast2/icecast2.tar.gz"
    dest : "{{ app_base }}/servers/icecast2"
    remote_src : yes
    mode : "u=rwx,g=rx,o=rx"
    owner : "azuracast"
    group : "www-data"
    extra_opts : "--strip-components=1"

- name : Build IceCast-KH-AC
  shell : "cd {{ app_base }}/servers/icecast2 && ./configure && make && make install"
  args :
    chdir : "{{ app_base }}/servers/icecast2"

- name : Clear OPAM directory
  file :
    path : "{{ app_base }}/.opam"
    state : absent

- name : Add OPAM/OCaml Repository
  apt_repository :
    repo : "ppa:avsm/ppa"
    update_cache : yes
  when :
    - ansible_distribution_release == 'xenial' or ansible_distribution_release == 'bionic'

- name : Install Liquidsoap Dependencies
  apt :
    name : "{{ packages }}"
    state : latest
    install_recommends : no
  vars :
    packages :
      - opam
      - ocaml
      - libavcodec-dev
      - libavfilter-dev
      - libavformat-dev
      - libavresample-dev
      - libavutil-dev
      - libfaad-dev
      - libfdk-aac-dev
      - libflac-dev
      - libmad0-dev
      - libmp3lame-dev
      - libogg-dev
      - libopus-dev
      - libpcre3-dev
      - libpostproc-dev
      - libsamplerate0-dev
      - libswresample-dev
      - libssl-dev
      - libtag1-dev
      - libvorbis-dev
      - autoconf
      - automake
      - m4
      - pkg-config
      - unzip
      - bubblewrap
      - ladspa-sdk
      - multimedia-audio-plugins
      - swh-plugins
      - tap-plugins

- name : Add Optional LADSPA Plugins (Focal)
  apt :
    name : "{{ packages }}"
    state : latest
    install_recommends : no
  vars :
    packages :
      - lsp-plugins-ladspa
  when :
    - ansible_distribution_release == 'focal'

- name : Initialize OPAM (Bionic)
  become_user : azuracast
  shell : "opam init -a --disable-sandboxing --bare && opam switch create 4.08.0"
  args :
    chdir : "{{ app_base }}"
    executable : "bash" # Fixes some possible hang issues.
  when :
    - ansible_distribution_release == 'xenial' or ansible_distribution_release == 'bionic'

- name : Initialize OPAM (Focal)
  become_user : azuracast
  shell : "opam init --disable-sandboxing -a --bare && opam switch create ocaml-system.4.08.1"
  args :
    chdir : "{{ app_base }}"
    executable : "bash" # Fixes some possible hang issues.
  when :
    - ansible_distribution_release == 'focal'

- name : Download specific Liquidsoap commit
  become_user : azuracast
  git :
    repo : https://github.com/savonet/liquidsoap.git
    dest : "{{ app_base }}/liquidsoap-src"
    version : 75d530c86bf638e3c50c08b7802d92270288e31b
    clone : yes
    update : yes
    force : yes

- name : Pin specific Liquidsoap commit
  become_user : azuracast
  shell : opam pin add --no-action liquidsoap .
  args :
    chdir : "{{ app_base }}/liquidsoap-src"
    executable : "bash" # Fixes some possible hang issues.

- name : Build and Install Liquidsoap and Dependencies
  become_user : azuracast
  shell : "opam install -y ladspa.0.1.5 ffmpeg.0.4.3 samplerate.0.1.4 taglib.0.3.3 mad.0.4.5 faad.0.4.0 fdkaac.0.3.1 lame.0.3.3 vorbis.0.7.1 cry.0.6.1 flac.0.1.5 opus.0.1.3 duppy.0.8.0 ssl liquidsoap"
  args :
    chdir : "{{ app_base }}"
  register : install_result
  async : 3600
  poll : 0

- name : Check on Liquidsoap Installation
  become_user : azuracast
  async_status :
    jid : "{{ install_result.ansible_job_id }}"
  register : check_result
  until : check_result.finished
  retries : 60
  delay : 60

- name : Link Liquidsoap binary (Xenial/Bionic)
  file :
    src : "{{ app_base }}/.opam/4.08.0/bin/liquidsoap"
    dest : /usr/local/bin/liquidsoap
    state : link
    force : yes
  when :
    - ansible_distribution_release == 'xenial' or ansible_distribution_release == 'bionic'

- name : Link Liquidsoap binary (Focal)
  file :
    src : "{{ app_base }}/.opam/ocaml-system.4.08.1/bin/liquidsoap"
    dest : /usr/local/bin/liquidsoap
    state : link
    force : yes
  when :
    - ansible_distribution_release == 'focal'

- name : Add Audiowaveform PPA
  apt_repository :
    repo : "ppa:chris-needham/ppa"
    update_cache : yes

- name : Install Audiowaveform
  apt :
    name : audiowaveform
    state : latest
    install_recommends : no

- name : Install Flac
  apt :
    name : flac
    state : latest
    install_recommends : no

- name : Install Vorbis-Tools
  apt :
    name : vorbis-tools
    state : latest
    install_recommends : no
