#!/bin/sh

# Environment
SCANDIR='/data/Scans/'
SCANNER='epson2:libusb:/dev/usb0:/dev/ugen0'
SCANIMAGE="scanimage -d $SCANNER"
SI_OPTS="-p -B --wait-for-button=yes"
SI_QUALITY="--mode Color --depth 8 --resolution 150"
TEMPFILE=$(mktemp)

cd $SCANDIR
x=0000

# main()
while true; do
	echo '*** Running a scanner test to make sure we can scan'
	if $SCANIMAGE -T 2> $TEMPFILE; then
		echo "*** Scanning scan_$x.png with $SI_QUALITY"
		echo '*** Press the scanner button when you are ready'
		$SCANIMAGE $SI_OPTS $SI_QUALITY | gm convert - scan_$x.png
	else
		cat $TEMPFILE
		echo -e '\n*** Scanner test unsuccessful!'
		exit 1
	fi
	x=$(printf %04d $(($x + 1)))
done
