#!/usr/bin/perl -w
# ....../hhms/Run.pl
# script to run HHMS "Background + Events Mode"
# File 'Agenda' gives list of S.Surf maps, with mass flux
# and scale factor + shocker input for each map.

$beginT = time;

$home = '.';
$MapDir = "$home/SSMaps";
$igmv = "$home/igmv";
$GrdDir = "$home/grids";
$gName = "$home/gName";
$grid0 = "$home/g3s.1660_000+090";
system("cp $grid0 $GrdDir");

chdir "$home" or die "Can't cd to $home: $!\n";
print `pwd`;
system("rm -f t3din*");
system("rm -f igmv.out");

system("mv -f Earth.ts Earth.ts.old") if (-e 'Earth.ts');
open (ST,">Stats.run");
print ST `date`;

open (AG,"Agenda") or die "Could not open Agenda: $!\n";
$line = <AG>;
chomp $line;
($startMap,$Fm0,$Bsc0) = split(/"/,$line,3);

while ( $line = <AG> ) {
  chomp $line;
  ($runN,$finalMap,$Fm1,$Bsc1,$gTitle,$shock) = split(/"/,$line,6);
  $gWcard   = sprintf "'g3?.*'";
  $grid = `$gName $gWcard`; chomp $grid;
  $file = sprintf "t3din.%3.3d", $runN;
  print "writing $file.\n";
  $time = time;
  print ST "$time $file";
  open (FH,">$file") or die "Could not open $file: $!\n";
  print FH "$GrdDir/$grid\n";
  print FH "$Fm0 $Bsc0 $MapDir/$startMap\n";
  print FH "$Fm1 $Bsc1 $MapDir/$finalMap\n";
  print FH "$gTitle\n";
  print FH "$shock\n";
  print FH "672. 672.0 240.0\n";
  print FH "1.0 1.0\n";
  print FH sprintf "%3.3d  0\n", $runN;
  close FH;
  print ST " writen\n";

  $startMap = $finalMap;
  $Fm0 = $Fm1;
  $Bsc0 = $Bsc1;

  system("rm -f bqmovie ") if (-e 'bqmovie');
  system("rm -f control.igm");
  system("cp $file t3din");
  system("nice $igmv >> igmv.out");
  exit unless -s 'control.igm';
}

$finalT = time;
$et = $finalT - $beginT;
print ST `date`;
print ST "ET = $et\n";
close ST;

print "Agenda Run Complete. ".`date`;
exit;
