#!/usr/local/bin/perl -s
# logpaper - print double logarithmic graph paper
#	  logpaper [ -s ] [ size ]
#	  optional argument specifies size of decades in decimal inches
#	  -r  to orient for wide graphs

$lpr = "/usr/ucb/lpr";
$size = 1;	# default square size
$angle = $r? 90:0;

while ($ARGV[0]) {
    if ($ARGV[0] =~ /^[\d.]+$/) {
        $size = $ARGV[0];
        shift;
    }
}

if ( ! -t STDOUT ) {	# if we're being piped somewhere, don't print
    print STDERR "Piping to standard output\n";
    select STDOUT;
} elsif ( -x $lpr ) {
    open (LPR,"| $lpr") || die "Cannot open pipe to $lpr\n";
    print STDERR "Piping to $lpr\n";
    select LPR;
} else {
    die "Cannot execute $lpr\n";
}

print STDERR "Using $size inch squares\n";
die "$0: Unable to print!\n" unless print <<"EOPS";
%!PS
% double-log graph paper

% measurements in inches
/spacing { $size } def	% this is set from Perl
/width { 11 } def
/height { 11 } def

% measurements in points,
% taken from inch measurements above
/pspacing { spacing 72 mul } def
/pwidth { width 72 mul } def
/pheight { height 72 mul } def

pwidth 2 div pheight 2 div translate
$angle rotate
pwidth -2 div pheight -2 div translate

/verticals
{ newpath
  0 pspacing pwidth
  { 1 1 10
    { log pspacing mul
      1 index add
      0 moveto
      0 pheight rlineto } bind for
    pop } bind for
  stroke } bind def

/horizontals
{ newpath
  0 pspacing pheight
  { 1 1 10
    { log pspacing mul
      1 index add
      0 exch moveto
      pwidth 0 rlineto } bind for
    pop } bind for
  stroke } bind def

% --- begin ---

0.1 setlinewidth
0.25 setgray
[ 0 1 1 ] 0 setdash
verticals
horizontals

showpage
EOPS
