#!/usr/bin/perl

if($ARGV[0] =~ /^-g/) {
  $ghost = 1;
  shift;
}
$suffix = ".eps";
if($ARGV[0] =~ /^-P/) {
  $post = "-P";
  $suffix = ".ps";
  shift;
}

if(-t STDOUT) {
  print STDERR "automatically invoking gv; redirect if you want something else\n";
  $ghost = 1;
}


$file = "/tmp/t.$$.$suffix";
if($ghost)  {
  open(W, "|jgraph $post > $file");
} else {
  open(W, "|jgraph $post "); 
}

for($i=0; $i<$#ARGV; $i++) {
  if($ARGV[$i] =~/marktype/ ||
     $ARGV[$i] =~/linetype/) {
    push @series, splice(@ARGV,$i,2);
    $i--; # do this one again, when we come to it.
  }
  if($ARGV[$i] =~/color/) {
    push @series, splice(@ARGV,$i,4);
    $i--; # do this one again, when we come to it.
  }
}

print W "newgraph\n";
print W join(" ", @ARGV) . "\n";
print W "xaxis hash_format g yaxis hash_format g\n";
if($post eq "-P") {
  print W "xaxis size 5 yaxis size 5\n"
}
print W " newcurve marktype x\n";
print W join(" ", @series) . "\n";
print W "pts\n";
while(<STDIN>) {
  print W;
}
close(W);

if($ghost){
  `gv $file`;
  unlink("$file");
}
