#!/usr/local/bin/perl -w
#A script to rotate ps file from landscape to portrait,
#reducing the size by 0.707 times.
#2007-06-27 Ken Ebisawa

if($#ARGV!=1){
    print "rotateps.pl psfile outputfile\n";
    exit(0);
}
$tmpfile="$$.ps";
$file=$ARGV[0];
$outfile=$ARGV[1];
$size=0.7071; # Paper side over height ratio = reduction rate
$width=596;   # A4 width in point
$string=`grep BoundingBox $file`;
$string=~/BoundingBox:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/;
$llx=$1;
$lly=$2;
$urx=$3;
$ury=$4;
$newllx=$size*$lly;
$newlly=$width-$size*$urx;
$newurx=$size*$ury;
$newury=$width-$size*$llx;
$command = "pstops \"1:0R\@${size}(0,$width)\" $file > $tmpfile\n";
system($command);
open(IN,$tmpfile);
open(OUT,">$outfile");
while(<IN>){
    $_=~s/\%\%BoundingBox:\s+\d+\s+\d+\s+\d+\s+\d+/\%\%BoundingBox:$newllx $newlly $newurx $newury/g;
    $_=~s/\%\%Orientation: Landscape/\%\%Orientation: Portrait/g;
    print OUT $_;
}
close(IN);
close(OUT);
$command="rm -f $tmpfile\n";
#system($command);

