package driver;

import pta.ContextSensPTA;
import pta.PTA;
import pta.SumContextSensPTA;
import pta.SumPTA;
import summary.Summarizer;
import util.PTATimer;

import static driver.PTAOptions.ptaPattern;

import java.io.FileNotFoundException;
import java.io.PrintStream;

public class Main {
	public static PTA pta = null;
	public static void main(String[] args){
		long time = System.currentTimeMillis();

		Config.v().init(args);
		
		if (PTAOptions.dumpJimple) {
			String jimplePath = PTAOptions.APP_PATH.replace(".jar", "");
			SootUtils.dumpJimple(jimplePath);
			System.out.println("Jimple files have been dumped to: " + jimplePath);
			return;
		}
		if (PTAOptions.dumpByte) {
			String classPath = PTAOptions.APP_PATH.replace(".jar", "")+"/clone";
			SootUtils.dumpByte(classPath);
			System.out.println("class files have been dumped to: " + classPath);
			return;
		}
		
		if(PTAOptions.sumonly) {
			Summarizer.run();
			return;
		}
		
		pta =
			ptaPattern.isSummary()?
					ptaPattern.isInsensitive()?new SumPTA():
					new SumContextSensPTA(ptaPattern.getContextDepth(), ptaPattern.getHeapContextDepth(),ptaPattern.getContextKind()):
			ptaPattern.isInsensitive()?new PTA():
			new ContextSensPTA(ptaPattern.getContextDepth(), ptaPattern.getHeapContextDepth(), ptaPattern.getContextKind());
		
		pta.run();
		
		System.out.println(pta.evaluator());
		System.out.println("Total time(including pre-analysis):" + (System.currentTimeMillis() - time) / 1000);
		if(ptaPattern.isSummary()) {
			System.out.println("sumTime: "+SumPTA.sumtime/1000.0);
			
//			if(!SumPTA.scalableMethods.isEmpty()) {
//				String fileName = PTAOptions.APP_PATH.replace(".jar", "-sum.log");
//				try {
//					PrintStream out = new PrintStream(fileName);
//					SumPTA.scalableMethods.forEach(m->out.println(m.getSignature()));
//					out.close();
//				} catch (FileNotFoundException e) {
//					e.printStackTrace();
//				}
//				System.out.println("sumlog has been dumped to: "+fileName);
//			}
		}
	}
}
