$IO_PROPERTY = "SSTL2";
$old = 1;
$delete = 1;


($filename) = @ARGV;
if (($filename eq "") || ($filename eq "-h"))
{
	print "\n\nUsage: perl edifmod.pl <edif_filename>\n\n";
	print "Created by : Anto Sutrisno\n";
        print "On March 20, 2003\n";
	die "Asta Lavista Baby...\n";


}


open (MYFILE, $filename) || die "Cannot open filename $filename. Program Quits\n";

$new_file = "$filename.new";
open (NEWFILE, ">$new_file") || die "Cannot open filename $new_file. Program Quits\n";

@port_array;
$count = 0;
%port_hash;

while(<MYFILE>)
{
	chomp($_);
	$count++;
	if ($_ =~ /port\b/)
	{
		$contain_port = "true";
		$port_count = $count;
		if ($_ =~ /array/)
		{
			@name  = split(/\s+/, $_);
			#print NEWFILE "bus: $name[4]\n";
			$port_name = $name[4];
		}
		else 
		{
		 	@name = split(/\s+/, $_);
			#print NEWFILE "signal: $name[2]\n";
			$port_name = $name[2];
		} 
	}	
	else
	{
		$next_count = $port_count + 1;
		$contain_port = "false";
		if (($_ =~ /property/) && ($_ =~ /LEVELMODE/) && ($_ =~ /$IO_PROPERTY/))
		{
			if (($count == $next_count) && ($port_name ne ""))
			{
				push(@port_array, $port_name);
			}
		}
	}	
		
}

foreach $item (@port_array)
{
	print "$item\n";

}
close(MYFILE);

open (MYFILE2, $filename) || die "Cannot open file $filename\n";

$sub_line = "\(property LEVELMODE \(string \"$IO_PROPERTY\"\)\)";
print "$sub_line\n";

while(<MYFILE2>)
{
	chomp($_);
	$line = $_;
	
	if (($line =~ /instance\b/))
	{
		
		if ($line =~ /rename/)
		{
			foreach $item (@port_array)
			{
				$item1 = "$item" . "_bdbuf";
				$item2 = "$item" . "_obuf";
				$item3 = "$item" . "_ibuf";
				$item4 = "$item" . "_";

				if (($line =~ /$item1/) || ($line =~ /$item2/) || ($line =~ /$item3/) || ($line =~ /$item4/))
				{
					$_ = $line;
					
					s/(\)\)\))/$1 $sub_line/g;
					$line = $_;
					#print "$line\n";
				}
			}			
		}
		else
		{
			foreach $item (@port_array)
			{
				$item1 = "$item" . "_bdbuf";
				$item2 = "$item" . "_obuf";
				$item3 = "$item" . "_ibuf";

				if (($line =~ /$item1/) || ($line =~ /$item2/) || ($line =~ /$item3/))
				{
					
					$_ = $line;
					
					s/(\)\)\))/$1 $sub_line/g;
					$line = $_;
					#print "$line\n";
			
				}
			}
		}
	}
	print NEWFILE "$line\n";	
}
close (MYFILE2);
close (NEWFILE);

if ($old = 1)
{
    system ("copy $filename $filename.old");
    system ("copy $new_file $filename");
    system ("del $new_file");
}

