#!/usr/bin/perl
#
# This program has been created by ASKARI AZIKIN (easkazi)
# Phone +62 21 7692222 (ext 7305)
# Mobile +62811892921
# Copyleft askari.azikin\@ericsson.com. 2009 - 2010
# www.ericsson.com 
#
# REVISION HISTORY:
#       10-Nov-2009	easkazi		Initial draft
#       15-Nov-2009	easkazi		Reduce temporary file created and replaced with @array Temp
#	01-Dec-2009     easkazi		Add MISC1 parameter & Trunk group name & delete E1 number
#       19-Jan-2010     easkazi         Add CIC Number (Formula: MISC1 % 32)
#


use strict;
#use warnings;		# Used in test mode only
#use diagnostics;	# Used in test mode only

#######\@FUNCTION########

sub trunk_group{
   my ($EXDEP, $NTCOP)= @_; 
                            
   my $trunk;
   my @temp;
   my @sorted;
   my @bump;
   my @data;
   my @output;
   my @intermediate;
   
   my %HoA;
   my %seen;
   
   print "Please enter name of trunk group will be searched(e.g TBPGY2O or TMUP5AO):";
   chomp($trunk = <STDIN>); 
   
   open (R, $EXDEP) or die ("Cannot open file $EXDEP");
 
   foreach my $R(<R>){
         chomp($R);
   	 if ($R =~ /($trunk)/){
   	 	my ($k, $v, $s) = split (/\s+/,$R); 
         	push @{$HoA{'$R'}{$k}}, $v."\t"."\t".$s; 
         }                                                                    
   }                                                                          
                                                                              
   close (R);
   
   open (R, $EXDEP) or die ("Cannot open file $EXDEP");
   
   foreach my $P(<R>){
   	 chomp($P);
   	 if ($P =~ /C   C   C/ && $P !~ /C   C   C   C/){
   	 	my ($k, $v) = split (/\s+/,$P);
   	 	push @{$HoA{'$P'}{$k}},$v;  
   	 }
   	  
   }
   close (R);
   
         
   foreach my $result(keys %{$HoA{'$P'}}){ 
        if (exists $HoA{'$R'}{$result}){
            foreach my $classR(@{$HoA{'$P'}{$result}}){ 
                 foreach my $classP(@{$HoA{'$R'}{$result}}){ 
                     push (@temp, $result."\t".$classR."\t".$classP."\n"); 
                                                                           
                 }
            }
        } 
           
   }
   
   @sorted = map  {$_->[0]}                    
             sort {$a->[2] <=> $b->[2]}         
             map  {[$_, split(/\s+/,$_)]}@temp; 
 
  
  
   foreach my $counted(@sorted){
        @temp = split(/\s+/,$counted);
        $seen{$temp[3]}++;              
        next if $seen{$temp[3]} > 1;    
        @bump = join ("\t",@temp, "\n");
        push (@data, @bump);            
                                        
   }
   
   foreach my $J(@data){
   	chomp($J);
   	my ($k, $v, $s, $t) = split (/\s+/,$J); 
        my $constants =32;
        my $z = int($v/$constants); 
        push @{$HoA{'$J'}{$t}},$k."\t"."\t".$v."\t"."\t".$z."\t"."\t".$s;
   }
    

   
   open (R, $NTCOP) or die ("Cannot open file $NTCOP");
   foreach my $L(<R>){
   	chomp ($L);
   	if ($L =~ /RTDMA-/){
   	  my ($k, $v, $s, $t, $y) = split(/\s+/,$L);
   	  push @{$HoA{'$L'}{$k}},$s."\t"."\t".$t."\t"."\t".$y;
   	 
   	}
   }
   close (R);
   
   foreach my $final(keys %{$HoA{'$L'}}){  
        if (exists $HoA{'$J'}{$final}){    
            foreach my $classY(@{$HoA{'$L'}{$final}}){ 
                 foreach my $classT(@{$HoA{'$J'}{$final}}){    
                     push (@intermediate, $final."\t".$classT."\t"."\t".$classY."\n"); 
                 }
            }
        } 
           
   }
  
  @output = map  {$_->[0]}                    
            sort {$a->[3] <=> $b->[3]}        
            map  {[$_, split(/\s+/,$_)]}@intermediate; 
 
 
  my @results;
  my $done = "done.txt";
  
  open (R, ">$done") or die ("can't create $done file");
 
  push (@results,"  SNT             DEV                 MISC1 	       CIC	      TRUNK_GROUP           DEVS                   EXTP                   MG\n"); 
  push (@results,"-------------------------------------------------------------------------------------------------------------------------------------------------\n");
  push (@results,@output);
  print R @results; 
  print "OUTPUT FILE WRITTEN TO '$done'\n";
  close (R);
}

  
#####\@MAIN########

if ($#ARGV != 1){ 
    print "USAGE: $0 <EXDEP> <NTCOP> \n"; 
    exit;
} 
else {
    my ($EXDEP, $NTCOP)= @ARGV;
    &trunk_group($EXDEP, $NTCOP);
          
}

__END__

