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

- name: Add OPAM/OCaml Repository
  apt_repository:
    repo: "ppa:avsm/ppa"
    update_cache: true

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

- name: Initialize OPAM
  become_user: azuracast
  shell: >-
    opam init -a --disable-sandboxing --bare
    && opam switch create 4.13.1
  args:
    chdir: "{{ app_base }}"
    executable: "bash"  # Fixes some possible hang issues.

- name: Build and Install Liquidsoap and Dependencies
  become_user: azuracast
  shell: >-
    ladspa.0.2.2 ffmpeg.1.1.3 ffmpeg-avutil.1.1.3 ffmpeg-avcodec.1.1.3 ffmpeg-avdevice.1.1.3
    ffmpeg-av.1.1.3 ffmpeg-avfilter.1.1.3 ffmpeg-swresample.1.1.3 ffmpeg-swscale.1.1.3 frei0r.0.1.2
    samplerate.0.1.6 taglib.0.3.9 mad.0.5.2 faad.0.5.0 fdkaac.0.3.2 lame.0.3.6 vorbis.0.8.0 cry.0.6.7
    flac.0.3.0 opus.0.2.1 dtools.0.4.4 duppy.0.9.2 ocurl.0.9.2 ssl.0.5.10 \
    liquidsoap.2.0.4
  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
  file:
    src: "{{ app_base }}/.opam/4.12.0/bin/liquidsoap"
    dest: /usr/local/bin/liquidsoap
    state: link
    force: true
