- name: "Install BitTorrent packages: transmission-daemon, transmission-cli"
  package:
    name:
      - transmission-daemon
      - transmission-cli
    state: present

- name: Create download dir {{ transmission_download_dir }}, owned by {{ transmission_user }}:{{ transmission_group }}    # /library/transmission
  file:
    path: "{{ transmission_download_dir }}"
    owner: "{{ transmission_user }}"
    group: "{{ transmission_group }}"
    mode: 0755
    state: directory

- name: Stop transmission-daemon before modifying its settings
  systemd:
    name: transmission-daemon
    state: stopped
  ignore_errors: yes

- name: Install /etc/transmission-daemon/settings.json from template
  template:
    src: settings.json.j2
    dest: /etc/transmission-daemon/settings.json
    mode: 0644
    owner: "{{ transmission_user }}"
    group: "{{ transmission_group }}"

- name: Enable & Restart transmission-daemon systemd service, incl daemon-reload
  systemd:
    name: transmission-daemon
    daemon_reload: yes
    enabled: yes
    state: restarted
  when: transmission_enabled | bool

- name: Add PAUSED KA Lite torrent(s) to transmission-daemon's queue
  shell: >
    /usr/bin/transmission-remote
    --start-paused
    -n {{ transmission_username }}:{{ transmission_password }}
    -a http://pantry.learningequality.org/downloads/ka-lite/{{ transmission_kalite_version }}/content/ka-lite-0.17-resized-videos-{{ item }}.torrent
  with_items: "{{ transmission_kalite_languages }}"
  when: transmission_enabled and transmission_provision and transmission_kalite_languages is defined and transmission_kalite_languages is not none
  ignore_errors: yes

- name: Disable transmission-daemon service, if not transmission_enabled
  systemd:
    name: transmission-daemon
    daemon_reload: yes
    enabled: no
    state: stopped
  when: not transmission_enabled

- name: Add 'transmission' variable values to {{ iiab_ini_file }}
  ini_file:
    path: "{{ iiab_ini_file }}"
    section: transmission
    option: "{{ item.option }}"
    value: "{{ item.value }}"
  with_items:
    - option: name
      value: transmission
    - option: description
      value: '"Transmission is a set of lightweight BitTorrent clients (in GUI, CLI and daemon form)."'
    - option: transmission_install
      value: "{{ transmission_install }}"
    - option: transmission_enabled
      value: "{{ transmission_enabled }}"
    - option: transmission_download_dir
      value: "{{ transmission_download_dir }}"
    - option: transmission_user
      value: "{{ transmission_user }}"
    - option: transmission_group
      value: "{{ transmission_group }}"
    - option: transmission_http_port
      value: "{{ transmission_http_port }}"
    - option: transmission_url
      value: "{{ transmission_url }}"
    - option: transmission_peer_port
      value: "{{ transmission_peer_port }}"
    - option: transmission_provision
      value: "{{ transmission_provision }}"
    - option: transmission_kalite_version
      value: "{{ transmission_kalite_version }}"
    - option: transmission_kalite_languages
      value: "{{ transmission_kalite_languages }}"
    - option: transmission_username
      value: "{{ transmission_username }}"
    - option: transmission_password
      value: "{{ transmission_password }}"
