From b421e330f4423297a58c9be51a7f246610f97473 Mon Sep 17 00:00:00 2001 From: Prunebutt Date: Sat, 27 Sep 2025 15:51:35 +0200 Subject: [PATCH] cleanup scanbuddy script --- scanbuddy.bash | 51 +++++++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/scanbuddy.bash b/scanbuddy.bash index 0fd8e4c..7e28472 100755 --- a/scanbuddy.bash +++ b/scanbuddy.bash @@ -13,7 +13,6 @@ sides=$simplex_source mode='c' resolution='300' - current_page=$(find "$PWD" -name "out*.pdf" | sort | tail -n 1); read -r current_pageno <<<"${current_page//[^0-9]/ }" current_pageno=${current_pageno:-0} @@ -77,6 +76,33 @@ scan() { -x 210 -y 297 # A4 } +clean() { + echo "Cleaning up..." + rm -rfv "$PWD/out*.pdf" +} + +dispatch() { + readarray -d '' out_files < <(find . -regextype posix-awk -regex "\./out[0-9]+\.pdf" -print0 | sort -Vz) + combined_file=$(mktemp --suff="_scanbuddy.pdf") + gs -q -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE="$combined_file" -dBATCH "${out_files[@]}" + echo "Merged pdf written to $combined_file" + + # send to paperless + PAPERLESS_STATUS=$(curl \ + -H "Authorization: Token $PAPERLESS_TOKEN" \ + -F document=@"$combined_file" \ + "$([ -v PAPERLESS_TAG ] && echo "-F tags=$PAPERLESS_TAG")" \ + "${PAPERLESS_URL}/api/documents/post_document/") || FAILED=1 + + if [ -v FAILED ]; then + printf "Paperless failed with message:\n%s" "$PAPERLESS_STATUS" + else + printf "Paperless consumption job: %s\n" "$PAPERLESS_STATUS" + fi + + clean +} + if [ "$#" -lt 1 ]; then echo "You need an argument!" exit 1 @@ -90,25 +116,12 @@ case "$1" in rm "$last_out_file" ;; dispatch) - readarray -d '' out_files < <(find . -regextype posix-awk -regex "\./out[0-9]+\.pdf" -print0 | sort -Vz) - combined_file=$(mktemp --suff="_scanbuddy.pdf") - gs -q -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE="$combined_file" -dBATCH "${out_files[@]}" - echo "Merged pdf written to $combined_file" - - # send to paperless - PAPERLESS_STATUS=$(curl \ - -H "Authorization: Token $PAPERLESS_TOKEN" \ - -F document=@"$combined_file" \ - "$([ -v PAPERLESS_TAG ] && echo "-F tags=$PAPERLESS_TAG")" \ - "${PAPERLESS_URL}/api/documents/post_document/") || FAILED=1 - - if [ -v FAILED ]; then - printf "Paperless failed with message:\n%s" "$PAPERLESS_STATUS" - else - printf "Paperless consumption job: %s\n" "$PAPERLESS_STATUS" - fi + dispatch "${@:2}" + ;; + clean) + clean ;; *) - echo "Usage: scanbuddy" + echo "Usage: scanbuddy scan|unscan|dispatch|clean" ;; esac