
import java.io.*;
import java.util.Date;
import java.text.SimpleDateFormat;

public class LogArchiver {

	private final String LOG_ARCHIVE = "*****LOG ARCHIVE-";
	private final String SOURCE_LOG_NAME = "flashlog.txt";
	private final String LOG_ARCHIVE_NAME = "flashlog_archive.txt";
	private final String DATE_TERMINATING_CHARACTER = "-";
	
	private File sourceFile;
	private File destinationFile;
	
	public LogArchiver(String pathToLogFolder){
		sourceFile = new File( pathToLogFolder + SOURCE_LOG_NAME);
		destinationFile = new File( pathToLogFolder + LOG_ARCHIVE_NAME);
		
		// If there is no source log file we can't do anything
		if( !fileExists( sourceFile )){
			System.out.println( "Can't read source file: " + sourceFile );
			return;
		}
		
		// If compareModifiedWithLastArchive() returns true and there is content in the source log file
		// then write the contents of the source log file to the log archive file
		if( compareModifiedWithLastArchive( new Date( sourceFile.lastModified() ) ) && sourceFile.length() > 0 ){
			writeToArchive();
		}
	}
	
	/**
	 * Writes contents of source log file to the archive with a date.
	 */
	private void writeToArchive(){
		System.out.println("Writing " + SOURCE_LOG_NAME + " contents to " + LOG_ARCHIVE_NAME);
		try {
			FileReader reader = new FileReader(sourceFile);
			BufferedReader in = new BufferedReader(reader);
			
			FileWriter writer = new FileWriter(destinationFile, true);
			BufferedWriter out = new BufferedWriter(writer);
			
			Date date = new Date();
			SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");				
			out.write(LOG_ARCHIVE + format.format(date) + DATE_TERMINATING_CHARACTER + "*****");
			out.newLine();
			
			String line;
			while ((line = in.readLine()) != null ) {
				out.write(line);
				out.newLine();
			}
			out.write("");
			out.newLine();
			out.close();
		}
		catch ( IOException e){
			e.printStackTrace();
		}
	}
	
	/**
	 * Compares the passed Date object with the date of the last recorded archive. If the Date object is newer, true is returned.
	 */
	private boolean compareModifiedWithLastArchive(Date lastModifiedDate)
	{
		// If the log archive file doesn't exist then we allow the write so that it will be created
		if ( !fileExists( destinationFile ) ) return true;
		
		StringBuilder sb = new StringBuilder();
	
		try {
			BufferedReader reader =	new BufferedReader(new FileReader(destinationFile));
			
			String line;
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
	
		}
		catch(IOException e) {
			e.printStackTrace();
		}
	
		String fileText = sb.toString();
		
		int lastDateStartIndex =  fileText.lastIndexOf(LOG_ARCHIVE) + LOG_ARCHIVE.length();
		int lastDateEndIndex = fileText.indexOf(DATE_TERMINATING_CHARACTER, lastDateStartIndex);
		
		// If we can't get the substring containing the last archived date (because the lastDateEndIndex isn't valid) it is assumed that the archive 
		// file hasn't had anything written to it yet, so we return true.
		if(lastDateEndIndex <= lastDateStartIndex) return true;
		
		Date lastArchiveDate = new Date(fileText.substring(lastDateStartIndex, lastDateEndIndex));
		
		if(lastModifiedDate.compareTo(lastArchiveDate) > 0) return true;
		else return false;
	}
	
	/**
	 * Returns whether or not a file exists
	 */
	private boolean fileExists(File file){
		if( !file.exists() || !file.canRead() ) return false;
		else return true;
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String logPath = "";
		for (int i = 0; i < args.length; i++){
			logPath = args[i];
		}
		LogArchiver archiver = new LogArchiver(logPath);
	}

}

