package com.parc.ccn.apps;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.stream.XMLStreamException;

import com.parc.ccn.config.ConfigurationException;
import com.parc.ccn.data.ContentName;
import com.parc.ccn.data.MalformedContentNameStringException;
import com.parc.ccn.library.CCNLibrary;
import com.parc.ccn.library.io.CCNFileInputStream;
import com.parc.ccn.library.io.CCNInputStream;

public class get_file {
	
	public static Integer timeout = null;
	public static boolean unversioned = false;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int startArg = 0;
		
		for (int i = 0; i < args.length - 2; i++) {
			if (args[i].equals("-unversioned")) {
				if (startArg <= i)
					startArg = i + 1;
				unversioned = true;
			} else if (args[i].equals("-timeout")) {
				if (args.length < (i + 2)) {
					usage();
					return;
				}
				try {
					timeout = Integer.parseInt(args[++i]);
				} catch (NumberFormatException nfe) {
					usage();
					return;
				}
				if (startArg <= i)
					startArg = i + 1;
			} else {
				usage();
				System.exit(1);
			}
		}
		
		if (args.length < startArg + 2) {
			usage();
			System.exit(1);
		}
		
		try {
			int readsize = 1024; // make an argument for testing...
			// If we get one file name, put as the specific name given.
			// If we get more than one, put underneath the first as parent.
			// Ideally want to use newVersion to get latest version. Start
			// with random version.
			ContentName argName = ContentName.fromURI(args[startArg]);
			
			CCNLibrary library = CCNLibrary.open();

			File theFile = new File(args[startArg + 1]);
			if (theFile.exists()) {
				System.out.println("Overwriting file: " + args[startArg + 1]);
			}
			FileOutputStream output = new FileOutputStream(theFile);
			
			long starttime = System.currentTimeMillis();
			CCNInputStream input;
			if (unversioned)
				input = new CCNInputStream(argName, null, library);
			else
				input = new CCNFileInputStream(argName, library);
			if (timeout != null) {
				input.setTimeout(timeout); 
			}
			byte [] buffer = new byte[readsize];
			
			int readcount = 0;
			int readtotal = 0;
			//while (!input.eof()) {
			while ((readcount = input.read(buffer)) != -1){
				//readcount = input.read(buffer);
				readtotal += readcount;
				output.write(buffer, 0, readcount);
				output.flush();
			}
			System.out.println("get_file took: "+(System.currentTimeMillis() - starttime)+"ms");
			System.out.println("Retrieved content " + args[1] + " got " + readtotal + " bytes.");
			System.exit(0);

		} catch (ConfigurationException e) {
			System.out.println("Configuration exception in get_file: " + e.getMessage());
			e.printStackTrace();
		} catch (MalformedContentNameStringException e) {
			System.out.println("Malformed name: " + args[0] + " " + e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("Cannot write file or read content. " + e.getMessage());
			e.printStackTrace();
		} catch (XMLStreamException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.exit(1);
	}
	
	public static void usage() {
		System.out.println("usage: get_file [-unversioned] [-timeout millis] <ccnname> <filename>");
	}
}

@@ -94,32 +94,64 @@
 .io.IOException;
+%0Aimport java.util.logging.Level;
 %0A%0Aimport javax.x
@@ -181,16 +181,45 @@
 ption;%0A%0A
+import com.parc.ccn.Library;%0A
 import c
@@ -1143,16 +1143,326 @@
  i + 1;%0A
+%09%09%09%7D else if (args%5Bi%5D.equals(%22-log%22)) %7B%0A%09%09%09%09Level level = null;%0A%09%09%09%09if (args.length %3C (i + 2)) %7B%0A%09%09%09%09%09usage();%0A%09%09%09%09%7D%0A%09%09%09%09try %7B%0A%09%09%09%09%09level = Level.parse(args%5B++i%5D);%0A%09%09%09%09%7D catch (NumberFormatException nfe) %7B%0A%09%09%09%09%09usage();%0A%09%09%09%09%7D%0A%09%09%09%09Library.logger().setLevel(level);%0A%09%09%09%09if (startArg %3C= i)%0A%09%09%09%09%09startArg = i + 1;%0A
 %09%09%09%7D els

Add -log option to get_file