package Lpr_HP5Si;

use strict;


# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------


my %pcj_header;
my %pcj_footer;


$pcj_header{"usuls3_a4"} 
    = "%-12345X\@PJL JOB\n"
    . "\@PJL SET RESOLUTION = 600\n"
    . "\@PJL SET ECONOMODE = OFF\n"
    . "\@PJL ENTER LANGUAGE = POSTSCRIPT\n";

$pcj_header{"usuls3_duplex"} 
    = "%-12345X\@PJL JOB\n"
    . "\@PJL SET RESOLUTION = 600\n"
    . "\@PJL SET ECONOMODE = OFF\n"
    . "\@PJL ENTER LANGUAGE = POSTSCRIPT\n";

$pcj_header{"usuls3_a3"} 
    = "%-12345X\@PJL JOB\n"
    . "\@PJL SET RESOLUTION = 600\n"
    . "\@PJL SET ECONOMODE = OFF\n"
    . "\@PJL ENTER LANGUAGE = POSTSCRIPT\n";


$pcj_footer{"usuls3_a4"}     = "%-12345X\@PJL EOJ\n";
$pcj_footer{"usuls3_duplex"} = "%-12345X\@PJL EOJ\n";
$pcj_footer{"usuls3_a3"}     = "%-12345X\@PJL EOJ\n";


# -----------------------------------------------------------------------------


my %status_dict;


$status_dict{"usuls3_a4"}
    = "statusdict /setpapertray known {" 
    . "statusdict begin 2 setpapertray end} if\n";
##  . "statusdict begin 0 setpapertray end} if\n";

$status_dict{"usuls3_duplex"} 
    = "statusdict /setpapertray known {" 
    . "statusdict begin 0 setpapertray end} if\n"
## Tray 2 produces nothing!
##  . "statusdict begin 2 setpapertray end} if\n";
    . "statusdict /setduplexmode known {" 
    . "statusdict begin true setduplexmode end} if\n";

$status_dict{"usuls3_a3"} 
    = "statusdict /setpapertray known {" 
    . "statusdict begin 1 setpapertray end} if\n";


# -----------------------------------------------------------------------------


my %setpagedevice_dict;


$setpagedevice_dict{"usuls3_a4"}     
    = "%%BeginFeature: *HPPaperPolicy PromptUser\n"
    . "<</DeferredMediaSelection true>> setpagedevice\n"
    . "%%EndFeature\n"
    . "%%BeginFeature: *PageSize A4\n"
    . "<</PageSize [595 842] /ImagingBBox null>> setpagedevice\n"
    . "%%EndFeature\n"
    . "%%BeginFeature: *Duplex None\n"
    . "<</Duplex false>> setpagedevice\n"
    . "%%EndFeature\n";

$setpagedevice_dict{"usuls3_duplex"} 
    = "%%BeginFeature: *HPPaperPolicy PromptUser\n"
    . "<</DeferredMediaSelection true>> setpagedevice\n"
    . "%%EndFeature\n"
    . "%%BeginFeature: *PageSize A4\n"
    . "<</PageSize [595 842] /ImagingBBox null>> setpagedevice\n"
    . "%%EndFeature\n"
    . "%%BeginFeature: *Duplex ????\n"
    . "<</Duplex true>> setpagedevice\n"
    . "%%EndFeature\n";

$setpagedevice_dict{"usuls3_a3"}     
    = "%%BeginFeature: *HPPaperPolicy PromptUser\n"
    . "<</DeferredMediaSelection true>> setpagedevice\n"
    . "%%EndFeature\n"
    . "%%BeginFeature: *PageSize A4\n"
    . "<</PageSize [842 1191] /ImagingBBox null>> setpagedevice\n"
    . "%%EndFeature\n"
    . "%%BeginFeature: *Duplex None\n"
    . "<</Duplex false>> setpagedevice\n"
    . "%%EndFeature\n";


# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------


sub new() {

    my $class = shift;
    my $self  = { };

    bless($self, $class);    

    $self->{NAME} = "Lpr_HP5Si";

    return $self;
  }


# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------


sub process_files() {

    my $self = shift;
    my $lpr  = shift;

    my $queue = $lpr->queue();
    my $files = $lpr->files();

    my @p_files = ();

    my $alt_queue = $queue;
    $alt_queue    =~ s/_text$//;

    foreach my $f (@$files) {

        if ($queue =~ m/_text$/) {

            my $a2ps_out_f = "/tmp/lpr_simonh_" . time . "_" . rand();
            system("/usr/local/bin/a2ps -o $a2ps_out_f $f"); 

            open(IN, $a2ps_out_f) 
                 || die "\n\n ** Can't open file :  $a2ps_out_f ** \n\n";
          }
        else {
            open(IN, $f) 
                 || die "\n\n ** Can't open file :  $f ** \n\n";
          }

        my $out_f = "/tmp/lpr_simonh_" . time . "_" . rand();

        open(OUT, ">$out_f") 
                 || die "\n\n ** Can't open file :  $out_f ** \n\n";

        if (defined $pcj_header{$alt_queue}) {
            print OUT $pcj_header{$alt_queue}; 
          } 
        else {
            die "\n\n ** Unknown printer: $queue, " 
              . "in " . $self->{NAME} . "::process_files() ** \n\n";
          }

        my $dict_added = 0;

        while (my $textline = <IN>) {

            print OUT $textline;

            if ($textline =~ m/^\%\%EndComments/) {
            unless ($dict_added) {

                $dict_added = 1;

                print OUT "% --- lpr_simonh :\n";

                if (($queue eq "usuls3") || ($queue eq "usuls3_a4")
                                         || ($queue eq "usuls3_a4_text")) {
##                      print OUT $status_dict{"usuls3_a4"};
                        print OUT $setpagedevice_dict{"usuls3_a4"};
                  }
                elsif ($queue eq "usuls3_duplex") {
##                      print OUT $status_dict{"usuls3_duplex"};
                        print OUT $setpagedevice_dict{"usuls3_duplex"};
                  }
                elsif ($queue eq "usuls3_a3") {
##                      print OUT $status_dict{"usuls3_a3"};
                        print OUT $setpagedevice_dict{"usuls3_a3"};
                  }
                else {
                        die "\n\n ** Unknown printer: $queue, " 
                          . "in " . $self->{NAME} . "process_files() ** \n\n";
                  }
                print OUT "% : --- lpr_simonh\n";
              }
              }
	  }

        if (defined $pcj_footer{$alt_queue}) {
            print OUT $pcj_footer{$alt_queue}; 
          } 
        else {
            die "\n\n ** Unknown printer: $queue, " 
              . "in " . $self->{NAME} . "process_files() ** \n\n";
          }

        close(IN);
        close(OUT);

        push(@p_files, $out_f);
      }

    return \@p_files;
  }


# -----------------------------------------------------------------------------
1;
# -----------------------------------------------------------------------------
