#! /bin/bash

# Johannes Meixner <jsmeix@suse.de>, 2004, 2005, 2006, 2007, 2008, 2010
#
# Copyright (c) 2010 Novell, Inc.
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, contact Novell, Inc.
#
# To contact Novell about this file by physical or electronic mail,
# you may find current contact information at www.novell.com

#set -x

export PATH="/sbin:/usr/sbin:/usr/bin:/bin"
export LC_ALL="POSIX"
export LANG="POSIX"
umask 022

MY_NAME=${0##*/}
OUTPUT_FORMAT="$1"
[ -z "$OUTPUT_FORMAT" ] && OUTPUT_FORMAT="ASCII"
[ "$OUTPUT_FORMAT" != "ASCII" -a "$OUTPUT_FORMAT" != "YCP" ] && { echo -en "\nUsage:\n$MY_NAME {ASCII|YCP}\n" 1>&2 ; exit 1 ; }

# Input:

# Create temporary file names:
TMP_DATA=$(mktemp -u /tmp/$MY_NAME.XXXXXX)
TMP_DATA_RAW=$(mktemp -u /tmp/$MY_NAME.XXXXXX)

# Get the raw data
MAXIMUM_WAIT="60"
if [ -x /usr/bin/scanimage ]
then scanimage -f '%d,%v,%m;' >$TMP_DATA_RAW &
     scanimagePID=$!
     for i in $( seq $MAXIMUM_WAIT )
     do ps $scanimagePID &>/dev/null || break
        sleep 1
     done
     if ps $scanimagePID &>/dev/null
     then [ "$OUTPUT_FORMAT" = "YCP" ] && echo "[]"
          # I do not know how to suppress the "...Killed..." notification on stderr
          # which disturbs my error message.
          # Therefore the error message is output before the kill command is executed.
          # This has also the advantage that usually the "...Killed..." notification
          # happens after the "exit" so that it is nicely lost in the middle of nowhere:
          echo "There is no response from the 'scanimage -L' command." 1>&2
          echo "Therefore it was aborted after $MAXIMUM_WAIT seconds timeout." 1>&2
          if grep -q '^net$' /etc/sane.d/dll.conf
          then echo "" 1>&2
               echo "When the 'net' metadriver is activated and something is wrong" 1>&2
               echo "in the network, it can happen that there is no response from" 1>&2
               echo "the 'scanimage -L' command. For example when the communication" 1>&2
               echo "with a server which should be used by the 'net' metadriver" 1>&2
               echo "gets distorted because a firewall drops some network traffic." 1>&2
               echo "In this case it should help to disable the 'net' metadriver" 1>&2
               echo "as a workaround until the issue in the network is fixed." 1>&2
          fi
          echo "" 1>&2
          kill -9 $scanimagePID &>/dev/null
          exit 10
     fi
else 
    echo "Cannot execute /usr/bin/scanimage" 1>&2
    exit 2
fi
tr ';' '\n' <$TMP_DATA_RAW | sort -u >$TMP_DATA

# Output:

# Output header:
if [ "$OUTPUT_FORMAT" = "YCP" ]
then echo "[" 
else echo "BACKEND|SANE_DEVICE|MANUFACTURER|MODEL"
fi

# Output scanner entries:
exec <$TMP_DATA
while read LINE
do BACKEND=$( echo $LINE | grep -o '^[^:]*' )
   SANE_DEVICE=$( echo $LINE | cut -d ',' -s -f 1 )
   MANUFACTURER=$( echo $LINE | cut -d ',' -s -f 2 )
   MODEL=$( echo $LINE | cut -d ',' -s -f 3 )
   if [ "$OUTPUT_FORMAT" = "YCP" ]
   then echo -e "  \$[ \"backend\":\"$BACKEND\",\n     \"sane_device\":\"$SANE_DEVICE\",\n     \"manufacturer\":\"$MANUFACTURER\",\n     \"model\":\"$MODEL\"\n  ],"
   else echo "$BACKEND|$SANE_DEVICE|$MANUFACTURER|$MODEL"
   fi
done

# Output a footer for YCP
if [ "$OUTPUT_FORMAT" = "YCP" ]
then echo -e "  \$[]\n]"
fi

# Remove the temporary files:
rm $TMP_DATA $TMP_DATA_RAW
exit 0

