import java.io.*;
import java.util.*;

public class SHJSTemplate{
	static HashMap map=new HashMap();
	public static void main(String[]args)throws Exception{
		new SHJSTemplate(args);
	}
	private void printUsageAndDie(){
		System.out.println(
			"usage:\n"+
			"java SHJSTemplate [-ds] [-o <outfile>] [-l <languages>] -d <lang directory>\n"+
			"  -ds   add default styles\n"+
			"  -o    file to write to\n"+
			"  -l    languages eg java+xml+php\n"+
			"  -d    path to the shjs/lang directory\n"+
			"  -?    or -help print this help screen\n\n"+
			"Eg:\n"+
			"java SHJSTemplate -ds -o sh_mytemplate.css -l css+html+jss -d x:/shjs/lang"
		);
		System.exit(0);
	}
	public SHJSTemplate(String[]args)throws Exception{
		boolean dstyles=false;
		String outName="sh_template.css";
		String dirName=null;
		String langPara="<all>";

		for(int i=0;i<args.length;i++){
			if(args[i].equals("-ds"))
				dstyles=true;
			else if(args[i].equals("-o"))
				outName=args[++i];
			else if(args[i].equals("-l"))
				langPara=args[++i];
			else if(args[i].equals("-d"))
				dirName=args[++i];
			else if(args[i].equals("-?")||args[i].equals("-help"))
				printUsageAndDie();
		}
		if(dirName==null)
			printUsageAndDie();

		System.out.println("default styles: "+dstyles);
		System.out.println("save as       : "+outName);
		System.out.println("languages     : "+langPara);
		System.out.println("dir           : "+dirName);

		String []lang=langPara.split("\\+");

		File dir=new File(dirName);
		File []files=dir.listFiles();
		System.out.println(files.length+" files found");
		for(int i=0;i<files.length;i++){
			if(!files[i].getName().endsWith(".min.js")){
				if(langPara.equals("<all>")){
					read(files[i]);
				}else{
					String s=files[i].getName();
					s=s.substring(3,s.indexOf('.'));
					System.out.println(s);
					for(int k=0;k<lang.length;k++){
						if(lang[k].equals(s)){
							read(files[i]);
							break;
						}
					}
				}
			}
		}
		Set entries=map.entrySet();
		Iterator iter=entries.iterator();
		BufferedWriter out=new BufferedWriter(new FileWriter(outName));
		while(iter.hasNext()){
			Map.Entry entry=(Map.Entry)iter.next();
			String style=(String)entry.getKey();
			Object []o=((HashSet)entry.getValue()).toArray();
			String []from=new String[o.length];
			for(int i=0;i<o.length;i++)
				from[i]=(String)o[i];
			Arrays.sort(from);
			out.write("/* ");
			for(int i=0;i<o.length;i++)
				out.write(from[i]+" ");
			out.write("*/\n");
			out.write("pre.sh_sourceCode span.");
			out.write(style);
			out.write("{\n");
			if(dstyles){
				out.write("  color: #000000;\n");
				out.write("  font-weight: normal;\n");
				out.write("  font-style: normal;\n");
			}
			out.write("}\n");
		}
		out.flush();
		out.close();
	}
	private void read(File f)throws Exception{
		System.out.println("reading:"+f.getName());
		String n=f.getName();
		String from=n.substring(n.indexOf('_')+1,n.indexOf('.'));
		BufferedReader in=new BufferedReader(new FileReader(f));
		String l="";
		while((l=in.readLine())!=null){
			String s=l.trim();
			if(s.startsWith("'style':")){
				//'style': 'sh_preproc'
				//'style': ['sh_normal', 'sh_port']
				String []p=s.substring(10,s.lastIndexOf('\'')).split("[^a-z_]");
				for(int i=0;i<p.length;i++){
					if(p[i].trim().length()>0){
						HashSet set;
						if(map.containsKey(p[i])){
							set=(HashSet)map.get(p[i]);
						}else{
							set=new HashSet();
							map.put(p[i],set);
						}
						set.add(from);
					}
				}
			}
		}
	}
}