aboutsummaryrefslogtreecommitdiff
path: root/copy-item-to-kobo
blob: aa751fb3c2cada29c01044e2035fabe5399db277 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
#
# A mini-wrapper to copy a local item to kobo.
#

# Parameters
BASENAME="`basename $0`"
BASEFOLDER="$HOME/data/doc"
REMOTE_BASE="/mnt/sd"
REMOTE="kobo"
PATTERN="$1"
TMPFILE="tmpwork"

# 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
  dirname="`dirname "$item"`"

  # Check if item has unspported chars in it's file name
  if echo "$item" | grep -q ':'; then
    echo "$BASENAME: unsupported character ':' in file name '$item'"
    echo "$BASENAME: please rename the file and try again"
    exit 1
  fi

  echo "Copying $item..."

  silentSsh $REMOTE <<EOC
    mkdir -p "$REMOTE_BASE/$dirname"
    touch "$REMOTE_BASE/$item"
EOC

  scp "$BASEFOLDER/$item" "$REMOTE:$REMOTE_BASE/$TMPFILE"
  silentSsh $REMOTE <<EOS
    mv "$REMOTE_BASE/$TMPFILE" "$REMOTE_BASE/$item"
EOS
done