#!/bin/bash
# $1 variable will contain the .bsm file name only no path
PARAMPATH=~/pages/TBEscan/
PARAMFILE="${PARAMPATH}${1}"
SHOWDEVICES=`sed -n 's|.*<ShowDevicesOnly>\(.*\)</ShowDevicesOnly>.*|\1|p' $PARAMFILE`
if [ $SHOWDEVICES = "Y" ]; then
	RETVAL=`scanimage -f %d`
	zenity --info --text "${RETVAL}"
	exit 0
fi 
OUTFILE=`sed -n 's|.*<OutputFile>\(.*\)</OutputFile>.*|\1|p' $PARAMFILE`
#Make sure OutputFile has the same file name as this file without the extension
SLENGTH=`echo -n $1 | wc -c`
SLENGTH=$[SLENGTH-4]
S=`echo -n $1 | cut -c -${SLENGTH}`
S=${S}.tif
if [ $S != $OUTFILE ]; then
	zenity --info --text "ERROR: Output File:${OUTFILE} must be equal to this file:${S}"
	exit 0
fi

RES=`sed -n 's|.*<Resolution>\(.*\)</Resolution>.*|\1|p' $PARAMFILE`
WEBREQUEST=`sed -n 's|.*<WebRequest>\(.*\)</WebRequest>.*|\1|p' $PARAMFILE`
USEADF=`sed -n 's|.*<UseADF>\(.*\)</UseADF>.*|\1|p' $PARAMFILE`
MODE=`sed -n 's|.*<PixelType>\(.*\)</PixelType>.*|\1|p' $PARAMFILE`

case "${MODE}" in
    0) MODE="Lineart";;
    2) MODE="Halftone";;
    1) MODE="Gray";;
    3) MODE="Color";;
esac 
####See if there is a way to check if there are documents in the scanner at this point
cd ~
scanimage --mode=${MODE} --resolution=${RES} --format=tiff --batch=scan%d.tif
tiffcp -c lzw scan*.tif ${PARAMPATH}${OUTFILE}
#Clean up the local tif files
rm scan*.tif
/usr/bin/firefox $WEBREQUEST

