fixed arrs clean up showing only orphans

This commit is contained in:
2026-04-24 17:22:34 -04:00
parent 212af90425
commit b3f72417a4
7 changed files with 1115 additions and 452 deletions
+27 -3
View File
@@ -69,6 +69,18 @@ else
SONARR_TV_ROOT="$HOST2_SONARR_TV_ROOT"
fi
# Load path map for this host
declare -A PATH_MAP
if [[ "$LOCAL_SERVER_NAME" == "$HOST1" ]]; then
for key in "${!HOST1_SONARR_PATH_MAP[@]}"; do
PATH_MAP["$key"]="${HOST1_SONARR_PATH_MAP[$key]}"
done
else
for key in "${!HOST2_SONARR_PATH_MAP[@]}"; do
PATH_MAP["$key"]="${HOST2_SONARR_PATH_MAP[$key]}"
done
fi
info "Sonarr instance: $LOCAL_SERVER_NAME$SONARR_URL"
info "TV root: $SONARR_TV_ROOT"
@@ -174,7 +186,11 @@ mkdir -p "$TMP_DIR"
trap "rm -rf $TMP_DIR" EXIT
TRACKED_FILE="$TMP_DIR/tracked_paths.txt"
echo "$EPISODEFILE_RESPONSE" | jq -r '.[].path' 2>/dev/null | sort > "$TRACKED_FILE"
while IFS= read -r api_path; do
[[ -z "$api_path" ]] && continue
translate_path "$api_path" PATH_MAP >> "$TRACKED_FILE"
done < <(echo "$EPISODEFILE_RESPONSE" | jq -r '.[].path' 2>/dev/null)
sort -u "$TRACKED_FILE" -o "$TRACKED_FILE"
TRACKED_COUNT=$(wc -l < "$TRACKED_FILE")
success "Sonarr tracks $TRACKED_COUNT episode files"
@@ -256,12 +272,20 @@ while IFS= read -r filepath; do
fi
fi
done < <(find "$SONARR_TV_ROOT" -type f 2>/dev/null)
done < <(
# Scan all host paths defined in PATH_MAP — covers all root folders managed by Sonarr
for host_path in "${PATH_MAP[@]}" "$SONARR_TV_ROOT"; do
[[ -d "$host_path" ]] && find "$host_path" -type f 2>/dev/null
done | sort -u
)
if [[ "$DRY_RUN" == false ]]; then
echo ""
info "Cleaning up empty folders..."
find "$SONARR_TV_ROOT" -mindepth 1 -type d -empty -delete 2>/dev/null
for host_path in "${PATH_MAP[@]}" "$SONARR_TV_ROOT"; do
[[ -d "$host_path" ]] && \
find "$host_path" -mindepth 1 -type d -empty -delete 2>/dev/null
done
success "Empty folders removed"
fi