package com.bayer.edam.excelimport;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;

public class DirectoryListingCleaner {
	
	private static Logger log = Logger.getLogger(DirectoryListingCleaner.class);
	private static String logfilePath = "D:\\ToolboxMigration-all.log";
	
	private static String listingFilePath = "D:\\ToolboxMigration-all.txt";
	private List<String> filenames;
	HashSet<String> uniqueFilenames = new HashSet<String>();
	
	public DirectoryListingCleaner() throws IOException {
		filenames = FileUtils.readLines(new File(listingFilePath), "UTF-8");
		
		int lineNo = 0;
		for (String filename : filenames) {
			lineNo++;
			if (!uniqueFilenames.contains(filename)) {
				uniqueFilenames.add(filename);
			}
			else {
				log.warn("Filename '" + filename + "' sorted out (line " + lineNo + ")");
			}
		}
		
		log.info("Unique filenames: " + uniqueFilenames.size());
		
	}


	public static void main(String[] args) {
		try {
			Layout layout = new PatternLayout("%d{ISO8601} - %p %m%n"); // "%d{ISO8601} - %p %m%n"
			BasicConfigurator.configure(new ConsoleAppender(layout));
			BasicConfigurator.configure(new RollingFileAppender(layout, logfilePath, false));
			log.setLevel(Level.INFO);

			new DirectoryListingCleaner();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
