
do "global-lib.pl";

 # globals to keep TTA info

  %ttaprops=();
  @ttahosts=();

 # Some vars to change ... Just if TTA change

 local $dir_path="/opt/tarantella/var/serverconfig/global/";
 local $ext_file=".properties";
 local $ttaproperties_files="applaunch array bootstrap cdm components ldap lictypes security  webauth";

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

# Loading Hash with file and properties %ttaprops (with @data in)  and @ttahosts
# get_ttaprops(has_toset,$dir_path,$extension,$names_toexclude) 
sub get_ttaprops 
{
     local @list_names=glob($_[0]."*".$_[1]);
     local $list_exclude=$_[2];
     local $nhost=0;
     local $target=0;
     local $k,$kname,$v;

     foreach $f_name (@list_names) {
         $FileName=$f_name;
         $FileName=~s/$dir_path//g;  
         if ( $FileName )  { $FileName=~s/$ext_file//g; }
         if ( $list_exclude =~ /$FileName/ ) { next; }
         local %config=();
         &read_file($f_name, \%config);
         if ( $ttaproperties_files =~ /$FileName/ ) {
                      $target="array";
         } else {
                  $target="host";
                  $ttahosts[$nhost] = { 'name' => $FileName  };
         }
         $ttaprops{$FileName} = { 
                           'name' => $FileName,
                           'target' => $target,
                           'path' => $f_name,
                           'config' => \%config
                          };
        if ( $target eq "host" ) { $nhost++; }
    }
}

     &get_ttaprops($dir_path,$ext_file,"");
     &show_hash(\%ttaprops);
     $lnum=0;
     foreach $item (@ttahosts) {
         print "***".$ttahosts[lnum]->{name}." = ".$item->{name}." \n";
         $lnum++;
     }
     print "logtimeout=".&search_in_hash(\%ttaprops,"tarantella.config.applaunch.sessions.longtimeout")."\n";

exit;

# Load info into %ttainfo and %ttahosts
sub load_ttaprop
{
     local %rv;
     local $nhost=0;

     &get_ttaprop(\%rv,$dir_path,$ext_file,"");
     foreach $item (keys (%rv)) {
                 $itemName=$rv{$item}->{'name'};
                 if ( $ttaproperties_files =~ /$itemName/ ) {
                      $ttaprops{$itemName} = { "name" => $fName };
                      $is_host="0";
                 } else {
                      $is_host="1";
                      $ttahosts{$nhost} = { "name" => $fName };
                 }
                 foreach $k ( keys (%{$rv{$item}->{'config'}})) {
                         local $v=${$rv{$item}->{'config'}}{$k};
                         $kname=$k;
                         if ( $is_host eq "1" ) {
                              $kname=~s/$host_cfg//g;  
                              $ttahosts{$nhost} = { $kname => $v };
                         } else {
                              $kname=~s/$array_cfg//g;  
                              $ttaprops{$itemName} = { $kname => $v };
                         }
                 }
                 if ( $is_host eq "1" ) { $nhost++; }
     }  
   
}


     &get_ttaprop($dir_path,$ext_file,"");
     &showrec_list(\%ttaprops);
     &showrec_list(\%ttahosts);

exit;
#     &showrec_list(\%h_hostsprop);
#     open(DebugLog, ">/tmp/hash_view");   
#     &show_hash(\%config,DebugLog);
#     &show_hash(\%h_hostsprop);
#     &showrec_list(\%config);
#     close (DebugLog);   
exit 1;
 %h_fname=%rv;
foreach $item (keys (%h_fname)) {
      push (@h_fname_list, $h_fname{$item} );
      %cfg=$h_fname{$item}->{'config'};
          print " ".$h_fname{$item}->{'name'}."-". $h_fname{$item}->{'path'}."\n";
                      foreach $k ( keys (%{$h_fname{$item}->{'config'}})) {
                            local $v=${$h_fname{$item}->{'config'}}{$k};
                           print " $k=$v  \n"
                      }

}
exit;
foreach $lk (@h_fname_list) {
                      print $lk->{'name'}." ";
                      print $lk->{'path'}."\n ";
                      foreach $k ( keys (%{$lk->{'config'}})) {
                            local $v=${$lk->{'config'}}{$k};
                           print " $k=$v  \n"
                      }
                }
 
