
use Tk;
use Tk::Text;
use Tk::Scrollbar;
#use Tk::NoteBook;
use XML::LibXML;
use XML::LibXML::XPathContext;

@level = ("carrerabuild1.proj",
	"carrerabuild2.proj",
	"carrerabuild3.proj",
	"carrerabuild4.proj",
	"carrerabuild5.proj",
	"carrerabuild6.proj",
	"carrerabuild7.proj",
	"carrerabuild8.proj",
	"carrerabuild9.proj",
	"carrerabuild10.proj",
	"carrerabuild11.proj",
	"carrerabuild12.proj",
	"carrerabuild13.proj",
	"carrerabuild14.proj",
	"carrerabuild15.proj",
	"carrerabuild16.proj",
	"carrerabuild17.proj",
	"carrerabuild18.proj",
	"carrerabuild19.proj",
	"carrerabuild20.proj",
	"carrerabuild21.proj",
	"carrerabuild22.proj",
	"carrerabuild23.proj",
	"carrerabuild24.proj",
	"carrerabuild25.proj",
	"carrerabuild26.proj",
	"carrerabuild27.proj",
	"carrerabuild28.proj",
	"carrerabuild29.proj",
	"carrerabuild30.proj",
	"carrerabuild31.proj",
	"carrerabuild32.proj",
	"carrerabuild33.proj",
	"carrerabuild34.proj",
	"carrerabuild35.proj",
	"carrerabuild36.proj",
	"carrerabuild37.proj",
	"carrerabuild38.proj",
	"carrerabuild39.proj",
	);
	
if (@ARGV)
{
 
 $help = $ARGV[0];
 
if ($help =~ m/(\/\?)$/)
{
  print "\t\t********************************\n";
  print "\t\t*************:OPTIONS:**********\n";
  print "Enter the path of the project : \n\tYou need to enter here the absolute path of the C-Sharp project.\n\t Once entered, click on Search button\n\tTool will find out all the dependent assemblies and find out the exact level in which given C-Sharp project can be added\n\tOutput will be displayed in the scrolled text window\n\n NOTE: PLEASE DONT SEARCH MORE THAN ONE PROJECT AT A GIVEN INSTANCE. START FRESH INSTANCE TO SEARCH NEXT PROJECT\n";
  
}
else 
{
 print "Type : DependencyLevelFinder.exe /? to get Usage help\n";
}
}

else
{
 
$s1=0;
$mw = MainWindow->new();
$mw->title("Project Level Finder");
#$mw->geometry( "800x400" );
$f = $mw->Frame->pack;

#$mw->Frame(-background => 'red')->pack(-ipadx => 100, -ipadx => 100, -fill => "y");
#$book = $mw->NoteBook()->pack( -fill=>'both', -expand=>1 );

#$book->configure(-backpagecolor=>'red');

#$tab1 = $book->add( "Sheet 1", -label=>"Search");
#$tab1->Frame(-backpagecolor => 'red')->pack(-ipadx => 100, -ipadx => 100, -fill => "y");


$f->Label( -text=>"Enter the path of project" )->pack (-anchor=>'n', -side=>'top');
$txt_entry = $f->Entry(-background => "white", -foreground => "red")->pack(-side => "top" , -ipady => 5, -ipadx => 200);

$f_sr = $f->Button( -text=>'Search', -command=>\&getSearch )->pack( -expand=>1,-anchor=> 'n');


$t = $f->Scrolled('Text', -scrollbars=>'w')->pack (-expand=>1, -anchor=>'n');

#$textWidget=$f->Text(
#          -height=>'20',
#          -width=>'10'
#          )->pack;
#$textWidget=$f->Scrolled(
#          'Text',
#          -scrollbars=>'w',
#          
#         )->pack;

$f_up = $f->Button( -text=>'Update', -state=>'disabled', -command=>\&getupdate )->pack(-expand=>1,-anchor=> 's', -side=>'bottom');

MainLoop;

sub abc
{
 $pop = localtime();
 return "$pop\n";
 }
 

sub getSearch {

   $csproj_text = $txt_entry->get();
   chomp($csproj_text);
  
 if (-e $csproj_text)
 {
 
 if ($csproj_text =~ m/(.+).csproj$/)
 {
   $csproj_text =~ m/(.+)\\(C|c)onvergence/;
   #print "$csproj_text\n";
   #print "$1\n";
   $csproj_text_root = $1;
   
   #print "$csproj_text\n";

 # print "Searching for dependent assemblies \n";
  $t->insert("end",&abc);
  $t->insert("end","****************************************\n");
  open FILE, "$csproj_text" or die $!;
  while (<FILE>)
  {
   if ($_ =~ m/HintPath/)
   {
    if ($_ =~ m/Symantec/)
     {
       $s1++;
       $ts = ($_ =~ m/(Symantec.+).dll/);
       push (@assem, $1);
     }  
   }
 }
  close FILE;
  if ($s1 == 0)
  { 
      $warn = $mw->messageBox(
                     -title   => 'Error',
                     -message => "Given Project does not contain any dependent assembly.This should go in First Level",
                     -type    => 'Ok',
                -icon    => 'question',
        );
    exit 0;
 } 
   
  
  $d = 0;
  $e = 0;
  $fin = 0;
  $t->insert("end", "Given Project is dependent on following assmblies\n");
  #$t->insert("end", "@assem\n");
   $t->insert("end", join( "\n", @assem, "\n" ));
  #print "@assem\n";
  
  foreach $as (@assem)
  {
   chomp($as);
     $t->insert("end", "Searching $as assembly in all levels\n");
   #print "Searching $as assembly in all levels\n";
   
   foreach $lv (@level)
        {
          chomp($lv);
          
          #$XML = "C:\\ParallelBuild\\$lv";
          $XML = "$csproj_text_root\\convergence\\trunk\\src\\ParallelBuild\\$lv";
             
          $document = XML::LibXML::XPathContext->new(XML::LibXML->load_xml(location => $XML)->documentElement);
              
          $document->registerNs(p =>
          'http://schemas.microsoft.com/developer/msbuild/2003');
             
          @include = map $_->value(),
            $document->findnodes('//p:ItemGroup/p:BuildProject/@Include');
          
          
          use Data::Dumper;
          #print Data::Dumper->Dump([\@include],[qw/*include/]);
          
          #print "@include\n";
    foreach $in (@include)
     {
       #print "$in\n";
       #$in =~ s/\$\(workspace\)/c:\\P4\\Carrera/;
       $in =~ s/\$\(workspace\)/$csproj_text_root/;
      # print "$in\n";
        open FILE, "$in" or die $!;
        while (<FILE>)
        {
       # print "pappu\n";
          if ($_ =~ m/<RootNamespace>/)
         {
           $_ =~ m/>(.+)</;
           $c = $1;
           if ($c eq $as)
           {
           # print "$1\n";
           $dat{RootNamespace} = $c;
           $d = 1;
           }
         # print "pappu\n";
         # print "$as assembly found in $lv level\n";
         }
         
        if ($_ =~ m/<AssemblyName>/)
         {
           $_ =~ m/>(.+)</;
           $c = $1;
           if ($c eq $as)
           {
           # print "$1\n";
           $dat{AssemblyName} = $c;
            $e = 1;
           }
         # print "pappu\n";
         # print "$as assembly found in $lv level\n";
        
         }
                  
        # print "$d\n";
        # print "$e\n";
        
    }
   close FILE;
    if ($d == 1 || $e == 1)
            {
            foreach $key ( keys %dat )
   	 	{
   	 	  if ($as eq $dat{$key})
   	 	  {
   	 	   $fin++;
   	 	 # print "$fin\n"; 
   	 	 $t->insert("end", "key: $key, value: $dat{$key}\n");
   	 	 # print "key: $key, value: $dat{$key}\n";
   	          }
   	          
   	        }
   	       if ($fin > 0)
   	       {
   	        $t->insert("end", "$as assembly found in level $lv\n Project Name is : $in\n");
   	         #print "$as assembly found in level $lv\n Project Name is : $in\n";
   	         push (@fin_arr, "$lv\n");
   	         $t->insert("end","****************************************\n");
   	         $fin = 0;
   	         $xa = 0;
   	         $ya = 0;
   	         
         }
         $d = 0;
         $e = 0;
       } 
       
      }
    }
  } 
 }
 
  else {

$ftp_warn = $mw->messageBox(
           -title   => 'Error',
           -message => "Given Path does not seems to be C-Sharp project file. Please try again.",
           -type    => 'Ok',
      -icon    => 'question',
  );
  
  $xa = 1;
 } 
} 
   
   else
   {
      $ftp_warn = $mw->messageBox(
           -title   => 'Error',
           -message => "Given Path does not exist, do you wish to continue?",
           -type    => 'YesNo',
      -icon    => 'question',
  );
   $ya = 1;  
  } 
  
  if ($ftp_warn eq "No")
  {
   exit 0;
   }
  if ($ftp_warn eq "Yes")
  {
   $txt_entry->delete('0.0','end');
  } 
   
  
  foreach (@fin_arr)
  {
   $_ =~ m/(\d+)\.proj/;
   push (@fin_arr1, "$1");
   }
   
   @all_level = sort (@fin_arr1);
   $lvl = pop(@all_level);
    $lvl++;
    
  
  if (($xa == 0) && ($ya == 0))
  {
  $f_sr->configure( -state => 'disabled' );
  $t->insert("end", "***************************SUMMARY***************************\n");
  $t->insert("end", "Given C-Sharp Project needs to be added in Level $lvl \n");
  
  $t->insert("end", "Press the 'Update' Button to add the given project in level $lvl \n");
  #$t->see('end');
  $f_up->configure( -state => 'normal' );
  
  }
}

}

sub getupdate
{
$t->see('end');
$but_ok = 0;
$f_up->configure( -state => 'disabled' );
$ftp_warn = $mw->messageBox(
           -title   => 'Info',
           -message => "Do you wish to continue?",
           -type    => 'YesNo',
      -icon    => 'question',
  );
 
 if ($ftp_warn eq "Yes")
 {
   
   $mw1 = MainWindow->new;
   $mw1->geometry( "200x100" );
   $mw1->title("Client Workspace");
   $mw1->Label(
           -text => "Enter the Perforce client name:"
       )->pack(-anchor=>'center');
       
   $mw1->Entry(
        -textvariable => \$text_clnt
    )->pack(-anchor=>'center');
    
  #$text_clnt_fin = $text_clnt;
  $mw1->Button(
            -text => "OK",
            -command => \&pf_op
  )->pack(-anchor=>'center');
  
  
  } 
  
 if ($ftp_warn eq "No")
  {
  exit 1;
  }
 }
 
 

sub pf_op
    {
       $t->insert("end","You entered: $text_clnt \n");
       $mw1->withdraw();
       
      
    }