#!/bin/sh # POSIX-only, /bin/sh compatible, ASCII-only # Dry-run by default; use --write to apply changes # Logs: $LOG_DIR (from env), Plan: $PLAN_OUT set -u # Load env if [ -f "/etc/synomap/synomap.env" ]; then . /etc/synomap/synomap.env fi # Defaults if not set QB_URL="${QB_URL:-}" QB_USER="${QB_USER:-}" QB_PASS="${QB_PASS:-}" MAP_FILE="${MAP_FILE:-}" DEST_ROOT="${DEST_ROOT:-}" QB_CATEGORIES="${QB_CATEGORIES:-sonarr,radarr}" QB_EXCLUDE_CATEGORIES="${QB_EXCLUDE_CATEGORIES:-hum}" TAG_DONE="${TAG_DONE:-SYNO}" PLAN_OUT="${PLAN_OUT:-/root/synomap_plan.txt}" LOG_DIR="${LOG_DIR:-/var/log/synomap}" LOCK_DIR="${LOCK_DIR:-/var/lock/synomap}" UMASK_VAL="${UMASK:-002}" umask "$UMASK_VAL" mkdir -p "$LOG_DIR" "$LOCK_DIR" 2>/dev/null || true DRY=1 WRITE=0 PLAN="" LIMIT="" log() { printf "%s %s " "$(date +%Y-%m-%dT%H:%M:%S)" "$*" | tee -a "$LOG_DIR/synomap.log" ; } nok() { log "[NOK] $*"; } ok() { log "[OK] $*"; } note(){ log "[NOTE] $*"; } qb_cookie="" qb_login() { qb_cookie="$(mktemp)" curl -sS -c "$qb_cookie" --data-urlencode "username=$QB_USER" --data-urlencode "password=$QB_PASS" "$QB_URL/api/v2/auth/login" >/dev/null 2>&1 || return 1 return 0 } qb_api() { m="$1"; shift p="$1"; shift if [ "$m" = "GET" ]; then curl -sS -b "$qb_cookie" "$QB_URL$p" 2>/dev/null else curl -sS -b "$qb_cookie" -X POST "$QB_URL$p" "$@" 2>/dev/null fi } qb_info_many() { qb_api GET "/api/v2/torrents/info" } qb_set_location() { qb_api POST "/api/v2/torrents/setLocation" --data "hashes=$1" --data-urlencode "location=$2" } qb_add_tags() { qb_api POST "/api/v2/torrents/addTags" --data "hashes=$1" --data-urlencode "tags=$2" } qb_recheck() { qb_api POST "/api/v2/torrents/recheck" --data "hashes=$1" } poll_state() { h="$1" tm="${2:-120}" i=0 while [ "$i" -lt "$tm" ]; do st="$(qb_api GET "/api/v2/torrents/info?hashes=$h" | tr -d '\r' | tr -d '\n' )" state="$(printf "%s" "$st" | sed -n 's/.*\"state\":\"\([^\"]*\)\".*/\1/p')" if [ -n "$state" ] ; then log "[STATE] $h = $state" case "$state" in checking*|queuedForChecking) : ;; downloading|stalledDL|stalledUP|uploading|pausedUP|pausedDL) break ;; *) : ;; esac fi sleep 2 i=$((i+2)) done } # CLI parse while [ $# -gt 0 ]; do case "$1" in --write) DRY=0; WRITE=1; shift;; --dry-run) DRY=1; WRITE=0; shift;; --plan) PLAN="$2"; shift 2;; --limit) LIMIT="$2"; shift 2;; *) note "arg ignored: $1"; shift;; esac done # synomap_miniplan_pick.sh # Build a simple plan from qbittorrent info and mapping file # Output lines: hash|src_save_path|dst_path|category if [ -n "$PLAN" ]; then PLAN_OUT="$PLAN" fi if [ ! -f "$MAP_FILE" ]; then nok "MAP_FILE missing: $MAP_FILE" exit 0 fi ok "Loading mapping: $MAP_FILE" M="$(mktemp)" grep -v '^[[:space:]]*$' "$MAP_FILE" | grep '|' > "$M" || true ok "Login to qBittorrent" if ! qb_login; then nok "qB login failed" rm -f "$M" exit 0 fi J="$(qb_info_many)" if [ -z "$J" ]; then nok "qB info empty" rm -f "$M" "$qb_cookie" exit 0 fi incl="$(printf "%s" "$QB_CATEGORIES" | tr ',' ' ')" excl="$(printf "%s" "$QB_EXCLUDE_CATEGORIES" | tr ',' ' ')" tmp="$(mktemp)" echo "" > "$tmp" echo "$J" | tr -d '\n\r' | sed 's/},{/}\n{/g' | while IFS= read -r obj; do h="$(printf "%s" "$obj" | sed -n 's/.*"hash":"\([^"]*\)".*/\1/p' | head -n1)" sp="$(printf "%s" "$obj" | sed -n 's/.*"save_path":"\([^"]*\)".*/\1/p' | head -n1 | sed 's/\\\\\//g;s/\\/\//g')" catg="$(printf "%s" "$obj" | sed -n 's/.*"category":"\([^"]*\)".*/\1/p' | head -n1)" tags="$(printf "%s" "$obj" | sed -n 's/.*"tags":"\([^"]*\)".*/\1/p' | head -n1)" [ -z "$h" ] && continue [ -z "$sp" ] && continue [ -z "$catg" ] && catg="" case ",$tags," in *",$TAG_DONE,"*) continue;; esac skip=0 for x in $excl; do [ "$catg" = "$x" ] && skip=1 && break done [ "$skip" -eq 1 ] && continue want=0 for c in $incl; do [ "$catg" = "$c" ] && want=1 && break done [ "$want" -eq 0 ] && continue dst="" while IFS= read -r mline; do c="$(printf "%s" "$mline" | cut -d'|' -f1)" src="$(printf "%s" "$mline" | cut -d'|' -f2)" dstroot="$(printf "%s" "$mline" | cut -d'|' -f3-)" case "$catg" in "$c") : ;; *) continue;; esac case "$sp" in "$src"*) rel="${sp#$src}"; dst="$dstroot$rel"; break;; *) : ;; esac done < "$M" [ -z "$dst" ] && continue printf "%s|%s|%s|%s " "$h" "$sp" "$dst" "$catg" >> "$tmp" done count="$(wc -l < "$tmp" | tr -d ' ')" [ -n "$LIMIT" ] && [ "$LIMIT" -gt 0 ] && head -n "$LIMIT" "$tmp" > "$tmp.head" && mv "$tmp.head" "$tmp" if [ "$DRY" -eq 1 ]; then ok "Plan preview ($count items):" sed -n '1,50p' "$tmp" fi mv "$tmp" "$PLAN_OUT" ok "Plan written to $PLAN_OUT" rm -f "$M" "$qb_cookie" exit 0