#!/opt/SUNWstade/bin/perl -I/opt/SUNWstade/lib
#<copyright>
# ----------------------------------------------------------
# Sun Proprietary/Confidential Code
# Copyright 2001, Sun Microsystems, Inc. All rights reserved.
# ----------------------------------------------------------
#</copyright>

use Spawn;
use System;
use FindBin;
use Debug;
use FileHandle;
use IPC::Open2;
use IPC::Open3;

# must write results to Q_FILE and O_FILE
# must send Q_FILE and P_FILE to master if any
#
$DEBUG = 1;

sub mylog {
  my($text) = @_;
  return if ($DEBUG < 1);
  open(O, ">>/tmp/rasmonitor"); 
  print O "$text\n";
  close(O);
}

$PORT     = "7658";
$master   = $ARGV[0];
$pid      = $ARGV[1];  # ST_5301
$comm     = $ARGV[2];
$register = $ARGV[3];

mylog("M=$master, P=$pid, C=$comm, R=$register");


for ($x=4; $x <= $#ARGV; $x++) {
  $opts .= $ARGV[$x] . " ";
}

my($local_p) = $FindBin::Bin;
my($ix) = rindex($local_p, "/");
$HOME   = substr($local_p,0,$ix);

System->set_home($HOME);

$DIR = System->get_home() . "/DATA/Proc";

if ($master eq "N") {
   $DIR .= "/" . System->hostname();
} else {
   $DIR .= "/$master";
}


if (!$comm) {
   Debug->err(SCHED => 
         "trying to run rasmonitor with no command ($pid,$register)");
   exit;
}

#($runerr, $testpid , $in, $outF, $err) = Spawn->run("$comm $opts \"dev=$register\"");
#($runerr, $testpid , $in, $outF) = Spawn->run2("$comm $opts \"dev=$register\"");

$testpid = open2(*Reader, *Writer, $comm ,$opts,"dev=$register");

mylog("ERR=$runerr") if ($runerr);

$PFILE = "$DIR/P_$pid";

$OFILE = "$DIR/O_$pid";
$QFILE = "$DIR/Q_$pid";
unlink $OFILE;

$SIG{CHLD} = 'IGNORE';
$SIG{PIPE} = 'IGNORE';

$page = 1;
$AFILE =  "$DIR/A_$pid";
unlink $AFILE;

while (1) {
   $cnt = 0;
   $out = "";
   $end = 0;
   unlink $QFILE;
   while ($l = <Reader>) {
      $cnt++;
      $out .= " $l";
      last if ($l =~ /\<cr\> to continue/);
      if (substr($l,0,4) eq "#END") {
         $end = 1;
         last ;
      }
   }

   if ($cnt == 0 || $end) {
      open(O, ">>$OFILE");
      print O "#QUESTION $page\n$out\n";
      close(O);

      open(O, ">>$PFILE");
      my $date = Util->get_today();
      print O substr($pid,3) . "\tSTOP\t$date\t0,\n";
      close(O);
      last;
 
   } else {
      open(O, ">$QFILE");
      print O "#QUESTION $page\n$out Q\n";
      close(O);
      open(O, ">>$OFILE");
      print O "#QUESTION $page\n$out Q\n";
      $page++;
      close(O);
      &send($master, $PORT, "Q_$pid"); # send question-file to master
   }
   $ans = 0;
   while (!-f $AFILE) {
      sleep(2);
      $ans += 2;
   }
   unlink $AFILE;
   print Writer "\n";
}

waitpid $testpid,0;

&send($master, $PORT, "P_$pid"); # send P_file to master

sub send {
  my($master, $PORT, $file, $data) = @_;
  return if ($master eq "N");

  open(O, System->get_home() . "/DATA/Proc/$file");
  @l = <O>; close(O);
  my $info = join("", @l);

  my $url = "http://$master:$PORT/?PUT=saveF&file=Proc/$file";

  $data = Util::Http->post($url, $info, 20);
  return $data;
}

