-
Notifications
You must be signed in to change notification settings - Fork 1
/
reMarkable-update.sh
executable file
·287 lines (245 loc) · 8.78 KB
/
reMarkable-update.sh
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# Small library for better log display
SCRIPT_ROOT=$( (cd "$(dirname "$0")" && pwd))
SCRIPT_NAME=$(basename $0)
SCRIPT_VERSION="v1.1.0"
source "$SCRIPT_ROOT/lib/logutils.sh"
TEMPLATEFILE="templates.json"
REMOTEFOLDER="/usr/share/remarkable/templates"
REMOTEHOMEFOLDER="/home/root/custom_templates"
LOCALBACKUPFOLDER="backups"
# trap ctrl-c and call ctrl_c()
trap cleanup INT
cleanup() {
logwarn "Exit or interruption detected. Cleaning up"
rm $TEMPLATEFILE 2>/dev/null
exit 999
}
usage() {
echo ""
echo "----------=== ${SCRIPT_NAME} - ${SCRIPT_VERSION} ===----------"
echo ""
echo "Source: https://github.com/arnaduga/r2-custom-updater"
echo "License: MIT"
echo ""
echo "Script to update a reMarkable tablet with custome templates (after an update)"
echo ""
echo "Usage: update.sh -r <IP_reMarkable> -i <SSH_identity_file> -t <template_folder> -c <custom_json_template_file>"
echo ""
echo ""
echo " -r <ip_reMarkable.json> MANDATORY : IP of the reMarkable tablet"
echo " -c <custom_json_template_file> MANDATORY : JSON file contains your custom template definition"
echo " -i <SSH_identity_file> MANDATORY : the private key to connect to reMarkable tablet"
echo " -t <template_folder> MANDATORY : template hosting all your custom template (png|svg)"
echo " -d OPTIONAL : Activate the debug log mode"
echo " -h OPTIONAL : Display THIS message"
echo ""
echo ""
}
checkPrereq() {
loginfo "Pre-requisites checks..."
# Checking mandatory arguments
logdebug "Checking parameter custom template json file"
if [ -z "${CUSTOMFILE}" ]; then
logfatal "Missing custom file (json) with your templates (-c)"
usage
exit 401
fi
logdebug "Checking existence of custom template json file"
if ! [ -f "${CUSTOMFILE}" ]; then
logfatal "Custom file '${CUSTOMFILE}' not found"
usage
exit 402
fi
logdebug "Checking parameter templates folder"
if [ -z "${TEMPLATEFOLDER}" ]; then
logfatal "Missing template folder (containing png|svg) files (-t)"
usage
exit 403
fi
logdebug "Checking existence templates folder"
if ! [ -e "${TEMPLATEFOLDER}" ]; then
logfatal "Template folder '${TEMPLATEFOLDER}' not found"
usage
exit 404
fi
logdebug "Checking parameter remote server"
if [ -z "${REMOTESERVER}" ]; then
logfatal "Missing remote server (-r)"
usage
exit 405
fi
logdebug "Checking parameter identity file"
if [ -z "${IDENTITYFILE}" ]; then
logfatal "Missing identity file parameter (-i). Note ONLY connection with identity file is managed"
usage
exit 406
fi
logdebug "Checking existence identity file"
if ! [ -f "${IDENTITYFILE}" ]; then
logfatal "Identity file '${IDENTITYFILE}' not found"
usage
exit 407
fi
# Checking commands prerequisites
loginfo "Checking dependencies"
for dependency in jq ssh; do
logdebug "Checking for command/tool $dependency"
if ! [ -x "$(command -v $dependency)" ]; then
logfatal >&2 "Required command is not on your PATH: $dependency."
logfatal >&2 "Please install it before you continue."
exit 408
fi
done
}
processAllCustom() {
getFileFromRemote "${REMOTEFOLDER}/$TEMPLATEFILE" "$TEMPLATEFILE"
logdebug "FOUND $(jq '.templates | length' $CUSTOMFILE) custom templates in $CUSTOMFILE"
jq -r -c '.templates[]' ${CUSTOMFILE} | while read i; do
loginfo "Processing template name $(jq -r -c '.name' <<<$i)"
checkFilename "${i}"
processOneTemplate "${i}"
done
# Backing up remote file
TIMESTAMP=$(date '+%Y-%m-%d-%H%M%S')
if [ ! -d "./${LOCALBACKUPFOLDER}" ]; then
mkdir ./${LOCALBACKUPFOLDER}
fi
cp $TEMPLATEFILE "${LOCALBACKUPFOLDER}/$TEMPLATEFILE.${TIMESTAMP}"
stdlog "In case of error, backup file is: ${LOCALBACKUPFOLDER}/$TEMPLATEFILE.${TIMESTAMP}"
# Copying file to remote
loginfo "Copy files to remote"
if [ -z $DRYRUN ]; then
copyFileToRemote "${TEMPLATEFILE}" "${REMOTEFOLDER}/${TEMPLATEFILE}"
else
loginfo "DRYRUN copy ${TEMPLATEFILE} to ${REMOTEFOLDER}/${TEMPLATEFILE}"
fi
loginfo "Restarting main reMarkable application (xochitl)"
if [ -z $DRYRUN ]; then
ssh -n -i $IDENTITYFILE root@$REMOTESERVER 'systemctl restart xochitl 2> /dev/null'
else
loginfo "DRYRUN restart interface"
fi
}
processOneTemplate() {
local SOURCE="${1}"
local TEMP_NAME=$(jq -c -r '.name' <<<$SOURCE)
local FILENAME=$(jq -c -r '.filename' <<<$SOURCE)
# Copying files. If does already exist, it will be refreshed with the latest version
local OPT1="${FILENAME}.png"
if [ -f "${TEMPLATEFOLDER}/${OPT1}" ]; then
if [ -z $DRYRUN ]; then
logdebug "copyFileToRemote ${TEMPLATEFOLDER}/${OPT1} --> ${REMOTEHOMEFOLDER}/${OPT1}"
copyFileToRemote "${TEMPLATEFOLDER}/${OPT1}" "${REMOTEHOMEFOLDER}/${OPT1}"
RES=$(ssh -n -i $IDENTITYFILE root@$REMOTESERVER "ln -fsv ${REMOTEHOMEFOLDER}/${OPT1} ${REMOTEFOLDER}/${OPT1}")
else
loginfo "DRYRUN copy ${TEMPLATEFOLDER}/${OPT1} to ${REMOTEHOMEFOLDER}/${OPT1}"
fi
fi
local OPT2="${FILENAME}.svg"
if [ -f "${TEMPLATEFOLDER}/${OPT2}" ]; then
if [ -z $DRYRUN ]; then
logdebug "copyFileToRemote ${TEMPLATEFOLDER}/${OPT2} --> ${REMOTEHOMEFOLDER}/${OPT2}"
copyFileToRemote "${TEMPLATEFOLDER}/${OPT2}" "${REMOTEHOMEFOLDER}/${OPT2}"
RES=$(ssh -n -i $IDENTITYFILE root@$REMOTESERVER "ln -fsv ${REMOTEHOMEFOLDER}/${OPT2} ${REMOTEFOLDER}/${OPT2}")
else
loginfo "DRYRUN copy ${TEMPLATEFOLDER}/${OPT2} to ${REMOTEHOMEFOLDER}/${OPT2}"
fi
fi
# Looking for the template in original file
GOTIT=$(jq --arg n "${TEMP_NAME}" '[.templates.[] | select( .name == $n )] | length' $TEMPLATEFILE)
if [[ $GOTIT != "1" ]] && [[ $GOTIT != "0" ]]; then
logfatal "There is an issue with the search response... Check logs"
logerror $GOTIT
exit 501
fi
if [[ $GOTIT -eq "1" ]]; then
logdebug "The template '${TEMP_NAME}' is already there. Skipping."
return
fi
logdebug "The template '${TEMP_NAME}' is NOT there. Processing."
RESULT=$(jq -a --argjson s "$SOURCE" '.templates += [$s]' $TEMPLATEFILE)
echo "${RESULT}" >$TEMPLATEFILE
}
checkFilename() {
local OBJECT="${1}"
FILENAME=$(jq -r -c '.filename' <<<$OBJECT)
local OPT1=${TEMPLATEFOLDER}/${FILENAME}".png"
local OPT2=${TEMPLATEFOLDER}/${FILENAME}".svg"
logdebug "Checking file existence: ${FILENAME}.(png|svg)"
if [ -f $OPT1 ] || [ -f $OPT2 ]; then
logdebug "Files found. Can continue."
else
logfatal "Template files not found. Missing '$OPT1' or '$OPT2' or both"
exit 409
fi
}
getFileFromRemote() {
local SOURCE=$1
local DEST=$2
local TEMPFILE=$(mktemp /tmp/$(basename $0).XXXXXX)
loginfo "Getting remote file ($SOURCE)"
scp -i $IDENTITYFILE root@$REMOTESERVER:$SOURCE $DEST 2>$TEMPFILE 1>/dev/null
logdebug "SCP log file: $TEMPFILE"
if [ $? == 0 ]; then
logdebug "Copy $SOURCE to $DEST ok"
else
logfatal "Error during copying file from $SOURCE to $DEST. Please check log file: $TEMPFILE "
exit 502
fi
}
copyFileToRemote() {
local SOURCE=$1
local DEST=$2
local TEMPFILE=$(mktemp /tmp/$(basename $0).XXXXXX)
loginfo "Copying file to remote ($SOURCE)"
scp -i $IDENTITYFILE $SOURCE root@$REMOTESERVER:$DEST 2>$TEMPFILE >/dev/null
if [ $? == 0 ]; then
logdebug "Copy $SOURCE to $DEST ok"
else
logfatal "Error during copying file from $SOURCE to $DEST. Please check log file: $TEMPFILE "
exit 503
fi
}
while getopts "r:i:c:t:dhn" option; do
case "${option}" in
d)
# DEBUG mode activated
debug="T"
logdebug "DEBUG mode activated. "
;;
n)
DRYRUN=1
loginfo "Dry run mode activated. No files will be PUSHED"
;;
h)
usage
exit 2
;;
c)
CUSTOMFILE=${OPTARG}
logdebug "Custom file set: $CUSTOMFILE"
;;
t)
TEMPLATEFOLDER=${OPTARG}
logdebug "Folder template set: $TEMPLATEFOLDER"
;;
r)
REMOTESERVER=${OPTARG}
logdebug "Remote server set: $REMOTESERVER"
;;
i)
IDENTITYFILE=${OPTARG}
logdebug "Identity file set: $IDENTITYFILE"
;;
*)
usage
exit 4
;;
esac
done
shift $((OPTIND - 1))
checkPrereq
stdlog "Starting update script"
processAllCustom
stdlog "Update script done."
exit 0