From daef3ff02a1b929bf80cd24ab24360e6786dc8d5 Mon Sep 17 00:00:00 2001 From: Boris Bukh <62670896+bbukh@users.noreply.github.com> Date: Tue, 13 Sep 2022 09:31:59 -0400 Subject: [PATCH 1/7] Quiet and help options --- pdf2remarkable.sh | 63 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 15 deletions(-) diff --git a/pdf2remarkable.sh b/pdf2remarkable.sh index 35a23a8..863307a 100755 --- a/pdf2remarkable.sh +++ b/pdf2remarkable.sh @@ -70,25 +70,58 @@ REMARKABLE_HOST=${REMARKABLE_HOST:-remarkable} REMARKABLE_XOCHITL_DIR=${REMARKABLE_XOCHITL_DIR:-.local/share/remarkable/xochitl/} TARGET_DIR="${REMARKABLE_HOST}:${REMARKABLE_XOCHITL_DIR}" -# Check if we have something to do -if [ $# -lt 1 ]; then +# Function to show help +show_help () +{ echo "Transfer PDF or EPUB document(s) to a reMarkable tablet." echo "See comments/documentation at start of script." - echo "usage: $(basename $0) [ -r ] path-to-file [path-to-file]..." + echo "usage: $(basename $0) [-q|--quiet| [-r|--toggle-restart] path-to-file [path-to-file]..." +} + +# Check if we have something to do +if [ $# -lt 1 ]; then + show_help exit 1 fi RESTART_XOCHITL_DEFAULT=${RESTART_XOCHITL_DEFAULT:-0} RESTART_XOCHITL=${RESTART_XOCHITL_DEFAULT} -if [ "$1" = "-r" ] ; then - shift - if [ $RESTART_XOCHITL_DEFAULT -eq 0 ] ; then - echo Switching - RESTART_XOCHITL=1 - else - RESTART_XOCHITL=0 + +BE_QUIET=0 +SCP_OPTIONS= + +# Print progess information +log () { + if [ $BE_QUIET -eq 0 ]; then + echo "$@" fi -fi +} + + +# Parse arguments +while :; do + case $1 in + -q|--quiet) + shift + BE_QUIET=1 + SCP_OPTIONS="-q" + ;; + -h|--help) + show_help + exit 0 + ;; + -r|--toggle-restart) + shift + if [ $RESTART_XOCHITL_DEFAULT -eq 0 ] ; then + RESTART_XOCHITL=1 + else + RESTART_XOCHITL=0 + fi + ;; + *) # No more optional arguments + break + esac +done # Create directory where we prepare the files as the reMarkable expects them tmpdir=$(mktemp -d) @@ -175,15 +208,15 @@ EOF fi # Transfer files - echo "Transferring $filename as $uuid" - scp -r ${tmpdir}/* "${TARGET_DIR}" + log "Transferring $filename as $uuid" + scp -r ${SCP_OPTIONS} ${tmpdir}/* "${TARGET_DIR}" rm -rf ${tmpdir}/* done rm -rf ${tmpdir} if [ $RESTART_XOCHITL -eq 1 ] ; then - echo "Restarting Xochitl..." + log "Restarting Xochitl..." ssh ${REMARKABLE_HOST} "systemctl restart xochitl" - echo "Done." + log "Done." fi From 9f789c013063d2f84ce82ba04b7a15bd5de32b3b Mon Sep 17 00:00:00 2001 From: Boris Bukh <62670896+bbukh@users.noreply.github.com> Date: Tue, 13 Sep 2022 09:34:34 -0400 Subject: [PATCH 2/7] Safety to prevent deleting the root directory if a bug creeps in. --- pdf2remarkable.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pdf2remarkable.sh b/pdf2remarkable.sh index 863307a..9e4b4bb 100755 --- a/pdf2remarkable.sh +++ b/pdf2remarkable.sh @@ -203,17 +203,17 @@ EOF else echo "Unknown extension: $extension, skipping $filename" - rm -rf ${tmpdir}/* + rm -rf ${tmpdir:?}/* continue fi # Transfer files log "Transferring $filename as $uuid" scp -r ${SCP_OPTIONS} ${tmpdir}/* "${TARGET_DIR}" - rm -rf ${tmpdir}/* + rm -rf ${tmpdir:?}/* done -rm -rf ${tmpdir} +rm -rf ${tmpdir:?} if [ $RESTART_XOCHITL -eq 1 ] ; then log "Restarting Xochitl..." From 316135dfa1979554eb2dd5e204761146fffd2a53 Mon Sep 17 00:00:00 2001 From: Boris Bukh <62670896+bbukh@users.noreply.github.com> Date: Tue, 13 Sep 2022 09:44:25 -0400 Subject: [PATCH 3/7] mktemp error handling --- pdf2remarkable.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pdf2remarkable.sh b/pdf2remarkable.sh index 9e4b4bb..2596aca 100755 --- a/pdf2remarkable.sh +++ b/pdf2remarkable.sh @@ -125,6 +125,10 @@ done # Create directory where we prepare the files as the reMarkable expects them tmpdir=$(mktemp -d) +if [ $? -ne 0 ]; then + echo "Cannot create a temporary directory. Exiting." + exit 1 +fi # Loop over the command line arguments, # which we expect are paths to the files to be transferred From b74c12194bfac9ee82a0b424fbd73db8b8526941 Mon Sep 17 00:00:00 2001 From: Boris Bukh <62670896+bbukh@users.noreply.github.com> Date: Tue, 13 Sep 2022 10:04:01 -0400 Subject: [PATCH 4/7] Option to record the list of uploaded files with their UUIDs to a separate file. --- pdf2remarkable.sh | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pdf2remarkable.sh b/pdf2remarkable.sh index 2596aca..8039434 100755 --- a/pdf2remarkable.sh +++ b/pdf2remarkable.sh @@ -75,7 +75,7 @@ show_help () { echo "Transfer PDF or EPUB document(s) to a reMarkable tablet." echo "See comments/documentation at start of script." - echo "usage: $(basename $0) [-q|--quiet| [-r|--toggle-restart] path-to-file [path-to-file]..." + echo "usage: $(basename $0) [-q|--quiet| [-r|--toggle-restart] [--uuids-file=] path-to-file [path-to-file]..." } # Check if we have something to do @@ -89,6 +89,7 @@ RESTART_XOCHITL=${RESTART_XOCHITL_DEFAULT} BE_QUIET=0 SCP_OPTIONS= +UUIDS_FILE= # Print progess information log () { @@ -118,6 +119,10 @@ while :; do RESTART_XOCHITL=0 fi ;; + --uuids-file=?*) + UUIDS_FILE=${1#*=} + shift + ;; *) # No more optional arguments break esac @@ -214,6 +219,11 @@ EOF # Transfer files log "Transferring $filename as $uuid" scp -r ${SCP_OPTIONS} ${tmpdir}/* "${TARGET_DIR}" + + # If successful, record the uuid to supplied file + [ $? -eq 0 ] && [ -n ${UUIDS_FILE} ] && echo "${uuid} ${filename}" >> ${UUIDS_FILE} + + # Clean up rm -rf ${tmpdir:?}/* done From 10b7d275f14e5d69a2e6731052dfcb14c95d66bd Mon Sep 17 00:00:00 2001 From: Boris Bukh <62670896+bbukh@users.noreply.github.com> Date: Tue, 13 Sep 2022 10:09:03 -0400 Subject: [PATCH 5/7] More help --- pdf2remarkable.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pdf2remarkable.sh b/pdf2remarkable.sh index 8039434..a512e6c 100755 --- a/pdf2remarkable.sh +++ b/pdf2remarkable.sh @@ -74,8 +74,12 @@ TARGET_DIR="${REMARKABLE_HOST}:${REMARKABLE_XOCHITL_DIR}" show_help () { echo "Transfer PDF or EPUB document(s) to a reMarkable tablet." - echo "See comments/documentation at start of script." - echo "usage: $(basename $0) [-q|--quiet| [-r|--toggle-restart] [--uuids-file=] path-to-file [path-to-file]..." + echo "usage: $(basename $0) path-to-file [path-to-file]..." + echo " -h --help print this usage information and exit" + echo " -q --quiet don't print progress information" + echo " -r --toggle-restart toggle whether to restart the tablet (default is given by RESTART_XOCHITL_DEFAULT)" + echo " --uuids-file write the list of uploaded files and their UUIDs to a file" + echo "See also comments/documentation at start of the script." } # Check if we have something to do From f120a9de92d4e6985cc563743842eb32bcb5e9fe Mon Sep 17 00:00:00 2001 From: Boris Bukh <62670896+bbukh@users.noreply.github.com> Date: Tue, 13 Sep 2022 20:47:43 -0400 Subject: [PATCH 6/7] Fixed a typo --- pdf2remarkable.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdf2remarkable.sh b/pdf2remarkable.sh index a512e6c..0dcd55c 100755 --- a/pdf2remarkable.sh +++ b/pdf2remarkable.sh @@ -74,7 +74,7 @@ TARGET_DIR="${REMARKABLE_HOST}:${REMARKABLE_XOCHITL_DIR}" show_help () { echo "Transfer PDF or EPUB document(s) to a reMarkable tablet." - echo "usage: $(basename $0) path-to-file [path-to-file]..." + echo "usage: $(basename $0) [options] path-to-file [path-to-file]..." echo " -h --help print this usage information and exit" echo " -q --quiet don't print progress information" echo " -r --toggle-restart toggle whether to restart the tablet (default is given by RESTART_XOCHITL_DEFAULT)" From 8c0c2bf811d0c153f8e44d32c44fc8efa4da3d32 Mon Sep 17 00:00:00 2001 From: Boris Bukh <62670896+bbukh@users.noreply.github.com> Date: Wed, 14 Sep 2022 22:23:14 -0400 Subject: [PATCH 7/7] Quotes --- pdf2remarkable.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdf2remarkable.sh b/pdf2remarkable.sh index 0dcd55c..61246db 100755 --- a/pdf2remarkable.sh +++ b/pdf2remarkable.sh @@ -225,7 +225,7 @@ EOF scp -r ${SCP_OPTIONS} ${tmpdir}/* "${TARGET_DIR}" # If successful, record the uuid to supplied file - [ $? -eq 0 ] && [ -n ${UUIDS_FILE} ] && echo "${uuid} ${filename}" >> ${UUIDS_FILE} + [ $? -eq 0 ] && [ -n "${UUIDS_FILE}" ] && echo "${uuid} ${filename}" >> ${UUIDS_FILE} # Clean up rm -rf ${tmpdir:?}/*