/* $Id: ZidestoreProxy.java,v 1.1 2004/01/09 16:54:18 burkhard Exp $
 * Created on 21.05.2003 by sell
 *
 */
package de.skyrix.zsp;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JOptionPane;
import javax.swing.UIManager;

import de.skyrix.zsp.conf.ZSPConfig;
import de.skyrix.zsp.gui.ConfigDialog;
import de.skyrix.zsp.gui.SystemTray;
import de.skyrix.zsp.logic.Proxy;
import de.skyrix.zsp.logic.ZidestoreConnection;
import de.skyrix.zsp.logic.ZidestoreSpider;
import de.skyrix.zsp.logic.cache.CacheManageException;
import de.skyrix.zsp.logic.cache.CacheManager;
import de.skyrix.zsp.logic.cache.SpoolerQueue;
import de.skyrix.zsp.logic.httpserver.HttpServer;
import de.skyrix.zsp.logic.httpserver.modules.GetModule;
import de.skyrix.zsp.logic.httpserver.modules.OptionsModule;
import de.skyrix.zsp.logic.httpserver.modules.PropFindModule;
import de.skyrix.zsp.logic.httpserver.modules.PropPatchModule;

/**
 * 
 * @author sell
 * @version 
 */
public class ZidestoreProxy {

	public ZidestoreProxy() {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
		}
		catch (Exception e) {
			try {
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			}
			catch (Exception ex) {
			}
		}
	}

	public static void main(String[] args) {
		String url = null;
		String userName = "";
		String password = "";

		System.setProperty(
			"zsp.config_file",
			System.getProperty("user.home") + "/zsp.conf");

		if (!ZSPConfig.initWithArguments(args)) {
			System.out.println("usage: \"java -jar zsp.jar [options]\"\n");
			System.out.println("available options:");
			System.out.println(
				"\t--configure\tstarts ZidestoreProxy in configuration mode");
			System.out.println(
				"\t--init-cache\tfills the local cache with data from zidestore server and quits");
			System.out.println("\t--empty-cache\tcleans up the cache directory");
			System.out.println("\t-u user\t\tzidestore user");
			System.out.println("\t-p password\tpassword for zidestore user");
			System.out.println("\t-U url\t\tURL of the zidestore server");
			System.out.println("\t-D dir\t\tdirectory to store cache data");
			System.out.println(
				"\t-i interval\tinterval in minutes for spider to check for changes");
			System.out.println(
				"\t-P port\t\tport number to listen on (default 23000)");
			System.out.println("\t--no-spider\tdisable spider");
			System.out.println(
				"If no options given, ZideStoreProxy tries to "
					+ "load them from its config file loacted in zsp.conf in "
					+ "users home directory.");
			System.out.println(
				"If no config file found, a configuration dialog "
					+ "will be displayed.");

			System.out.println();
			System.exit(0);
		}

		ZidestoreProxy zsp = new ZidestoreProxy();

		// check for configuration mode 
		if ("true".equals(System.getProperty("zsp.configMode"))) {
			ConfigDialog configDialog = new ConfigDialog();
			configDialog.pack();
			configDialog.show();
			System.exit(0);
		}

		String errorMessage = ZSPConfig.checkConfig();
		while (errorMessage != null) {
			JOptionPane.showMessageDialog(
				null,
				"ZideStoreProxy detected an error in its configuration: "
					+ errorMessage,
				"Configuration error",
				JOptionPane.ERROR_MESSAGE);

			ConfigDialog configDialog = new ConfigDialog();
			configDialog.pack();
			configDialog.show();

			errorMessage = ZSPConfig.checkConfig();
		}

		try {
			CacheManager.getInstance(ZSPConfig.getProperty("cache_dir"));
		}
		catch (CacheManageException e) {
			System.err.println("Error: " + e.getMessage());
			System.exit(1);
		}

		if ("true".equals(System.getProperty("zsp.cleanMode"))) {
			System.out.println("Cleaning up cache directory...");
			try {
				System.out.println(CacheManager.getInstance().clearCache());
			}
			catch (Exception e) {
				e.printStackTrace();
				System.err.println("Error: " + e.getMessage());
				System.exit(1);
			}
			System.exit(0);
		}

		new ZidestoreConnection();

		InetAddress address = null;
		try {
			address = InetAddress.getLocalHost();
			System.setProperty("zsp.proxyName", address.getCanonicalHostName());
		}
		catch (UnknownHostException e) {
			System.out.println("Unable to determine local IP address.");
			System.exit(1);
		}

		//Trayicon is only avalable when running at MS Windows
		try {
			SystemTray.startSystemTray();
		}
		catch (Error e) {
		}

		// initialize the proxy
		new Proxy();
		//Proxy.setOnline(false);

		// start the spider
		ZidestoreSpider spider = new ZidestoreSpider(userName, password, url);

		if ("true".equals(System.getProperty("zsp.initMode"))) {
			spider.runSpiderOnce();
			System.exit(0);
		}

		try {
			//Registering HttpServer method modules
			HttpServer.registerOptionModule(new OptionsModule());
			HttpServer.registerOptionModule(new GetModule());
			HttpServer.registerOptionModule(new PropFindModule());
			//HttpServer.registerOptionModule(new PropPatchModule());

			new HttpServer();
		}
		catch (Exception ioe) {
			System.err.println("Couldn't start server:\n" + ioe);
			System.exit(-1);
		}

		//  Creating Spooler
		SpoolerQueue.getInstance();
//		queue.createMessage(
//			CacheManager.getInstance().getMessage(
//				"/zidestore/so/sell/IPM/public/Contacts/",
//				"new-msg-1065023243820"));
//
//		new Spooler(queue);

		//System.exit(0);

	}
}
