import java.io.*;//File, FileReader, BufferedReader, IOException, FileNotFoundException
import java.util.regex.*;//Pattern, Matcher

class Finder {
	PrintWriter output;
	public Finder(String path) throws FileNotFoundException{
		File f = new File(path);
		output = new PrintWriter(f);
	}
	public void close(){
		output.close();
	}
	public void printFile(File f) throws IOException {
		if(f.isDirectory()){
			for(File x : f.listFiles()){
				printFile(x.getAbsolutePath());
			}
		}else{
			System.out.println("print(" + f + ");");
			output.println("print(" + f + ");");
			BufferedReader r = new BufferedReader(new FileReader(f));
			for (String line = r.readLine(); line != null; line = r.readLine()) {
				System.out.println(line);
				output.println(line);
			}
			System.out.println();
			output.println();
		}
	}

	public void printFile(String path) throws IOException {
		printFile(new File(path));
	}

	boolean nameMatches(String pattern, String name){
		pattern = pattern.replace(".","\\.");
		pattern = pattern.replace("*",".*");
		//System.out.println(pattern);
		//Pattern p = Pattern.compile(pattern);
		//Matcher m = p.matcher(name);
		//boolean b = m.matches();
		//return Pattern.compile(pattern).matcher(name).matches();
		return Pattern.matches(pattern, name);
	}

	public boolean findText(String textToFind, File f, String pattern) throws IOException {
		//System.out.println("findText(" + textToFind + ", " + f + ");");
		boolean ret=false;
		if(f.isDirectory()){
			for(File x : f.listFiles()){
				if(!ret)ret=findText(textToFind, x.getAbsolutePath(), pattern);
				else return ret;
			}
		}else if(nameMatches(pattern, f.getName())){
			textToFind = textToFind.toLowerCase();
			
			int lineNum = 0;
			BufferedReader r = new BufferedReader(new FileReader(f));
			
			for (String line = r.readLine(); line != null && !ret; line = r.readLine()) {
				lineNum++;
				if (line.toLowerCase().contains(textToFind)) {
					System.out.println("A keresett kifejezés a \"" + f + "\" fájl " + lineNum + ". sorában található.");
					output.println("A keresett kifejezés a \"" + f + "\" fájl " + lineNum + ". sorában található.");
					ret=true;
				}
			}
			return ret;
		}
	return ret;
	}

	public boolean findText(String textToFind, String path, String pattern) throws IOException {
		return findText(textToFind,new File(path), pattern);
	}

	public static void main(String[] args) throws Exception {
		Finder f;
		if(args.length<1){
			System.err.println("Please give me one or two command line parameters. "+
			"Hint: Use quotation marks around your input (java Finder \"folder\" \"output.txt\").");
			return;
		}else if(args.length>=2){
			f = new Finder(args[1]);
		}else{
			f = new Finder("output.txt");
		}
		try {
			f.printFile(args[0]);
			System.out.print("Add meg a keresendö szöveget: ");
			String query = System.console().readLine();
			f.findText(query, args[0],"");
			System.out.print("Add meg a keresendö fájlnévmintát: ");
			String pattern = System.console().readLine();
			File parent = new File(System.getProperty("user.dir"));
			for(File x : parent.listFiles()){
				f.findText(query, x.getAbsolutePath(), pattern);
			}
			f.close();
		} catch(FileNotFoundException e) {
			System.err.println("A(z) \"" + args[0] + "\" fájl nem található. (Van olvasási jogom?)");
			//e.printStackTrace();
		} catch(IOException e) {
			System.err.println("Olvasási hiba.");
		}
	}
}
