#!/usr/bin/perl -w

sub generateDEF
{
    # parameter : (original DEF, .pl file from Capo, final DEF)
    open(ORIG,"< $_[0]") || die "Can't open $_[0] for input\n";
    open(PLACED,"< $_[1]") || die "Can't open $_[1] for input\n";
    open(OUT,"> $_[2]") || die "Can't open $_[2] for output\n";

    $lineNum = -1;
    while ($line = <ORIG>)
    {
	++$lineNum;
	print OUT $line;
	if ($line =~ /^\s*COMPONENTS/)
	  {last;}
    }
    
    while ($line = <PLACED>)
    {
	if($line =~ /#/)
	   { next; }
        if($line =~ /UCLA/)
           { next; }
	if($line =~ /[a-zA-Z0-9]+/)
	   { 
	       chop $line;
	       @words = split " ", $line;
	       $nodeName = $words[0];
	       $xloc = $words[1];
	       $yloc = $words[2];
	       $orient = $words[4];

	       $XLOCS{$nodeName} = $xloc;
	       $YLOCS{$nodeName} = $yloc;
	       $ORIENTS{$nodeName} = $orient;
	   }
	   
    }

    while ($line = <ORIG>)
    {
	++$lineNum;
	if($line =~ /#/)
	   { next; }
	if($line =~ /[a-zA-Z0-9]+/)
	   { 
	       if ($line =~ /^\s*END COMPONENTS/)
	       {
		   print OUT $line;
		   last;
	       }
	       @words = split " ", $line;
	       $numWords = @words + 0;
	       $nodeName = $words[1];
	       $spacing = "\t";

	       if($numWords == 4 && $words[3] eq ';')
	       {
		   print OUT $words[0];
		   print OUT $spacing;
		   print OUT $words[1];
		   print OUT $spacing;
		   print OUT $words[2];
		   print OUT $spacing;
		   print OUT "+\tPLACED (\t";
		   if(exists($XLOCS{$nodeName})) 
		   {
		       $xloc = $XLOCS{$nodeName};
		       $yloc = $YLOCS{$nodeName};
		       $orient = $ORIENTS{$nodeName};
		   }
		   else
		   {
		       $xloc = 0;
		       $yloc = 0;
		       $orient = "N";
		   }
		   print OUT $xloc;
		   print OUT $spacing;
		   print OUT $yloc;
		   print OUT $spacing;
		   print OUT ")";
		   print OUT $spacing;
		   print OUT $orient;
		   print OUT $spacing;
		   print OUT ";";
		   print OUT "\n";		   
		   
	       }
	       elsif($numWords == 11 && $words[10] eq ';')
	       {
		   print OUT $words[0];
		   print OUT $spacing;
		   print OUT $words[1];
		   print OUT $spacing;
		   print OUT $words[2];
		   print OUT $spacing;
		   print OUT $words[3];
		   print OUT $spacing;
		   print OUT $words[4];
		   print OUT $spacing;
		   print OUT $words[5];
		   print OUT $spacing;
		   if(exists($XLOCS{$nodeName})) 
		   {
		       $xloc = $XLOCS{$nodeName};
		       $yloc = $YLOCS{$nodeName};
		       $orient = $ORIENTS{$nodeName};
		   }
		   else
		   {
		       $xloc = $words[6];
		       $yloc = $words[7];
		       $orient = $words[9];
		   }
		   print OUT $xloc;
		   print OUT $spacing;
		   print OUT $yloc;
		   print OUT $spacing;
		   print OUT $words[8];
		   print OUT $spacing;
		   print OUT $orient;
		   print OUT $spacing;
		   print OUT $words[10];
		   print OUT "\n";	       
	       }
	       else  #two line special case
	       {
		   ++$lineNum;
		   $nextLine = <ORIG>;
		   @nextWords = split " ", $nextLine;
		   $numNextWords = @nextWords + 0;
		   $totalWords = $numNextWords + $numWords;
		   if($totalWords != 11)
		   {
		       print "ERROR in lineNum ";
		       print $lineNum;
		       print "\n";
		       exit 0;
		   }
		   @words = (@words, @nextWords);

		   print OUT $words[0];
		   print OUT $spacing;
		   print OUT $words[1];
		   print OUT $spacing;
		   print OUT $words[2];
		   print OUT $spacing;
		   print OUT $words[3];
		   print OUT $spacing;
		   print OUT $words[4];
		   print OUT $spacing;
		   print OUT $words[5];
		   print OUT $spacing;
		   if(exists($XLOCS{$nodeName})) 
		   {
		       $xloc = $XLOCS{$nodeName};
		       $yloc = $YLOCS{$nodeName};
		       $orient = $ORIENTS{$nodeName};
		   }
		   else
		   {
		       $xloc = $words[6];
		       $yloc = $words[7];
		       $orient = $words[9];
		   }
		   print OUT $xloc;
		   print OUT $spacing;
		   print OUT $yloc;
		   print OUT $spacing;
		   print OUT $words[8];
		   print OUT $spacing;
		   print OUT $orient;
		   print OUT $spacing;
		   print OUT $words[10];
		   print OUT "\n";
	       }
	   }
	else{print OUT $line;}
    }    

    print OUT  while <ORIG> ;

    close(ORIG);
    close(PLACED);
    close(OUT);    
}

die "Use: convertPlToDef orig.def placed.pl complete.def\n" if $#ARGV<0;

generateDEF($ARGV[0], $ARGV[1], $ARGV[2]);
