#! /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 a temporary file:
TMP_DATA=$(mktemp -u /tmp/$MY_NAME.XXXXXX)

# Get the raw data
# The 'sort -u' is crucial because the YaST scanner module requires
# a list of unique backend names (e.g. in Scanner::Overview ):
grep '^[[:alnum:]][[:alnum:]_-]*$' /etc/sane.d/dll.conf | sort -u >$TMP_DATA

# Output:

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

# Output scanner entries:
exec <$TMP_DATA
while read BACKEND
do if [ "$OUTPUT_FORMAT" = "YCP" ]
   then echo -e "  \"$BACKEND\","
   else echo "$BACKEND"
   fi
done

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

# Remove the temporary file
rm $TMP_DATA
exit 0

