btw i use nix
1#!/usr/bin/env sh
2
3# Transfer PDF and EPUB file(s) to a reMarkable
4# Adrian Daerr and contributors, 2017 -- 2022 - public domain
5# https://github.com/adaerr/reMarkableScripts
6#
7# - The files will appear in reMarkable's top-level "My Files" directory,
8# - After finishing all transfers, you have to restart the xochitl
9# service on the tablet in order to force a scan of its document
10# directory ${REMARKABLE_XOCHITL_DIR} (so that you see the newly
11# transferred files), e.g. by sending the tablet the following
12# command:
13# ssh remarkable systemctl restart xochitl
14# This script will do that for you at the end if you
15# (set the environment variable RESTART_XOCHITL_DEFAULT to 1)
16# xor (specify "-r" as first command line parameter).
17# - See list of prerequisites, and more environment variables below.
18#
19# Disclaimer and liability limitation:
20# [see also all-caps text borrowed from GPL below]
21# - This is a dirty hack based on superficial reverse-engineering.
22# - Expect this script to break at any time, especially upon a
23# reMarkable system upgrade
24# - I am not responsible for any damage caused by this script,
25# including (but not limited to) bricking your reMarkable, erasing
26# your documents etc. YOU ARE USING THIS SOFTWARE ON YOUR OWN RISK.
27#
28# Disclaimer of Warranty.
29#
30# THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
31# APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE
32# COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM
33# “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
34# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
35# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE
36# RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.
37# SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
38# NECESSARY SERVICING, REPAIR OR CORRECTION.
39#
40# Limitation of Liability.
41#
42# IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
43# WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO
44# MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE
45# LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL,
46# INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
47# INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
48# DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
49# YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE
50# WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS
51# BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
52#
53# Prerequisites:
54#
55# * The ssh access has to be configured under the host alias 'remarkable'
56# (or another alias specified in the env variable REMARKABLE_HOST),
57# e.g. by putting the following in .ssh/config :
58# | host remarkable
59# | Hostname 10.11.99.1
60# | User root
61# | ForwardX11 no
62# | ForwardAgent no
63# (and setup ssh public key authentication to avoid typing your passwd)
64#
65# * Beyond core utilities (date, basename,...), the following software
66# has to be installed on the host computer:
67# - uuidgen
68
69# This is where ssh will try to copy the files associated with the document
70REMARKABLE_HOST=${REMARKABLE_HOST:-remarkable2}
71REMARKABLE_XOCHITL_DIR=${REMARKABLE_XOCHITL_DIR:-.local/share/remarkable/xochitl/}
72TARGET_DIR="${REMARKABLE_HOST}:${REMARKABLE_XOCHITL_DIR}"
73
74# Check if we have something to do
75if [ $# -lt 1 ]; then
76 echo "Transfer PDF or EPUB document(s) to a reMarkable tablet."
77 echo "See comments/documentation at start of script."
78 echo "usage: $(basename $0) [ -r ] path-to-file [path-to-file]..."
79 exit 1
80fi
81
82RESTART_XOCHITL_DEFAULT=${RESTART_XOCHITL_DEFAULT:-0}
83RESTART_XOCHITL=${RESTART_XOCHITL_DEFAULT}
84if [ "$1" = "-r" ] ; then
85 shift
86 if [ $RESTART_XOCHITL_DEFAULT -eq 0 ] ; then
87 echo Switching
88 RESTART_XOCHITL=1
89 else
90 RESTART_XOCHITL=0
91 fi
92fi
93
94# Create directory where we prepare the files as the reMarkable expects them
95tmpdir=$(mktemp -d)
96
97# Loop over the command line arguments,
98# which we expect are paths to the files to be transferred
99for filename in "$@" ; do
100
101 # reMarkable documents appear to be identified by universally unique IDs (UUID),
102 # so we generate one for the document at hand
103 uuid=$(uuidgen | tr '[:upper:]' '[:lower:]')
104
105 extension="${filename##*.}"
106
107 # Copy the file itself
108 cp -- "$filename" "${tmpdir}/${uuid}.${extension}"
109
110 # Add metadata
111 # The lastModified item appears to contain the date in milliseconds since Epoch
112 cat <<EOF >>${tmpdir}/${uuid}.metadata
113{
114 "deleted": false,
115 "lastModified": "$(date +%s)000",
116 "metadatamodified": false,
117 "modified": false,
118 "parent": "",
119 "pinned": false,
120 "synced": false,
121 "type": "DocumentType",
122 "version": 1,
123 "visibleName": "$(basename -- "$filename" ".$extension")"
124}
125EOF
126
127 if [ "$extension" = "pdf" ]; then
128 # Add content information
129 cat <<EOF >${tmpdir}/${uuid}.content
130{
131 "extraMetadata": {
132 },
133 "fileType": "pdf",
134 "fontName": "",
135 "lastOpenedPage": 0,
136 "lineHeight": -1,
137 "margins": 100,
138 "pageCount": 1,
139 "textScale": 1,
140 "transform": {
141 "m11": 1,
142 "m12": 1,
143 "m13": 1,
144 "m21": 1,
145 "m22": 1,
146 "m23": 1,
147 "m31": 1,
148 "m32": 1,
149 "m33": 1
150 }
151}
152EOF
153
154 # Add cache directory
155 mkdir ${tmpdir}/${uuid}.cache
156
157 # Add highlights directory
158 mkdir ${tmpdir}/${uuid}.highlights
159
160 # Add thumbnails directory
161 mkdir ${tmpdir}/${uuid}.thumbnails
162
163 elif [ "$extension" = "epub" ]; then
164
165 # Add content information
166 cat <<EOF >${tmpdir}/${uuid}.content
167{
168 "fileType": "epub"
169}
170EOF
171
172 else
173 echo "Unknown extension: $extension, skipping $filename"
174 rm -rf ${tmpdir}/*
175 continue
176 fi
177
178 # Transfer files
179 echo "Transferring $filename as $uuid"
180 scp -r ${tmpdir}/* "${TARGET_DIR}"
181 rm -rf ${tmpdir}/*
182done
183
184rm -rf ${tmpdir}
185
186if [ $RESTART_XOCHITL -eq 1 ] ; then
187 echo "Restarting Xochitl..."
188 ssh ${REMARKABLE_HOST} "systemctl restart xochitl"
189 echo "Done."
190fi