import java.io.*;
import java.lang.Long;
class changemodificationdate{

    static long numfiles=0;
    static long numwriteprotected=0;

    public static void main(String[] args){
	long diff = 0;
	File path;

	if(args.length!=2){//Not enough arguments
	    System.out.println("Usage:");
	    System.out.println("changemodificationdate diff path");
	    System.out.println(" where diff is milliseconds to add, for instance -52");
	    System.out.println("       path is path to a directory whos entire content recursively should");
	    System.out.println("       have changed modification date. Could also be a single file");
	    System.exit(1);
	}

	//Parse diff argument
	try {
	    diff = Long.parseLong(args[0]);
	}catch(NumberFormatException e){
	    System.err.println("ERROR: first argument \"" + args[0] + "\" have to be a number!");
	    System.exit(1);
	}

	//Parse path
	path = new File(args[1]);
	if(!(path.isDirectory()||path.isFile())){
	    System.err.println("ERROR: the specified path \"" + args[1] +"\" is not an existing directory or file!");
	    System.exit(1);
	}
	

	//We got here if both diff and path are valid...
	
	//Change modification date recursively:
	numfiles=0;
	numwriteprotected=0;
	change(diff, path, true); //Just fake the change, won't modify anything
	if(numwriteprotected!=0){
	    System.err.println("ERROR: Aborting operation without performing any changes, since " + numwriteprotected + " files not writable.");
	    System.exit(2);
	}

	numfiles=0;
	numwriteprotected=0;
	change(diff, path, false); //Performs the change
	System.out.println("Number of files changed: " + numfiles);
	System.out.println("Write protected files: " + numwriteprotected);
	System.exit(0);
    }

    static void change(long diff, File path, boolean justfaking){
	long time;
	if(path.isFile()){
	    if(path.canWrite()){
		//System.out.println("change(" + diff + ", \"" + path+"\")");
		time = path.lastModified();
		if(!justfaking){
		    time += diff;
		    if(!path.setLastModified(time)){
			System.err.println("ERROR: unknown error changing modification date");
		    }else{
			System.out.println("change(" + diff + ", \"" + path+"\")");
		    }
		    
		}
		numfiles++;
	    }else{ //!canWrite()
		System.err.println("ERROR: Write Protected: \"" + path + "\"");
		numwriteprotected++;
	    }
	    
	}
	
	if(path.isDirectory()){
	    File[] additional = path.listFiles();
	    if(additional==null){
		System.err.println("ERROR: ?");
	    }else {
		for(int i=0; i<additional.length; i++){
		    change(diff,additional[i],justfaking);
		}
	    }
	}
    }
    
}
