#!/bin/sh
LOCKFILE="/tmp/scan.lock"
TMPFILE="scans/$(hostname)_$(date +%Y-%m-%d_%H-%M-%S)"
SSH_CMD="ssh -i /etc/ssh/ssh_host_rsa_key scan@linux"
SSH_CMD_BG="ssh -n -i /etc/ssh/ssh_host_rsa_key scan@linux"
case $1 in
1) SCAN_PARAM="--mode Color --resolution 200"; CONVERT_PARAM="-quality 80" ;;
2) SCAN_PARAM="--mode Gray --resolution 200"; CONVERT_PARAM="-quality 80 -colorspace Gray" ;;
3) SCAN_PARAM="--mode Color --resolution 400"; CONVERT_PARAM="-quality 70" ;;
4) SCAN_PARAM="--mode Gray --resolution 400"; CONVERT_PARAM="-quality 70 -colorspace Gray" ;;
esac
case $1 in
1|2|3|4)
if [ -f $LOCKFILE ]; then
echo "Error: Another scanning operation is currently in progress"
exit
else
echo "button $1 has been pressed on $2"
touch $LOCKFILE
scanimage $SCAN_PARAM --format tiff | $SSH_CMD 'cat > '"$TMPFILE"".tiff"
rm -f $LOCKFILE
$SSH_CMD_BG convert $CONVERT_PARAM "$TMPFILE"".tiff" "$TMPFILE"".jpg"
$SSH_CMD_BG convert $CONVERT_PARAM "$TMPFILE"".jpg" "$TMPFILE"".pdf"
$SSH_CMD_BG rm "$TMPFILE"".tiff"
fi
;;
esac