#!/usr/bin/env python
import sys
from xml.dom import minidom

def process_tasks(task_set, tset):
	for task in task_set:
		tset[task.getAttribute("id")] = float(task.getAttribute("work"))
		subtask_set = task.getElementsByTagName("task")
		if len(subtask_set) > 0:
			process_tasks(subtask_set, tset)

if len(sys.argv) != 2:
	    sys.exit("Must provide the planner file name")

tset={}
dom = minidom.parse(sys.argv[1])
tasks_set = dom.getElementsByTagName("tasks")
for tasks in tasks_set:
	task_set = tasks.getElementsByTagName("task")
	process_tasks(task_set, tset)

res_cost = {}
res_rate = {}
res_name = {}
resources_set = dom.getElementsByTagName("resources")
for resources in resources_set:
	resource_set = resources.getElementsByTagName("resource")
	for resource in resource_set:
		res_name[resource.getAttribute('id')] = resource.getAttribute('name')
		res_rate[resource.getAttribute('id')] = float(resource.getAttribute('std-rate'))
		res_cost[resource.getAttribute('id')] = 0.0

allocs_set = dom.getElementsByTagName("allocations")
for allocations in allocs_set:
	alloc1s_set = allocations.getElementsByTagName("allocation")
	for alloc1 in alloc1s_set:
		tid=alloc1.getAttribute("task-id")
		rid=alloc1.getAttribute("resource-id")
		porc=float(alloc1.getAttribute("units"))
		# calculations
		work_per_resource = tset[tid] * porc / 360000.0
		rate_per_resource = res_rate[rid]
		cost_per_resource = work_per_resource * rate_per_resource
		res_cost[rid] = res_cost[rid] + cost_per_resource

total = 0.0
for rid in res_name.keys():
	print rid, res_name[rid], "\t\t", res_cost[rid]
	total = total + res_cost[rid]
print "Total: \t\t", total

