#!/usr/bin/perl
# Fax Printer Program for the Brother MFC FCL compatable devices
# Author: Franklin Meng
# File: faxprint.pl
# Version 0.9b
# last modified: 9/03/2002
# License: GPL


# Author: Steven Schwarznau
# Last Modified: 12/28/2006
# Changes
# 	This script will send a pdf-file as a g3-fax
#	It supports usb-printers and network printers (lpr/lpd)
#	It supports multi-page pdf's too
# Requirements
#	gs (ghostscript)
#	pdftops (PDF to PS Converter)
#	pdfinfo (PDF Meta Data to get number of pages)
#	psselect (to select the single pages of a ps-file (for fax with more than one page))
#	lpr (don't forget to set up a printer (deb: /etc/printcap))
# Parameters
#	-t 01234	# fax-number
#	-f file		# pdf-file
#	-d device  (optional) # set it only when using usb-printer
#	-p printer (optional) # used to set the lpr-printer (default: BRFAX)
# Hints
#	Don't use pdf2ps, this can cause bad quality
#	pdf2ps don't support pdf's with more than one page, pdftops do.



$title = "Brother MFC Fax Printer Program\nRevised by Steven Schwarznau\n\n";
$ver = "Version 0.9b-pdf ";
$date = "09/3/2002 ";
$lic = "License GPL\n";
#error flag used for errors
$errorflag = 0;

# What are the inputs I need?
# 1. Phone number obviously
# 2. Device for output
# 3. File that we will be working on

# Things to note is that we need to take into account multiple
# telephone numbers.  Maybe we should limit this program to
# only one phone number for now.

# Here we want to get arguments for the program to run
# We will parse through all the arguments.
# we are expecting
#  telephone number
#  file name
#  device name
while(@ARGV) {
$ARGV = shift @ARGV;
# get the telephone number
  if ($ARGV eq "-t") {
    $ARGV = shift @ARGV;
    $phone_num = $ARGV;
  }
# get the file name
  if ($ARGV eq "-f") {
    $ARGV = shift @ARGV;
    $cur_file_name = $ARGV;
  }
# get the device
  if ($ARGV eq "-d") {
    $ARGV = shift @ARGV;
    $dev = $ARGV;
  }
# get the lpr-printer
  if ($ARGV eq "-p") {
    $ARGV = shift @ARGV;
    $printer = $ARGV;
  }
}
print ($title, $ver, $date, $lic);

# check for errrors
if (!(defined($phone_num))) {
  print  "Error no phone number defined.\n";
  $errorflag++;
}
if (!(defined($cur_file_name))) {
  print "Error no fax file defined.\n";
  $errroflag++;
}

#if errrors were encountered exit program
if ($errorflag) {
  die "Several errors were encountered.\n";
}

# print some info
print "Phone: $phone_num\n";

# Generate fax pages in G3 format
#system "fax make $cur_file_name";     # This is where we generate the fax output

$output_file_name_ps = $cur_file_name . ".ps";

# convert pdf-file to ps-file
system "pdftops -q $cur_file_name $output_file_name_ps";

# convert ps-file to faxg3-format with high quality

# get number of pages from pdf meta
$pages = `pdfinfo -meta 03415916059.pdf | grep "Pages" | cut -d ":" -f 2`;
$pages =~ s/^\s+//;		# left trim

for($i = 1; $i <= $pages; $i++) {
  $output_file_name_g3 = $output_file_name_ps . ".g3." . $i;
  system "psselect -p $i $output_file_name_ps $output_file_name_ps.$i";
  system "gs -q -r204x196 -sDEVICE=dfaxhigh -dNOPAUSE -dBATCH -sOutputFile=" . $output_file_name_g3 . " $output_file_name_ps.$i";
}

print "Pages: $pages\n";

# Find fax files and size of each file
# place info into some sort of an array
for($i=0;defined($filename = glob("$output_file_name_ps.g3.*")) ; $i++) {
  @file_array[$i] = $filename;
  @size_array[$i] = (stat("$filename")) [7];
}


# Generate the file that we will be using to output the file
$output_file_name = $cur_file_name . ".fax";
open(OUT, ">$output_file_name");

#output to file
print OUT "\x00\x1b%-12345X\@PJL\n";
print OUT "\@PJL ENTER LANGUAGE=FCL\n";

## merge info into output file
# telephone numbers
## append fax pages

#merge in date information
$sec = (localtime) [0];
$min = (localtime) [1];
$hour = (localtime) [2];
$year = (localtime) [5] + 1900;
$month = (localtime) [4] + 1;
$day = (localtime) [3];
printf OUT "\x1bDATE[%d/%02d/%02d(%02d:%02d:%02d)\x092]\n", $year,$month,$day,$hour,$min,$sec;

#merge in telephone info
print OUT "\x1bDIALNUM[$phone_num]\n";
print OUT "\x1bSENDFAX[]\n";

#loop to merge in multiple pages of the fax
for($i = 0; $i < $pages; $i++){
  print OUT "\x1bRESOLUTION[FINE]\n";   #fax resolution info
  print OUT "\x1bPUTDATA[$size_array[$i],";     #fax data headers
  #merge in data
  open(MERGE_FILE, "$file_array[$i]");
  print "Fax file $i $file_array[$i]\n";
  while (<MERGE_FILE>) {
    print OUT "$_";
  }
  close(MERGE_FILE);
  print OUT "]\n";
}

print OUT "\x1b%-12345X\n";

close(OUT);

## end fax output file to port

## delete temp file.
unlink ("$output_file_name_ps");
for($i = 1; $i <= $pages; $i++) {
  $output_file_name_g3 = $output_file_name_ps . ".g3." . $i;
  unlink ("$output_file_name_g3");
  unlink ("$output_file_name_ps.$i");
}


if (!(defined($pinter))) {
  $printer = "BRFAX";
}

if (!(defined($dev))) {
  print "LPR-Printer: $printer\n";
  system "lpr -P" . $printer . " $output_file_name";
} else {
  print "Device: $dev\n";
  system "cp $output_file_name $dev";
} 
my $datestring = sprintf("%d-%02d-%02d_%02d-%02d-%02d_", $year,$month,$day,$hour,$min,$sec);
system "mv $cur_file_name " . $datestring . $cur_file_name . ".snt";
system "mv $output_file_name " . $datestring . $output_file_name;

print "Fax generator completed the MFC should now be faxing\n";

