home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-2 / Inter.Net 55-2.iso / Mandrake / mdkinst / usr / bin / perl-install / printerdrake.pm < prev    next >
Encoding:
Perl POD Document  |  2000-01-12  |  9.3 KB  |  264 lines

  1. package printerdrake;
  2.  
  3.  
  4.  
  5.  
  6. use common qw(:common :file :functional :system);
  7. use detect_devices;
  8. use run_program;
  9. use commands;
  10. use modules;
  11. use network;
  12. use log;
  13. use printer;
  14.  
  15. 1;
  16.  
  17. sub getinfo($) {
  18.     my ($prefix) = @_;
  19.     my $entry = {};
  20.  
  21.     printer::set_prefix($prefix);
  22.     printer::read_configured_queue($entry);
  23.  
  24.     add2hash($entry, {
  25.               want         => 0,
  26.               complete     => 0,
  27.               str_type     => $printer::printer_type_default,
  28.               QUEUE        => "lp",
  29.               SPOOLDIR     => "/var/spool/lpd/lp",
  30.               DBENTRY      => "PostScript",
  31.               PAPERSIZE    => "legal",
  32.               CRLF         => 0,
  33.               AUTOSENDEOF  => 1,
  34.  
  35.               DEVICE       => "/dev/lp0",
  36.  
  37.               REMOTEHOST   => "",
  38.               REMOTEQUEUE  => "",
  39.  
  40.               NCPHOST      => "", 
  41.               NCPQUEUE     => "", 
  42.               NCPUSER      => "", 
  43.               NCPPASSWD    => "", 
  44.  
  45.               SMBHOST      => "", 
  46.               SMBHOSTIP    => "", 
  47.               SMBSHARE     => "", 
  48.               SMBUSER      => "", 
  49.               SMBPASSWD    => "", 
  50.               SMBWORKGROUP => "", 
  51.              });
  52.     $entry;
  53. }
  54.  
  55. sub copy_printer_params($$) {
  56.     my ($from, $to) = @_;
  57.  
  58.     foreach (keys %$from) {
  59.     $to->{$_} = $from->{$_} if $_ ne 'configured'; 
  60.     }
  61. }
  62.  
  63.  
  64. sub main($$$$) {
  65.     my ($prefix, $printer, $in, $install) = @_;
  66.  
  67.     unless ($::testing) {
  68.     printer::set_prefix($prefix);
  69.     &$install('rhs-printfilters');
  70.     }
  71.     printer::read_printer_db();
  72.  
  73.     $printer->{complete} = 0;
  74.     if ($::expert || scalar keys %{$printer->{configured}}) {
  75.     $in->ask_from_entries_ref(_("Local Printer Options"),
  76.                   _("Every print queue (which print jobs are directed to) needs a
  77. name (often lp) and a spool directory associated with it. What
  78. name and directory should be used for this queue?"),
  79.                   [_("Name of queue:"), _("Spool directory:")],
  80.                   [\$printer->{QUEUE}, \$printer->{SPOOLDIR}],
  81.                   changed => sub
  82.                   {
  83.                       $printer->{SPOOLDIR} = "$printer::spooldir/$printer->{QUEUE}" unless $_[0];
  84.                   },
  85.                  ) or return; 
  86.     }
  87.     copy_printer_params($printer->{configured}{$printer->{QUEUE}}, $printer); 
  88.  
  89.     $printer->{str_type} =
  90.       $in->ask_from_list_(_("Select Printer Connection"),
  91.               _("How is the printer connected?"),
  92.               [ keys %printer::printer_type ],
  93.               $printer::printer_type_inv{$printer->{TYPE}},
  94.              );
  95.     $printer->{TYPE} = $printer::printer_type{$printer->{str_type}};
  96.  
  97.     if ($printer->{TYPE} eq "LOCAL") {
  98.     {
  99.         my $w = $in->wait_message(_("Test ports"), _("Detecting devices..."));
  100.         eval { modules::load("parport_pc"); modules::load("parport_probe"); modules::load("lp"); };
  101.     }
  102.  
  103.     my @port = ();
  104.     my @parport = detect_devices::whatPrinter();
  105.     eval { modules::unload("parport_probe") };
  106.     my $str;
  107.     if ($parport[0]) {
  108.         my $port = $parport[0]{port};
  109.         $printer->{DEVICE} = $port;
  110.         my $descr = common::bestMatchSentence2($parport[0]{val}{DESCRIPTION}, @printer::entry_db_description);
  111.         $printer->{DBENTRY} = $printer::descr_to_db{$descr};
  112.         $str = _("A printer, model \"%s\", has been detected on ", $parport[0]{val}{DESCRIPTION}) . $port;
  113.         @port = map { $_->{port}} @parport;
  114.     } else {
  115.         @port = detect_devices::whatPrinterPort();
  116.     }
  117.     $printer->{DEVICE} = $port[0] if $port[0];
  118.  
  119.     return if !$in->ask_from_entries_ref(_("Local Printer Device"),
  120.                          _("What device is your printer connected to  \n(note that /dev/lp0 is equivalent to LPT1:)?\n") . $str ,
  121.                          [_("Printer Device:")],
  122.                          [{val => \$printer->{DEVICE}, list => \@port }],
  123.                         );
  124.     } elsif ($printer->{TYPE} eq "REMOTE") {
  125.     return if !$in->ask_from_entries_ref(_("Remote lpd Printer Options"),
  126.                          _("To use a remote lpd print queue, you need to supply
  127. the hostname of the printer server and the queue name
  128. on that server which jobs should be placed in."),
  129.                          [_("Remote hostname:"), _("Remote queue")],
  130.                          [\$printer->{REMOTEHOST}, \$printer->{REMOTEQUEUE}],
  131.                         );
  132.     } elsif ($printer->{TYPE} eq "SMB") {
  133.     return if !$in->ask_from_entries_ref(
  134.         _("SMB (Windows 9x/NT) Printer Options"),
  135.         _("To print to a SMB printer, you need to provide the
  136. SMB host name (Note! It may be different from its
  137. TCP/IP hostname!) and possibly the IP address of the print server, as
  138. well as the share name for the printer you wish to access and any
  139. applicable user name, password, and workgroup information."),
  140.         [_("SMB server host:"), _("SMB server IP:"),
  141.          _("Share name:"), _("User name:"), _("Password:"),
  142.          _("Workgroup:")],
  143.         [\$printer->{SMBHOST}, \$printer->{SMBHOSTIP},
  144.          \$printer->{SMBSHARE}, \$printer->{SMBUSER},
  145.          {val => \$printer->{SMBPASSWD}, hidden => 1}, \$printer->{SMBWORKGROUP}
  146.         ],
  147.          complete => sub {
  148.          unless (network::is_ip($printer->{SMBHOSTIP})) {
  149.              $in->ask_warn('', _("IP address should be in format 1.2.3.4"));
  150.              return (1,1);
  151.          }
  152.          return 0;
  153.          },
  154.                        );
  155.     &$install('samba');
  156.     } elsif ($printer->{TYPE} eq "NCP") {
  157.     return if !$in->ask_from_entries_ref(_("NetWare Printer Options"),
  158.         _("To print to a NetWare printer, you need to provide the
  159. NetWare print server name (Note! it may be different from its
  160. TCP/IP hostname!) as well as the print queue name for the printer you
  161. wish to access and any applicable user name and password."),
  162.         [_("Printer Server:"), _("Print Queue Name:"),
  163.          _("User name:"), _("Password:")],
  164.         [\$printer->{NCPHOST}, \$printer->{NCPQUEUE},
  165.          \$printer->{NCPUSER}, {val => \$printer->{NCPPASSWD}, hidden => 1}],
  166.                        );
  167.     &$install('ncpfs');
  168.     }
  169.  
  170.     my $action;
  171.     my @action = qw(ascii ps both done);
  172.     my %action = (
  173.           ascii  => _("Yes, print ASCII test page"),
  174.           ps     => _("Yes, print PostScript test page"),
  175.           both   => _("Yes, print both test pages"),
  176.           done   => _("No"),
  177.          );
  178.  
  179.     do {
  180.     $printer->{DBENTRY} ||= $printer::thedb_gsdriver{$printer->{GSDRIVER}}{ENTRY};
  181.     $printer->{DBENTRY} =
  182.       $printer::descr_to_db{
  183.                 $in->ask_from_list_(_("Configure Printer"),
  184.                             _("What type of printer do you have?"),
  185.                             [@printer::entry_db_description],
  186.                             $printer::db_to_descr{$printer->{DBENTRY}},
  187.                            )
  188.                    };
  189.  
  190.     my %db_entry = %{$printer::thedb{$printer->{DBENTRY}}};
  191.  
  192.     my @list_res = @{$db_entry{RESOLUTION} || []};
  193.     my @res = map { "$_->{XDPI}x$_->{YDPI}" } @list_res;
  194.     my @list_col      = @{$db_entry{BITSPERPIXEL} || []};
  195.     my @col           = map { "$_->{DEPTH} $_->{DESCR}" } @list_col;
  196.     my %col_to_depth  = map { ("$_->{DEPTH} $_->{DESCR}", $_->{DEPTH}) } @list_col;
  197.     my %depth_to_col  = reverse %col_to_depth;
  198.     my $is_uniprint = $db_entry{GSDRIVER} eq "uniprint";
  199.  
  200.     $printer->{RESOLUTION} = @res ? $res[0] || "Default" : "Default";
  201.     $printer->{CRLF} = $db_entry{DESCR} =~ /HP/;
  202.     $printer->{BITSPERPIXEL} = @list_col ? $depth_to_col{$printer->{BITSPERPIXEL}} || $col[0] : "Default";
  203.  
  204.     $in->ask_from_entries_refH('', _("Printer options"), [
  205. _("Paper Size") => { val => \$printer->{PAPERSIZE}, type => 'list', , not_edit => !$::expert, list => \@printer::papersize_type },
  206. _("Eject page after job?") => { val => \$printer->{AUTOSENDEOF}, type => 'bool' },
  207. @list_res > 1 ? (
  208. _("Resolution") => { val => \$printer->{RESOLUTION}, type => 'list', , not_edit => !$::expert, list => \@res } ) : (),
  209. _("Fix stair-stepping text?") => { val => \$printer->{CRLF}, type => "bool" },
  210. @list_col > 1 ? (
  211. $is_uniprint ? (
  212. _("Uniprint driver options") => { val => \$printer->{BITSPERPIXEL}, type => 'list', , not_edit => !$::expert, list => \@col } ) : (
  213. _("Color depth options") => { val => \$printer->{BITSPERPIXEL}, type => 'list', , not_edit => !$::expert, list => \@col } ), ) : ()
  214. ]);;
  215.  
  216.     $printer->{BITSPERPIXEL} = $col_to_depth{$printer->{BITSPERPIXEL}} || $printer->{BITSPERPIXEL}; 
  217.  
  218.     $printer->{complete} = 1;
  219.     copy_printer_params($printer, $printer->{configured}{$printer->{QUEUE}} ||= {});
  220.     printer::configure_queue($printer);
  221.     $printer->{complete} = 0;
  222.     
  223.     $action = ${{reverse %action}}{$in->ask_from_list('', _("Do you want to test printing?"),
  224.                               [ map { $action{$_} } @action ], $action{'done'})};
  225.  
  226.     my $pidlpd;
  227.     my @testpages;
  228.     push @testpages, "/usr/lib/rhs/rhs-printfilters/testpage.asc"
  229.       if $action eq "ascii" || $action eq "both";
  230.     push @testpages, "/usr/lib/rhs/rhs-printfilters/testpage". ($printer->{PAPERSIZE} eq 'a4' && '-a4') .".ps"
  231.       if $action eq "ps" || $action eq "both";
  232.  
  233.     if (@testpages) {
  234.         my $w = $in->wait_message('', _("Printing test page(s)..."));
  235.  
  236.         
  237.         foreach (("/var/spool/lpd/$printer->{QUEUE}/lock", "/var/spool/lpd/lpd.lock")) {
  238.         $pidlpd = (cat_("$prefix$_"))[0]; kill 'TERM', $pidlpd if $pidlpd;
  239.         unlink "$prefix$_";
  240.         }
  241.         run_program::rooted($prefix, "lprm", "-P$printer->{QUEUE}", "-"); sleep 1;
  242.         run_program::rooted($prefix, "lpd"); sleep 1;
  243.  
  244.         run_program::rooted($prefix, "lpr", "-P$printer->{QUEUE}", $_) foreach @testpages;
  245.  
  246.         sleep 5; 
  247.         local *F; open F, "chroot $prefix/ /usr/bin/lpq -P$printer->{QUEUE} |";
  248.         my @lpq_output = grep { !/^no entries/ && !(/^Rank\s+Owner/ .. /^\s*$/) } <F>;
  249.  
  250.         undef $w; 
  251.         if (@lpq_output) {
  252.         $action = $in->ask_yesorno('', _("Test page(s) have been sent to the printer daemon.
  253. This may take a little time before printer start.
  254. Printing status:\n%s\n\nDoes it work properly?", "@lpq_output"), 1) ? 'done' : 'change';
  255.         } else {
  256.         $action = $in->ask_yesorno('', _("Test page(s) have been sent to the printer daemon.
  257. This may take a little time before printer start.
  258. Does it work properly?"), 1) ? 'done' : 'change';
  259.         }
  260.     }
  261.     } while ($action ne 'done');
  262.     $printer->{complete} = 1;
  263. }
  264.