#! /usr/athena/bin/perl

$first_line = <>;
die "Didnt look like an appropriate file.\n" if $first_line !~ /bulb file/;
$second_line = <>;

$second_line =~ /^(\S+)( (.+))?\n$/ || die "FAILED: bad format for filename and bounds.";

$filename = $1;
$bounds = $3;

$filename =~ m|^(/[^/]+/[^/]+)/(.+)$|
  || die "FAILED: looks like file was broken.\n";
$fs = $1;

if(! -e $fs) {
  print STDERR "Attaching bulb.mit.edu:$fs ...\n";
  system("attach -q -h -n -m $fs -e bulb:$fs");
  die "FAILED: Unable to attach filesystem $fs.  $!\n" if $?;
}

if ($bounds) {
  $bounds =~ /^-bounds (.+)$/ || die "Illegal bounds spec";
  $ba = $1;
  print STDERR "Running tviewer on '$filename' '$bounds'...\n";
  system("tviewer","$filename","-logging","-f", "/mit/library/app-defaults/Tviewer","-bounds","$ba");
  die "FAILED: $!\n" if $?;
  exit(0);
} else {
  print STDERR "Running tviewer on '$filename'";
  system("tviewer","$filename","-logging","-f", "/mit/library/app-defaults/Tviewer");
  die "FAILED: $!\n" if $?;
  exit(0);
}
