/*
 * A lister of files added to an SVN repository and 
 * containing some string value. Parses an svn dump.
 * 
 * (c) 2008 Geomatys http://www.geomatys.fr/
 * 
 */
package fr.geomatys.svn.tools;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;

/*
 * The class finds all references to files being added to the SVN repository
 * which contain SEARCH_KEY in the file name (the Node.Path in SVN terminology).
 * 
 * Usage notes:
 *   the *action* is down at the end around line 80
 *   comment/uncomment the right lines to get different output
 *     all files (uncomment the if block)
 *     file list only (comment the line that appends "  \\")
 *     file list for use in shell script (keep the above line).
 * you get the idea. 
 * 
 */
public class SvndumpLister {
	
	//THIS IS THE STRING WE WILL LOOK FOR IN THE NODE PATH NAMES
	// The empty string matches all.
	private static String SEARCH_KEY = ".class";


	/**
	 * The main
	 */
	public static void main(String[] args) throws IOException {
		
		//Setup variables for the method
		String lnstr;
		String nodePath = null;
		
		
		
		if (args.length != 2){
			System.out.println("Usage: java -cp path/to/bin SvnLister inDumpfile outListfile");
			System.exit(0);
		}
		File infile  = new File(args[0]);
		File outfile = new File(args[1]);
		
		//Create a RandomAccessFile on the output
		final RandomAccessFile raf = new RandomAccessFile(infile,"r");
		final BufferedWriter   out = new BufferedWriter(new FileWriter(outfile));
		
		
		////////////////////////////////////////////////////////
		// DO STUFF
		////////////////////////////////////////////////////////
		while (null != (lnstr = raf.readLine()) ) {
			
			if (lnstr.isEmpty()){
				continue;
			}
			
			if ( lnstr.startsWith("Node-path: ") ){
				nodePath = lnstr.substring(11);
				continue;
			}
			
			if ( lnstr.startsWith("Node-action: ") ){
				//If we are not in an add situation, we don't care so we reset and bail out.
				if ( ! (lnstr.substring(13)).contains("add") ){
					nodePath = null;
				}
				continue;
			}
			
			if (lnstr.startsWith("Node-kind: ") ){
				//if it's a directory we reset and continue
				if ( ! (lnstr.substring(11)).contains("file") ){
					nodePath = null;
				}
				//THIS ACTUALLY DOES THE ONLY WORK
				if ( (null != nodePath) ){
					
//					if ( nodePath.contains(SEARCH_KEY) ){
					if ( nodePath.endsWith(SEARCH_KEY) ){
//						out.append(nodePath);
						out.append(nodePath+"  \\");//For input to svndumpfilter
						out.newLine();
					}
				}
				continue;
			}
			
		}//while --- parsing
		
		out.close();
		
	}//main

}//class

