#!/bin/bash
#
#  Authors: Jeffrey Stedfast <fejj@novell.com>
#
#  Copyright 2006 Novell, Inc. (www.novell.com)
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  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, write to the Free Software
#  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
#

#  The purpose of this script is to take a HAL UDI and attempt to
#  match it against any already-configured printers that CUPS knows
#  about. This means we need to match against both hal:// URIs (as
#  generated by the CUPS hal backend) and all the various usb:// URIs
#  that the CUPS usb backend is capable of generating (which is quite
#  a lot, sadly).

#  If the printer is not found to already be configured, launch
#  gnome-cups-add to configure the printer.


if [ -z "$1" ]; then
    echo "Usage: `basename $0` hal-udi"
    exit 0
fi

udi="$1"
parent_udi=`hal-get-property --udi $udi --key info.parent`

vendor=`hal-get-property --udi $udi --key printer.vendor | sed -e "s/ $//" -e "s/ /%20/g"`
product=`hal-get-property --udi $udi --key printer.product | sed -e "s/ /%20/g"`
description=`hal-get-property --udi $udi --key printer.description | sed -e "s/ /%20/g"`
serial=`hal-get-property --udi $parent_udi --key usb.serial`
device=`hal-get-property --udi $udi --key printer.device`

# Get the canonical name for some vendors
if [ "$vendor" = "APPLE" ]; then
    cvendor="Apple"
elif [ "$vendor" = "Canon%20Inc.%20(Kosugi%20Offic" -o "$vendor" = "CANON" ]; then
    cvendor="Canon"
elif [ "$vendor" = "Dymo-CoStar" -o "$vendor" = "DYMO" ]; then
    cvendor="Dymo"
elif [ "$vendor" = "EPSON" ]; then
    cvendor="Epson"
elif [ "$vendor" = "Hewlett-Packard" -o "$vendor" = "Hewlett%20Packard" -o "$vendor" = "hp" ]; then
    cvendor="HP"
elif [ "$vendor" = "Kyocera-Mita" -o "$vendor" = "Kyocera%20Mita" ]; then
    cvendor="Kyocera"
elif [ "$vendor" = "Lexmark-International" -o "$vendor" = "Lexmark%20International" ]; then
    cvendor="Lexmark"
elif [ "$vendor" = "MINOLTA-QMS" -o "$vendor" = "MINOLTA%20QMS" -o "$vendor" = "MINOLTA" ]; then
    cvendor="Minolta"
elif [ "$vendor" = "OKI%20DATA%20CORP" -o "$vendor" = "OKI" -o "$vendor" = "OKIDATA" ]; then
    cvendor="Okidata"
elif [ "$vendor" = "Raw%20Queue" -o "$vendor" = "Postscript" ]; then
    cvendor="Generic"
else
    cvendor=""
fi



for linebuf in `lpstat -v`
do
  uri=`echo $linebuf | sed -e "s/device for printer: //"`
  driver=`echo $uri | cut -d: -f1`
  if [ "$driver" = "hal" ]; then
      if [ "$uri" = "hal://$udi" ]; then
	  exit 0
      fi
  elif [ "$driver" = "usb" ]; then
      # First check to see if the usb uri matches the new format (w/o the serial #)
      usb="usb://$vendor/$product"
      if [ "$uri" = "$usb" ]; then
	  exit 0
      fi
      
      # Sometimes the usb backend uses the printer description instead of the product string.
      usb="usb://$vendor/$description"
      if [ "$uri" = "$usb" ]; then
	  exit 0
      fi
      
      # Now try with the serial #
      usb="usb://$vendor/$product?serial=$serial"
      if [ "$uri" = "$usb" ]; then
	  exit 0
      fi
      
      usb="usb://$vendor/$description?serial=$serial"
      if [ "$uri" = "$usb" ]; then
	  exit 0
      fi
      
      # Now try using the canonical vendor names
      if [ -n "$cvendor" ]; then
	  usb="usb://$cvendor/$product"
	  if [ "$uri" = "$usb" ]; then
	      exit 0
	  fi
	  
	  usb="usb://$cvendor/$description"
	  if [ "$uri" = "$usb" ]; then
	      exit 0
	  fi
	  
	  usb="usb://$cvendor/$product?serial=$serial"
	  if [ "$uri" = "$usb" ]; then
	      exit 0
	  fi
	  
	  usb="usb://$cvendor/$description?serial=$serial"
	  if [ "$uri" = "$usb" ]; then
	      exit 0
	  fi
      fi
      
      # Sometimes the usb backend doesn't include the vendor, yay for consistancy.
      usb="usb://$product"
      if [ "$uri" = "$usb" ]; then
	  exit 0
      fi
      
      usb="usb://$description"
      if [ "$uri" = "$usb" ]; then
	  exit 0
      fi
      
      usb="usb://$product?serial=$serial"
      if [ "$uri" = "$usb" ]; then
	  exit 0
      fi
      
      usb="usb://$description?serial=$serial"
      if [ "$uri" = "$usb" ]; then
	  exit 0
      fi
      
      # OK, apparently it didn't match any of the new format variants... now try the old formats
      
      # usb:/dev/usb/lp0
      usb="usb:$device"
      if [ "$uri" = "$usb" ]; then
	  exit 0
      fi
      
      # usb:///dev/usb/lp0
      usb="usb://$device"
      if [ "$uri" = "$usb" ]; then
	  exit 0
      fi
      
      # usb:/dev/usblp0
      device=`echo $device | sed -e "s:/lp:lp:"`
      usb="usb:$device"
      if [ "$uri" = "$usb" ]; then
	  exit 0
      fi
      
      # usb:///dev/usblp0
      usb="usb://$device"
      if [ "$uri" = "$usb" ]; then
	  exit 0
      fi
  fi
done

# No matches found for passed printer udi
gnomesu -- gnome-cups-add
