# GENERAL
max-concurrent-downloads={{ aria.dl.max.concurrent | string }}
dir={{ ansible_user_home.stdout + "/downloads/aria2" }}
log={{ ansible_user_home.stdout + "/.aria2/aria2.log" }}
log-level={{ aria.log }}
console-log-level=notice
continue=true

{% if aria.dl.resume %}
always-resume=true
{% else %}
always-resume=false
{% endif %}

{% if aria.dl.overwriting %}
allow-overwrite=true
{% else %}
allow-overwrite=false
{% endif %}

{% if aria.dl.autorenaming %}
auto-file-renaming=true
{% else %}
auto-file-renaming=false
{% endif %}

file-allocation={{ aria.alloc }}
disk-cache={{ aria.dcache | string }}
enable-mmap=true
enable-color=true
human-readable=true
keep-unfinished-download-result=true
max-download-result=500
max-resume-failure-tries=0

# RPC
{% if aria.rpc.enabled %}
enable-rpc=true

{% if mode == "prod" or aria.rpc.scheme == "https" %}
rpc-secure=true

{% if not certbot.containerized %}
rpc-certificate={{ "/etc/letsencrypt/live/" + web_fqdn + "/fullchain.pem" }}
rpc-private-key={{ "/etc/letsencrypt/live/" + web_fqdn + "/privkey.pem" }}
{% else %}
rpc-certificate={{ ansible_user_home.stdout + "/.config/letsencrypt/live/" + web_fqdn + "/fullchain.pem" }}
rpc-private-key={{ ansible_user_home.stdout + "/.config/letsencrypt/live/" + web_fqdn + "/privkey.pem" }}
{% endif %}

{% endif %}

{% if aria.rpc.listen_all %}
rpc-listen-all=true
{% else %}
rpc-listen-all=false
{% endif %}

rpc-listen-port={{ aria.rpc.port | string }}

{% if aria.rpc.credentials is undefined or aria.rpc.credentials == None %}
rpc-secret={{ aria.rpc.secret | default(lookup('password', './.tmp/' + inventory_hostname + '-aria/aria.secret', chars=['ascii_lowercase', 'digits'], length=32)) }}
{% else %}
rpc-user={{ aria.rpc.credentials.username }}
rpc-password={{ aria.rpc.credentials.password }}
{% endif %}

{% else %}
enable-rpc=false
{% endif %}


# (HT/SF/F)TP
{% if aria.conn.proxy.enabled %}
all-proxy={{ aria.conn.proxy.uri }}
no-proxy={{ aria.conn.proxy.excluded | join(",") }}
{% endif %}

max-connection-per-server={{ aria.conn.max.per_server | string }}
split={{ aria.conn.split | string }}
max-tries={{ aria.conn.max.attempts | string }}
retry-wait=15
netrc-path={{ ansible_user_home.stdout + "/.netrc" }}
server-stat-if={{ ansible_user_home.stdout + "/.aria2/dl.log" }}
server-stat-of={{ ansible_user_home.stdout + "/.aria2/dl.log" }}
uri-selector={{ aria.dl.algorithm }}


# HTTP
{% if aria.http.gzip %}
http-accept-gzip=true
{% else %}
http-accept-gzip=false
{% endif %}

{% if aria.http.cache %}
http-no-cache=true
{% else %}
http-no-cache=false
{% endif %}

{% if aria.http.sustain %}
enable-http-keep-alive=true
{% else %}
enable-http-keep-alive=false
{% endif %}

{% if aria.http.agent is defined and aria.http.agent != None %}
user-agent={{ aria.http.agent }}
{% endif %}


# (S)FTP
{% if aria.ftp.mode == "passive" %}
ftp-pasv=true
{% elif aria.ftp.mode == "active" %}
ftp-pasv=false
{% endif %}

ftp-type={{ aria.ftp.data_type }}


# METALINKS/TORRENTS


# TORRENTS
bt-detach-seed-only=true

{% if aria.trnt.local_discovery %}
bt-enable-lpd=true
{% else %}
bt-enable-lpd=false
{% endif %}

{% if aria.trnt.encrypt %}
bt-force-encryption=true
{% else %}
bt-force-encryption=false
{% endif %}

bt-max-peers={{ aria.trnt.peers.max }}

{% if aria.trnt.trackers is defined and aria.trnt.trackers != None and (aria.trnt.trackers | length) > 0 %}
bt-tracker={{ aria.trnt.trackers | join(",") }}
{% endif %}

{% if aria.trnt.dht.enabled %}
enable-dht=true
enable-dht6=true
dht-file-path={{ ansible_user_home.stdout + "/.aria2/dht.dat" }}
dht-file-path6={{ ansible_user_home.stdout + "/.aria2/dht6.dat" }}

{% if aria.trnt.dht.entrypoint is defined and aria.trnt.dht.entrypoint != None %}
dht-entry-point={{ aria.trnt.dht.entrypoint }}
dht-entry-point6={{ aria.trnt.dht.entrypoint }}
{% endif %}

dht-listen-port={{ aria.trnt.dht.port | string }}
{% endif %}

{% if aria.trnt.peers.agent is defined and aria.trnt.peers.agent != None %}
peer-agent={{ aria.trnt.peers.agent }}
{% endif %}

{% if aria.trnt.peers.prefix is defined and aria.trnt.peers.prefix != None %}
peer-id-prefix={{ aria.trnt.peers.prefix }}
{% endif %}

{% if aria.trnt.peers.exchange %}
enable-peer-exchange=true
{% else %}
enable-peer-exchange=false
{% endif %}

seed-ratio={{ aria.trnt.seeding.ratio | string }}
seed-time={{ aria.trnt.seeding.time | string }}

max-overall-upload-limit={{ aria.ul.max.overall | string }}
max-upload-limit={{ aria.ul.max.per_entry | string }}

# METALINK
{% if aria.meta.follow == "mem" %}
follow-metalink={{ aria.meta.follow }}
{% elif aria.meta.follow %}
follow-metalink=true
{% else %}
follow-metalink=false
{% endif %}

metalink-language={{ aria.meta.lang }}

{% if aria.meta.locs is defined and aria.meta.locs != None and (aria.meta.locs | length) > 0 %}
metalink-location={{ aria.meta.locs | join(",") }}
{% endif %}

metalink-preferred-protocol={{ aria.meta.protocol_pref }}