#!/opt/SUNWstade/bin/perl -I/opt/SUNWstade/lib
use System;
use strict;
use Util;
use Getopt::Std;
use Snapshot;

use vars qw($BASE %opts $DIR $SID $renv $renv);

System->set_home("/opt/SUNWstade");

($renv) = PDM::ConfigFile->read();
System->set_renv($renv);

my $command = shift @ARGV;

if (!getopts("b:s:h", \%opts) || !$opts{s}) {
  &usage();
  exit(1);
}
$BASE = $opts{b} || "/var/opt/SUNWstade/SNAP";

sub usage {
  print <<EOF;

  Usage: ras_snap [start|fail|repair|stop] -s <snapshot_id> -h -b <base_dir>
           -s : snapshot_id
	   -b : base directory for snapshots, use /var/opt/SUNWstade/SNAP by default
  Example: 
           run rasagent at least once. (manually or wait for the cron).
           ras_snap start -s 001.sp
           ...
	   ras_snap fail -s 001.sp
	   inject fault...
	   rasagent... (or cron, use -r to ignore the round-robin and do all devices every time).

	   ras_snap repair -s 001.sp
	   fix fault...
           rasagent... (or cron)

	   ras_snap stop -s 001.sp
EOF
}


$SID = $opts{s};
$DIR = "$BASE/$SID";

mkdir $BASE,0777 if (!-d $BASE);
mkdir $DIR, 0777 if (!-d $DIR);

if ($command eq "start") {
  &start($SID);

} elsif ($command eq "stop") {
  &stop($SID);

} elsif ($command eq "fail") {
  &fail($SID);

} elsif ($command eq "repair") {
  &repair($SID);

} else {
  &usage();
  exit(1);
}

print "Done.\n";

# BASE_REPORTS
# TEST_REPORTS       
# BASE reports are copied to OLD_REPORTS before 
# running rasagent -T 
# set env RAS_SNAP=$BASE/SID

########################
## START
########################

sub stop {
   print "Ending this recording sequence..\n";
   unlink "/opt/SUNWstade/DATA/SNAP";
}
   

sub start {
   print "Copying reports to $DIR...\n";
   unlink "/opt/SUNWstade/DATA/SNAP";

   foreach my $d0 ('BASE','FAIL','REPAIR') {
     system("rm -r $DIR/$d0 2>/dev/null");
     mkdir "$DIR/$d0", 0777;
     mkdir "$DIR/$d0/OLD_REPORTS", 0777;
     mkdir "$DIR/$d0/NEXT_REPORTS", 0777 if ($d0 ne "BASE");
   }
   mkdir "$DIR/topo", 0777;
   foreach my $e ('MAXEVENTS', 'SEQUENCER', 'EDOCS', 'state/state', 
                   'THRESHOLDS') {
      unlink "/opt/SUNWstade/DATA/$e.db";
      unlink "/opt/SUNWstade/DATA/$e.db.lock";
   }

   system("cp /opt/SUNWstade/DATA/OLD_REPORTS/*:* $DIR/BASE/OLD_REPORTS");
   system("cp /opt/SUNWstade/DATA/rasagent.conf $DIR");
   system("cp /opt/SUNWstade/DATA/topo/* $DIR/topo");
   unlink "$DIR/Events.log";

   my $seek4 = (stat("/opt/SUNWstade/DATA/Events.log"))[7];

   my $snap = {     logfile => $renv->{logfile},
                date_start  => Util->get_today(),
	        time_start  => time,
                t300logfile => $renv->{t300logfile} ,
                dsp_logfile => $renv->{dsp_logfile} ,
                     sun_os => System->os_version(),
	    events_log_seek => $seek4,
	   };

   Snapshot->seekLogfile($snap, $renv);

   Util->serialize0("$DIR/snapshot.conf", $snap);
}


########################
## FAIL
########################

sub fail {
   my $snap = Util->deserialize0("$DIR/snapshot.conf");


   if (!$snap->{time_start}) {
      print "Error: run 'ras_snap start' first! \n";
      exit(1);
   }
   open(O, ">/opt/SUNWstade/DATA/SNAP");
   print O "fail=$DIR";
   close(O);
   print "\n On the next rasagent run, monitoring information will be saved\n".
         " in $DIR\n";
}

sub repair {
   my $snap = Util->deserialize0("$DIR/snapshot.conf");


   if (!$snap->{time_start}) {
      print "Error: run 'ras_snap start' first! \n";
      exit(1);
   }
   open(O, ">/opt/SUNWstade/DATA/SNAP");
   print O "repair=$DIR";
   close(O);
   print "\n On the next rasagent run, monitoring information will be saved\n".
         " in $DIR\n";
}

