#!/usr/bin/perl
#
# faxlpq version 1.0.1, last changed 9/11/1997
#
# (c) 1996 Horst F <horstf(al)weltentor.boerde.de>
#
# faxlpq shows the mgetty fax queue in lpq (hpux) format
#
# NOTE: I'm neither a perl nor network expert (as well
# English isn't my natural language too)

#####
# This script could be part of a samba/mgetty based fax solution
# for Win* clients. 
#
# It is intended to be used in conjunction with ``Winsock Respond Daemon''
# written by  "Horst F." <horstf(al)weltentor.boerde.de>
#
# You can download the current version from
# http://www.boerde.de/~horstf/
#
# called from smb.conf:
#   lpq command = /usr/bin/faxlpq %U
#

use strict;
no strict "refs";

my($user, $spooldir, $format);

###
### Configuration section
###

### Username field
###   'mail' if you use guest account for fax service
###   'user' else
### Note: if the resulting username is the guest user, samba
###      will replace it with the current session setup user
    $user = 'mail';

### Spooldirectory
    $spooldir = "/var/spool/fax/outgoing";

### Secure path
    $ENV{PATH} = '/usr/local/bin:/usr/bin:/bin' .
             ':/usr/local/sbin:/usr/sbin/:/sbin' .
             ':/usr/X11R6/bin';

### Output format (should be the same as printing in smb.conf), 
### currently only bsd and hpux
### Feel free to add your own and send me your additions
    $format = 'bsd';

###
### Nothing to configure below
###

my($dir, $status, %job, $entry, $value, $name, $last, @month, $jobtime,
   $file, $phone, $jobnum, $bytes, $faxfile, $size, $recv, @mtime, $prio);

@month = ('Jan','Feb','Mar','Apr','Mai','Jun',
          'Jul','Aug','Sep','Oct','Nov','Dez');

-t STDERR || open(STDERR, "|logger -tfax") ||
   die "$0: Can't open logger: $!\n";

$last = select(STDERR); $| = 1; select($last);

# You have to modify faxrunq to disable
# faxing if /etc/nofaxsend exist
-e '$spooldir/nofaxsend' && print "fax disabled\n";

# Open spooldir
opendir(SPOOL, $spooldir) || die "$0: Can't open $spooldir: $!\n";

# walk through the dirs
while ($dir = readdir(SPOOL)) {

  # Ignore some entries
  $dir =~ /^\./ && next;                 # Ignore hidden files
  -d "$spooldir/$dir" || next;           # Ignore other then dirs
  $dir =~ /^F\d\d\d\d\d\d\.?\d?/ || next; # Ignore false dir names
  
  # Open the JOB file
  if (open(JOB,"$spooldir/$dir/JOB")) { $status = 'queued'; }
  elsif (open(JOB, "$spooldir/$dir/JOB.locked")) { $status='active'; }
  elsif (open(JOB, "$spooldir/$dir/JOB.suspended")) { $status='suspended'; }
  else { next; }

  # Use sequence number as job number
  ($jobnum = $dir) =~ s/^F(\d\d\d\d\d\d).*$/$1/;
  $jobnum =~ s/^0+//;

  # Read the JOB file
  undef %job;
  while (<JOB>) {
    tr/\r\n//d;
    ($entry, $value) = split(/ +/, $_, 2);
    if ( $entry ne 'Status' ) { $job{$entry} = $value; }
  }

  @mtime = localtime((stat(JOB))[9]);
  close(JOB);

  # Calculate job time
  $jobtime = sprintf("%s %d %02d:%02d", $month[$mtime[4]], 
                      $mtime[3], $mtime[2], $mtime[1]);

  # Set the job owner
  $name = $job{$user};

  # Don't show jobs the current session setup user not owns
  if ("$ARGV[0]") { $name eq $ARGV[0] || next; }

  # Set the job name from receivers name (if exist) and phone number
  $phone = $job{'phone'};
  $recv = "Fax: $job{'verbose_to'}";
  if ("$recv") { $recv .= " <$phone>"; }
  else { $recv = "<$phone>"; }

  # We have to replace spaces in jobname, because samba replaces
  # a jobname that includes spaces with "STDIN"
  $recv =~ s/ /./g;   

  # Sum sizes of g3 files
  $bytes = 0;
  foreach $faxfile (split(/ +/,$job{'pages'})) {
    $size = (stat("$spooldir/$dir/$faxfile"))[7];
    $bytes += $size;
  }

  # Print the lpq line
  if ( $format eq 'hpux' ) {
    print "\tfence priority : 1\n";
    if ($status eq 'suspended' ) { $prio = 0; }
    else { $prio = 1 };
    print "fax-$jobnum $name priority $prio $jobtime ";
    if ( $status eq 'queued' ) { print "from $name\n"; }
    elsif ( $status eq 'active' ) { print "on fax\n"; }
    else { print "from $name\n"; }
    print "\t$recv $bytes bytes\n";
  }
  elsif ( $format eq 'bsd' ) {
    print "$status $name $jobnum $recv $size bytes\n" 
      unless $status eq 'suspended';
  }
# add here elsif ( $format eq 'yoursystem' ) { print ...; }
# if you use another printing system (and send me your additions)
}
closedir(SPOOL);
