Advertisement
Guest User

Untitled

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