#!/usr/bin/perl
#
# ag_scanner
# This script is part of the YaST2 Scanner installation module
# Copyright SuSE Gmbh - 2001
#
# Author: Klaas Freitag <freitag@suse.de>
#         Gabi Strattner <gs@suse.de>
#
# $Id: ag_scanner,v 1.21 2003/08/29 12:10:39 gs Exp $
#

# ag_scanner provides two different functions, selected through the
# ycp 'path', which is mapped to the perl var $action in this script.
#
# The former first action, 'scsi', queries the system for know SCSI scanners.
# The SCSI scanners are now detected by hwinfo, thus the scsi-path was deleted.
#
# This is a kind of hardware detection, the devices  need not to be configured
#  in SANE but are also found if they are.
#
# The second action, 'configured', returns all scan devices configured in SANE.
# This is done by calling scanimage -s (a patched version) which returns the
# sane configured scanners, even network accessible scanners.
#

BEGIN { unshift @INC, "/usr/lib/YaST2/agents_non_y2/";
	unshift @INC, "/usr/lib/YaST2/servers_non_y2/"; }

use strict;
use ycp;

use scannerDB;
# ##############################################################################

my @scanners;

while ( <STDIN> )
{
    ycpDoVerboseLog();

    if( /result/i )
    {
	y2debug("got result -> say goodbye!");
	exit(0);
    }

    my ($action, $path, $argument) = ycp::ParseCommand ($_);

    y2debug( " vvv starting ag_scanner with <$action> <$path> vvv" );
    
    if ( $action eq "Read" ) {
	if ( $path =~ /configured/i ) {
	    y2debug( "Handling action <configured>" );
	    @scanners = performScanimage();

	} elsif ( $path =~ /net/i ) {
	    y2debug( "Handling action <net>" );
	    @scanners = performScanimage( 1 );

	} elsif ( $path =~ /ptal/i ) {
	  @scanners = ();
	  y2debug( "Handling action <ptal>" );
	  open OUT, "ptal-device |"; 
	  my @devlist = <OUT>;
	  close OUT || y2error("error closing OUT");

	  # Example result for USB connection: mlc:usb:PSC_2200_Series
	  # TO DO: support jet direct connected devices, e.g. hpjd:192:168:11.100
	  #        (get model from 'ptal-devid hpjd:192:168:11.100 -mdl')

	  foreach my $dev ( @devlist ) {
	      if ( $dev =~ /^mlc/ ) {
		  my ($mlc, $device, $model) = split( /\:/, $dev );
		  # get rid of _ in model string
		  $model =~ s/_/ /g;
		  push ( @scanners , 
			 { bus => "PTAL", 
			   class_id => "",
			   device => trim($model),
			   device_id => "",
			   resource =>"",
			   rev => "",
			   sub_class_id => "",
			   sub_device => "",
			   sub_vendor => "Hewlett-Packard",
			   unique_key => "",
			   vendor => "Hewlett-Packard",
			   vendor_id => "",
			   dev_name => trim($dev),
			   class => "scanner",
			   scanner_driver => "hpoj",
			   host => ""
			 } );
	      }
      }
	}
	else {
	    y2error( "ERROR: Unspecified action !" );
	}
	y2debug (" ^^^ Leaving ag_scanner - goodbye ^^^" );

	ycp::Return( \@scanners );
    }
    
    # Dir command
    elsif ( $action eq "Dir" ) {
	my @paths = ("net","ptal","configured");

	ycp::Return( \@paths );
      }
    
    # Result
    elsif ( $action eq "result" ) {
	exit;
    }

    # Unknown
    else {
        y2error ("Unknown instruction ", ycpGetCommand, " or argument: ", ycpGetArgType);
        ycp::Return ( "false" );
    }
}

# EOF
