#!/usr/bin/perl

# Predefined constants

my $debug = 0;

my @highlightLevels = qw(heavy normal none);
my @languages = qw(68000 a2psrc ssh ada c csh cpp caml chlog claire clisp coqv dc_shell eiffel elisp eps tclx fortran for77-kwds for77-fixed for77-free for90-fixed for90-free for90-kwds for-fixed for-free gnuc gmake html idl is5rul java lace lex mail make mib matlab4 modula2 module3 o2c oberon objc octave initora plsql sql oracle pascal perl ps ppd pre pretex prolog promela python card rexx sather scheme sdl88 sql92 symbols tcsh tex texinfo texscript tk tcl udiff unity verilog vhdl vtcl vrml wdiff yacc zsh);
my @orientations = qw(landscape portrait);
my @pageSizes = ( '10x14', 'a3', 'a4', 'a4dj', 'a5', 'b4', 'b5', 'executive', 'folio', 'ledger', 'legal', 'letter', 'letterdj', 'quarto', 'statement', 'tabloid' );
my @syntaxHelp = ( 'Options:',
		   '',
		   '--help',
		   '--list=languages',
		   '--list=orientation',
		   '--list=paper',
		   '--list=highlights',
		   '',
		   '--paper=<paperSize>  |',
		   '--page=<paperSize>   +- Synonyms',
		   '--medium=<paperSize> |',
		   '--listpaper',
		   '',
		   '--orientation=<orientation>',
		   '--listorientation',
		   '--language=<language>',
		   '',
		   '--column=<virtualPagesPerPhysicalPage>       |',
		   '--columns=<virtualPagesPerPhysicalPage>      +- Synonyms',
		   '--virtualPages=<virtualPagePerPhysicalPage>  |',
		   '',
		   '--output=<outputFileSuffix>',
		   '--linesPerPage=<linesPerPage>',
		   '--highlightLevel=<highLightLevel>',
		   '',
		   '--lineNumbers=<lineNumbers>           |',
		   '--lineNumbering=<linueNumbers>        +- Synonyms',
		   '--lineNumberingInterval=<lineNumbers> |',
		   ''
		 );

my $minFontSize = 6;
my $a2psLocation = '/usr/bin/a2ps';

# Program defaults...

my $fontSize = 8;
my $highlightLevel = 'heavy';
my $language = '';
my $lineNumberingInterval = '1';
my $linesPerPage = 66;
my $orientation = 'landscape';
my $outputSuffix = '';
my $pageSize = 'letter';
my $virtualPagesPerPhysicalPage = 1;

my @files = ();
my @commandLines = ();

# Parse incoming arguments... if any

my @args = @ARGV;
$args [0] = '--help' if ($ARGV [0] =~ /^\s*$/);

if (($args[0] =~ /--help/i) or ($args[0] =~ /--list=.*/i)) {
	my ($parameter, $value, @helpList);
	if ($args[0] =~ /--(list)=(.*)/) {
		($parameter, $value) = ($1, $2);
	} elsif ($args[0] =~ /--help/i) {
		$parameter = 'help';
	}

	print "printCode v1.0\n";
	print "Syntax: printCode [options] <filename(s)>\n\n";
	
	if ($parameter =~ /^help$/i) {
		@helpList = @syntaxHelp;
	} elsif ($parameter =~ /^list$/i) {
		if ($value =~ /^languages$/i) {
			print "List of available languages...\n\n";

			@helpList = @languages;
		} elsif ($value =~ /^orientation$/i) {
			print "List of available paper orientations...\n\n";

			@helpList = @orientations;
		} elsif ($value =~ /^paper$/i) {
			print "List of available paper sizes...\n\n";

			@helpList = @pageSizes;
		} elsif ($value =~ /^highlights$/i) {
			print "List of available highlight levels...\n\n";

			@helpList = @highlightLevels;
		}
	}

	foreach my $helpLine (@helpList) {
		print $helpLine."\n";
	}
	exit;
}

foreach my $argument (@ARGV) {
	if ($argument =~ /^--(.*?)=(.*)/) {
		# We're looking at a command line argument here...
		my ($parameter, $value) = ($1, $2);

		if ($parameter =~ /^fontsize$/i) {
			$fontSize = $value if (($value =~ /[0-9]+/) and ($value >= $minFontSize));
		} elsif ($parameter =~ /^highlight(level)?$/i) {
			$hightlightLevel = lc $value if (grep /^$value$/i, @highlightLevels);
		} elsif ($parameter =~ /^language$/i) {
			$language = lc $value if (grep /^$value$/i, @languages);
		} elsif ($parameter =~ /^lineNumber(ingInterval|ing|s)?$/i) {
			$lineNumberingInterval = $value if ((($value =~ /[0-9]+/) and ($value >= 1)) or ($value =~ /^off$/i));
			$lineNumberingInterval = '' if ($value =~ /^off$/i);
		} elsif ($parameter =~ /^linesPerPage$/i) {
			$linesPerPage = $value if (($value =~ /[0-9]+/) and ($value >= 40));
		} elsif ($parameter =~ /^orientation$/i) {
			$orientation = $value if (grep /^$value$/i, @orientations);
		} elsif ($parameter =~ /^output/) {
			$outputSuffix = $value;
		} elsif ($parameter =~ /^(page|medium|paper)$/i) {
			$pageSize = $value if (grep /^$value$/i, @pageSizes);
		} elsif ($parameter =~ /^(column|columns|virtualPages)$/) {
			$virtualPagesPerPhysicalPage = $value if ($value >= 1);
		}
	} else {
		# We're looking at a filename to be printed here...
		if (-e $argument and -r $argument and -f $argument) {
			# Make sure it exists, its readable and its a file (not a dir)
			push @files, $argument;
		}
	}
}


# Generate a2ps commandline...
foreach my $file (@files) {
	my $commandLine = $a2psLocation.' ';
	my $filename = (reverse split /\//, $file)[0];

	# Handle font size...
	$commandLine .= '--font-size='.$fontSize.' ' unless ($fontSize =~ /^\s*$/);
	# Handle highlight level...
	$commandLine .= '--highlight-level='.$highlightLevel.' ';
	# Handle language...
	$commandLine .= '--pretty-print='.$language.' ' unless ($language =~ /^\s*$/);
	# Handle page size...
	$commandLine .= '--medium='.$pageSize.' ' unless ($pageSize =~ /^\s*$/);
	# Handle line numbering...
	$commandLine .= '--line-numbers='.$lineNumberingInterval.' ' unless ($lineNumberingInterval =~ /^\s*$/);
	# Handle lines per page...
	$commandLine .= '--lines-per-page='.$linesPerPage.' ' unless ($linesPerPage =~ /^\s*$/);
	# Handle orientation...
	$commandLine .= '--'.$orientation.' ';
	# Handle virtual pages...
	$commandLine .= '--columns='.$virtualPagesPerPhysicalPage.' ';
	# Handle output file extension...
	$commandLine .= '-o '.$filename.".$outputSuffix.ps " unless ($outputSuffix =~ /^\s*$/);
	$commandLine .= '-d ' if ($outputSuffix =~ /^\s*$/);
	# Process this file...
	$commandLine .= $file."\n";

	push @commandLines, $commandLine;
}


# Print out all commandlines so far...
foreach my $commandLine (@commandLines) {
	print $commandLine if $debug;
	system $commandLine;
}
