aboutsummaryrefslogtreecommitdiff
path: root/copy-annotations-from-kobo
diff options
context:
space:
mode:
Diffstat (limited to 'copy-annotations-from-kobo')
-rwxr-xr-xcopy-annotations-from-kobo67
1 files changed, 67 insertions, 0 deletions
diff --git a/copy-annotations-from-kobo b/copy-annotations-from-kobo
new file mode 100755
index 0000000..f86d1fe
--- /dev/null
+++ b/copy-annotations-from-kobo
@@ -0,0 +1,67 @@
+#!/bin/bash
+#
+# A mini-wrapper to copy a remote kobo annotation into a local one.
+#
+
+# Parameters
+BASENAME="`basename $0`"
+BASEFOLDER="$HOME/data/doc"
+REMOTE_BASE="/mnt/sd"
+REMOTE="kobo"
+PATTERN="$1"
+
+# Thanks https://serverfault.com/a/764403
+# https://serverfault.com/questions/36421/stop-ssh-login-from-printing-motd-from-the-client#38160
+function silentSsh {
+ local connectionString="$1"
+ shift
+ local commands="$*"
+ if [ -z "$commands" ]; then
+ commands=`cat`
+ fi
+ ssh -T $connectionString "$commands"
+}
+
+# Check
+if [ -z "$PATTERN" ]; then
+ echo "usage: $BASENAME <pattern>"
+ exit 1
+fi
+
+# Dispatch
+show --search "$PATTERN" | while read item; do
+ #sdr="`echo $item | sed -e 's/\.pdf$//' -e 's/\.epub$//' -e 's/\.djvuf$//'`.sdr"
+ basename="`echo $item | sed -e 's/\.[^.]*$//'`"
+ extension="$(echo $item | sed -e "s|$basename.||")"
+ sdr="$basename.sdr"
+
+ # Check if remote file exists
+ silentSsh $REMOTE <<EOT
+ test -e "$REMOTE_BASE/$sdr/metadata.$extension.lua"
+EOT
+
+ # Skip if remote file does not exist
+ if [ "$?" != "0" ]; then
+ echo "Skipping $item as it's not available at $REMOTE..."
+ continue
+ fi
+
+ echo "Processing $item..."
+
+ # Make sure that the annotation file exists
+ mkdir -p "$BASEFOLDER/$sdr"
+ touch "$BASEFOLDER/$sdr/metadata.$extension.lua"
+
+ # Make sure it's not locked by git annex
+ if [ -L "$BASEFOLDER/$sdr/metadata.$extension.lua" ]; then
+ git -C $BASEFOLDER annex unlock "$sdr/metadata.$extension.lua"
+ fi
+
+ # Update
+ silentSsh kobo >"$BASEFOLDER/$sdr/metadata.$extension.lua" <<EOF
+ cat "$REMOTE_BASE/$sdr/metadata.$extension.lua"
+EOF
+
+ # Stage
+ git -C $BASEFOLDER add "$sdr/metadata.$extension.lua"
+done