# Library 
# monitor-lib  

sub check_when 
{
     local ($isnow=false);
     local ($job, $is_now);
     local (@l_when);
     local (@mins = (0..59));
     local (@hours = (0..23));
     local (@days = (1..31));
     local (@months = map { $text{"month_$_"}."=".$_ } (1 .. 12));
     local (@weekdays = map { $text{"day_$_"}."=".$_ } (0 .. 6));
     local ($min,$max,$w,$v,$j,$jj,$disp,$code,$t_val);
     
     print "now: ".&set_now($now).":( $t_mins $t_hours $t_days $t_months $t_weekdays )\n";
     @l_when=split(/ /, $monWhen, -1);
          $job = { 'mins' =>     $l_when[0],
                   'hours' =>    $l_when[1],
                   'days' =>     $l_when[2],
                   'months' =>   $l_when[3],
                   'weekdays' => $l_when[4] };

          $is_now = { 'mins' =>  false,
                   'hours' =>    false,
                   'days' =>     false,
                   'months' =>   false,
                   'weekdays' => false };
     
     foreach $arr ("mins", "hours", "days", "months", "weekdays") {
	     # Find out which ones are being used
	     local %inuse;
	     $min = ($arr =~ /days|months/ ? 1 : 0);
	     $max = $min+scalar(@$arr)-1;
	     foreach $w (split(/,/ , $job->{$arr})) {
                             print "$arr:$w -- $1 $2\n"; 
		     if ($w eq "*") {
                             $is_now{$arr}=true;
                             print "$arr --".$job->{$arr}."\n"; 
			     # all values
                             last;
			     for($j=$min; $j<=$max; $j++) { $inuse{$j}++; }
		     }
		     elsif ($w =~ /^\*\/(\d+)$/) {
			     # only every Nth
			     for($j=$min; $j<=$max; $j+=$1) { $inuse{$j}++; }
			     print "for($j=$min; $j<=$max; $j += $1) { \n";
		     }
		     elsif ($w =~ /^(\d+)-(\d+)\/(\d+)$/) {
			     # only every Nth of some range
			     for($j=$1; $j<=$2; $j+=$3) { $inuse{int($j)}++; }
			     print "for($j=$1; $j<=$2; $j + = $3) { \n";
		     }
		     elsif ($w =~ /^(\d+)-(\d+)$/) {
			     # all of some range
			     for($j=$1; $j<=$2; $j++) { $inuse{int($j)}++; }
			     print "for($j=$1; $j<=$2; $j ++) { \n";
		     }
		     else {
			     # One value
			     $inuse{int($w)}++;
		     }
	     }
#	     if ($job->{$arr} eq "*") { undef(%inuse); }
#             for($j=0; $j<@$arr; $j+=12) {
#                     $jj = $j+11;
#                     if ($jj >= @$arr) { $jj = @$arr - 1; }
#                     @sec = @$arr[$j .. $jj];
#                     foreach $v (@sec) {
#                             if ($v =~ /^(.*)=(.*)$/) { $disp = $1; $code = $2; }
#                             else { $disp = $code = $v; }
#                             if  ( $inuse{$code} eq 1 ) {
#                                eval ("\$t_val=\$t_$arr;");
#                                if ( $t_val eq $code ) { 
#                                     print "$t_$arr eq $code \n"; 
#                                     $is_now{$arr}=true;
#                                     last;
#                                }
#                             }
#                     }
#             }
#             if ( $is_now{$arr} ne true ) { $isnow=false ; last };
      }
      print "$arr: $t_val ne $code \n";
      return $isnow;
}


# -----------------------------------------------------------------
sub outData
{
        &get_data;
        if ( $debug gt 0 ) { &print_data(); };
	print "global env \n";
        if ( "$WinAppServer" ne "" ) {
	    print "set uname               \"$tta3tier_login\" \n";
 	    print "set pword               \"$tta3tier_password\" \n";
 	    print "set env(TTA_Domain)           \"$tta3tier_domain\" \n";
        } else {
	    print "set args(username)             \"$tta3tier_login\" \n";
 	    print "set args(password)             \"$tta3tier_password\" \n";
	}
#        if ( "$extra_cmd" eq true ) { &out_data_cmd; }
        &out_data_cmd; 
}

sub out_data_cmd 
{
 	    print "set app(login)             \"$login\" \n";
 	    print "set app(password)             \"$password\" \n";
 	    print "set app(ttalogin)             \"$ttalogin\" \n";
 	    print "set app(password)             \"$ttapassword\" \n";
 	    print "set app(host)             \"$NameHost\" \n";
 	    print "set app(domain)             \"$domain\" \n";
	    print "set appextra_cmd            \"";
            if ( "$extra_cmd" eq true ) { 
                eval ("print  \"$tta3tier_stringcmd\"") ; 
                if ( $debug eq 2 ) {
                     print DebugLog "-------- APP LAUNCH VALUES -------------------\n";
                     eval ("print DebugLog \"appextra_cmd - $tta3tier_stringcmd \n\"");
                }
            };
	    print "\"\n";
            if ( "$extra_expect" eq true ) {
                 for ( $i=1 ; $i< $numKeyLimit+1; $i++ ) {
                          $obj=eval("\$appkey$i");
#                          if ( "$obj" ne "" ) {
	                       print "set expe_appkey$i      \"";
                               eval ("print  \"$obj\"") ;
	                       print "\"\n";
                               if ( $debug eq 2 ) { eval ("print DebugLog \"appkey$i -- $obj \n\"") };
#                          }
                 }
            }
            if ( $debug eq 2 && "$extra_cmd" eq true ) {
                  print DebugLog "----------------------------------------------\n";
            }
}

sub out_empty_data
{
        $login="";$ttalogin="";$password="";$ttapassword="";$domain;
        $appkey1="";$appkey2="";$appkey3="";$appkey4="";$appkey5="";$appkey6;
        $host=$NameHost;
        $extra_cmd=false;
        $tta3tier_stringcmd="";
        $extra_expect=false;

        &out_data_cmd; 
}

sub get_data
{

   $tta3tier_domain = $entry->{tta3tierdomain}[0];
   $tta3tier_login = $entry->{tta3tierlogin}[0];
   $tta3tier_password = $entry->{tta3tierpassword}[0];
   $tta3tier_stringcmd = $entry->{tta3tierstringcmd}[0];
   $tta3tier_title = $entry->{tta3tiertitle}[0];
   $tta3tier_oudn = $entry->{tta3tieroudn}[0];
   $tta3tier_appkey1 = $entry->{tta3tierappkey1}[0];
   $tta3tier_appkey2 = $entry->{tta3tierappkey2}[0];
   $tta3tier_appkey3 = $entry->{tta3tierappkey3}[0];
   $tta3tier_appkey4 = $entry->{tta3tierappkey4}[0];
   $tta3tier_appkey5 = $entry->{tta3tierappkey5}[0];
   $tta3tier_appkey6 = $entry->{tta3tierappkey6}[0];

  if ( $tta3tier_stringcmd ne "" ) {
       $extra_cmd=true;
       $login=$tta3tier_login;
       $password=$tta3tier_password;
       $ttalogin=$tta_username;
       $ttapassword=$tta_password;
       $domain =$tta3tieR_domain;
       if ( $tta3tier_appkey1 ne "" ) { 
                $extra_expect=true;
   		$appkey1=$tta3tier_appkey1;
   		$appkey2=$tta3tier_appkey2;
   		$appkey3=$tta3tier_appkey3;
   		$appkey4=$tta3tier_appkey4;
   		$appkey5=$tta3tier_appkey5;
   		$appkey6=$tta3tier_appkey6;
       }
   }
}

sub print_data
{
   print DebugLog "-------- LDAP CACHE VALUES -------------------\n";
   print DebugLog "login	= $tta3tier_login\n";
   print DebugLog "pasword	=$tta3tier_password\n";
   print DebugLog "stringcmd	=$tta3tier_stringcmd\n";
   print DebugLog "title	=$tta3tier_title\n";
   print DebugLog "domain	= $tta3tier_domain\n";
   print DebugLog "oudn		=$tta3tier_oudn\n";
   print DebugLog "appkey1	=$tta3tier_appkey1\n";
   print DebugLog "appkey1	=$tta3tier_appkey2\n";
   print DebugLog "appkey1	=$tta3tier_appkey3\n";
   print DebugLog "appkey1	=$tta3tier_appkey4\n";
   print DebugLog "appkey1	=$tta3tier_appkey5\n";
   print DebugLog "appkey1	=$tta3tier_appkey6\n";
   print DebugLog "-------- TTA LOGIN VALUES --------------------\n";
   print DebugLog "tta_username	= $tta_username\n";
   print DebugLog "tta_pasword	= $tta_password\n";
   print DebugLog "tta_host	= $tta_host\n";
   print DebugLog "----------------------------------------------\n";
   if ( "$WinAppServer" ne "" ) {  
       print DebugLog "-------- Windows APP VALUES ---------------\n";
       print DebugLog "uname	= $ttai3tier_username\n";
       print DebugLog "pword	= $ttai3tier_password\n";
       print DebugLog "TTA_Domain	= $ttai3tier_domain\n";
       print DebugLog "----------------------------------------------\n";
   }
#   print DebugLog "TTA_LOGFILE	= $tta_logfile\n";
#   print DebugLog "logfile_ext	= $logfile_ext\n";
}

sub print_results
{
   print DebugLog "----------- ENVIRONMENT VALUES ----------------\n";
   print DebugLog "TTA_UserName=$ENV{'TTA_UserName'}\n"; 
   print DebugLog "UserName=$UserName\n";
   print DebugLog "TTA_ApplicationName=$ENV{'TTA_ApplicationName'}\n"; 
   print DebugLog "AppName=$AppName\n";
   print DebugLog "TTA_HostName=$ENV{'TTA_HostName'}\n"; 
   print DebugLog "TTA_DomainName=$ENV{'TTA_DomainName'}\n"; 
   if ( "$WinAppServer" ne "" ) {  
     print DebugLog "TTA_WindowsApplicationServer=	= $WinAppServer\n";
   }
   print DebugLog "----------- SCANED PARAMS --------------------\n";
   print DebugLog " Usuario ($userType):       $NameUser\n";
   print DebugLog " Organization:      $OuName\n";
   print DebugLog " Aplicacion:       $NameApp \n";
   print DebugLog " Grupo Aplicacion:    $NameAppGrp \n";
   print DebugLog " Host:          $NameHost \n";
   print DebugLog " Grupo Hosts:       $NameHostGrp \n";
   print DebugLog " Domain:       $NameDomain \n";
   print DebugLog "----------------------------------------------\n";
   print DebugLog "TTA_CONNECTIONSERVICE: $ConnectionService \n";
}
sub endclose_debug
{
   if ( "$use_ttalogin_values" eq true ) {
        print DebugLog "using TTA login Values\n";
   } else {
        print DebugLog "using LDAP login Values\n";
   }
   print DebugLog "userType        = $userType\n";
   print DebugLog "----------------------------------------------\n";
   close (DebugLog);
}
1;  # Return true
