---

- name: deploy aria2/yaaw to raspbian

  hosts: pi

  vars:
    ansible_become: yes

  tasks:

  - name: ensure softwares are installed
    apt:
      name: '{{item}}'
      state: present
    with_items:
      - aria2
      - nginx
      - samba

  - name: create aria2 service file
    copy:
      content: |
        [Unit]
        Description=High speed download utility
        After=network.target

        [Service]
        User=pi
        Environment=DIR=/home/pi/share
        ExecStartPre=/bin/mkdir -p ${DIR}
        ExecStart=/usr/bin/aria2c --enable-rpc \
                                  --rpc-listen-all=true \
                                  --rpc-allow-origin-all \
                                  --dir ${DIR}
        Restart=on-failure
         
        [Install]
        WantedBy=multi-user.target
      dest: /etc/systemd/system/aria2.service
    notify:
      - reload systemd
      - restart aria2

  - name: ensure services are running
    service:
      name: '{{item}}'
      state: started
      enabled: yes
    with_items:
      - aria2
      - nginx
      - smbd

  - name: create nginx yaaw site
    copy:
      content: |
        server {
            listen 80;
            server_name _;
         
            location / {
                index index.html;
                root /var/www/yaaw-master;
            }
         
            location /share {
                autoindex on;
                alias /home/pi/share;
            }
        }
      dest: /etc/nginx/sites-available/yaaw

  - name: download yaaw archive
    unarchive:
      src: https://github.com/binux/yaaw/archive/master.zip
      dest: /var/www
      copy: no

  - name: disable nginx default site
    file:
      dest: /etc/nginx/sites-enabled/default
      state: absent

  - name: enable nginx yaaw site
    file:
      src: /etc/nginx/sites-available/yaaw
      dest: /etc/nginx/sites-enabled/yaaw
      state: link
    notify:
    - restart nginx
    changed_when: yes

  - block:

    - name: update samba config for global
      ini_file:
        dest: /etc/samba/smb.conf
        section: "{{ item.section }}"
        option: "{{ item.option }}"
        value: "{{ item.value }}"
      with_items:
        - {section: global, option: guest account, value: pi}
        - {section: global, option: netbios name, value: pi}
      notify:
        - restart samba

    - name: update samba config for share
      blockinfile:
        dest: /etc/samba/smb.conf
        insertafter: EOF
        content: |
          [share]
          path = /home/pi/share
          browseable = yes
          read only = no
          guest ok = yes
      notify:
        - restart samba

  handlers:

    - name: reload systemd
      command: systemctl daemon-reload

    - name: restart aria2
      service:
        name: aria2
        state: restarted

    - name: restart nginx
      service:
        name: nginx
        state: restarted

    - name: restart samba
      service:
        name: smbd
        state: restarted
