
#  -*- perl -*-
eval 'exec perl -S $0 "$@"'
  if 0;

($calldir, $i) = split('/', $0, 2);
while ($i) { 
  ($j, $i) = split('/', $i, 2);
  if ($i) { $calldir = "$calldir/$j"; }
}

$prefix = "dr2dgnplot_tmp";
$count = int(1000 * rand);
if ($ENV{TMPDIR} eq "") { $tmpdir = "/tmp"; } else { $tmpdir = $ENV{TMPDIR} }
if (substr($tmpdir, length($tmpdir)-1, 1) eq "/")
  { $tmpdir = substr($tmpdir, 0, length($tmpdir)-1); }
do { $tmp1 = $tmpdir."/".$prefix."\_$count"; ++$count; } while (-f $tmp1);
do { $tmp2 = $tmpdir."/".$prefix."\_$count"; ++$count; } while (-f $tmp2);

sub catch { `rm -f $tmp1 $tmp2`; }
$SIG{INT} = 'catch';

if (!(open(F, "$calldir/draw_mesh_doedges $ARGV[0] 2>&1 |")))
  { catch(); die "Start program impossible : $!\n";}

if (!(open(FT, ">$tmp1"))) { catch; die "Open file impossible : $!\n";}

while (<F>) {
  if ($_ =~ /N =/) { ($a, $N) = split('=', $_, 2);  }
  elsif (($_ =~ /=/) || ($_ =~ /for/)) {
    print $_; while (<F>) { print $_; }
    close(FT); close(F); catch; exit(1);
  }
  else { print FT $_; }
}

close(FT);
close(F);

if (!(open(GNF, ">$tmp2"))) { catch; die "Open file impossible : $!\n";}
print GNF "set data style line \n";
if ($N <= 2) { print GNF "plot \'$tmp1\' title \'mesh\'\n"; }
else { print GNF "splot \'$tmp1\' title \'mesh\'\n"; }
print GNF "pause -1\n";
print GNF "set term postscript\n";
print GNF "set output \'$ARGV[0].ps\'\n";
print GNF "replot\n";
close(GNF);
`gnuplot $tmp2`;

`rm -f $tmp1 $tmp2`;
