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

  - name: Download IceCast-KH Source
    get_url:
      url: https://github.com/karlheyes/icecast-kh/archive/icecast-2.4.0-kh7.tar.gz
      dest: "{{ app_base }}/servers/icecast2/icecast2.tar.gz"

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

  - name: Build IceCast-KH
    shell: "cd {{ app_base }}/servers/icecast2 && ./configure && make && make install"
    args:
      chdir: "{{ app_base }}/servers/icecast2"
      creates: "/usr/local/bin/icecast"

  - name: Link IceCast Resource Files
    file: src="{{ item.src }}" dest="{{ item.dest }}" state=link
    with_items:
     - { src: "{{ www_base }}/resources/error.mp3", dest: "/usr/local/share/icecast/web/error.mp3" }
     - { src: "{{ www_base }}/resources/status-json.xsl", dest: "/usr/local/share/icecast/web/status-json.xsl" }
     - { src: "{{ www_base }}/resources/xml2json.xslt", dest: "/usr/local/share/icecast/web/xml2json.xslt" }

  - name: Install OPAM
    apt: pkg="opam" install_recommends=yes state=latest

  - name: Install LiquidSoap Dependencies
    apt: pkg="{{ item }}" install_recommends=no state=latest
    with_items:
     - libpcre3-dev
     - libfdk-aac-dev
     - libmad0-dev
     - libmp3lame-dev
     - libtag1-dev
     - libfaad-dev
     - libflac-dev
     - libogg-dev
     - libopus-dev
     - pkg-config
     - m4
     - aspcud
     - camlp4

  - name: Build LiquidSoap
    become_user: azuracast
    shell: "opam init -y && opam update && opam install -y taglib.0.3.3 mad.0.4.5 faad.0.3.3 fdkaac.0.2.1 lame.0.3.3 vorbis.0.7.0 cry.0.6.0 flac.0.1.2 opus.0.1.2 liquidsoap.1.3.3"
    args:
      chdir: "{{ app_base }}"

  - name: Download ShoutCast 2 (x86)
    get_url:
      url: http://download.nullsoft.com/shoutcast/tools/sc_serv2_linux-latest.tar.gz
      dest: "{{ app_base }}/servers/shoutcast2/sc_serv.tar.gz"
    when: ansible_architecture == 'i386'

  - name: Download ShoutCast 2 (x64)
    get_url:
      url: http://download.nullsoft.com/shoutcast/tools/sc_serv2_linux_x64-latest.tar.gz
      dest: "{{ app_base }}/servers/shoutcast2/sc_serv.tar.gz"
    when: ansible_architecture == 'x86_64'

  - name: Extract ShoutCast 2 Binary
    unarchive:
      src: "{{ app_base }}/servers/shoutcast2/sc_serv.tar.gz"
      dest: "{{ app_base }}/servers/shoutcast2"
      remote_src: yes
      mode: "u=rwx,g=rx,o=rx"
      owner: "azuracast"
      group: "www-data"
      # creates: "{{ app_base }}/servers/shoutcast2/sc_serv"