package org.greencubes.versions;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;

import org.greencubes.util.Util;
import org.json.JSONArray;
import org.json.JSONObject;

public class Main {

	private static File dir;
	private static URI directoryUrl;
	
	public static void main(String[] args) {
		if(args.length == 0) {
			System.err.println("Please, specify directory!");
			System.exit(1);
		}
		File versionFile = new File("version.md5");
		try {
			dir = new File(Util.join(args, " "));
			directoryUrl = dir.toURI();
			versionFile.createNewFile();
			FileWriter fw = new FileWriter(versionFile);
			JSONObject json = new JSONObject();
			JSONArray jsonList = new JSONArray();
			make(dir, fw, jsonList);
			fw.close();
			json.put("files", jsonList);
			fw = new FileWriter(new File("version.js"));
			json.writeWithIdent(fw);
			fw.close();
			System.out.println("Done.");
		} catch(Exception e) {
			e.printStackTrace();
			System.exit(1);
		}	
	}
	
	private static void make(File file, FileWriter fw, JSONArray jsonList) throws IOException {
		for(File f : file.listFiles()) {
			if(f.isDirectory()) {
				make(f, fw, jsonList);
				continue;
			}
			String filename = Util.getRelativeName(f, directoryUrl);
			try {
				String hash = Util.getMD5Checksum(f);
				fw.append(filename).append(';').append(hash).append('\n');
				JSONObject joFileInfo = new JSONObject();
				joFileInfo.put("length", file.length());
				joFileInfo.put("name", filename);
				joFileInfo.put("hash", hash);
				jsonList.put(joFileInfo);
			} catch(Exception e) {
				e.printStackTrace();
			}
		}
	}

}

@@ -516,28 +516,15 @@
 ile(
-Util.join(args, %22 %22)
+args%5B0%5D
 );%0A%09
@@ -784,24 +784,160 @@
  jsonList);%0A
+%09%09%09for(int arg = 1; arg %3C args.length - 1; arg += 2) %7B // Put parameters to resulting json%0A%09%09%09%09json.put(args%5Barg%5D, args%5Barg + 1%5D);%0A%09%09%09%7D%0A
 %09%09%09fw = new 

Fix arguments bug, pass arguments to json file