#!/usr/bin/perl
open(IN, "raw.csv");
open(TASK, ">tasks.planner");
open(ALLOC, ">allocations.planner");
open(COSTS, ">costs.csv");
$rate = 64.26;
$id = 0;
$total_cost = 0.0;
while (<IN>) {
    print;
    chomp;
    split/,/;
    print "$_[3] $_[0] $_[6]";
    $id++;
    $name = $_[3] . " (" . $_[0] . ")";
    $work = $_[6]*5*8*60*60;
    $contributed = $_[4];
    $scientist = $_[5];
    $total = $_[6];
    $FTE = $_[7];
    print "name = $name, work = $work\n";
    print TASK "<task id=\"$id\" name=\"$name\" note=\"\" work=\"$work\" duration=\"$work\" start=\"20091029T000000Z\" end=\"20091029T164500Z\" work-start=\"20091029T080000Z\" percent-complete=\"0\" priority=\"0\" type=\"normal\" scheduling=\"fixed-work\"/>\n";
    print "contributed = $contributed, scientist = $scientist, total = $total\n";
    if ($FTE > 0.0) {
	$units_contributed = 100*$FTE*($contributed/$total);
	$units_scientist = 100*$FTE*($scientist/$total);
    } else {
	$units_contributed = 100;
	$units_scientist = 100;
    }	
    if ($contributed > 0) {
	print ALLOC "<allocation task-id=\"$id\" resource-id=\"1\" units=\"$units_contributed\"/>\n";
    }
    if ($scientist > 0) {
	print ALLOC "<allocation task-id=\"$id\" resource-id=\"2\" units=\"$units_scientist\"/>\n"; 
    }
    $cost = $scientist*$rate*40.0;
    print COSTS "$id,$name,$cost\n";
    $total_cost += $cost;
}
print "total_cost = $total_cost\n";
exit;
