package de.geis.pdfdownloader;

import java.io.File;

import org.tanukisoftware.wrapper.WrapperListener;
import org.tanukisoftware.wrapper.WrapperManager;

public class Main implements WrapperListener {
	
    private FileSystemWatcher m_watcher;
	
	private Main() {
		
	}
	
	@Override
	public Integer start(String[] args) {

		// Parameter werden geladen
		Parameters.setDownloadFolder(new File(args[0]));
		Parameters.setErrorFolder(new File(args[1]));
		Parameters.setFileFolder(new File(args[2]));
		Parameters.setConfigFolder(new File(args[3]));
		Parameters.setTestFolder(new File(args[4]));

		Parameters.setDelayTime(Integer.parseInt(new File(args[5]).toString()));
		Parameters.setVersuche(Integer.parseInt(new File(args[6]).toString()));

		System.out.println("DELAY = " + Parameters.getDelayTime());
		System.out.println("VERSUCHE = " + Parameters.getVersuche());

		m_watcher = new FileSystemWatcher();
		
		try {
			m_watcher.start();
		} catch (Exception e) {
			e.printStackTrace();
		} 

		return null;
	}

	@Override
	public int stop(int exitCode) {
		return exitCode;
	}

	@Override
	public void controlEvent(int event) {
		if ((event == WrapperManager.WRAPPER_CTRL_LOGOFF_EVENT)
				&& (WrapperManager.isLaunchedAsService() || WrapperManager.isIgnoreUserLogoffs())) {
			// Ignore
		} else {
			WrapperManager.stop(0);
			// Will not get here.
		}
	}

	public static void main(String[] args) {
		WrapperManager.start(new Main(), args);
	}
}