package org.ccnx.ccn.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.SortedSet;
import java.util.TreeSet;

import org.ccnx.ccn.CCNHandle;
import org.ccnx.ccn.config.ConfigurationException;
import org.ccnx.ccn.profiles.nameenum.BasicNameEnumeratorListener;
import org.ccnx.ccn.profiles.nameenum.CCNNameEnumerator;
import org.ccnx.ccn.protocol.ContentName;
import org.ccnx.ccn.protocol.MalformedContentNameStringException;



public class repo_ls implements BasicNameEnumeratorListener{

	private String prefix = "";
	private ContentName name = null;
	private long timeout = -1;
	private SortedSet<ContentName> allNames;

	public static void main(String[] args) {
		repo_ls lister = new repo_ls();
		lister.init(args);
		lister.enumerateNames();
		System.exit(0);
	}
	
	private void init(String[] args){
		// first look for prefix and timeout in the args list

		for (int i = 0; i < args.length; i++) {
			if(!args[i].equals("-timeout")){
				prefix = args[i];
			}
			else{
				i++;
				if(i >= args.length){
					usage();
					System.exit(1);
				}
				else{
					timeout = Long.parseLong(args[i]);
					System.out.println("monitoring for "+timeout+" millis");
				}
			}
		}
		
		try {
			if(prefix==null || prefix.equals(""))
				name = new ContentName();
			else
				name = ContentName.fromNative(prefix);
			System.out.println("monitoring prefix "+name.toString());
		} catch (MalformedContentNameStringException e) {
			System.err.println(e.toString());
			System.err.println("could not create parse prefix, please be sure it is a valid name prefix");
			System.exit(1);
		}
		
		if(timeout > 0)
			System.out.println("monitoring prefix for "+timeout+"ms");
				
		allNames = new TreeSet<ContentName>();
		
	}

	private void enumerateNames(){
		try {
			CCNHandle handle = CCNHandle.open();

			CCNNameEnumerator ccnNE = new CCNNameEnumerator(handle, this);
			ccnNE.registerPrefix(name);
			
			if(timeout > 0){
				try {
					Thread.sleep(timeout);
				} catch (InterruptedException e) {
					System.err.println("error while waiting for responses from CCNNameEnumerator");
				}
			
				System.out.println("finished waiting for responses, cleaning up state");
				ccnNE.cancelPrefix(name);
			}
			else{
				//we do not have to exit
				while(true){
					
				}
			}
			
		} catch (ConfigurationException e) {
			System.err.println("Configuration Error");
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println(e.toString());
			e.printStackTrace();
		}
		
	}

	
	public void usage(){
		System.out.println("usage: repo_ls <ccnprefix> [-timeout millis]");
	}

	public int handleNameEnumerator(ContentName prefix, ArrayList<ContentName> names) {
		allNames.addAll(names);
		System.out.println("==========");
		System.out.println("Contents under "+name.toString());
		for(ContentName c: allNames)
			System.out.println("  + "+c);
		System.out.println("----------");

		return 0;
	}
	
}

@@ -211,24 +211,62 @@
 nException;%0A
+import org.ccnx.ccn.impl.support.Log;%0A
 import org.c
@@ -320,16 +320,16 @@
 stener;%0A
-
 import o
@@ -641,10 +641,12 @@
 t = 
--1
+2000
 ;%0A%09p
@@ -924,17 +924,19 @@
 gs list%0A
+%09%09
 %0A
-
 %09%09for (i
@@ -975,16 +975,26 @@
 %7B%0A%09%09%09if(
+i == 0 && 
 !args%5Bi%5D
@@ -1181,70 +1181,8 @@
 %5D);%0A
-%09%09%09%09%09System.out.println(%22monitoring for %22+timeout+%22 millis%22);%0A
 %09%09%09%09
@@ -1328,34 +1328,24 @@
 ix);%0A%09%09%09
-System.out.println
+Log.fine
 (%22monito
@@ -1610,34 +1610,24 @@
 %3E 0)%0A%09%09%09
-System.out.println
+Log.fine
 (%22monito
@@ -2090,34 +2090,24 @@
 %09%09%09%0A%09%09%09%09
-System.out.println
+Log.fine
 (%22finish
@@ -2183,16 +2183,34 @@
 (name);%0A
+%09%09%09%09printNames();%0A
 %09%09%09%7D%0A%09%09%09
@@ -2580,16 +2580,36 @@
 t millis
+ (default is 2000ms)
 %5D%22);%0A%09%7D%0A
@@ -2720,16 +2720,60 @@
 names);%0A
+%09%09return 0;%0A%09%7D%0A%09%0A%09public void printNames()%7B%0A
 %09%09System
@@ -2921,16 +2921,16 @@
 + %22+c);%0A
+
 %09%09System
@@ -2960,21 +2960,8 @@
 -%22);
-%0A%0A%09%09return 0;
 %0A%09%7D%0A

fix input parsing for repo_ls, switched to a default 2s timeout and switched printlns to logging statements