home · contact · privacy
Fix. master
authorChristian Heller <c.heller@plomlompom.de>
Thu, 15 May 2025 22:06:00 +0000 (00:06 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Thu, 15 May 2025 22:06:00 +0000 (00:06 +0200)
14 files changed:
bookworm/copy/seedbox/home/plom/.local/bin/print_torrents
bookworm/copy/seedbox/home/plom/.plomlib/constants_seedbox [new symlink]
bookworm/copy/seedbox/home/plom/.plomlib/expected_rtorrent_files [new symlink]
bookworm/copy/seedbox/home/plom/.plomlib/for_line_in [new symlink]
bookworm/scripts/lib/constants_seedbox [new file with mode: 0644]
bookworm/scripts/lib/expected_rtorrent_files [new file with mode: 0644]
bookworm/scripts/lib/for_line_in [new file with mode: 0644]
testing/scripts/lib/constants_seedbox [new symlink]
testing/scripts/lib/expected_rtorrent_files [new symlink]
testing/scripts/lib/for_line_in [new symlink]
testing/scripts/sync_rtorrent_download.sh
trixie/scripts/lib/constants_seedbox [new symlink]
trixie/scripts/lib/expected_rtorrent_files [new symlink]
trixie/scripts/lib/for_line_in [new symlink]

index 0e04b98c53b8e49d87dc2fdb6bbacb9932fb0310..56f0125fc26f88be72b690bbf06dee47e2ae4869 100755 (executable)
@@ -1,39 +1,60 @@
 #!/bin/sh
 set -e
 cd $(dirname "$0")
+. lib/constants_seedbox
+. lib/expected_rtorrent_files
+. lib/for_line_in
 
 PATTERN_TORRENTNAME=".*e4:name[0-9]+:(.+)12:piece length.*"
 PATTERN_DU_NONSUMMARY="^[[:graph:]]+[[:blank:]]+\.\/"
 PATTERN_DU_SIZE_NAME="^([[:graph:]]+)\t\.\/(.+)$"
+PATH_SESSIONS="${HOME}/session"
+PATH_DOWNLOADS="${HOME}/downloads"
+SESSION_IGNORE='--hide=rtorrent.lock --hide=rtorrent.input_history'
 
-cd "${HOME}/session"
+cd "${PATH_SESSIONS}"
 HASHED_TITLES=
-FILENAMES_TORRENTS=$(ls *.torrent)
-for _FILENAME in ${FILENAMES_TORRENTS}; do
-    _HASH=$(printf '%s' "${_FILENAME}" | cut -d '.' -f 1)
-    _TITLE=$(cat -v "${_FILENAME}" | head -1 | sed -E "s/${PATTERN_TORRENTNAME}/\1/g")
-    HASHED_TITLES="${_HASH}:${_TITLE}\n${HASHED_TITLES}"
-done
+COLLECTED_HASHES=
+hash_titles() {
+    local FILENAME=$1
+    local HASH=$(printf '%s' "${FILENAME}" | sed 's/\'"${EXT_TORRENT}"'.*//g' | grep -E "${PATTERN_HASH_ONLY}")
+    if [ ! -z "${HASH}" ]; then
+        echo "${ALREADY_COLLECTED}" | grep "${HASH}" > /dev/null && continue
+       ALREADY_COLLECTED=$(printf '%s\n%s' "${HASH}" "${ALREADY_COLLECTED}")
+        local TITLE=$(cat -v "${FILENAME}" | head -1 | sed -E "s/${PATTERN_TORRENTNAME}/\1/g")
+       HASHED_TITLES=$(printf '%s:%s\n%s' "${HASH}" "${TITLE}" "${HASHED_TITLES}")
+    fi
+}
+for_line_in "$(ls -1 *${EXT_TORRENT}*)" hash_titles
+print_error( ) { printf '? ? %s\n' "$1"; }
+for_line_in "$(ls -1 ${SESSION_IGNORE} | grep -vE "^${PATTERN_HASH}"'\'"${EXT_TORRENT}")" print_error
 
-cd "${HOME}/downloads"
+cd "${PATH_DOWNLOADS}"
 SIZED_DOWNLOADS=$(du -ad1 -bh | grep -E "${PATTERN_DU_NONSUMMARY}" | sed -E "s/${PATTERN_DU_SIZE_NAME}/\1:\2/g")
-for _LINE in $(echo "${SIZED_DOWNLOADS}" | sed 's/ /REPLACED_WHITESPACE/g'); do
-    _LINE=$(printf '%s' "${_LINE}" | sed 's/REPLACED_WHITESPACE/ /g') 
-    _SIZE=$(printf '%s' "${_LINE}" | cut -d':' -f 1)
-    _DOWNLOAD=$(printf '%s' "${_LINE}" | cut -d':' -f 2-)
-    _FOUND=
-    for _LINE in $(echo "${HASHED_TITLES}" | sed 's/ /REPLACED_WHITESPACE/g'); do
-        _LINE=$(printf '%s' "${_LINE}" | sed 's/REPLACED_WHITESPACE/ /g') 
-        _HASH=$(printf '%s' "${_LINE}" | cut -d':' -f 1)
-        _TITLE=$(printf '%s' "${_LINE}" | cut -d':' -f 2-)
-        if [ "${_TITLE}" = "${_DOWNLOAD}" ]; then 
-            _FOUND=1
-            printf '%s' "${_HASH}"
-            break 
+print_entry() {
+    local LINE=$1
+    local STATUS='?'
+    local HASH='?'
+    local SIZE=$(printf '%s' "${LINE}" | cut -d':' -f 1)
+    local TITLE_DOWNLOADED=$(printf '%s' "${LINE}" | cut -d':' -f 2-)
+    local FOUND=
+    find_hash() {
+        local LINE=$1
+        local MAYBE_HASH=$(printf '%s' "${LINE}" | cut -d':' -f 1)
+        local TITLE_HASHED=$(printf '%s' "${LINE}" | cut -d':' -f 2-)
+        if [ "${TITLE_HASHED}" = "${TITLE_DOWNLOADED}" ]; then
+            HASH=${MAYBE_HASH}
+            FOUND=1
+            break
         fi
-    done
-    if [ -z "${_FOUND}" ]; then
-        printf 'no_torrent_files'
-    fi 
-    printf ' %s %s\n' "${_SIZE}" "${_DOWNLOAD}"
-done
+    }
+    for_line_in "${HASHED_TITLES}" find_hash
+    if [ ! -z "${FOUND}" ]; then
+        local PATH_TORRENT="${PATH_SESSIONS}/${HASH}${EXT_TORRENT}"
+        if [ $(ls -1 "${PATH_TORRENT}"* | wc -l) -eq 3 ] && expected_rtorrent_files "${PATH_TORRENT}"; then
+            STATUS='+'
+        fi
+    fi
+    printf '%s %s %s %s\n' "${STATUS}" "${HASH}" "${SIZE}" "${TITLE_DOWNLOADED}"
+}
+for_line_in "${SIZED_DOWNLOADS}" print_entry
diff --git a/bookworm/copy/seedbox/home/plom/.plomlib/constants_seedbox b/bookworm/copy/seedbox/home/plom/.plomlib/constants_seedbox
new file mode 120000 (symlink)
index 0000000..e625e65
--- /dev/null
@@ -0,0 +1 @@
+../../../../../scripts/lib/constants_seedbox
\ No newline at end of file
diff --git a/bookworm/copy/seedbox/home/plom/.plomlib/expected_rtorrent_files b/bookworm/copy/seedbox/home/plom/.plomlib/expected_rtorrent_files
new file mode 120000 (symlink)
index 0000000..f71bc5e
--- /dev/null
@@ -0,0 +1 @@
+../../../../../scripts/lib/expected_rtorrent_files
\ No newline at end of file
diff --git a/bookworm/copy/seedbox/home/plom/.plomlib/for_line_in b/bookworm/copy/seedbox/home/plom/.plomlib/for_line_in
new file mode 120000 (symlink)
index 0000000..0a015e7
--- /dev/null
@@ -0,0 +1 @@
+../../../../../scripts/lib/for_line_in
\ No newline at end of file
diff --git a/bookworm/scripts/lib/constants_seedbox b/bookworm/scripts/lib/constants_seedbox
new file mode 100644 (file)
index 0000000..9b812b8
--- /dev/null
@@ -0,0 +1,5 @@
+EXT_TORRENT='.torrent'
+PATTERN_HASH_CHAR='[A-Z0-9]'
+PATTERN_HASH="${PATTERN_HASH_CHAR}{40}"
+PATTERN_HASH_ONLY="^${PATTERN_HASH}\$"
+
diff --git a/bookworm/scripts/lib/expected_rtorrent_files b/bookworm/scripts/lib/expected_rtorrent_files
new file mode 100644 (file)
index 0000000..4db0a17
--- /dev/null
@@ -0,0 +1,12 @@
+expected_rtorrent_files() {
+    local PATH_REL_TORRENT=$1
+    local EXT
+    for EXT in '' '.rtorrent' '.libtorrent_resume'; do
+        if [ ! -f "${PATH_REL_TORRENT}${EXT}" ]; then 
+            return 1
+        fi 
+    done
+    return 0
+}
+
+
diff --git a/bookworm/scripts/lib/for_line_in b/bookworm/scripts/lib/for_line_in
new file mode 100644 (file)
index 0000000..7e49bcc
--- /dev/null
@@ -0,0 +1,12 @@
+for_line_in() {
+    local __LINES="$1"
+    local __COMMAND="$2"
+    local __OLD_IFS=${IFS}
+    local IFS=$(printf '\n\r') 
+    local __LINE 
+    for __LINE in ${__LINES}; do
+        IFS=${__OLD_IFS}
+        ${__COMMAND} "${__LINE}"
+    done
+    IFS=${__OLD_IFS}
+}
diff --git a/testing/scripts/lib/constants_seedbox b/testing/scripts/lib/constants_seedbox
new file mode 120000 (symlink)
index 0000000..0798f6b
--- /dev/null
@@ -0,0 +1 @@
+../../../trixie/scripts/lib/constants_seedbox
\ No newline at end of file
diff --git a/testing/scripts/lib/expected_rtorrent_files b/testing/scripts/lib/expected_rtorrent_files
new file mode 120000 (symlink)
index 0000000..74450e3
--- /dev/null
@@ -0,0 +1 @@
+../../../trixie/scripts/lib/expected_rtorrent_files
\ No newline at end of file
diff --git a/testing/scripts/lib/for_line_in b/testing/scripts/lib/for_line_in
new file mode 120000 (symlink)
index 0000000..9d87670
--- /dev/null
@@ -0,0 +1 @@
+../../../trixie/scripts/lib/for_line_in
\ No newline at end of file
index 9ac2994ef2a1924ba19d77387d09cfb866a9f4f4..57afbf48c9b58233ab9d93b0031b53483a220f8d 100755 (executable)
@@ -4,53 +4,114 @@ cd $(dirname "$0")
 . lib/abort
 . lib/abort_if_exists
 . lib/abort_if_offline
+. lib/constants_seedbox
 . lib/expect_n_args
+. lib/expected_rtorrent_files
+. lib/for_line_in
 . lib/path_tmp_timestamped
 
+DIRNAME_DOWNLOAD='download'
+DIRNAME_SESSION='session'
+
 prefixed_msg_init
 abort_if_offline
 
-BACKUPS_DIR="${HOME}/del/torrentexp"
+expect_n_args 2 3 'LOCAL_STORE SERVER [SELECTION]' $@
+LOCAL_STORE="$1"
+SERVER="$2"
+SELECTION="$3"
+if [ ! -d "${LOCAL_STORE}" ]; then
+    abort "Aborting, no local storage directory at ${LOCAL_STORE}."
+fi
+if echo "${SELECTION}" | grep -v "^${PATTERN_HASH_CHAR}*\$" > /dev/null; then
+    abort "Bad SELECTION format: ${SELECTION}"
+fi
 
-expect_n_args 1 2 'SERVER [SELECTION]' $@
-SERVER="$1"
-SELECTION="$2"
+prefixed_msg 'Collecting known media …'
+OVERVIEW_LOCAL=
+cd "${LOCAL_STORE}"
+parse_line() {
+    local TITLE="$1"
+    local HASH='?'
+    local SIZE='?'
+    local STATUS='?'
+    local PATH_DOWNLOAD="${TITLE}/${DIRNAME_DOWNLOAD}"
+    local PATH_SESSION="${TITLE}/${DIRNAME_SESSION}"
+    if [ -d "${PATH_DOWNLOAD}" ]; then
+        SIZE=$(du --bytes --human-readable --summarize "${PATH_DOWNLOAD}" | cut -f 1)
+    fi
+    if [ -d "${PATH_SESSION}" ]; then
+        local SESSION_FILES_LISTING=$(ls -1 "${PATH_SESSION}")
+        if [ $(echo "${SESSION_FILES_LISTING}" | wc -l) -eq 3 ]; then
+            local MAYBE_HASH=$(basename $(echo "${SESSION_FILES_LISTING}" | head -1) "${EXT_TORRENT}" | grep -E "${PATTERN_HASH_ONLY}")
+            if [ ! -z "${MAYBE_HASH}" ] && expected_rtorrent_files "${PATH_SESSION}/${MAYBE_HASH}${EXT_TORRENT}"; then
+                HASH="${MAYBE_HASH}"
+            fi
+        fi
+    fi
+    if [ "${SIZE}" != "?" -a "${HASH}" != "?" ]; then
+       STATUS='+'
+    fi
+    OVERVIEW_LOCAL=$(printf '%s %s %s %s\n%s' "${STATUS}" "${HASH}" "${SIZE}" "${TITLE}" "${OVERVIEW_LOCAL}")
+}
+for_line_in "$(ls -1)" parse_line
+cd - > /dev/null
+OVERVIEW_REMOTE=$(ssh -t -q "${SERVER}" 'sh -cl print_torrents')
 
-OVERVIEW=$(ssh -t -q "${SERVER}" './print_torrents' | tr -d "\r")
 if [ -z "${SELECTION}" ]; then
-    prefixed_msg 'Available:'
-    for _LINE in $(echo "${OVERVIEW}" | sed 's/ /REPLACED_WHITESPACE/g'); do
-        _LINE=$(printf '%s' "${_LINE}" | sed 's/REPLACED_WHITESPACE/ /g') 
-        prefixed_msg "- ${_LINE}" 
-    done
+    prefixed_msg 'Found remotely:'
+    for_line_in "${OVERVIEW_REMOTE}" prefixed_msg
+    prefixed_msg 'Found locally:'
+    for_line_in "${OVERVIEW_LOCAL}" prefixed_msg
     exit 0
 fi
-LEN_SELECTION=$(printf '%s' "${SELECTION}" | wc -c)
+
 FOUND=
-for _LINE in $(echo "${OVERVIEW}" | sed 's/ /REPLACED_WHITESPACE/g'); do
-    _LINE=$(printf '%s' "${_LINE}" | sed 's/REPLACED_WHITESPACE/ /g') 
-    _LINE_START=$(printf '%s' "${_LINE}" | cut -c -${LEN_SELECTION}) 
-    if [ "${_LINE_START}" = "${SELECTION}" ]; then
-        HASH=$(printf '%s' "${_LINE}" | cut -d' ' -f 1) 
-        TITLE=$(printf '%s' "${_LINE}" | cut -d' ' -f 3-) 
+find_match() {
+    local LINE_CUT=$(printf '%s' "$1" | cut -c 3-)
+    local HASH_START=$(printf '%s' "${LINE_CUT}" | cut -c -${#SELECTION})
+    if [ "${HASH_START}" = "${SELECTION}" ]; then
+        if [ ! -z "${FOUND}" ]; then
+            abort "Ambiguous selection, more than one hash starting with: ${SELECTION}"
+        fi
         FOUND=1
-        break
-    fi 
-done
+        HASH=$(printf '%s' "${_LINE_CUT}" | cut -d' ' -f 1)
+        TITLE=$(printf '%s' "${_LINE_CUT}" | cut -d' ' -f 3-)
+    fi
+}
+SYNC_DOWN=
+for_line_in "${OVERVIEW_REMOTE}" find_match
+if [ ! -z "${FOUND}" ]; then
+    SYNC_DOWN=1
+fi
+for_line_in "${OVERVIEW_LOCAL}" find_match
 if [ -z "${FOUND}" ]; then
     abort "No known hash starting with: ${SELECTION}"
 fi
-TARGET_DIR="${BACKUPS_DIR}/${TITLE}"
-abort_if_exists "${TARGET_DIR}"
-prefixed_msg "Downloading torrent files and content for: ${HASH} ${TITLE}"
-TMP_DIR=$(path_tmp_timestamped "${TITLE}")
-SESSION_DIR="${TMP_DIR}/session"
-DOWNLOAD_DIR="${TMP_DIR}/download"
-mkdir -p "${SESSION_DIR}"
-mkdir -p "${DOWNLOAD_DIR}"
-scp "${SERVER}:~/session/${HASH}"* "${SESSION_DIR}/"
-scp -r "${SERVER}:~/downloads/${TITLE}" "${DOWNLOAD_DIR}/"
-prefixed_msg "Storing in: ${TARGET_DIR}"
-mv "${TMP_DIR}" "${TARGET_DIR}"
+prefixed_msg_no_nl "Hash ${HASH} selected for "
+if [ -z "${SYNC_DOWN}" ]; then printf 'LOCAL'; else printf 'REMOTE'; fi
+printf ' media: %s\n' "${TITLE}"
+
+TARGET_DIR="${LOCAL_STORE}/${TITLE}"
+TMP_DIR=/$(path_tmp_timestamped "${TITLE}")
+if [ -z "${SYNC_DOWN}" ]; then
+    prefixed_msg 'Uploading …'
+    #ssh -t -q "${SERVER}" "mkdir ${TMP_DIR}"
+    #scp -r "${TARGET_DIR}/"* "${SERVER}:${TMP_DIR}/"
+    #ssh -t -q "${SERVER}" "mv ${TMP_DIR}/download downloads/${TITLE}"
+    #ssh -t -q "${SERVER}" "mv ${TMP_DIR}/session/* session/"
+    #ssh -t -q "${SERVER}" "rmdir -p ${TMP_DIR}/session"
+else
+    prefixed_msg 'Downloading …'
+    #abort_if_exists "${TARGET_DIR}"
+    #SESSION_DIR="${TMP_DIR}/session"
+    #DOWNLOAD_DIR="${TMP_DIR}/download"
+    #mkdir -p "${SESSION_DIR}"
+    #mkdir -p "${DOWNLOAD_DIR}"
+    #scp "${SERVER}:~/session/${HASH}"* "${SESSION_DIR}/"
+    #scp -r "${SERVER}:~/downloads/${TITLE}" "${DOWNLOAD_DIR}/"
+    #prefixed_msg "Storing in: ${TARGET_DIR}"
+    #mv "${TMP_DIR}" "${TARGET_DIR}"
+fi
 
 prefixed_msg_exit
diff --git a/trixie/scripts/lib/constants_seedbox b/trixie/scripts/lib/constants_seedbox
new file mode 120000 (symlink)
index 0000000..2b5a74d
--- /dev/null
@@ -0,0 +1 @@
+../../../bookworm/scripts/lib/constants_seedbox
\ No newline at end of file
diff --git a/trixie/scripts/lib/expected_rtorrent_files b/trixie/scripts/lib/expected_rtorrent_files
new file mode 120000 (symlink)
index 0000000..04bc8a5
--- /dev/null
@@ -0,0 +1 @@
+../../../bookworm/scripts/lib/expected_rtorrent_files
\ No newline at end of file
diff --git a/trixie/scripts/lib/for_line_in b/trixie/scripts/lib/for_line_in
new file mode 120000 (symlink)
index 0000000..be20aa1
--- /dev/null
@@ -0,0 +1 @@
+../../../bookworm/scripts/lib/for_line_in
\ No newline at end of file