cleanup scanbuddy script

This commit is contained in:
Prunebutt 2025-09-27 15:51:35 +02:00
parent 8542ea2ef7
commit b421e330f4

View file

@ -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