package net.oni2.aeinstaller.updater;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import net.oni2.ProxySettings;
import net.oni2.aeinstaller.updater.backend.Paths;
import net.oni2.aeinstaller.updater.gui.MainWin;

/**
 * @author Christian Illy
 * @version 1
 */
public class AEInstaller2Updater {

	/**
	 * @param args
	 *            Command line arguments
	 */
	public static void main(String[] args) {
		boolean debug = false;
		for (String a : args) {
			if (a.equalsIgnoreCase("-debug"))
				debug = true;
			if (a.equalsIgnoreCase("-usewd"))
				Paths.useWorkingDirectory = true;
		}
		if (!debug) {
			try {
				PrintStream ps = new PrintStream(new File(Paths.getPrefsPath(),
						"updater_output.log"));
				System.setOut(ps);
				System.setErr(ps);
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			}
		}

		if (Paths.getProxySettingsFilename().exists()) {
			ProxySettings.deserializeFromFile(Paths.getProxySettingsFilename());
		}

		System.setProperty("networkaddress.cache.ttl", "5");
		System.setProperty("networkaddress.cache.negative.ttl", "1");

		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		JFrame.setDefaultLookAndFeelDecorated(true);

		SwingUtilities.invokeLater(new Runnable() {

			public void run() {
				try {
					new MainWin().setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
