import sys
import os
from profiller import profiller
from application import application

import signal
import subprocess
import time

import collections
import itertools
import sys
import os.path

from energy_parser import *

# we do not need clang parser for this operation
#from parser import parser
from transformer import transformer
import ConfigParser
from application.code_block import Code_block

if (len(sys.argv) < 2):
        print "Usage: python profiling.py <application>"
        exit()

#os.system("rm ./temp/*")

configParser = ConfigParser.RawConfigParser()
configParser.read('../config.txt')

rasp_ip = configParser.get('raspberry', 'ip')

os.system("cd ../ && "+configParser.get(sys.argv[1], 'build'))

new_application = application.Application(configParser.get(sys.argv[1], 'dir'), configParser.get(sys.argv[1], 'exec'), configParser.get(sys.argv[1], 'source'), configParser.get(sys.argv[1], 'build'))

print "Application initialized"

print "==========================================================================================="

pro = subprocess.Popen("./get-power.sh")
#execute program
time.sleep(0.5)
os.system("echo \"Start\" >> ./module-power-input.txt")
start = time.time()
os.system("cd ../ && "+configParser.get(sys.argv[1], 'exec'))
end = time.time()
os.system("echo \"End\" >> ./module-power-input.txt")

pro.kill()

print "time = "+str(end - start)+" secs"
energy_results()
