1#!/usr/bin/perl
2
3use English;
4use IO::Socket;
5
6my $JSUCC = 0;
7my $JABORT = 33;
8my $JNOSPOOL = 38;
9my $JNOPRINT = 39;
10
11my $debug = 0;
12my $optind;
13
14# pull out the options
15my($key,$value,$opt,$long,$opt_c);
16
17for( $i = 0; $i < @ARGV; ++$i ){
18    $opt = $ARGV[$i];
19    print STDERR "XX opt= $opt\n" if $debug;
20    if( $opt eq '-c' ){
21        $opt_c = 1;
22    } elsif( ($key, $value) = ($opt =~ /^-(.)(.*)/) ){
23        if( $value eq "" ){
24            $value = $ARGV[++$i];
25        }
26        ${"opt_$key"} = $value;
27        print STDERR "XX opt_$key = " . ${"opt_$key"} . "\n" if $debug;
28    } else {
29        $optind = $i;
30        last;
31    }
32    print STDERR "XX opt_P = $opt_P\n" if $debug;
33}
34
35$long = 0;  # short
36
37if( defined($opt_T) ){
38    print STDERR "XX CHECK_REMOTE opt_T=$opt_T\n" if $debug;
39    if( $opt_T =~ /debug/ ){
40        $debug = 1;
41    }
42    if( $opt_T =~ /short/ ){
43        $long = 1;
44    }
45    if( $opt_T =~ /long/ ){
46        $long = 0;
47    }
48}
49
50print STDERR "XX CHECK_REMOTE opt_P=$opt_P\n" if $debug;
51print STDERR "XX CHECK_REMOTE " . join(" ",@ARGV) . "\n" if $debug;
52
53if( !defined($opt_P) ){
54    print STDERR "$0: no -P value\n";
55    exit( $JABORT );
56}
57
58$printer = $opt_P;
59
60while( checkstatus( $printer, $long ) ){
61    print STDERR "XX CHECK_REMOTE sleeping\n" if $debug;
62    sleep(10);
63}
64
65exit $JSUCC;
66
67sub checkstatus {
68    my ($printer,$long) = @_;
69    my ($remote,$port);
70    my ($count, $socket, $line);
71
72    if( $long ){
73        $long = 4;
74    } else {
75        $long = 3;
76    }
77    if( $printer =~ /@/ ){
78        ($printer,$remote) = $printer =~ m/(.*)@(.*)/;
79    }
80    $remote="localhost" unless $remote;
81
82    if( $remote =~ /%/ ){
83        ($remote,$port) = $remote =~ m/(.*)%(.*)/;
84    }
85    $port = 515 unless $port;
86    print STDERR "XX CHECK_REMOTE remote='$remote', port='$port', pr='$printer', op='$long'\n" if $debug;
87
88    $socket = getconnection( $remote, $port );
89
90    $count = -1;
91    # send the command
92    printf $socket "%c%s\n", $long, $printer;
93
94    while ( defined( $line = <$socket>) && $count < 0 ){
95        chomp $line;
96        print STDERR "XX CHECKREMOTE '$line'\n" if $debug;
97        if( $line =~ /printing disa/ ){
98            print STDERR "XX CHECKREMOTE printing disable\n" if $debug;
99            exit $JNOPRINT;
100        } elsif( $line =~ /spooling disa/ ){
101            print STDERR "XX CHECKREMOTE printing disable\n" if $debug;
102            exit $JNOSPOOL;
103        } elsif( $line =~ /([0-9]*)\s+job.?$/ ){
104            $count = $1;
105            print STDERR "XX CHECKREMOTE $count jobs\n" if $debug;
106        }
107    }
108    close $socket;
109    if( $count < 0 ){
110        print STDERR "CHECKREMOTE cannot decode status\n";
111        exit $JABORT;
112    }
113    return $count;
114}
115
116sub getconnection {
117    my ($remote,$port) = @_;
118    my ($socket);
119    print STDERR "XX CHECK_REMOTE remote='$remote', port=$port\n" if $debug;
120    $socket = IO::Socket::INET->new(
121        Proto => "tcp",
122        PeerAddr => $remote,
123        PeerPort => $port,
124        );
125    if( !$socket ){
126        print STDERR "CHECK_REMOTE IO::Socket::INET failed - $!\n";
127        exit $JABORT;
128    }
129    $socket->autoflush(1);
130    $socket;
131}
132