#!/usr/bin/perl

if ($#ARGV != 1) { die "Usage: convertAMPL_upit.pl <file.upit> <file.prec> ";}

#open file
$filename = $ARGV[0];

printf "data;";

printf STDERR  "Reading file $filename\n";
open (FH, "<", "$filename") || die "Can't open $filename: $!";
while(<FH>)
{
	chomp $_;
	my @col = split(/:/,$_);
	if ($col[0] eq 'NBLOCKS') { 
		printf "param NBLOCKS := $col[1];\n";
		$nb = $col[1];
	}
		if ($col[0] eq 'OBJECTIVE_FUNCTION')
		{
			print "param: OBJECTIVE_FUNCTION :=";
			for($i = 0 ; $i < $nb ; $i++)
			{
				$_ = <FH>;
				chomp $_;
				my @col = split(/ /,$_);
				print "\n$col[0] $col[1]";
			}
			printf ";\n"
		}

}

close(FH);

$filename = $ARGV[1];

printf STDERR  "Reading file $filename\n";
open (FH, "<", "$filename") || die "Can't open $filename: $!";

#printf("param: NPREC :=");
#for ($i = 0 ; $i < $nb ; $i++)
#{
#	$_ = <FH>;
#	chomp $_;
#	my @col = split(/ /,$_);
#	if ($col[0] != $i) { die "ERROR: row $i does not coincide (block $col[0]\n";}
#	$nprec[$i] = $col[1];
#	printf "\n$col[0] $col[1]"
#}
#printf ";\n";
#
### Rewind and re-read precedences
#seek FH, 0, SEEK_SET or die "Cannot seek on file $filename: $!";


for ($i = 0 ; $i < $nb ; $i++)
{
	$_ = <FH>;
	chomp $_;
	my @col = split(/ /,$_);
	if ($col[0] != $i) { die "ERROR: row $i does not coincide (block $col[0]\n";}
	printf("set PREC[$col[0]] :=");
	for ($j = 0 ; $j < $col[1] ; $j++)
	{
		printf " $col[2+$j]";
	}
	printf(";\n");
}



	


