package Lpr_HP8550;

use strict;


## my $transparency_string = "<<\n"
##                         . "/MediaType (Transparency)\n"
##                         . ">> setpagedevice\n";

my $transparency_string = "%%SimonhSetup\n" 
                        . "<< /MediaType (Transparency) >> setpagedevice\n"
                        . "%%EndSimonhSetup\n";

my %pcj_header;

$pcj_header{"usucls_a4"} 
    = "%-12345X\@PJL JOB NAME=\"Simonh's Stuff\"\n"
    . "\@PJL SET RET=OFF\n"
    . "\@PJL SET DUPLEX=OFF\n"
    . "\@PJL SET PRINTONBACKSIDE=OFF\n"
    . "\@PJL SET CLEANING=OFF\n"
    . "\@PJL SET ECONOMODE=OFF\n"
    . "\@PJL SET OUTBIN=UPPER\n"
    . "\@PJL SET FINISH=NONE\n"
    . "\@PJL SET PAGEPROTECT=AUTO\n"
    . "\@PJL SET PAPER=A4\n"
    . "\@PJL SET HOLD=OFF\n"
    . "\@PJL SET RESOLUTION=600\n"
    . "\@PJL ENTER LANGUAGE=POSTSCRIPT\nM";

$pcj_header{"usucls_ohp"} 
    = "%-12345X\@PJL JOB NAME=\"Simonh's Stuff\"\n"
    . "\@PJL SET RET=OFF\n"
    . "\@PJL SET DUPLEX=OFF\n"
    . "\@PJL SET PRINTONBACKSIDE=OFF\n"
    . "\@PJL SET CLEANING=OFF\n"
    . "\@PJL SET ECONOMODE=OFF\n"
    . "\@PJL SET OUTBIN=UPPER\n"
    . "\@PJL SET FINISH=NONE\n"
    . "\@PJL SET PAGEPROTECT=AUTO\n"
    . "\@PJL SET PAPER=A4\n"                 # ...A4 again...
    . "\@PJL SET HOLD=OFF\n"
    . "\@PJL SET RESOLUTION=600\n"
    . "\@PJL ENTER LANGUAGE=POSTSCRIPT\nM";

$pcj_header{"usucls_a3"} 
    = "%-12345X\@PJL JOB NAME=\"Simonh's Stuff\"\n"
    . "\@PJL SET RET=OFF\n"
    . "\@PJL SET DUPLEX=OFF\n"
    . "\@PJL SET PRINTONBACKSIDE=OFF\n"
    . "\@PJL SET CLEANING=OFF\n"
    . "\@PJL SET ECONOMODE=OFF\n"
    . "\@PJL SET OUTBIN=UPPER\n"
    . "\@PJL SET FINISH=NONE\n"
    . "\@PJL SET PAGEPROTECT=AUTO\n"
    . "\@PJL SET PAPER=A3\n"
    . "\@PJL SET HOLD=OFF\n"
    . "\@PJL SET RESOLUTION=600\n"
    . "\@PJL ENTER LANGUAGE=POSTSCRIPT\nM";


sub new() {

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

    bless($self, $class);    

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

    return $self;
  }


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

sub process_files() {

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

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

    my @p_files = ();

    foreach  my $f (@$files) {
        my $out_f = "/tmp/lpr_simonh_" . time . "_" . rand();

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

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

        if ($queue eq "usucls_ohp") {

            my $dict_added = 0;

            while (my $textline = <IN>) {

                print OUT $textline;
###             if ($textline =~ m/^%%BeginSetup/) {
                if ($textline =~ m/^%%EndComments/) {
                    unless ($dict_added) {
                        $dict_added = 1;
                        print OUT $transparency_string;
                      }
                  }
              }
          }
        else {
            while (my $textline = <IN>) {
                print OUT $textline;
              }
          }

        close(IN);
        close(OUT);

        push(@p_files, $out_f);
      }


    return \@p_files;
  }


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