#!/usr/bin/perl
#  Direct-to-printer (old HP Jetdirect style printers) filter
#  to integrate with rhs-printfilters.  
#  Supplied by Joshua Buysse, University of Minnesota

# needs perl 5.004 for IO:Socket
require 5.004;

# Maximum number of times to retry connection
$max_retries = 3600; # at one second each, one hour.

# needed for the dirname() function
use File::Basename;
use IO::Socket;
use Cwd;

# The first parameter to the script is the name of the 
# accounting file, in the spool directory for the printer.
$acct_file = $ARGV[0];

# we need the spool_dir to pick up the config files
$spool_dir = dirname($acct_file);

$config_file = "$spool_dir/.config";

if (-e $config_file) {

    open(CONFIG, $config_file) || die "No config file found!";

    while (<CONFIG>) {
	chomp;
	s/#.*//;     # no comments
	s/^\s+//;    # no leading white
	s/\s+$//;    # no trailing white

	# If there's nothing left, we're done.
	next unless length;

	# split the fields
	my ($key,$value) = split /\s*=\s*/, $_, 2;

	$config{$key} = $value;
    }
}
else {
    # This section was added to support the newer lpd
    # versions that get data from printcap instead of .config. -mlp

    # Get queue name:
    if (length($spool_dir) < 2) {
        $spool_dir = cwd ();
    }
    $inp = `pcap -S $spool_dir`;
    chop $inp;
    eval "\$$inp";

    # Get port:
    $inp = `pcap -P$queue:port`;
    chop $inp;
    eval "\$$inp";

    # Get printer_ip:
    $inp = `pcap -P$queue:printer_ip`;
    chop $inp;
    eval "\$$inp";

    $config{'port'} = $port;
    $config{'printer_ip'} = $printer_ip;
}

# the config hash should contain port and printer_ip as keys

# if the port isn't set, use the default of 9100
$config{'port'} = 9100 unless $config{'port'};
$config{'printer_ip'} || die "Config file does not specify printer IP.";

# now, open a socket to the printer.

$retry_count = 0;

do {
  $socket = IO::Socket::INET->new(PeerAddr => $config{'printer_ip'},
  				  PeerPort => $config{'port'},
				  Proto    => "tcp",
				  Type     => SOCK_STREAM);
  if (! $socket) {
    sleep 1;
    $retry_count++;
  }
} until ($socket || ($retry_count > $max_retries));

$socket || die "Unable to open socket after $retry_count retries.";

while (<STDIN>) {
  print $socket $_;
}

close($socket);





