#!/usr/bin/perl

 $fileName="tta.server";
 $cfg_file="hosts_list";
# $fileName="tta.applications";
# $cfg_file="app_list";

 $ens_root="o=SINPLEX";
 $maxsess="9999";
 $portalive="139";
 @data=();
 open(FILENAME, $fileName);
         $nlins=1;
         while(<FILENAME>) {
              @line=split(/--/, $_,-1);
              $i=1;
              while ( $i< $#line+1 ) {
                 @attLine=split(/ /, $line[$i],-1);
#                 print "$attLine[0] = $attLine[1] \n";
                 $attrName=$attLine[0];
                 $attrValue=$line[$i];
                 $attrValue=~s/$attrName //;
                 $attrValue=~s/\r|\n//g;
                 $data[$nlins]{$attrName}=$attrValue;
                 $i++;
              }
              $nlins++;
         }
 close (FILENAME);
 open(STTAFILE, ">>$cfg_file");
 $lin=1;
 while ( $lin < $nlins ) {
   $line_stta="";
   $alias=$data[$lin]{'description'};
   $alias=~s/ $//g;
   $alias=~s/"//g;
   $descrip=$alias;
   $alias=~s/ /_/g;
   $alias=~ tr/A-Z/a-z/;
   $ens=$data[$lin]{'name'};
   $ens=~s/$ens_root\///g;
   $ens=~s/" //g;
   $ens=~s/"//g;
   if ( $fileName =~ /server/ ) {
     @line=split(/\//, $ens,-1);
     $dn="";
     for ( $i=$#line ; $i >= 0 ; $i-- ) {
         if ( $i < $#line ) { 
             $dn .= ",";
             $dn .= $line[$i]; 
         } else {
             $dn .= $line[$i]; 
         }
     }
     $dn =~ s/cn=/ou=/g;
     $grp_alias="servidores";
     $ipaddress=$data[$lin]{'address'};
     @line=split(/\./, $ipaddress,-1);
     $objname=$line[0];
     $lin_stta=$alias."::".$objname."::".$grp_alias."::"."$dn"."::"."$ens"."::".$ipaddress."::".$maxsess."::".$portalive."::".$descrip."::";
   } elsif ( $fileName =~ /application/ ) {
     @line=split(/\//, $ens,-1);
     $dn="";
     for ( $i=$#line ; $i >= 0 ; $i-- ) {
         if ( $i < $#line ) { 
              $dn .= ","; 
              $dn .= $line[$i]; 
         } else {
              $objname=$line[$#line];
              $objname=~s/cn=/ou=/g;
              $dn=$objname;
              $objname=~s/ou=//g;
        }
     }
     $grp_alias="apps";
     $lin_stta=$alias."::".$objname."::".$grp_alias."::".$dn."::".$ens."::".$maxsess."::".$descrip."::";
   }
#    foreach $item ( keys (%{$data[$lin]})) {
#      print "$lin: ".$item." = ".$data[$lin]{$item}."\n"; 
#    }
#    print "--- $data[$lin]{'name'} \n";
    print STTAFILE "$lin_stta\n";
#    print "$lin --- > $lin_stta\n";
    $lin++;
 }
 close (STTAFILE);

