scanbuddy/scanbuddy.bash

128 lines
2.9 KiB
Bash
Raw Normal View History

2025-09-21 21:51:54 +02:00
#!/usr/bin/env bash
2025-09-21 22:38:29 +02:00
# option values for the brother DS-740D scanner
simplex_source='Automatic Document Feeder(left aligned)'
duplex_source='Automatic Document Feeder(left aligned,Duplex)'
color_mode='24bit Color[Fast]'
bw_mode='Black & White'
grayscale_mode='True Gray'
2025-09-21 21:51:54 +02:00
valid_resolutions=("100" "150" "200" "300" "400" "600" "1200")
2025-09-21 22:38:29 +02:00
# default options for scanning
sides=$simplex_source
mode='c'
resolution='300'
2025-09-21 21:51:54 +02:00
current_page=$(find "$PWD" -name "out*.pdf" | sort | tail -n 1);
read -r current_pageno <<<"${current_page//[^0-9]/ }"
current_pageno=${current_pageno:-0}
2025-09-22 12:02:44 +02:00
last_out_file="out$current_pageno.pdf"
2025-09-21 21:51:54 +02:00
next_out_file="out$((current_pageno+1)).pdf"
scan() {
2025-09-25 17:42:36 +02:00
# device="brother5:bus2;dev2"
device=$(scanimage -f "%d%n" | grep brother5 | head -n 1)
echo "Selecting scanner '$device'"
2025-09-21 22:38:29 +02:00
usage() {
echo "scanbuddy.bash scan -d -r <resolution($resolution)> -b <c|b|g>"
}
while getopts "dr:c:" flag; do
2025-09-21 21:51:54 +02:00
case ${flag} in
d)
echo "Scanning duplex"
2025-09-21 22:38:29 +02:00
sides=$duplex_source
2025-09-21 21:51:54 +02:00
;;
r)
if [[ ! " ${valid_resolutions[*]} " =~ [[:space:]]${OPTARG}[[:space:]] ]]; then
2025-09-21 22:38:29 +02:00
echo "Resolution '$OPTARG' not supported!"
echo "Supported resolutions: $(printf %s "${valid_resolutions[*]}")"
2025-09-21 21:51:54 +02:00
exit 1
fi
2025-09-21 22:38:29 +02:00
resolution=$OPTARG
2025-09-21 21:51:54 +02:00
;;
2025-09-21 22:38:29 +02:00
c)
mode=$OPTARG
2025-09-21 21:51:54 +02:00
;;
*)
;;
esac
done
2025-09-21 22:38:29 +02:00
case $mode in
c)
scanner_mode=$color_mode
;;
b)
scanner_mode=$bw_mode
;;
g)
scanner_mode=$grayscale_mode
;;
*)
echo "Mode '$mode' not supported! Supported modes: (c)olour|(b)lack & white|(g)reyscale"
;;
esac
echo "Scanning to '$next_out_file'..."
2025-09-22 00:22:42 +02:00
scanimage \
2025-09-25 17:42:36 +02:00
--device-name="$device" \
2025-09-21 22:38:29 +02:00
--format=pdf \
--output-file="$next_out_file" \
--mode="$scanner_mode" \
--resolution="$resolution" \
2025-09-22 00:22:42 +02:00
--source="$sides" \
-x 210 -y 297 # A4
2025-09-21 21:51:54 +02:00
}
2025-09-27 15:51:35 +02:00
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
}
2025-09-25 17:42:36 +02:00
if [ "$#" -lt 1 ]; then
echo "You need an argument!"
exit 1
fi
2025-09-21 21:51:54 +02:00
case "$1" in
scan)
scan "${@:2}"
;;
2025-09-22 12:02:44 +02:00
unscan)
2025-09-25 17:42:36 +02:00
rm "$last_out_file"
2025-09-22 12:02:44 +02:00
;;
dispatch)
2025-09-27 15:51:35 +02:00
dispatch "${@:2}"
;;
clean)
clean
2025-09-22 12:02:44 +02:00
;;
2025-09-21 21:51:54 +02:00
*)
2025-09-27 15:51:35 +02:00
echo "Usage: scanbuddy scan|unscan|dispatch|clean"
2025-09-21 21:51:54 +02:00
;;
esac