#!/bin/sh
# rcrsn51 2010-10-28
# v2.3 2012-06-02 - PDF back in

export TEXTDOMAIN=peasyscan
export OUTPUT_CHARSET=UTF-8

################ Functions ################

combowidget () {
HEADING=$1; DATA=$2
COMBOLIST=""
for P in $DATA; do
  COMBOLIST="$COMBOLIST<item>$P</item>"
done

export DIALOG="
<window title=\" \">
<vbox>
   <frame $HEADING>
    <combobox>
      <variable>COMBOPICK</variable>
     $COMBOLIST 
    </combobox>
   </frame>
  <hbox>
     <button ok></button>
     <button cancel></button>
  </hbox>
</vbox>
</window>
"

I=$IFS; IFS=""
for STATEMENTS in  $(gtkdialog3 --program DIALOG); do
  eval $STATEMENTS
done
IFS=$I
}

STARTHELP () {
  defaultbrowser http://www.murga-linux.com/puppy/viewtopic.php?t=61046
}
export -f STARTHELP

#################################

export PEASYPATH="/usr/local/peasyscan"

[ -f $HOME/.sane/xscanimage/xscanimage.rc ] || $PEASYPATH/reset

URI=""
SOURCE=""
MODE=""
RESOLUTION=""
PAPER=""
OTHER=""

CONFIGFILELIST=`ls $PEASYPATH/config/* 2> /dev/null | cut -d "/" -f 6 `
if [ -n "$CONFIGFILELIST" ]; then
  combowidget "$(gettext 'Select a scanner configuration file (or Cancel)')" "$CONFIGFILELIST"
  [ $EXIT = "abort" ] && exit
  if [ $EXIT = "OK" ]; then
    CONFIGFILE=$PEASYPATH/config/$COMBOPICK
    URI=`grep -i "uri" $CONFIGFILE | cut -d ">" -f 2 | cut -d "<" -f 1`
    SOURCE=`grep -i "source" $CONFIGFILE | cut -d ">" -f 2 | cut -d "<" -f 1`
    MODE=`grep -i "mode" $CONFIGFILE | cut -d ">" -f 2 | cut -d "<" -f 1`
    RESOLUTION=`grep -i "resolution" $CONFIGFILE | cut -d ">" -f 2 | cut -d "<" -f 1`
    PAPER=`grep -i "paper" $CONFIGFILE | cut -d ">" -f 2 | cut -d "<" -f 1`
    OTHER=`grep -i "other" $CONFIGFILE | cut -d ">" -f 2 | cut -d "<" -f 1`
  fi
fi

if [ -z "$URI" ]; then
  SCANNERLIST=$(scanimage -L | grep "device" | cut -d "\`" -f 2 | cut -d "'" -f 1)
  SCANNERCOUNT=`echo $SCANNERLIST | wc -w`
  if [ "$SCANNERCOUNT" -eq 1 ]; then
    URI=$SCANNERLIST
  elif [ "$SCANNERCOUNT" -gt 1 ]; then
    combowidget "$(gettext 'You have several scanners. Pick the URI')" "$SCANNERLIST" 
    [ $EXIT = "abort" -o $EXIT = "Cancel" ] && exit
    URI=$COMBOPICK
  fi
fi

if [ -z "$URI" ]; then
  Xdialog --title " " --msgbox "$(gettext 'No scanner detected!')" 0 0
  exit
fi

export URI
export SOURCE
export MODE
export RESOLUTION
export PAPER
export OTHER

export DIALOG="
<window title=\"Peasyscan v2.5\">
<vbox>
  <frame $(gettext 'Select the image format')>
    <radiobutton> 
      <variable>F1</variable>
      <label>PNG</label>
    </radiobutton>
    <radiobutton> 
      <variable>F2</variable>
      <label>JPG</label>
    </radiobutton>
    <radiobutton> 
      <variable>F3</variable>
      <label>PDF</label>
    </radiobutton>
    <radiobutton> 
      <variable>F4</variable>
      <label>$(gettext 'TXT - OCR Mode')</label>
    </radiobutton>
  </frame>
  <frame $(gettext 'Name the scanned image as')>
    <hbox>
      <entry accept=\"savefilename\">
      <variable>SAVEFILENAME</variable>
      </entry>
      <button>
        <input file stock=\"gtk-open\"></input>
        <action type=\"fileselect\">SAVEFILENAME</action>
      </button>
    </hbox>
  </frame> 
  <hbox>
   <button>
     <label>$(gettext 'Help')</label>
     <action>STARTHELP &</action>
   </button>
   <button>
     <label>$(gettext 'Auto')</label>
     <action>$PEASYPATH/ADF &</action>
   </button>
   <button>
     <label>$(gettext 'Reset')</label>
     <action>$PEASYPATH/reset</action>
   </button>
   <button>
     <label>$(gettext 'Start')</label>
     <action>$PEASYPATH/scan &</action>
   </button>
   <button>
     <label>$(gettext 'Save')</label>
     <action>$PEASYPATH/save</action>
     </button>
   <button><label>$(gettext 'Quit')</label></button>
  </hbox>
</vbox>
</window>
"

gtkdialog3 --program DIALOG > /dev/null
rm -f /tmp/out.pnm /tmp/out.png
