#!/usr/bin/perl -w
# ===============================================================================
# oopstops	prefilter to sanitize PostScript jobs generated by OpenOffice 2.x
# -------------------------------------------------------------------------------
# 1.00 - 2007-03-17/Bl
#	First implementation
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2 of the License, or (at your
#  option) any later version.
#
#  This program is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
#  Public License for more details.
#
# Description:
# ------------
#	OpenOffice as of version 2.x has built-in CUPS support, parses
#	the user selectable options in the printer's PPD and inserts the respecitve
#	PostScript code into the PostScript job. However, it does not insert all 
#	defined defaults nor the JCL settings, but it does honour the JobPatchFile
#	keyword. In addition, if a default setting in the PPD does not correspond
#	to one of the choices listed in the respective UI block, OpenOffice silently 
#	selects the first choice (NOTE: this cannot be corrected by this filter).
#	Moreover, the page related features are inserted into the page setup section
#	of the first page. This violates the page independence and prohibits successful
#	working of job attivutes like number-up or same-up.
#
#	We therefore modify the PostScript job as follows:
#	- any JobPatchFile features are discaarded, as they are re-inserted by the 
#	  pstops filter.
#	- the first page's page setup is moved to the end of the general setup section
#	  (in fact, the respective comments are moved instead).



use IO::Handle;

#
# Check the arguments
#
die ("ERROR: wrong number of arguments\n") if (scalar @ARGV < 5);

$jobid = $username = $title = $copies = $options = undef;
$jobid = shift;				# Job ID
$username = shift;			# Job requesting user name
$title = shift;				# Job title
$copies = shift;			# Number of requested copies
$options = shift;			# Textual representation of job attributes
$psfile = shift;			# read from file ?


#
# Normalize options string
#
$options =~ s/^\s+//;
$options =~ s/\s+$//;
$options = ' ' . $options . ' ';

#
# Check from where to read
#
if (defined $psfile)
{
	open (FILI, "<$psfile") || die ("ERROR: $psfile: $!\n");
}
else
{
	open (FILI, "<&STDIN") || die ("ERROR: STDIN: $!\n");
}

# STDOUT->autoflush (1);

#
# Parse the input until and including the page setup of the first page
# and relocate the the th Et)u   eMM8u	 uE8 uU  U: 
  $u
      $E  M9 A  u   NME>&    $\   D$D$$   B<^wrB<v\}uˍv 뮍    u     u     $)j$\[^_]fD$E$g$$$+$E؋$\$w;$YE]mU\$4$IEUأU\$4$<(G$
E\$4$E\$4$ SEȉ$\$qEأ$\$$y$\[^_]Í$t& $zt& u
   t%u
   u&$>Y$+F
$M*΍NMEED_teB<wT$\   D$D$$B<^vD$E$f\}u뢋U$    D$
UWVS'(  l$    D$$$ ǉ$E<$D$ED$`D$    <$tx@0B4BDB<B8B@\$0ʍ`\$(D$\$ \$\$\$ $tWED$ED$$]  D$4$Aa  ED$ED$$t-       B  ED$ED$$t. 4      f*  ED$ED$$~t.       f  ED