#!/bin/sh

export TEXTDOMAIN=peasyscan
export OUTPUT_CHARSET=UTF-8

eval_gettext () {
  local myMESSAGE=$(gettext "$1")
  eval echo -n \"$myMESSAGE\"
}
export -f eval_gettext

if [ -z "$SAVEFILENAME" ]; then
  Xdialog --title " " --msgbox "$(gettext 'No Save file selected!')" 0 0
  exit
fi

if [ -e "$SAVEFILENAME" ]; then
  Xdialog --title " " --yesno "$(gettext 'File exists! Overwrite it?')" 0 0
  [ $? -eq 1 ] && exit
fi

touch "$SAVEFILENAME" 2> /dev/null
if [ $? -eq 1 ]; then
  Xdialog --title " " --msgbox "$(gettext 'Invalid filename!')" 0 0
  exit
fi
rm "$SAVEFILENAME"
 
if [ ! -e /tmp/out.pnm ]; then
  Xdialog --title " " --msgbox "$(gettext 'Nothing was scanned!')" 0 0
  exit
fi 

if [ "$F1" = "true" ]; then
  pnmtopng /tmp/out.pnm > "$SAVEFILENAME"
elif [ "$F2" = "true" ]; then
  pnmtojpeg /tmp/out.pnm > "$SAVEFILENAME"
elif [ "$F3" = "true" ]; then
  ppm2tiff /tmp/out.pnm /tmp/out.tif
  tiff2pdf -p "letter" -o "$SAVEFILENAME" /tmp/out.tif
  rm /tmp/out.tif
elif [ "$F4" = "true" ]; then     
  $PEASYPATH/ocr &
fi

Xdialog --title " " --infobox "$(eval_gettext 'Image saved to $SAVEFILENAME')" 0 0 2000
