package Lpr;

use strict;



sub new() {

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

    bless($self, $class);    

    $self->{NAME} = "lpr";
    $self->{LPR}  = $lpr;

    $self->parse_args($args);

    return $self;
  }


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

sub parse_args() {
    my $self = shift;
    my $argv = shift;

    my @options;
    my @files;

    my $arg_str = "";

    # -- check given args looks ok;  expecting
    #        lpr -P<queue> filename [filename ...] :

    foreach (@$argv) {
        $arg_str .= " " . $_;
      }

    unless ($arg_str =~ m/^\s+\-Pusu[a-z0-9\-]+\s+.+/) {
        $self->die("Bad arguments!", 
                   "Expecting    :  lpr -P<queue> <filename> <filename_2> ...")
      }

    my $parsing_options = 1;

    foreach my $a (@$argv) {

        if ($a =~ m/^\-/) {
            unless ($parsing_options) {
                die "\n\n ** Cannot mix options and files! ** \n\n";
              }
            push(@options, $a);

            if (($a eq "-Pusuls3") || ($a eq "-Pusuls3-a4"))
                                          {$self->{QUEUE} = "usuls3_a4"}
            if ($a eq "-Pusuls3-text")    {$self->{QUEUE} = "usuls3_a4_text"}
            if ($a eq "-Pusuls3-a3")      {$self->{QUEUE} = "usuls3_a3"}
            if ($a eq "-Pusuls3-duplex")  {$self->{QUEUE} = "usuls3_duplex"}

            if ($a eq "-Pusucls-a3")      {$self->{QUEUE} = "usucls_a3"}
            if ($a eq "-Pusucls-a4")      {$self->{QUEUE} = "usucls_a4"}
            if ($a eq "-Pusucls-ohp")     {$self->{QUEUE} = "usucls_ohp"}

            if ($a eq "-Pusua0p-matt-a0") {$self->{QUEUE} = "usua0p_matt_a0"}

            if ($a eq "-Pusuls2")         {$self->{QUEUE} = "usuls2"}

            if ($a eq "-Puph-laser1-m")  {$self->{QUEUE} = "uph_laser1_m"}

            if ($a eq "-Pusuls2-text")  {$self->{QUEUE} = "usuls2_text"}
          }
        else{
            $parsing_options = 0;
            push(@files, $a);
          }
      }

    foreach (@options) { print "\nopt  :  $_"; }
    print "\n ";
    foreach (@files) {  print "\nfile :  $_"; }

    $self->{OPTIONS} = \@options;
    $self->{FILES}   = \@files;
  }


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

sub check_files() {

    my $self = shift;

    foreach (@{$self->files()}) {
        open(IN, $_) || die "\n\n ** Can't open file: $_ ** \n\n";
        my $first_line = <IN>;
  
        if ($self->{QUEUE} =~ m/_text$/) {

            # -- check its an Ascii file :

#            if (($first_line =~ m/\S/) && ($first_line =~ m/\W/) 
#                                       && ($first_line =~ m/\D/)) {
#                die "\n\n ** File:  $_, does not look like Ascii. ** \n\n";
#              }

            # -- check its _not_ postscript :
            if ($first_line =~ m/^%!PS-Adobe/i) {
                $self->die("This looks like postscript sent to a text queue!",
                           "Not sending to queue.");
              }
          }
        else {
            unless ($first_line =~ m/^%!PS-Adobe/i) {
                die "\n\n ** File :  $_, does not look " 
                  . "like Postscript. ** \n\n";
              }
          }

        close(IN);
      }
  }


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

sub print_files() {

    my $self    = shift;
    my $files   = shift;

    print "\n\nAny messages below not enclosed in [...] are " 
        . "from /usr/ucb/ps :";
    print "\n";

    foreach my $fil (@$files) {

        my $cmd = $self->{LPR};

        foreach my $opt (@{$self->{OPTIONS}}) {

            if ($opt =~ m/^\-Pusuls3/) {
                $opt = "-Pusuls3";
              }

            if ($opt =~ m/^\-Pusuls2/) {
                $opt = "-Pusuls2";
              }

            $cmd .= " $opt";
          }

        print "\n[ $cmd $fil :  ] \n\n";
        `$cmd $fil`;
      }
  }

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

sub queue() {

    my $self = shift;
    
    return $self->{QUEUE};
  }


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

sub lpr() {

    my $self = shift;
    
    return $self->{LPR};
  }


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

sub options() {

    my $self = shift;
    
    return $self->{OPTIONS};
  }


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

sub files() {

    my $self = shift;
    
    return $self->{FILES};
  }


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

sub die () {
    my $self    = shift;
    my @strings = @_;

    print "\n";
    foreach (@strings) {
        print "\n ** $_";
      }
    print "\n";
    print "\n";
    exit;
  }

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