#!/usr/bin/perl -w
#
# Copyleft @2009 EID/OI/XR Askari Azikin, E-mail: askari.azikin@ericsson.com
# This perl script just working for BSC trunk group (MRALT) and MSC-2G trunk group (UPDR)
# And still not working for BICC trunk group - On Progress
# Last Updated - 28 October 2009
#
use strict;

#####Usage Information#####
if (@ARGV && $ARGV[0] eq '-h') {
        print "Usage: ./pcmlist.pl \n";
        exit;
}

my ($trunk, @sorted);
my ($infile, $outfile, $nextfile, $temp, $temp1, $temp2) = qw/ exdep.txt output.txt nextfile.txt temp.txt temp1.txt temp2.txt/;

print "Please enter name of trunk group will be searched(e.g TBPGY2O or TMUP5AO):";
chomp($trunk = <STDIN>);

open (JX, "$infile" ) or die $!; 
open (JXL, ">$outfile" ) or die $!;
open (JL, ">$nextfile" ) or die $!;
 
                     

my @line = <JX>; #Read file into array
foreach (@line) {
    if ($_ =~ /($trunk)/) { 
       my @ad = split; 
       print JXL ($ad[0]."  ".$ad[1]."  ".$ad[2]."\n"); 
    }  
}
close JXL;  

foreach (@line) {
    if ($_ =~ /C   C   C/ && $_ !~ /C   C   C   C/) { 
       my @dx = split;  
       print JL ($dx[1]."  ".$dx[0]."\n"); 
    }
}
close JX;
close JL;
#------------------------#

my @idd; # input array for data.txt
my %hash;

open (JL, "<$nextfile") or die $!;
while (<JL>){
   chomp;
   my @idd = split (" ",$_);
   $hash{$idd[1]}=$_;   
}
close JL;


open (JXL, "<$outfile") or die $!;
open (JXC, ">$temp") or die $!;

while(<JXL>){ 
   my @idd = split ("  ",$_);
   if (exists ($hash{$idd[0]})) {  
      print JXC ($hash{$idd[0]}." ".$idd[1]." ".$idd[2]); 
   }
}

close JXL;
close JXC;

open (JXC, "<$temp") or die $!;
open (JXH, ">$temp1") or die $!;

my @ax = <JXC>;

print JXH @sorted = map {$_->[0]}   
             sort {$a->[1] <=> $b->[1]} 
             map {[$_, split (" ",$_)]} @ax;
close JXC;
close JXH;

open (JXH, "<$temp1") or die $!;
open (JXM, ">$temp2") or die $!;
#----NEXT JOB---# 

my @adz;

while (<JXH>){
    my @adx = split;
    push (@adz, $adx[3]."\n"); 
}
print JXM @adz; 
close JXH;
close JXM;

my ($temp3) = qw /temp3.txt/;

open (JXM, "<$temp2") or die $!;
open (JXF, ">$temp3") or die $!;

my %seen;

while (<JXM>){
      print JXF if ! $seen{$_}; 
      $seen{$_} = 1;
}

close JXM;
close JXF;

my ($temp4) = qw /temp4.txt/;

open (JXF, "<$temp3") or die $!;
open (JXN, ">$temp4") or die $!;

my $count = "0"; # start with counted
while (my $line = <JXF>){
      $count++;
      print JXN "$count $line";      
}
close JXF;
close JXN;


open (JXN, "<$temp4") or die $!;

my @dix ;
my %data;

while (<JXN>){
      chomp;
      my @dix = split(" ",$_);
      $data{$dix[1]} = $_; 
}
close JXN;

my ($file, $temp5) = qw /ntcop.txt temp5.txt/;

open (JL, "<$file") or die $!;
open (JLX, ">$temp5") or die $!;

my @x = <JL>; 
foreach (@x) {
      if ($_ =~ /RTDMA-/) {
         my @dix = split(" ",$_);
           if (exists $data{$dix[0]}){ 
              print JLX ($data{$dix[0]}." ".$dix[2]." ".$dix[3]." ".$dix[4]."\n"); 
           }
      }
   }
close JL;
close JLX;

my ($temp6) = qw /temp6.txt/;

open (JXL, "<$temp5") or die $!;
open (JXH, ">$temp6") or die $!;

my @au = <JXL>;

print JXH @sorted = map {$_->[0]}   
             sort {$a->[1] <=> $b->[1]} 
             map {[$_, split (" ",$_)]} @au;
close JXL;
close JXH;

my ($temp7) = qw /RESULT.TXT/;

open (JXH, "<$temp6") or die $!;
open (JXO, ">$temp7") or die $!;

my @rak = <JXH>;
my @result;

push (@result,"E1          SNT         DEV       EXTP      MG \n");
push (@result,"-----------------------------------------------\n");
push (@result,@rak);
print JXO @result; 
print "OUTPUT FILE WRITTEN TO '$temp7'\n\n";

close JXH;
close JXO;

#---END PROGRAM-----#







