/*
Copyright (C) 2007 Mikhail Koryak / DoesThatEvenCompile.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
package com.doesthatevencompile.codeSnipletizer;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

import com.doesthatevencompile.utils.ASCIIFile;
import com.doesthatevencompile.utils.FileUtil;
import com.doesthatevencompile.utils.SearchableItemListener;

public class SnipletMaker implements SearchableItemListener {
	private FileUtil finder;
	private String outputDir;
	private ASCIIFile template;
	public SnipletMaker(String inputDir, String outputDir, final String ext){
		finder = new FileUtil(inputDir,this, new FileFilter(){
			public boolean accept(File f) {
				if(ext != null){
					if(f.getName().toLowerCase().endsWith(ext.toLowerCase()) || f.isDirectory()){
						return true;
					} else {
						return false;
					}
				} else {
					return true;
				}
			}
		});
		File t = new File(System.getProperty("user.dir")+"/template.htm");
		System.out.println("Making sniplets from template: "+t.getAbsolutePath());
		try {
			this.template  = new ASCIIFile(t);
		} catch (IOException e) {
			System.out.println("Couldnt find the template file, at: "+t.getAbsolutePath());
			e.printStackTrace();
		}
		this.outputDir = outputDir;
		finder.findFiles();
	}

	public void searchFinished(int numFound, File path) {
		System.out.println("All files made into sniplets, converted: "+numFound+" files from: "+path.getAbsolutePath());
		System.out.println("by DoesThatEvenCompile.com 2007");
	}

	public void supportedFileFound(File f) {
		try {
			ASCIIFile file = new ASCIIFile(f);
			//one may not want this to happen:
			int index = (f.getName().lastIndexOf(".") != -1)?f.getName().lastIndexOf("."):f.getName().length();
			ASCIIFile out = new ASCIIFile(new File(outputDir, f.getName().substring(0,index)+".htm"));
			String contents = template.getContents();
			contents = contents.replaceAll("%FILENAME%", f.getName());
			contents = contents.replaceAll("%BODY%",file.getContents());
			out.setContents(contents);
			System.out.println("Snipletted file:"+f.getName());
		} catch (IOException e) {
			System.out.println("Probably couldnt find output dir: "+outputDir);
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		//new SnipletMaker("D:/6 - Programming/workspace/WebsiteUtils","D:/web design/java_sniplets_out/", "java"); if(1<2)return;

		if(args.length < 2){
			System.out.println("Usage:njava -jar MakeSniplets.jar [sniplets dir] [output dir] [type extention]n"+
					"edit template.htm and remember that %FILENAME% is replaced by the name ofn"+
					"the snipleted file, and %BODY% is replaced by the body of that file.nn"+
					"example: java -jar MakeSniplets.jar D:/java_sniplets/ D:/java_sniplets_out/ javan"+
					"example: java -jar MakeSniplets.jar D:/java_sniplets/ c:/outputs/");
		} else {
			String ext = (args.length == 3)?args[2]:null;
			new SnipletMaker(args[0],args[1], ext);
		}
	}
}