# Parser
# V1.0
# Koltogyan Sergey Rubenovich ksr@ami.ua
my $READCONF="sys:\\etc\\1trstbcp\\trstbcp.cfg";

# DEFAULT delay time
my $d_delay=10;

# DEFAULT number of delay times
my $d_numdelay=10;

# DEFAULT Name of the module to check for
my $d_nlm="TEST.NLM";

my $R_NLM=$d_nlm;
my $R_NUMDELAY=$d_delay;
my $R_DELAY=$d_delay;
my $R_COMMAND="E";
my $R_UNLNK;

# The NRM XML file that keeps a note of which modules are loaded
$nrm_file="_admin:/Novell/NRM/NRMModules.xml";

# Quit nicely if this file is discovered.
$stop_file="SYS:TRSTBCP.TXT";

# Where the logs live.
$log_dir="SYSDATA:results\\trstbcp";

# Exit if the log directory's not there.
die "Log folder $log_dir does not exist" if (not -d $log_dir);

# The overall log file. Indicidual job logs will have incremental numbers
$log_file="$log_dir\\trstbcp.log";


mylog (" \n", $log_file);
mylog ("****************************************************", $log_file);
mylog ("                                    START NEW TRUST ", $log_file);

open (FL1, "<$READCONF") || die("Did not Open Source File !!");
mylog ("Read first string from ($READCONF) file", $log_file);


while (not eof(FL1) ) {
    $BASESTR="";
    $BASESTR=readline(FL1);
    @massiv=split(/\,/,$BASESTR);
    $R_NLM=$massiv[0];
    $R_COMMAND=$massiv[1];
    $R_NUMDELAY=$massiv[2];
    $R_DELAY=$massiv[3];
    $R_UNLNK=0;	
    $R_UNLNK=$massiv[4];
    $R_UNLNK =~ s/\n//g;
    if ($R_UNLNK ne "0") {unlink($R_UNLNK);};
    mylog ("Start for ($R_COMMAND)", $log_file);
    system("$R_COMMAND");
    $counttm=0;
    sleep($R_DELAY);
    while ( get_module_status($R_NLM) != 0) 
	{
	if ($counttm== $R_NUMDELAY)  { goto ENDAVR1; };
        $counttm=$counttm+1;
	sleep($R_DELAY);
	}
    mylog ("End for ($R_COMMAND)", $log_file);
    next;
}
goto END1;
ENDAVR1:
    mylog ("ERORRO END for ($R_COMMAND)", $log_file);
    mylog ("SCRIPT STOP", $log_file);
END1:
close(FL1);
    mylog ("------SCRIPT END--------------------", $log_file);
exit(0);



# -Sub

sub check_stop {
  # Check to see whether the 'abort' file exists and exit if it does

  if (-e $stop_file) {
    mylog("STOP file ($stop_file) exists. Terminating.\n", $log_file);
    exit;
    }
  }

sub mylog {

   # log output to screen and (optionally) to file
   # $_[0] is the string to be logged, $_[1] is the logfile

   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime;

   $out=sprintf("%02d",$mday);
   $out.="/";
   $out.=sprintf("%02d",$mon+1);
   $out.="/";
   $out.=1900+$year;
   $out.=" ";
   $out.=sprintf("%02d",$hour);
   $out.=":";
   $out.=sprintf("%02d",$min);
   $out.=":";
   $out.=sprintf("%02d",$sec);
   $out.=" $_[0]";
   $out.="\n";
   print $out;
   if ($_[1]) {
      $LOGFILE=">>" . $_[1];
      open LOGFILE or die "Can't open log file $_[1]: $!";
      print LOGFILE $out;
      close LOGFILE;
      }
   }


sub get_module_status {

  # Check whether a nlm is loaded. Returns 0 if not running, >=1 otherwise
  # Relies on NRM XML files
  #
  # Input:  Name of NLM to test (case sensitive)
  # Outout: Number of instances found (zero if not found)
  
#  my $COUNT=0;
#  my $ETALON=">".$_[0]."<";
#  open nrmfile, "<$nrm_file"  or die "Unable to open $nrm_file. Is NSS loaded?";
#  while (<nrmfile>) {if (/$ETALON/) {++$COUNT}}
#  close nrmfile;
#  return $COUNT;



 my $COUNT=0;
  my $ETALON=">".$_[0]."<";
  my $ff;
  open nrmfile, "<$nrm_file"  or die "Unable to open $nrm_file. Is NSS loaded?";
  while (<nrmfile>) 
    {
      $ff=<nrmfile>;	
      while ($ff =~ /$ETALON/g) {++$COUNT;}
    }
  close nrmfile;
  return $COUNT;





   }


