package de.onyxbits.raccoon.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;

import org.apache.commons.io.IOUtils;

/**
 * A simple session log for recording the files that were downloaded. The log is
 * meant to be used as an input for shellscripts, so the logformat is dead
 * simple: every line contains the filename of one successfully downloded file.
 * 
 * @author patrick
 * 
 */
public class DownloadLogger {

	public static final String LOGCOMPLETE = "downloads-complete.txt";
	public static final String LOGCOMPLETEOLD = "downloads-complete.old";
	private File completeLog;
	private File completeLogOld;

	/**
	 * Create a new logger
	 * 
	 * @param logfile
	 *          the file to log to.
	 */
	public DownloadLogger(Archive archive) {
		completeLog = new File(new File(archive.getRoot(), Archive.LOGDIR), LOGCOMPLETE);
		completeLogOld = new File(new File(archive.getRoot(), Archive.LOGDIR), LOGCOMPLETEOLD);
	}

	/**
	 * List what was downloaded in the last session
	 * 
	 * @return new files in the storage.
	 */
	public List<FileNode> getLastSessionDownloads() {
		Vector<FileNode> ret = new Vector<FileNode>();
		try {
			String tmp = IOUtils.toString(new FileInputStream(completeLogOld));
			StringTokenizer tk = new StringTokenizer(tmp, "\n");
			while (tk.hasMoreTokens()) {
				ret.add(new FileNode(new File(tk.nextToken())));
			}
		}
		catch (Exception e) {
		}
		return ret;
	}

	/**
	 * Clears the download log. This should be called when starting the session.
	 */
	public synchronized void clear() {
		if (completeLogOld.exists()) {
			completeLogOld.delete();
		}

		if (completeLog.exists()) {
			completeLog.renameTo(completeLogOld);
		}
	}

	/**
	 * Log a completed download to the logfile
	 * 
	 * @param file
	 *          the file that was downloaded
	 * @throws IOException
	 *           if writing fails.
	 */
	public synchronized void addEntry(File file) throws IOException {
		completeLog.getParentFile().mkdirs();
		FileWriter fw = new FileWriter(completeLog, true);
		fw.write(file.getAbsolutePath() + "\n");
		fw.close();
	}
}

@@ -755,16 +755,61 @@
 ogOld;%0A%0A
+%09private static Object lock = new Object();%0A%0A
 %09/**%0A%09 *
@@ -1712,16 +1712,41 @@
 ear() %7B%0A
+%09%09synchronized (lock) %7B%0A%09
 %09%09if (co
@@ -1762,32 +1762,33 @@
 Old.exists()) %7B%0A
+%09
 %09%09%09completeLogOl
@@ -1801,19 +1801,21 @@
 te();%0A%09%09
+%09
 %7D%0A%0A
+%09
 %09%09if (co
@@ -1836,16 +1836,17 @@
 ts()) %7B%0A
+%09
 %09%09%09compl
@@ -1878,16 +1878,21 @@
 ogOld);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A
@@ -2131,16 +2131,42 @@
 ption %7B%0A
+%09%09synchronized (lock) %7B%0A%0A%09
 %09%09comple
@@ -2197,16 +2197,17 @@
 dirs();%0A
+%09
 %09%09FileWr
@@ -2253,16 +2253,17 @@
 rue);%0A%09%09
+%09
 fw.write
@@ -2295,16 +2295,17 @@
  %22%5Cn%22);%0A
+%09
 %09%09fw.clo
@@ -2310,13 +2310,17 @@
 lose();%0A
+%09%09%7D%0A
 %09%7D%0A%7D%0A

synchronize on a shared object so multiple downloadloggers can safely be used simultanously.