#!/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

$title = "Brother MFC Fax Printer Program\n";
$ver = "Version 0.9b ";
$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 device
  if ($ARGV eq "-d") {
    $ARGV = shift @ARGV;
    $dev = $ARGV;
  }
# get the file name
  if ($ARGV eq "-f") {
    $ARGV = shift @ARGV;
    $cur_file_name = $ARGV;
  }
}

print ($title, $ver, $date, $lic);

# check for errrors
if (!(defined($phone_num))) {
  print  "Error no phone number defined.\n";
  $errorflag++;
}
if (!(defined($dev))) {
  print "Error no output device 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 "Port: $dev\n";
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

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

$pages = $i;   # number of pages to fax

# 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
system "cp $output_file_name $dev";

## delete temp file.
unlink ("$output_file_name");

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