# Library 
# ttacache-lib  

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 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"); 
   }
}

# 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 scan_env {
         if ( $debug eq 2 ) { print DebugLog "\nScanning $UserName ....\n";}
   	 $lnumUser =0 ;
         # First LDAP user  and second TTA user
         $userType="LDAP";
         @linUserName = split(/,/, $UserName);
         if ("$linUserName[0]" eq "$UserName") { 
              $userType="TTA";
              @linUserName = split(/\//, $UserName);
         }
	 $OuName="";
	 foreach $obj (@linUserName) {
              @UserPath= split(/=/,$obj);
              $lnumUser++;
  	      if ( $debug eq 2 ) { print DebugLog "$UserPath[0] -- $UserPath[1] \n"; }
              if ( "$userType" eq "LDAP" ) {
                   if ( $UserPath[0] eq "cn"  &&  $lnumUser eq 1 ) {
		        $NameUser=$UserPath[1];
                        break;
                   } else {
                        if ( "$OuName" ne "" ) { $OuName=",".$OuName; }
		        $OuName="ou=".$UserPath[1].$OuName;
	           }
              } else {
                   $lastOuName=$OuName;
                   if ( "$OuName" ne "" ) { $OuName=",".$OuName; }
		   $OuName="ou=".$UserPath[1].$OuName;
	      }
          }
          if ( "$userType" ne "LDAP" ) { 
                $NameUser=$UserPath[1]; 
                $OuName=$lastOuName;
          }
          if ( "$tta_username" eq "" ) { $tta_username=$NameUser; }
  	  if ( $debug eq 2 ) { print DebugLog "\nScanning $AppName ....\n";}
          $lnumApp = 0;
          @linAppName = split(/\//, $AppName);
          $NameAppGrp="";
	  foreach $obj (@linAppName) {
              @AppPath= split(/=/,$obj);
              if ( $lnumApp lt $#linAppName ) { 
                  if ( "$NameAppGrp" ne "" ) { $NameAppGrp=",".$NameAppGrp; }
	          $NameAppGrp="ou=".$AppPath[1].$NameAppGrp;
              }
              $lnumApp++;
  	      if ( $debug eq 2 ) { print DebugLog "$AppPath[0] -- $AppPath[1] \n"; }
          }
          $NameApp=$AppPath[1];
  	  if ( $debug eq 2 ) { print DebugLog "\nScanning $HostName ....\n";}
          $lnumHost = 0;
          @linHostName = split(/\//, $HostName);
          $NameHostGrp="";
	  foreach $obj (@linHostName) {
              @HostPath= split(/=/,$obj);
              if ( $lnumHost lt $#linHostName ) { 
                  if ( "$NameHostGrp" ne "" ) { $NameHostGrp=",".$NameHostGrp; }
	          $NameHostGrp=$HostPath[0]."=".$HostPath[1].$NameHostGrp;
              }
              $lnumHost++;
  	      if ( $debug eq 2 ) { print DebugLog "$HostPath[0] -- $HostPath[1] \n"; }
          }
          if ( $HostPath[1].length eq 0 ) {
               $NameHost=$HostPath[0];
          } else {
               $NameHost=$HostPath[1];
          }
          if ( $NameHostGrp.length eq 0 ) {
               $NameHostGrp=$NameAppGrp;
               $NameHostGrp=~s/RIMA/servidores/g;
          }
  	  if ( $debug eq 2 ) { print DebugLog "\n";}
}

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
