#! /usr/bin/env dash # shellcheck source=/dev/null SHELL_LIBRARY_VERSION="1.7.1" . %SHELL_LIBRARY_PATH # needed for help() and version # shellcheck disable=2034 AUTHORS="Soispha"; # shellcheck disable=2034 YEARS="2023"; # shellcheck disable=2034 VERSION="1.0.0"; help() { cat << EOF Scan images and turn them into a pdf. Usage: $NAME [OPTIONS] --name --device OPTIONS: --out-dir | -o [FILE] Path to place the generated pdf files (default: ./pdf). --name | -n NAME Name for the pdf files (e.g. _1.pdf). --num-pages | -p NUM Number of pages to merge into one pdf (default: 1). --device | -d DEVICE Device used for scanning. --method | -m METHOD Method to use for scanning (default: ADF). --help | -h Display this help and exit. --version | -v Display version and copyright information and exit. ARGUMENTS: FILE := [[fd . --max-depth 3]] A name of a file to store, default is: ./pdf NAME | * := [[fd . --max-depth 3]] The basename of the generated files NUM | *([0-9]) := 0 | 1 | 2 | 3 | 4 Possible numbers of pages, can be more than 4 DEVICE := [[$(cat %DEVICE_FUNCTION)]] Possible scanner names METHOD := ADF | Flatbed The scanning method to use, not all scanners support both of these. The default is ADF EOF } scan() { number_of_pages="$1"; device="$2"; output_directory="$(readlink -f "$3")"; name="$4"; method="$5"; [ -z "$number_of_pages" ] && die "Parameter 'number_of_pages' is not set!" [ -z "$device" ] && die "Parameter 'device' is not set!" [ -z "$output_directory" ] && die "Parameter 'output_directory' is not set!" [ -z "$name" ] && die "Parameter 'name' is not set!" [ -z "$method" ] && die "Parameter 'method' is not set!" tiff_temp_path="$(mktmp -d)"; cd "$tiff_temp_path" || die "Bug" msg "Started scanning..."; if [ "$method" = "Flatbed" ]; then for i in $(seq "$number_of_pages"); do do_until_success \ "scanimage --format=tiff --progress --source='$method' --device='$device' --output-file=$i.tiff" \ "warn 'Retrying scan, as we assume a network error!'" if [ "$number_of_pages" -ne 1 ];then msg "Finished turn, please change side!"; readp "Press enter to continue" noop fi done else for i in $(seq "$number_of_pages");do do_until_success \ "scanimage --format=tiff --progress --source='$method' --device='$device' --batch=%d.tif --batch-increment='$number_of_pages' --batch-start='$i'" \ "warn 'Retrying scan, as we assume a network error!'" if [ "$number_of_pages" -ne 1 ];then msg "Finished turn, please change side!"; readp "Press enter to continue" noop fi done fi msg "Creating output directory..."; mkdir "$output_directory"; cd "$output_directory" || die "Bug" counter=0; image_cache="$(mktmp)" msg "Converting images to pdfs..."; while read -r scanned_image; do echo "$scanned_image" >> "$image_cache" : $((counter += 1)) if [ "$counter" = "$number_of_pages" ]; then counter=0; process_images "$image_cache" "$name" "$output_directory" echo "" > "$image_cache" fi done < "$(tmp_pipe fd . "$tiff_temp_path" "|" sort -n)" } process_images() { image_cache="$1"; name="$2"; output_dir="$3"; set -- while read -r image; do set -- "$@" "$image" done < "$image_cache" while [ -e "$output_dir/${name}.pdf" ]; do name="${name}_$(tr -dc 'A-Za-z0-9!"#$%&'\''()*+,-./:;<=>?@[\]^_`{|}~'