# ----------------------------------------
# Perl script to run enscript, which converts text files to postscript
# and prints them.
#
# Enscript has many options for pretty printing files and producing
# fancy headers and other effects.
#
# This script drives enscript in its simplest form to allow printing of
# text views from HDL Designer.

BEGIN {
    my $hdldsHome = $ENV{'HDS_HOME'};
    my $resources = "$hdldsHome/resources";
    my @temp = ("$resources/perl/lib/");
    if (-e "$resources/perl/lib/sun4-solaris/5.004/") {
        unshift (@temp, "$resources/perl/lib/sun4-solaris/5.004/");
    }
    elsif ( -e "$resources/perl/lib/PA-RISC1.1/5.004/") {
        unshift (@temp, "$resources/perl/lib/PA-RISC1.1/5.004/");
    } 
    elsif ( -e "$resources/perl/lib/i686-linux/5.004/") {
        unshift (@temp, "$resources/perl/lib/i686-linux/5.004/");
    } 
    foreach my $directory (@temp) {
        push @libs, $directory;
    }
    @INC = @libs;
}

use FileHandle;
use Getopt::Long;

# Make output appear in the correct order
STDOUT->autoflush(1);        
    
my $progName = "printText";
my $debug = 0;

my $hdsHome = $ENV{'HDS_HOME'};
my $printCmd;

if ($^O eq 'MSWin32') {
    $ENV{'PATH'} = "$hdsHome\\resources\\enscript;$ENV{'PATH'}";
    $ENV{'ENSCRIPT_LIBRARY'} = "$hdsHome\\resources\\enscript";
    $printCmd = "enscript.exe";
} else {
    $ENV{'ENSCRIPT_LIBRARY'} = "$hdsHome/resources/enscript/share/enscript";
    $printCmd = "$hdsHome/resources/enscript/bin/enscript";
}
# ----------------------------------------
# Get the arguments

my $copies = 1;                    # number of copies to print
my $orientation = 'portrait';      # orientation landscape/portrait
my $paper = 'A4';                  # paper size
my $destination = 'printer';       # destination printer / file
my $printer;                       # name of destination printer/file

GetOptions('copies=s' => \$copies,
           'orientation=s' => \$orientation,
           'paper=s' => \$paper,
           'destination=s' => \$destination,
           'printer=s' => \$printer,
           'debug' => \$debug);

if ($debug) {
    print " copies = $copies\n";
    print " orientation = $orientation\n";
    print " paper = $paper\n";
    print " destination = $destination\n";
    print " printer = $printer\n";
    for (@ARGV) {
        print "$_\n";
    }
}

$paper = getPaperName($paper);

my $printerOption;

# Quote printer and paper names if they contain spaces
$printer = '"' . $printer . '"' if ($printer =~ / /);
$paper = '"' . $paper . '"' if ($paper =~ / /);

if ($destination eq 'file') {
    $printerOption = "--output=$printer";
} else {
    if ($^O eq 'MSWin32') {
        # On Windows need to set to destination device
        $printerOption = "--printer=$printer" if $printer;
    } else {
        # On Unix enscript pipes output to the command sepcified by
        # Spooler: in the enscript.cfg file
        if ($printer =~ /^["]*\s*lp\s/) {
            # Using the lp command printer defined by -d
            if ($printer =~ /\s-d\s*(\w+)/) {
                $printerOption = "--printer=$1";
            }
        } elsif ($printer =~ /^["]*\s*lpr\s/) {
            # Using the lp command printer defined by -P
             if ($printer =~ /\s-P\s*(\w+)/) {
                 $printerOption = "--printer=$1";
             }
         }
    }
}


my $command = "$printCmd --copies=$copies --media=$paper --$orientation $printerOption";

for (@ARGV) {
    s/\\/\//g;   # Convert DOS backslash to forward slash. This means
                 # that the header will appear with directory separators
    if (/ /) { # argument contains spaces
        $command .= ' "' . $_ . '"';   # Add each additional command line arg with quotes
    } else {
        $command .= " " . $_;
    }
}
eval {
    runCommand($command);
};
if($@) {
    print "$progName : Error running $printCmd, aborting.\n";
    exit 1;
}

# ----------------------------------------
# Run a command - also prints output

sub runCommand {
    my $cmd = shift;
    print "PATH=$ENV{'PATH'}\n" if $debug;
    print "$progName : Running command\n$cmd\n";
    system($cmd) == 0 or die;
}

# ----------------------------------------
# Convert the paper name from HDL Designer to a
# media recognised by enscipt. The enscript
# media names are defined in enscript.cfg, and
# new ones can be defined if required
sub getPaperName{
    my $name = shift;
    
    if ($name eq 'A4' || $name eq 'A3' || $name eq 'A5' || $name eq 'Letter') {
        return $name;
    } elsif ($name eq 'LETTER') {
        return 'Letter';
    } else {
        my %paperTypes = ('A0  (841mm x 1189mm)' => 'A0',
                          'A1  (594mm x 841mm)'  => 'A1',
                          'A2  (420mm x 594mm)'  => 'A2',
                          'A3  (297mm x 420mm)'  => 'A3',
                          'A4  (210mm x 297mm)'  => 'A4',
                          'A5  (148mm x 210mm)'  => 'A5',
                          'B0  (1000mm x 1414mm)'=> 'B0',
                          'B1  (707mm x 1000mm)' => 'B1',
                          'B2  (500mm x 707mm)'  => 'B2',
                          'B3  (353mm x 500mm)'  => 'B3',
                          'B4  (250mm x 353mm)'  => 'B4',
                          'B5  (176mm x 250mm)'  => 'B5',
                          'A (8.5" x 11")'       => 'Letter',
                          'B (11" x 17")'        => 'B',
                          'C (17" x 22")'        => 'C',
                          'D (22" x 34")'        => 'D',
                          'E (34" x 44")'        => 'E',
                          'Broad Sheet (18" x 24")' => 'BroadSheet',
                          'Double (11" x 17")'   => 'Double',
                          'Legal (8.5" x 14")'   => 'Legal',
                          'Letter (8.5" x 11")'  => 'Letter',
                          'Half (5.5" x 8.5")'   => 'Half');
        $name = $paperTypes{$name};
    }
    $name = 'A4' if (!$name); # default to A4 if not set
    return $name;
}
