-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf2remarkable
More file actions
executable file
·163 lines (140 loc) · 3.4 KB
/
pdf2remarkable
File metadata and controls
executable file
·163 lines (140 loc) · 3.4 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash
# Inspired by Adrian Daerr 2017/2018 - public domain
transferPdf() {
local pdfname="$1"
local tmpdir="$2"
local target_dir="$3"
uuid="$(uuidgen)"
cp -- "$pdfname" "${tmpdir}/${uuid}.pdf"
cat <<EOF >>"${tmpdir}/${uuid}.metadata"
{
"deleted": false,
"lastModified": "$(date +%s)000",
"metadatamodified": false,
"modified": false,
"parent": "",
"pinned": false,
"synced": false,
"type": "DocumentType",
"version": 1,
"visibleName": "$(basename -- "$pdfname" .pdf)"
}
EOF
cat <<EOF >"${tmpdir}/${uuid}.content"
{
"extraMetadata": {
},
"fileType": "pdf",
"fontName": "",
"lastOpenedPage": 0,
"lineHeight": -1,
"margins": 100,
"pageCount": 1,
"textScale": 1,
"transform": {
"m11": 1,
"m12": 1,
"m13": 1,
"m21": 1,
"m22": 1,
"m23": 1,
"m31": 1,
"m32": 1,
"m33": 1
}
}
EOF
mkdir "${tmpdir}/${uuid}.cache"
mkdir "${tmpdir}/${uuid}.highlights"
mkdir "${tmpdir}/${uuid}.thumbnails"
convert -density 300 "$pdfname"'[0]' \
-colorspace Gray \
-separate -average \
-shave 5%x5% \
-resize 280x374 \
"${tmpdir}/${uuid}.thumbnails/0.jpg"
echo "Transferring $pdfname as $uuid"
scp -r "${tmpdir}/${uuid}."* "$target_dir"
rm -rf "${tmpdir}/${uuid}."*
}
checkConnection() {
local host="$1"
echo "Checking connection to host: $host"
ssh -o ConnectTimeout=5 -o BatchMode=yes -q "$host" "exit 0"
# shellcheck disable=SC2181
if [[ $? != 0 ]]; then
return 10
fi
}
usage() {
cat <<EOF
Usage: $(basename "$0") [-h|--help] [--no-restart] FILE...
Options:
-h, --help Help message
--no-restart Do not restart xochitl after transfer
--host HOST override the ssh-host
--xochitl-dir PATH override the xochitl-dir
--local use 'local-remarkable' as hostname
EOF
}
shortOpts="h"
longOpts="help,no-restart,host:,xochitl-dir:,local"
args=$(getopt -o "$shortOpts" -l "$longOpts" -- "$@")
# shellcheck disable=SC2181
if [[ $? != 0 ]]; then
usage
exit 1
fi
eval set -- "$args"
restart="true"
remarkable_host=${REMARKABLE_HOST:-remarkable}
remarkable_xochitl_dir=${REMARKABLE_XOCHITL_DIR:-.local/share/remarkable/xochitl/}
while true; do
case "$1" in
-h | --help)
shift
usage
exit 1
;;
--no-restart)
shift
restart="true"
;;
--host)
shift
remarkable_host="$1"
shift
;;
--local)
shift
remarkable_host="local-remarkable"
;;
--)
shift
break
;;
*)
usage
exit 1
;;
esac
done
if [[ $# -lt 1 ]]; then
usage
exit 1
fi
if ! checkConnection "$remarkable_host"; then
echo "Connection to host '$remarkable_host' not possible, aborting..."
exit 255
fi
echo -e "Connection successful: host=$remarkable_host\n"
tmpdir="$(mktemp --directory --suffix='.pdf2remarkable')"
for pdfname in "$@"; do
transferPdf "$pdfname" "$tmpdir" "${remarkable_host}:${remarkable_xochitl_dir}"
done
rmdir "$tmpdir"
if [[ $restart == "true" ]]; then
echo "Restarting Xochitl..."
ssh "$remarkable_host" "systemctl restart xochitl"
echo "Done."
fi