package v6.apps.zelvak;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

import javax.imageio.ImageIO;

public class Zevlak {
	
	public static void main(String[] args) {
		/*if(args.length == 0){
			System.out.println("interactive mode is not implemented");
			System.exit(1);
		}*/
		if(args.length != 3){
			System.out.println("usage: zevlak application steps output");
			System.exit(1);
		}
		final String format = "png";
		Reader in = null;
		try{
			try{
				in = new InputStreamReader(new FileInputStream(args[0]));
				final RunningContext context = new RunningContext();
				context.setMaxStep(Integer.parseInt(args[1]));
				final Application application = Parser.parse(in);
				try {
					application.run(context);
				} catch (InterruptedException e) {
					// never mind
				}
				ImageIO.write(context.getImage(), format, new File(args[2]));
			}finally{
				if(in != null){
					in.close();
				}
			}
		}catch (IOException e) {
			System.err.println("I/O problem: "+e);
			System.exit(2);
		}catch (WTFException e) {
			System.err.println("invalid application: "+e);
			System.exit(3);
		}catch (RunningException e) {
			System.err.println("application runtime problem: "+e);
			System.exit(4);
		}
	}
	
}
