# Library 
# monitor-lib  

do './lists-lib.pl';  
do './ttasess-lib.pl';
do './print-lib.pl';

sub initialize () {
   &read_file("$config_directory/config", \%config);
   $DebugFile=$config{cache_debuglog};
   if ( $debug eq 0 ) { $debug=$config{cache_scandebug}; }; 
   if ( $debug gt 0 ) { 
         @linlogfile = split(/\//, $tta_logfile);
         $logfile_ext=$linlogfile[$#linlogfile];
         $logfile_ext=~s/.log//g;
         $logfile_ext=~s/script//g;
         $DebugFile=$config{cache_debuglog}.$logfile_ext.".log";
         open(DebugLog, ">$DebugFile"); 
   }
}

# load_language([module])
# Returns a hashtable mapping text codes to strings in the appropriate language
sub load_language
{
local %text;
if ($module_name) {
        local $mod = $_[0] ? "../$_[0]" : ".";
        &read_file("../lang/$default_lang", \%text);
        &read_file("../lang/$current_lang", \%text);
        &read_file("$mod/lang/$default_lang", \%text);
        &read_file("$mod/lang/$current_lang", \%text);
        }
else {
        &read_file("lang/$default_lang", \%text);
        &read_file("lang/$current_lang", \%text);
        if ($_[0]) {
                &read_file("$_[0]/lang/$default_lang", \%text);
                &read_file("$_[0]/lang/$current_lang", \%text);
                }
        }
foreach $k (keys %text) {
        $text{$k} =~ s/\$([A-Za-z0-9\.\-\_]+)/text_subs($1,\%text)/ge;
        }
return %text;
}

sub text_subs
{
local $t = $_[1]->{$_[0]};
return defined($t) ? $t : '$'.$_[0];
}
 
# text(message, [substitute]+)
sub text
{
local $rv = $text{$_[0]};
local $i;
for($i=1; $i<@_; $i++) {
        $rv =~ s/\$$i/$_[$i]/g;
        }
return $rv;
}

# read_file(file, &assoc, [&order], [lowercase])
# Fill an associative array with name=value pairs from a file
sub read_file
{
    open(ARFILE, $_[0]) || return 0;
    while(<ARFILE>) {
	    s/\r|\n//g;
            if (!/^#/ && /^([^=]+)=(.*)$/) {
		    $_[1]->{$_[3] ? lc($1) : $1} = $2;
		    push(@{$_[2]}, $1) if ($_[2]);
        	    }
            }
    close(ARFILE);
    return 1;
}

# -----------------------------------------------------------------
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
