.dots/scripts/scan/scan.sh
2024-09-26 11:05:50 +02:00

27 lines
793 B
Bash
Executable file

#!/usr/bin/env bash
# author: Daniel Sommer <daniel.sommer@velvettear.de>
# license: MIT
name="$1"
directory="$2"
[[ -z "$directory" ]] && directory="$(mktemp -d)"
[[ -z "$name" ]] && name="$(date '+%Y-%m-%d_%H%M%S').png"
[[ "$name" != *".png" ]] && name="$name.png"
mkdir -p "$directory"
[[ ! -d "$directory" ]] && printf "error: directory '"$directory"' does not exist and can not be created!\n" >&2 && exit 1
output="$directory/$name"
scanimage --format="png" --output-file "$output" --resolution "300" --mode "Color" 2> /dev/null
[[ ! -e "$output" ]] && printf "error: no document was scanned!\n" >&2 && exit 1
if [[ "$(du "$output" | awk '{print $1}')" == "0" ]]; then
printf "error: scanned document '"$output"' is empty!\n" >&2
rm -rf "$output"
exit 1
fi
printf "$output\n"