#! /usr/bin/perl

# this script merges several ps-files into one .ps file
# BS (June 2003)

$argc = $#ARGV;

if ($argc < 2) {
	printf("\n Usage: merge_ps file1.ps file2.ps <file_n.ps> output.ps \n\n");
	printf("\n This will merge all given *.ps files into output.ps \n\n");
}


open(DATE, "date \"+%Y-%m-%d_%H-%M\"|");
$date = <DATE>;
chomp($date);
close(DATE);

open(MERGE, ">/tmp/merging_$date.ps");

print MERGE <<'EOF';
%!PS
% Written by Helge Blischke, see
% http://groups.google.com/groups?ic=1&selm=3964A684.49D%40srz-berlin.de
%
% The following 2 procs encapsulate the jobs to be processed
% much as is done with EPS images:
/_begin_job_
{
        /tweak_save save def
        /tweak_dc countdictstack def
        /tweak_oc count 1 sub def
        userdict begin
}bind def

/_end_job_
{
        count tweak_oc sub{pop}repeat
        countdictstack tweak_dc sub{end}repeat
        tweak_save restore
}bind def
EOF

for ($ia=0; $ia < $argc; $ia++) {
	print MERGE <<"EOF";
_begin_job_
($ARGV[$ia])run
_end_job_
EOF
}


close(MERGE);

$ia = $argc;

# Call the merging command !!!!
system("pstoedit -f gs:pswrite /tmp/merging_$date.ps $ARGV[$ia] 2>/dev/null");



system("rm -f /tmp/merging_$date.ps");


