#!/usr/local/bin/python

import sys
import os

import isi.fs.siq as siq
from isi.fs.siq_upgrade import *

#Expecting at least 1 argument with the path to upgrade
if len(sys.argv) <= 1:
	print "Expecting 1 or more arguments"
	sys.exit(1)

siqu = siq.SyncIQUtils()

for i in range(1, len(sys.argv)):
	filename = sys.argv[i]
	if filename[-21:-14] == "report-" and filename[-4:] == ".xml":
		pid = filename[-54:-22]
		try:
			rfile, rdom = open_and_parse_xml(filename)
			report = parse_report(rdom, pid)
			gc_file = filename[:-3] + "gc"
			rgcfg = siqu.get_gconfig_report(gc_file)
			open_and_write_gc(rgcfg, report)
			os.unlink(filename)
		except Exception, exc:
			print "Exception while parsing %s: %s" % (filename, str(exc))
			print "*** Please run 'isi sync reports rebuild' to fix this issue."
			sys.exit(1)
	else:
		print "%s is not an xml file" % filename
		sys.exit(1)
	
