PageRenderTime 14ms CodeModel.GetById 6ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/vp_plugins/server_side/vprn

http://cupsfilter.googlecode.com/
Perl | 106 lines | 65 code | 14 blank | 27 comment | 12 complexity | 0d3dfbb49fa19349990c16c5c565dd22 MD5 | raw file
  1#!/usr/bin/perl -w
  2use strict;
  3use locale;
  4use File::Copy;
  5use File::Temp;
  6use Time::localtime;
  7
  8
  9#----------------------- CONFIG ------------------------------------
 10# For debuging and troubleshooting. Nice hunt !
 11my $DEBUG=1;                                                    
 12# enable log
 13my $LOG=1;                                                              
 14
 15my @argv = @ARGV;                       #keep hands off the argument array
 16my $SPOOL = "/var/tmp";                 #where are my speedy memory drive?
 17my $LOG_FILE = "/var/tmp/backend.log";
 18my $VPRINTER = "/opt/vprn/debug/p_monitor";
 19my $DATE_BIN = "/bin/date";             #path to date
 20my $ENABLE ="/usr/sbin/cupsenable";     #path to enable binary
 21my $FH_TEMP;                            #File handle to temp file
 22my $DEVICE_URI =$ENV{DEVICE_URI};
 23#--------------------------------------------------------------------------------------------------
 24
 25#print "Work it \n";
 26my ($jobID,$userName,$jobTitle,$copies,$printOptions,$printFile) = @argv;
 27
 28#--------------------------------------------------------------------------------------------------
 29# Check arguments and environment
 30# Usage: $0 job-id user title copies options [file]
 31#
 32## CUPS API says there must be an exact number of args 
 33# If no arguments, device discovery mode
 34if (!$argv[0]){
 35        print STDERR ("direct vprn backend \"Unknown\" \"TechnoServ Filter \"\n");
 36        exit 0;
 37}
 38
 39# If too many arguments, send error
 40if (scalar(@argv) < 5 || scalar(@argv) > 6){
 41        print STDERR ("Usage: vprn job-id user title copies options [file]\n");
 42        exit 1;
 43}
 44
 45save2log(join('_',@argv)) if  ($LOG);
 46
 47## get the uri CUPS has
 48#exit 1 if (!defined $DEVICE_URI);
 49
 50#--------------------------------------------------------------------------------------------------
 51#The hard, but fine work :)
 52# Safe create temp file
 53$FH_TEMP = File::Temp->new( TEMPLATE => 'cups_jobXXXXX',
 54                                DIR => $SPOOL,
 55                                SUFFIX => '.~tmp');
 56
 57# If we were called with 6 arguments, read from STDIN to a tempfile:
 58if (!defined $printFile){
 59    $FH_TEMP = File::Temp->new( TEMPLATE => 'cups_jobXXXXX',
 60                                DIR => $SPOOL,
 61                                SUFFIX => '.~tmp');
 62    copy(\*STDIN,$FH_TEMP) or die "Copy failed: $!"; 
 63}else{
 64    $FH_TEMP = $printFile;
 65}
 66
 67#???????? ?????? ?? ??????
 68save2log ("$VPRINTER $FH_TEMP \n") if  ($LOG);
 69#my $result =
 70`$VPRINTER $FH_TEMP`;
 71exit 0;
 72#exit $result;
 73
 74#-----------------------------------------------------------------------------------
 75sub datetime2string{
 76        #Arg:nothing
 77        #Returns: date time string
 78        my @months_names=("JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER");
 79        
 80    my $tm = localtime();
 81    my ($day,$month,$year,$hh,$mm) = ($tm->mday,$tm->mon,$tm->year,$tm->hour,$tm->min);
 82    $year+=1900;
 83    if (length($day) == 1){
 84        $day='0'.$day;
 85    }
 86    if (length($hh) == 1){
 87        $hh='0'.$hh;
 88    }
 89    if (length($mm) == 1){
 90        $mm='0'.$mm;
 91    }
 92    my $date =join('/',$year,$months_names[$month],$day);
 93    my $time =join(':',$hh,$mm);
 94    return join ('_',$date,$time); 
 95}
 96
 97sub save2log{
 98        #Arg: info_str
 99        #Returns: nothing
100        my($info_str)= @_;
101        open (LOG,">> $LOG_FILE") or die "can't open file $LOG_FILE: $!";
102        print LOG "--------------------------Date ", datetime2string()," ---------------------\n";
103        print LOG $info_str,"\n";
104        print LOG "---------------------------------------------------------------------------\n";
105        close (LOG);
106}