From 5c2b30a924055a7d2944026addb3227e890a7a05 Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Thu, 29 Apr 2021 11:39:49 +0300
Subject: [PATCH] Trigger a Kodi library scan after a download completes.

---
 daemon/Dockerfile    |  2 ++
 daemon/kodi-scan     | 42 ++++++++++++++++++++++++++++++++++++++++++
 daemon/settings.json |  4 ++--
 3 files changed, 46 insertions(+), 2 deletions(-)
 create mode 100755 daemon/kodi-scan

diff --git a/daemon/Dockerfile b/daemon/Dockerfile
index 3cca629..0170961 100644
--- a/daemon/Dockerfile
+++ b/daemon/Dockerfile
@@ -4,11 +4,13 @@ RUN apk add --update --no-cache --repository http://dl-cdn.alpinelinux.org/alpin
         gosu \
     && \
     apk add --update --no-cache \
+        curl \
         transmission-daemon \
     && \
     mkdir -m 777 /run/transmission
 COPY --chown=root:root entrypoint /usr/local/sbin/docker-entrypoint
 COPY --chown=root:root settings.json /etc/transmission/
+COPY --chown=root:root kodi-scan /usr/local/bin/
 VOLUME /var/lib/transmission
 WORKDIR /var/lib/transmission
 ENV HOME /var/lib/transmission
diff --git a/daemon/kodi-scan b/daemon/kodi-scan
new file mode 100755
index 0000000..b46337a
--- /dev/null
+++ b/daemon/kodi-scan
@@ -0,0 +1,42 @@
+#!/bin/sh
+set -eu
+
+scan() {
+    case "${1:-}" in
+        audio) curl \
+                --data-binary \
+                '{ "jsonrpc": "2.0", "method": "AudioLibrary.Scan", "id": "transmission"}' \
+                -H 'content-type: application/json;' \
+                http://172.18.0.1:8080/jsonrpc ;;
+        video) curl \
+                --data-binary \
+                '{ "jsonrpc": "2.0", "method": "VideoLibrary.Scan", "id": "transmission"}' \
+                -H 'content-type: application/json;' \
+                http://172.18.0.1:8080/jsonrpc ;;
+        *) scan video; scan audio ;;
+    esac
+}
+
+starts_with() {
+    [ "${1##$2}" != "$1" ]
+}
+
+is_in_dir() {
+    ! starts_with "$(realpath --relative-base "$1" "$2")" '/'
+}
+
+if [ -z "${TR_TORRENT_DIR:-}" ]
+then
+    scan
+elif is_in_dir '/var/lib/transmission/Downloads/TV Shows' "$TR_TORRENT_DIR"
+then
+    scan video
+elif is_in_dir '/var/lib/transmission/Downloads/Movies' "$TR_TORRENT_DIR"
+then
+    scan video
+elif is_in_dir '/var/lib/transmission/Downloads/Music' "$TR_TORRENT_DIR"
+then
+    scan audio
+else
+    scan
+fi
diff --git a/daemon/settings.json b/daemon/settings.json
index c92e48d..9698f2f 100644
--- a/daemon/settings.json
+++ b/daemon/settings.json
@@ -55,8 +55,8 @@
     "rpc-whitelist": "127.0.0.*,192.168.*.*,10.*.*.*,172.16.*.*,172.17.*.*,172.18.*.*,172.19.*.*,172.2?.*.*,172.30.*.*,172.31.*.*",
     "rpc-whitelist-enabled": false,
     "scrape-paused-torrents-enabled": true,
-    "script-torrent-done-enabled": false,
-    "script-torrent-done-filename": "",
+    "script-torrent-done-enabled": true,
+    "script-torrent-done-filename": "/usr/local/bin/kodi-scan",
     "seed-queue-enabled": false,
     "seed-queue-size": 10,
     "speed-limit-down": 100,
-- 
GitLab