TheMathemagicians/scanner/card_scanner

60 lines
1.5 KiB
Plaintext
Raw Normal View History

2024-11-24 17:47:43 +00:00
#!/bin/bash
check_error() {
if test $? -ne 0; then
echo "card_scanner: $1 exited with unexpected error"
exit 1
fi
}
STORAGE_DIR="$HOME/.local/share/sevenkeys/scanimages/"
mkdir -p "$STORAGE_DIR"
printf "Database profile: "
read profile
printf "Storage area name: "
read storageAreaName
ADD_CARDS=0
echo "scantap: Beginning scan loop"
while true; do
rng="$(cat /dev/random | tr -cd 'a-f0-9' | head -c 32)"
filename="$STORAGE_DIR/$rng.png"
scanimage --output-file="$filename" --source "ADF Front" --mode Color --page-width 63mm --page-height 88mm 2>/dev/null
# scanimage exits with code 7 if no documents are available in scanner
if test $? -eq 7; then
if test $ADD_CARDS -eq 0; then
ADD_CARDS=1
echo "scantap: No more cards in feeder" >&2
fi
# If we have generated a zero-length file, then delete it
if ! test -s "$filename"; then
rm "$filename"
fi
continue
fi
ADD_CARDS=0
check_error "scanimage"
convert -rotate 180 "$filename" "$filename"
check_error "convert"
cardLocationId="$(./sevenkeys --profile="$profile" store --storagearea="$storageAreaName" --id="00000000-0000-0000-0000-0000000000000")"
check_error "sevenkeys"
if test "$profile" == "development"; then
databaseName="sevenkeys_development"
else
databaseName="sevenkeys"
fi
mysql --silent --silent --user=root --password="$(pass show sevenkeys/mysql)" \
-e "USE $databaseName; INSERT INTO CardScan (CardLocationId, Filename) VALUES ('$cardLocationId', '$rng.png');"
check_error "mysql"
done