# Get required build dependencies
- apt: name={{ item }} state=installed update_cache=yes cache_valid_time=3600
  with_items: 
    - subversion
    - build-essential
    - automake
    - libtool
    - libcppunit-dev
    - libsigc++-2.0-dev
    - libssl-dev
    - libcurl4-openssl-dev
    - unzip
    - unrar-free
    - curl
    - libncurses5-dev
    - apache2
    - php5
    - php5-cli
    - php5-curl
    - libapache2-mod-scgi
    - python-passlib
    # Optional
    - ffmpeg
    - mediainfo
    - screen
    - vim
  sudo: yes
  tags: depends

# Create contribution directory
- file: path={{ contrib }} state=directory

# Get source
- subversion: dest={{ contrib }}/{{ dir_xmlrpc }} repo={{ svn_xmlrpc_c }}
  tags: source
- get_url: dest={{ contrib}} url={{ item }}.tar.gz
  with_items:
    - "{{ src_libtorrent }}{{ ver_libtorrent }}"
    - "{{ src_rtorrent }}{{ ver_rtorrent }}"
    - "{{ src_rutorrent }}{{ ver_rutorrent }}"
    - "{{ src_ruplugins }}{{ ver_ruplugins }}"
  tags: source

# Extract source
- command: tar xvf {{ ver_libtorrent }}.tar.gz  chdir={{contrib}} creates={{ ver_libtorrent }}
  tags: source
- command: tar xvf {{ ver_rtorrent }}.tar.gz  chdir={{contrib}} creates={{ ver_rtorrent }}
  tags: source
- command: tar xvf {{ ver_rutorrent }}.tar.gz  chdir={{contrib}} creates={{ dir_rutorrent }}
  tags: web
- command: tar xvf {{ ver_ruplugins }}.tar.gz  chdir={{contrib}} creates={{ dir_ruplugins }}
  tags: web

# Build and install binaries and libs
- name: Build and Install libtorrent
  command: chdir={{ contrib }}/{{ ver_libtorrent }} {{ item }} #creates=/usr/bin/libtool
  sudo: yes
  with_items:
    - ./autogen.sh
    - ./configure
    - make 
    - make install 
  tags: source

- name: Build and Install xmlrpc
  command: chdir={{ contrib }}/{{ dir_xmlrpc }} {{ item }} #creates=/usr/local/bin/xmlrpc-c-config
  sudo: yes
  with_items:
    - ./configure --disable-cplusplus
    - make 
    - make install
  tags: source

- name: Build and Install rtorrent
  command: chdir={{ contrib }}/{{ ver_rtorrent }} {{ item }} #creates=/usr/local/bin/rtorrent
  sudo: yes
  with_items:
    - ./autogen.sh
    - ./configure --with-xmlrpc-c
    - make 
    - make install
    - ldconfig 
  tags: source
    
# Deploy rtorrent and rutorrent to /var/www
- name: Create rtorrent directories
  file: path={{ item }} state=directory
  with_items:
    - "{{ home }}/rtorrent"
    - "{{ home }}/rtorrent/session"
    - "{{ home }}/rtorrent/watch"
    - "{{ home }}/rtorrent/download"
  tags: web

- name: Deploy rutorrent
  command: rsync -a {{ contrib }}/{{ dir_rutorrent }}/ /var/www/rutorrent/
  sudo: yes
  tags: web

- name: Deploy rutorrent plugins
  command: rsync -a {{ contrib }}/{{ dir_ruplugins }}/ /var/www/rutorrent/plugins/
  sudo: yes
  tags: web

- file: path=/var/www/rutorrent/ 
        state=directory 
        recurse=yes
        owner={{ apache_user }} 
        group={{ apache_user }} 
  sudo: yes
  tags: web

