---
# file: roles/config/tasks/main.ym;

## tags: apache

- name: Remove default sites-enabled
  command: a2dissite default
  sudo: yes
  tags: apache
  notify:
    - restart apache

- name: Enable apache ssl
  command: a2enmod ssl
  sudo: yes
  tags: apache
  notify:
    - restart apache

## tags: ssl, newkey

- name: Create key and cert dir
  file:
    path=/etc/apache2/ssl
    state=directory
    owner=root
    group=root
  sudo: yes
  tags: ssl

- name: Create openssl conf file
  template:
    src=openssl.conf.j2
    dest={{ ssl_dir }}/openssl.conf
    owner=root
    group=root
    mode=0640
  sudo: yes
  tags: ssl

- name: Delete current certs (tag newkey)
  file: path={{ ssl_dir }}/{{ item }} state=absent
  with_items:
    - "{{ ssl_key }}"
    - "{{ ssl_cert }}"
  sudo: yes 
  tags: newkey

- name: Create key and cert file
  command: 
    openssl req -x509 -nodes -days 365 -newkey rsa:2048 -config {{ ssl_dir }}/openssl.conf -keyout {{ ssl_dir }}/{{ ssl_key }} -out {{ ssl_dir }}/{{ ssl_cert}}
    creates={{ ssl_dir }}/{{ ssl_key }}
    creates={{ ssl_dir }}/{{ ssl_cert }}
  sudo: yes
  tags: ssl
  notify:
    - restart apache

## tags: rutorrent

- name: Deploy default rtorrent config
  template: 
    src=rtorrent.rc.j2 
    dest={{ home }}/.rtorrent.rc
  tags: ruconfig

- name: Enable scgi for ru/r connectivity
  file: 
    src=/etc/apache2/mods-available/scgi.load 
    dest=/etc/apache2/mods-enabled/scgi.load 
    state=link
  sudo: yes
  tags: 
    - ruconfig
    - apache
  notify:
  - restart apache

- name: Remove old rupassed file
  file: 
    path={{ ru_passwd_file }}
    state=absent
  sudo: yes
  tags: ruconfig

- name: Create rupasswd file
  htpasswd: 
    path={{ ru_passwd_file }}
    name={{ ru_user }} 
    password={{ ru_password }}
    create=yes
    state=present
    owner=root
    group=www-data
    mode=0640
  sudo: yes
  tags: ruconfig

- name: Create rutorrent virtual directory
  template:
    src=rutorrent.j2
    dest=/etc/apache2/sites-available/rutorrent
    owner=root
    group=root
  sudo: yes
  tags: 
    - rutorrent
    - apache

- name: Enable rutorrent site
  command: a2ensite rutorrent
  sudo: yes
  notify:
    - restart apache
  tags: rutorrent
