package application;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public class TaskEnvironment {
	public static final String HELP = "help";
	public static final String H = "h";
	public static final String PATH = "path";
	public static final String P = "p";
	public static final String STARTSWITH = "startswith";
	public static final String S = "s";
	public static final String ENDSWITH = "endswith";
	public static final String E = "e";
	public static final String OUT = "out";
	public static final String O   = "o";

	public static String path = "";
	public static String startswith = "";
	public static String endswith = "";
	
	public static String default_out_path = "./";
	public static String out_path = default_out_path;
	
	public static boolean accepted = false;
	
	public static void scanProgramArgs(String[] args) {
		Options options = new Options();
		options.addOption(H,  HELP, false, "display this help and exit");
		options.addOption(P,  PATH, false, "path to image files");
		options.addOption(S,  STARTSWITH, true, "file name stats with pattern");
		options.addOption(E,  ENDSWITH, true, "file name ends with pattern");
		options.addOption(O,  OUT, true, "path to output files. default is " + default_out_path);

		CommandLineParser parser = new DefaultParser();
		CommandLine line;
		try {
			line = parser.parse(options, args);
			if (line.hasOption(HELP)) {
				print_help(options);
//				print_transformer();
				System.exit(0);
			}
			if (line.hasOption(PATH)) {
				path = line.getOptionValue(PATH);
			}
			if (line.hasOption(OUT)) {
				out_path = line.getOptionValue(OUT);
				if(!out_path.endsWith("/"))
					out_path += "/";
			}
			if (line.hasOption(STARTSWITH)) {
				startswith = line.getOptionValue(STARTSWITH);
			}
			if (line.hasOption(ENDSWITH)) {
				endswith = line.getOptionValue(ENDSWITH);
			}
		} catch (ParseException ex) {
			ex.printStackTrace();
		}
		
		if (!accepted) {
			print_help(options);
		}
	}

	private static void print_help(Options options) {
		HelpFormatter formatter = new HelpFormatter();
		formatter.printHelp(TD.class.getName(), options);
		System.out.println();
		System.out.println("java -Xmx24g application.TD --path split --startswith clip_ --endswith .png");
	}

}
