-
Notifications
You must be signed in to change notification settings - Fork 0
/
rm2-templates
executable file
·231 lines (206 loc) · 7.03 KB
/
rm2-templates
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
#!/bin/bash
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Author : Aims Palamountain,
# Description : Copy template images to the remarkable2 device.
# Dependencies : ssh, nc, jq, sed
# Notations : This script performs uni-directional synchronisation of
# new templates. This script has only been tested on the
# reMarkable2. Use with caution and back up your device
# prior to using.
# Current version (MAJOR.MINOR)
VERSION="1.0"
# Remarkable2 SSH Address. The device must be plugged in to the machine and
# turned on.
SSH_ADDRESS="10.11.99.1"
# This is the location of all template files on the reMarkable2 device
TEMPLATE_DIRECTORY="/usr/share/remarkable/templates/"
# The expected name of the templates json file to be pulled down, and
# uploaded to the Remarkable2 device.
CONFIG_FILE="./source/templates.json"
# This directory is where we find out template images. These images are
# organised in sub directories which represent the category which the
# template will apear under, on the device.
LOCAL_TEMPLATE_DIR="./templates"
# When set to true only generate the new template.json file locally.
# No ssh connection to the device will be made. This is useful when
# needing to verify output of the script before making changes
# to the device itself.
DRY_RUN=false
# When running in dry run mode, output templates and configurations here.
DRY_RUN_DIR="./dry-run"
# Colors and formatting to aid in clear script output
red=$'\e[1;31m' # Error
grn=$'\e[1;32m' # Device interaction or key message
white=$'\e[0m' # Everything else
# Kills SSH session and exits with return 1
function exit_failed {
ssh -S root@"$SSH_ADDRESS" -O exit root@"$SSH_ADDRESS"
rm ${TEMP_FILE_NAME}
exit 1
}
trap 'exit_failed' ERR
# Display the usage options for this script
function usage {
echo "Usage: rm2-templates [-v | -h | ssh address]"
echo
echo "Upload template files to the reMarkable2 device"
echo "Templates for upload must be placed in the ./templates dir"
echo "This script will not yet preserve existing custom templates"
echo
echo "For more, see https://github.com/ammeep/remarkable-splasher"
echo
echo "Options:"
echo -e "-v\t\t\tDisplay version and exit"
echo -e "-h\t\t\tDisplay usage and exit"
echo -e "-d\t\t\tRun in dry run mode, without ssh access to the device"
echo -e "-dc\t\t\tClean up previous dry runs"
echo -e "-r\t\t\tReset the templates.json file on the device"
echo -e "ip\t\t\tSSH address of the device (default set to 10.11.99.1)"
echo
}
# Attempt to make an ssh connection to the device.
# If the device is not plugged in, or ssh fails, exit.
function test_device_connection {
echo $grn
echo "Attempting to connect to the reMarkable2...$white"
echo "Ensure your device is on and plugged in"
ssh -q root@"$SSH_ADDRESS" exit
if [ "$?" -ne 0 ]; then
echo $red
echo "Failed to establish connection $white"
exit -1
fi
echo
echo "Established an ssh connection to ${SSH_ADDRESS}"
echo "Do not lock your device until the script has completed!"
}
function create_temp_dir {
mkdir -p temp
}
# For all templates found in the ./templates directory, generate a new
# temporary json file with remarkable2 template json objects. This new
# file will be uploaded to the device, or copied to the dry run directory
function generate_new_templates {
TEMPLATES_JSON=`jq '.' ${CONFIG_FILE}`
while read FILE
do
NAME=$(basename "${FILE%.*}")
CATEGORY="$(basename "$(dirname "$FILE")")"
if [[ "$CATEGORY" == "templates" ]]; then CATEGORY="Custom"; fi
CATEGORY="$(tr '[:lower:]' '[:upper:]' <<< ${CATEGORY:0:1})${CATEGORY:1}"
JSON="{
name: \"$NAME\",
filename: \"$NAME\",
iconCode: \"\\ue9d5\",
categories: [\"$CATEGORY\"]
}"
ITEM=".templates += [${JSON}]"
TEMPLATES_JSON=`jq "$ITEM" <<< "${TEMPLATES_JSON}"`
scp "$FILE" ./temp
done < <(find ${LOCAL_TEMPLATE_DIR} -name '*.png' -or -name '*.svg')
echo $TEMPLATES_JSON | jq . > ./temp/templates.json
}
# Copy newly generated templates to the reMarkable2, or to the dry
# run directory.
function copy_new_templates {
# test this scp to another directory on the reMarkable2 first!!
if [ "$DRY_RUN" = false ] ; then
echo $grn
read -rp "Upload new templates [y/N]: $white" input
if [[ "$input" =~ [yY] ]]; then
echo
echo "Copying new templates to the device"
echo
scp -r ./temp/* root@"$SSH_ADDRESS":$TEMPLATE_DIRECTORY
echo
fi
else
echo
echo "Copying new templates to ${DRY_RUN_DIR}"
scp -r ./temp ${DRY_RUN_DIR}
fi
}
# Force a restart of xochitl in order to reload the templates
# without needing to restart the device
function restart_xochitl {
if [ "$DRY_RUN" = false ] ; then
echo "Restartin xochitl"
ssh root@"$SSH_ADDRESS" 'systemctl restart xochitl'
fi
}
# Remove temporary templates config file, that was generated as an
# intermediary step before the new config file is copied to the device.
function clean_up {
echo "Cleaning up"
if [ -d "./temp" ]; then rm -Rf "./temp"; fi
}
# Reset the device template.json file to it's original state.
# Note: this does not remove old template images from disk.
function reset_template_config {
echo $grn
echo "Resetting template config file on the device$white"
scp ./${CONFIG_FILE} root@"$SSH_ADDRESS":${$TEMPLATE_DIRECTORY}${CONFIG_FILE}
echo $grn
echo "Success $white Original template config restored"
}
# Enable dry run mode
function switch_to_dry_run {
echo $grn
echo "Dry Run $white"
echo
echo "No ssh connection will be attempted"
echo "Templates & config will be copied locally"
echo "Verify for expected output: ${DRY_RUN_DIR}"
echo
echo "Starting dry run..."
echo
DRY_RUN=true
}
# Remove all dry run artifacts
function clean_up_dry_run {
echo
if [ -d "$DRY_RUN_DIR" ]; then rm -Rf $DRY_RUN_DIR; fi
echo "$cyn Dry Run: $white clean up complete"
echo
}
# Check Arguments
if [ "$#" -gt 1 ] || [[ "$1" == "-h" ]]; then
usage
exit -1
elif [[ "$1" == "-v" ]]; then
echo "template-upload version: $VERSION"
exit -1
elif [[ "$1" == "-r" ]]; then
test_device_connection
reset_template_config
exit -1
elif [[ "$1" == "-d" ]]; then
switch_to_dry_run
elif [[ "$1" == "-dc" ]]; then
clean_up_dry_run
exit -1
elif [ "$#" -eq 1 ]; then
SSH_ADDRESS="$1"
fi
if [ "$DRY_RUN" = false ] ; then
test_device_connection
fi
create_temp_dir
generate_new_templates
copy_new_templates
restart_xochitl
clean_up
echo $grn
echo "Done! $white"
echo