- 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 }}
  file:
    state: directory
    path: "{{ transmission_download_dir }}"    # /library/transmission
    owner: "{{ transmission_user }}"     # debian-transmission
    group: "{{ transmission_group }}"    # debian-transmission
    # mode: '0755'

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

- name: Back up prior /etc/transmission-daemon/settings.json (original file from apt, or new symlink contents) to /etc/transmission-daemon/settings.json.old*
  copy:
    src: /etc/transmission-daemon/settings.json
    dest: /etc/transmission-daemon/settings.json.old
    mode: preserve
    owner: "{{ transmission_user }}"     # debian-transmission
    group: "{{ transmission_group }}"    # debian-transmission
    backup: yes
  ignore_errors: yes

- name: "Back up IIAB's templated version to /etc/transmission-daemon/settings.json.iiab"
  template:
    src: settings.json.j2
    dest: /etc/transmission-daemon/settings.json.iiab
    owner: "{{ transmission_user }}"     # debian-transmission
    group: "{{ transmission_group }}"    # debian-transmission
    mode: '0600'

- name: Install /var/lib/transmission-daemon/.config/transmission-daemon/settings.json from template
  template:
    src: settings.json.j2
    dest: /var/lib/transmission-daemon/.config/transmission-daemon/settings.json
    owner: "{{ transmission_user }}"     # debian-transmission
    group: "{{ transmission_group }}"    # debian-transmission
    mode: '0600'

- name: "Reverse Transmission's fragile OOTB symlink -- instead we establish /etc/transmission-daemon/settings.json -> /var/lib/transmission-daemon/.config/transmission-daemon/settings.json -- REASON: /etc/transmission-daemon/settings.json was intermittently being IGNORED, as Transmission sometimes breaks its own symlink from /var/lib/transmission-daemon/.config/transmission-daemon/settings.json (by turning it into a file instead)"
  file:
    path: /etc/transmission-daemon/settings.json
    src: /var/lib/transmission-daemon/.config/transmission-daemon/settings.json
    state: link
    force: yes


# RECORD Transmission AS INSTALLED

- name: "Set 'transmission_installed: True'"
  set_fact:
    transmission_installed: True

- name: "Add 'transmission_installed: True' to {{ iiab_state_file }}"
  lineinfile:
    path: "{{ iiab_state_file }}"    # /etc/iiab/iiab_state.yml
    regexp: '^transmission_installed'
    line: 'transmission_installed: True'
