#!/usr/bin/perl

# This script will take in row size and column size and characteristic
# of a field as input (row <= col). It will use the maple proc rmatroid
# to find all bases of the matroid defined by a rxc randomly generated integral
# matrix. These bases are also converted to their matroid polytope vertices.
# Then these vertices are entered into a cddr+ file, with the extra vertex
# of the origin. cdd+ then computes the facet description. The output file
# is then converted for ehrhart input. ehrhart computes the rational
# function. Maple is then called on again to simplify this expression so
# one can see the h*-polynomial.

if(@ARGV != 3){
    print("Usage: rmatroid.pl <rows> <cols> <char>\n");
    exit(1);
}

$rows = $ARGV[0];
$cols = $ARGV[1];
$fchar = $ARGV[2];

# print ("$rows $cols $fchar\n");

print ("Generating random matrix and finding bases\n");
`rm tempmap.map`;
open (TMAPLE, ">tempmap.map") || die ("Couldn't open file.\n");

print TMAPLE ("read \"rmatroid\":\n");
print TMAPLE ("randomize():\n");
print TMAPLE ("rmatroid($rows,$cols,$fchar);\n");

close (TMAPLE);

$mapRes = `maple tempmap.map`;

$mapRes =~ /Number of bases: (\d+)/;
$numBases = $1;
# print ("numb bas $numBases\n");

#  print ("$mapRes");

print ("Writing to cddr+ ready file\n");
$numBases++;
$cols++;
$extOutput = "V-representation\nbegin\n$numBases $cols integer\n";
$numBases--;
$cols--;


$cont = $mapRes;
while ($cont =~ /(\[(\d+,\s*)*\s*\d+\])/ ) {
    $numPerms++;
    $cont = $';
    $tempLine = $1;

    $newLine = "";
    while ($tempLine =~ /(\d+)(,|\])/gc) {
        $newLine = $newLine . "$1 ";
    }
    $extOutput = $extOutput . "1 $newLine\n";
}

$extOutput = $extOutput . "1 ";

$i = 0;
while ($i <= $cols-1){
    $extOutput = $extOutput . "0 ";
    $i++;
}

$extOutput = $extOutput . "\nend";


`rm tempcdd.ext`;
open (TEMPCDD, ">tempcdd.ext") || die ("Couldn't open file\n");

print TEMPCDD ("$extOutput");

close TEMPCDD;

print ("Runnind cddr+\n");
`cddr+ tempcdd.ext`;

print ("Converting .ine file to latte ready file\n");
`cat tempcdd.ine | inetolat.pl > tempcdd.lat`;

print ("Calling latte(ehrhart)\n");
`ehrhart tempcdd.lat`;

`rm tempmap.map`;
open (TMAPLE, ">tempmap.map") || die ("Couldn't open file.\n");

print TMAPLE ("read \"tempcdd.lat.rat\":\n");
print TMAPLE ("y := simplify(x);\n");
print TMAPLE ("read \"unimodal\":\n");
#print TMAPLE ("printf(\"h-vector is \");\n");
#print TMAPLE ("if (enimodal(numer(y)) = 1 ) then printf(\"UNIMODAL\\n\"); else printf(\"NOT UNIMODAL(counterexample!)\\n\"); end if:\n");
print TMAPLE ("unimodal(numer(y));\n");
print TMAPLE ("save y, \"tempcdd.lat.rat.simp\";\n");

close (TMAPLE);

print("Simplifying rational function with maple\n");
$mapOutput = `maple tempmap.map`;

print("$mapOutput"); 

if ($mapOutput =~ /(UNIMODAL)/) {
    $hvec = $1;
}

if ($mapOutput =~ /(counterexample)/) {
    $hvec = $1;
}

open (RMLOG, ">>rmatroid.log") || die ("Couldn't open rmatroid.log\n");

print RMLOG ("$mapRes");
#print RMLOG ("$mapOutput");
print RMLOG ("$hvec\n");

close (RMLOG);


open (RMLOGSHORT, ">>rmatroid.short.log") || die ("Couldn't open rmatroid.short.log\n");

print RMLOGSHORT ("$hvec\n");

close (RMLOGSHORT);
