#!/usr/local/bin/perl

use strict;
use English;
use Socket;
use LPRng;
use Sys::Hostname;

my(
	$Printer, $Pc_value, $Debug, $pr, $remote, $port, $option,
	$options, $SOCK, $line, $prefix, $firstpart, $count, $username
);

$| = 1;

$Debug = 0;
Set_Debug($Debug);
Setup_LPRng( "P:");

# get the printer name
$Printer = Get_printer_name();
if( not $Printer ){
	die "missing printer name";
}

print "Printer '$Printer'\n" if $Debug;

$Pc_value = Setup_pc_entry( $Printer );

($pr, $remote, $port ) = Get_remote_pr_host( $Printer, $Pc_value );
print "pr '$pr', remote '$remote', port '$port'\n" if $Debug;

$option = 5;

$username = getpwuid($UID);

$SOCK = getconnection( $remote, $port );

$options =  join (" ", @ARGV);
printf  "%d%s %s%s\n", $option, $pr, $username, $options ? (" " . $options) : "" if $Debug;
printf $SOCK "%c%s %s%s\n", $option, $pr, $username, $options ? (" " . $options) : "";
$prefix = "";

while ( defined( $line = <$SOCK> ) ) { 
	print $line; 
} 
close ($SOCK) or die "close: $!"; 
exit 0;
