#!/usr/bin/perl -w
# $FreeBSD$
#
# epsgeom - extract geometry from a EPS file.
#
# Copyright (C) 2004 The FreeBSD Project. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

my $x;
my $y;
my $width;
my $height;

my $gx;
my $gy;

if (@ARGV != 4) {
	die "Error: invalid arguments.\n";
}

my $mode = shift @ARGV;
my $hres = shift @ARGV;
my $vres = shift @ARGV;
my $file = shift @ARGV;

my $realfile = `realpath ${file}`;
chomp $realfile;

open IN, "<$realfile" or die "Error: cannot open $realfile.\n";
while(<IN>) {
	last if (($x,$y,$width,$height) =
		/^%%BoundingBox:\s+([-\d]+)\s+([-\d]+)\s+([-\d]+)\s+([-\d]+)/);
	#print STDERR "DEBUG: $_";
}
close IN;

if (not defined($x)) {
	die "Error: no BoundingBox found.\n";
}

$width -= $x;
$height -= $y;

# (int)(((double)hres * (double)width  / 72.0) + 0.5),
$gx = $hres * $width / 72.0 + 0.5;
# (int)(((double)vres * (double)height / 72.0) + 0.5),
$gy = $vres * $height / 72.0 + 0.5;

my %replace = (
	       '@X@' => int $x,
	       '@Y@' => int $y,
	       '@MX@' => int -$x,
	       '@MY@' => int -$y,
	       '@WIDTH@'  => int $width,
	       '@MWIDTH@' => int -$width,
	       '@HEIGHT@' => int $height,
	       '@MHEIGHT@' => int -$height,
	       '@ANGLE@' => int 0,
	       '@INPUT@' => $realfile,
	       );

if ($mode eq "-replace") {
	foreach my $i (keys %replace) {
		printf "-e s,%s,%s,g ", $i, $replace{$i};
	}
} elsif ($mode eq "-offset") {
	#print STDERR "DEBUG: (int -$x), $y\n";
	printf "<< /PageOffset [%d %d] >> setpagedevice\n", (int -$x), $y;
	open IN, "<$realfile" or die "Error: cannot open $realfile.\n";
	print while(<IN>);
	close IN;
	print "\n";
	print "showpage\n";
} elsif ($mode eq "-geom") {
	#print STDERR "DEBUG: $x,$y,$width,$height\n";
	printf "%dx%d", $gx, $gy;
} else {
	die "Error: invalid mode specified.\n";
}

__END__