public static void main(String[] arg)
	{
		try
		{
			statistics.setRunningSince(System.currentTimeMillis());

			System.out.println(version);
			System.out.println("");
			System.out.println("Please wait while initializing...");

			config = new Config();

			for(int loop=0; loop<arg.length; loop++)
			{
				String currentSwitch = arg[loop];

				try
				{
					if (arg[loop].equals("--create-config"))
					{
						config.writeConfig(arg[++loop]);
						config.setConfigFilename(arg[loop]);
					}
					else if (arg[loop].equals("--allow-all-ssl"))
						allowAllSSL();
					else if (arg[loop].equals("--use-screen"))
						config.setUseScreen(arg[++loop]);
					else if (arg[loop].equals("--footer"))
						config.setFooter(arg[++loop]);
					else if (arg[loop].equals("--list-screens"))
					{
						showAvailableScreens();
						System.exit(0);
					}
					else if (arg[loop].equals("--source"))
					{
						NagiosDataSource nds = null;
						NagiosVersion nv = null;
						String type = arg[++loop];
						String versionStr = arg[++loop];

						if (versionStr.equals("1"))
							nv = NagiosVersion.V1;
						else if (versionStr.equals("2"))
							nv = NagiosVersion.V2;
						else if (versionStr.equals("3"))
							nv = NagiosVersion.V3;
						else
							errorExit("Nagios version '" + versionStr + "' not known");

						if (type.equalsIgnoreCase("http") || type.equalsIgnoreCase("http-auth")) {
							boolean withAuth = type.equalsIgnoreCase("http-auth");
							String urlStr = arg[++loop];
							String resultStr = testUrlString(urlStr);
							if (resultStr != null)
								errorExit("Cannot parse url " + urlStr + ": " + resultStr);
							URL url = new URL(urlStr);
							String resultUrl = testUrl(url, withAuth);
							if (resultUrl != null)
								errorExit("Cannot use url " + url + " (" + resultUrl + ")");
							if (withAuth) {
								String username = arg[++loop];
								String password = arg[++loop];
								String pn = arg[++loop];
								nds = new NagiosDataSource(url, username, password, nv, pn);
							}
							else {
								String pn = arg[++loop];
								nds = new NagiosDataSource(url, nv, pn);
							}
						}
						else if (type.equalsIgnoreCase("file")) {
							String filename = arg[++loop];
							String pn = arg[++loop];
							String result = testFile(filename);
							if (result != null)
								errorExit("Cannot access file " + filename + " (" + result + ")");
							nds = new NagiosDataSource(filename, nv, pn);
						}
						else if (type.equalsIgnoreCase("tcp") || type.equalsIgnoreCase("ztcp") || type.equalsIgnoreCase("ls")) {
							String host = arg[++loop];
							int port;
							try {
								port = Integer.valueOf(arg[++loop]);
								String pn = arg[++loop];
								String result = testPort(host, port);
								if (result != null)
									errorExit("Cannot open socket on " + host + ":" + port + " (" + result + ")");
								if (type.equalsIgnoreCase("ls"))
									nds = new NagiosDataSource(host, port, pn);
								else
									nds = new NagiosDataSource(host, port, nv, type.equalsIgnoreCase("ztcp"), pn);
							}
							catch(NumberFormatException nfe) {
								errorExit("--source: expecting a port-number but got '" + arg[loop] + "'");
							}
						}
						else
							errorExit("Data source-type '" + type + "' not understood.");

						config.addNagiosDataSource(nds);
					}
					else if (arg[loop].equals("--sort-order")) {
						boolean reverse = false, numeric = false;

						for(;;) {
							++loop;
							if (arg[loop].equals("reverse"))
								reverse = true;
							else if (arg[loop].equals("numeric"))
								numeric = true;
							else
								break;
						}

						config.setSortOrder(arg[loop], numeric, reverse);
					}
					else if (arg[loop].equals("--no-header"))
						config.setShowHeader(false);
					else if (arg[loop].equals("--anti-alias"))
						config.setAntiAlias(true);
					else if (arg[loop].equals("--scrolling-header"))
						config.setScrollingHeader(true);
					else if (arg[loop].equals("--scroll-pixels-per-sec"))
						config.setScrollingPixelsPerSecond(Integer.valueOf(arg[++loop]));
					else if (arg[loop].equals("--fullscreen")) {
						String mode = arg[++loop];
						if (mode.equalsIgnoreCase("none"))
							config.setFullscreen(FullScreenMode.NONE);
						else if (mode.equalsIgnoreCase("undecorated"))
							config.setFullscreen(FullScreenMode.UNDECORATED);
						else if (mode.equalsIgnoreCase("fullscreen"))
							config.setFullscreen(FullScreenMode.FULLSCREEN);
						else if (mode.equalsIgnoreCase("allmonitors"))
							config.setFullscreen(FullScreenMode.ALLMONITORS);
						else
							errorExit("Fullscreen mode " + mode + " not recognized");
					}
					else if (arg[loop].equals("--header"))
						config.setHeader(arg[++loop]);
					else if (arg[loop].equals("--web-username"))
						config.setWebUsername(arg[++loop]);
					else if (arg[loop].equals("--web-password"))
						config.setWebPassword(arg[++loop]);
					else if (arg[loop].equals("--row-border"))
						config.setRowBorder(true);
					else if (arg[loop].equals("--service-issue"))
						config.setServiceIssue(arg[++loop]);
					else if (arg[loop].equals("--host-issue"))
						config.setHostIssue(arg[++loop]);
					else if (arg[loop].equals("--random-img"))
						config.setRandomWebcam(true);
					else if (arg[loop].equals("--no-gui"))
					{
						System.err.println("Don't forget to invoke the JVM with -Djava.awt.headless=true !");
						config.setRunGui(false);
					}
					else if (arg[loop].equals("--config"))
					{
						String filename = arg[++loop];
						String result = testFile(filename);
						if (result != null)
							errorExit("Cannot open configuration file " + filename + " (" + result + ")");
						config.loadConfig(filename);
						if (config.getAllowAllSSL())
							allowAllSSL();
					}
					else if (arg[loop].equals("--predict"))
						config.setBrainFileName(arg[++loop]);
					else if (arg[loop].equals("--performance-data-filename"))
						config.setPerformanceDataFileName(arg[++loop]);
					else if (arg[loop].equals("--exec"))
						config.setExec(arg[++loop]);
					else if (arg[loop].equals("--adapt-img"))
						config.setAdaptImageSize(true);
					else if (arg[loop].equals("--counter"))
						config.setCounter(true);
					else if (arg[loop].equals("--verbose"))
						config.setVerbose(true);
					else if (arg[loop].equals("--sound"))
						config.setProblemSound(arg[++loop]);
					else if (arg[loop].equals("--listen-port"))
					{
						try
						{
							config.setHTTPServerListenPort(Integer.valueOf(arg[++loop]));
						}
						catch(NumberFormatException nfe)
						{
							errorExit("--listen-port: expecting a port-number but got '" + arg[loop] + "'");
						}
					}
					else if (arg[loop].equals("--listen-adapter"))
						config.setHTTPServerListenAdapter(arg[++loop]);
					else if (arg[loop].equals("--list-bgcolors"))
					{
						config.listColors();
						System.exit(0);
					}
					else if (arg[loop].equals("--warning-bg-color"))
						config.setWarningBgColor(arg[++loop]);
					else if (arg[loop].equals("--warning-bg-color-soft"))
						config.setWarningBgColorSoft(arg[++loop]);
					else if (arg[loop].equals("--critical-bg-color"))
						config.setCriticalBgColor(arg[++loop]);
					else if (arg[loop].equals("--critical-bg-color-soft"))
						config.setCriticalBgColorSoft(arg[++loop]);
					else if (arg[loop].equals("--nagios-unknown-bg-color"))
						config.setNagiosUnknownBgColor(arg[++loop]);
					else if (arg[loop].equals("--bgcolor"))
						config.setBackgroundColor(arg[++loop]);
					else if (arg[loop].equals("--bgcolor-fade-to"))
						config.setBackgroundColorFadeTo(arg[++loop]);
					else if (arg[loop].equals("--problem-row-gradient"))
						config.setProblemRowGradient(arg[++loop]);
					else if (arg[loop].equals("--textcolor"))
						config.setTextColor(arg[++loop]);
					else if (arg[loop].equals("--nrows"))
						config.setNRows(Integer.valueOf(arg[++loop]));
					else if (arg[loop].equals("--min-row-height"))
						config.setMinRowHeight(Integer.valueOf(arg[++loop]));
					else if (arg[loop].equals("--interval"))
						config.setSleepTime(Integer.valueOf(arg[++loop]));
					else if (arg[loop].equals("--flash"))
						config.setFlash(true);
					else if (arg[loop].equals("--webcam-timeout"))
						config.setWebcamTimeout(Integer.valueOf(arg[++loop]));
					else if (arg[loop].equals("--image"))
					{
						String type = arg[++loop];
						String what = arg[++loop];
						if (what.length() > 7 && what.substring(0, 7).equalsIgnoreCase("http://"))
						{
							String result = testUrlString(what);
							if (result != null)
								errorExit("Cannot open image-url " + what + " (" + result + ")");
							URL url = new URL(what);
							result = testUrl(url, false);
							if (result != null)
								errorExit("Cannot open image-url " + what + " (" + result + ")");
						}
						else
						{
							String result = testFile(what);
							if (result != null)
								errorExit("Cannot open image-file " + what + " (" + result + ")");
						}
						config.addImageUrl(type + " " + what);
					}
					else if (arg[loop].equals("--problem-columns"))
						config.setNProblemCols(Integer.valueOf(arg[++loop]));
					else if (arg[loop].equals("--cam-rows"))
						config.setCamRows(Integer.valueOf(arg[++loop]));
					else if (arg[loop].equals("--cam-cols"))
						config.setCamCols(Integer.valueOf(arg[++loop]));
					else if (arg[loop].equals("--reduce-textwidth"))
						config.setReduceTextWidth(true);
					else if (arg[loop].equals("--max-quality-graphics"))
						config.setMaxQualityGraphics(true);
					else if (arg[loop].equals("--flexible-n-columns"))
						config.setFlexibleNColumns(true);
					else if (arg[loop].equals("--disable-http-fileselect"))
						config.setDisableHTTPFileselect();
					else if (arg[loop].equals("--prefer"))
						config.setPrefers(arg[++loop]);
					else if (arg[loop].equals("--always-notify"))
						config.setAlwaysNotify(true);
					else if (arg[loop].equals("--suppress-flapping"))
						config.setShowFlapping(false);
					else if (arg[loop].equals("--show-flapping-icon"))
						config.setShowFlappingIcon(true);
					else if (arg[loop].equals("--also-acknowledged"))
						config.setAlsoAcknowledged(true);
					else if (arg[loop].equals("--font"))
						config.setFontName(arg[++loop]);
					else if (arg[loop].equals("--list-fonts"))
					{
						config.listFonts();
						System.exit(0);
					}
					else if (arg[loop].equals("--no-network-change"))
						config.setNoNetworkChange(true);
					else if (arg[loop].equals("--critical-font"))
						config.setCriticalFontName(arg[++loop]);
					else if (arg[loop].equals("--warning-font"))
						config.setWarningFontName(arg[++loop]);
					else if (arg[loop].equals("--warning-textcolor"))
						config.setWarningTextColor(arg[++loop]);
					else if (arg[loop].equals("--critical-textcolor"))
						config.setCriticalTextColor(arg[++loop]);
					else if (arg[loop].equals("--row-border-color"))
						config.setRowBorderColor(arg[++loop]);
					else if (arg[loop].equals("--graph-color"))
						config.setGraphColor(arg[++loop]);
					else if (arg[loop].equals("--latency-file"))
						config.setLatencyFile(arg[++loop]);
					else if (arg[loop].equals("--ignore-aspect-ratio"))
						config.setKeepAspectRatio(false);
					else if (arg[loop].equals("--also-scheduled-downtime"))
						config.setAlsoScheduledDowntime(true);
					else if (arg[loop].equals("--show-services-for-host-with-problems"))
						config.setShowServicesForHostWithProblems(true);
					else if (arg[loop].equals("--also-soft-state"))
						config.setAlsoSoftState(true);
					else if (arg[loop].equals("--also-disabled-active-checks"))
						config.setAlsoDisabledActiveChecks(true);
					else if (arg[loop].equals("--disable-http-compression"))
						config.setAllowHTTPCompression(false);
					else if (arg[loop].equals("--transparency"))
						config.setTransparency(Float.valueOf(arg[++loop]));
					else if (arg[loop].equals("--max-check-age"))
						config.setMaxCheckAge(Long.valueOf(arg[++loop]));
					else if (arg[loop].equals("--header-transparency"))
						config.setHeaderTransparency(Float.valueOf(arg[++loop]));
					else if (arg[loop].equals("--hosts-filter-exclude"))
						config.setHostsFilterExclude(arg[++loop]);
					else if (arg[loop].equals("--hosts-filter-include"))
						config.setHostsFilterInclude(arg[++loop]);
					else if (arg[loop].equals("--services-filter-exclude"))
						config.setServicesFilterExclude(arg[++loop]);
					else if (arg[loop].equals("--services-filter-include"))
						config.setServicesFilterInclude(arg[++loop]);
					else if (arg[loop].equals("--scroll-splitter") || arg[loop].equals("--splitter"))
						config.setLineScrollSplitter(arg[++loop].trim());
					else if (arg[loop].equals("--draw-problems-service-split-line"))
						config.setDrawProblemServiceSplitLine(true);
					else if (arg[loop].equals("--sparkline-width"))
						config.setSparkLineWidth(Integer.valueOf(arg[++loop]));
					else if (arg[loop].equals("--scroll-if-not-fitting"))
						config.setScrollIfNotFit(true);
					else if (arg[loop].equals("--counter-position"))
						config.setCounterPosition(arg[++loop]);
					else if (arg[loop].equals("--no-problems-text"))
						config.setNoProblemsText(arg[++loop]);
					else if (arg[loop].equals("--state-problems-text"))
						config.setStateProblemsText(arg[++loop]);
					else if (arg[loop].equals("--no-problems-text-position"))
						config.setNoProblemsTextPosition(arg[++loop]);
					else if (arg[loop].equals("--no-authentication"))
						config.setAuthentication(false);
					else if (arg[loop].equals("--header-always-bgcolor"))
						config.setHeaderAlwaysBGColor(true);
					else if (arg[loop].equals("--color-bg-to-state"))
						config.setSetBgColorToState(true);
					else if (arg[loop].equals("--filter-unknown"))
						config.setDisplayUnknown(false);
					else if (arg[loop].equals("--filter-down"))
						config.setDisplayDown(false);
					else if (arg[loop].equals("--logo"))
					{
						String what = arg[++loop];
						if (what.length() > 7 && what.substring(0, 7).equalsIgnoreCase("http://"))
						{
							String result = testUrlString(what);
							if (result != null)
								errorExit("Cannot open logo-image url " + what + " (" + result + ")");
							URL url = new URL(what);
							result = testUrl(url, false);
							if (result != null)
								errorExit("Cannot open image-url " + what + " (" + result + ")");
						}
						else
						{
							String result = testFile(what);
							if (result != null)
								errorExit("Cannot open logo-image file " + what + " (" + result + ")");
						}
						config.setLogo(what);
					}
					else if (arg[loop].equals("--logo-position"))
						config.setLogoPosition(arg[++loop]);
					else if (arg[loop].equals("--logfile"))
						log.setLogFile(arg[++loop]);
					else if (arg[loop].equals("--proxy-host"))
						config.setProxyHost(arg[++loop]);
					else if (arg[loop].equals("--proxy-port"))
						config.setProxyPort(Integer.valueOf(arg[++loop]));
					else if (arg[loop].equals("--suppress-services-for-scheduled-host-downtime"))
						config.setHostScheduledDowntimeShowServices(false);
					else if (arg[loop].equals("--suppress-services-for-acknowledged-host-problems"))
						config.setHostAcknowledgedShowServices(false);
					else if (arg[loop].equals("--enable-double-buffering"))
						config.setDoubleBuffering(true);
					else if (arg[loop].equals("--sparkline-mode"))
					{
						String mode = arg[++loop];
						if (mode.equals("avg-sd"))
							config.setSparklineGraphMode(SparklineGraphMode.AVG_SD);
						else if (mode.equals("min-max"))
							config.setSparklineGraphMode(SparklineGraphMode.MIN_MAX);
					}
					else if (arg[loop].equals("--version") || arg[loop].equals("-version"))
					{
						System.out.println(getVersion());
						System.exit(0);
					}
					else if (arg[loop].equals("--help") || arg[loop].equals("--h"))
					{
						showHelp();
						System.exit(0);
					}
					else
					{
						showHelp();
						errorExit("Parameter " + arg[loop] + " not understood.");
					}
				}
				catch(ArrayIndexOutOfBoundsException aioobe)
				{
					errorExit(currentSwitch + ": expects more parameters than currently given");
				}
				catch(NumberFormatException nfeGlobal)
				{
					errorExit(currentSwitch + ": one of the parameters given should've been a number");
				}
			}

			CoffeeSaint coffeeSaint = new CoffeeSaint();
			Gui gui = null;
			if (config.getRunGui())
			{
				System.out.println("Start gui");

				Monitor monitor = null;
				gui = new Gui(config, coffeeSaint, statistics);

				if (config.getUseScreen() != null)
				{
					monitor = coffeeSaint.selectScreen(config.getUseScreen());
					if (monitor == null)
					{
						System.err.println("Screen '" + config.getUseScreen() + "' is not known.");
						System.err.println("Please use '--list-screens' to see a list of known displays.");
					}
				}
				else
				{
					GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
					GraphicsDevice gd = ge.getDefaultScreenDevice();
					JFrame f = new JFrame();
					/* create frame to draw in */
					Rectangle useable = ge.getMaximumWindowBounds();
					//f.setMaximizedBounds(useable);
					f.setLocation(useable.x, useable.y);
					f.setSize(useable.width, useable.height);
					GraphicsConfiguration gc = gd.getDefaultConfiguration();
					monitor = new Monitor(gd, gc, gc.getDevice().getIDstring(), useable);
					monitor.setJFrame(f);
				}

				JFrame f = monitor.getJFrame();

				if (config.getFullscreen() == FullScreenMode.FULLSCREEN)
				{
					System.out.println("FULLSCREEN");
					f.setUndecorated(true);
					f.setResizable(false);
					monitor.getGraphicsDevice().setFullScreenWindow(f);
				}
				else if (config.getFullscreen() == FullScreenMode.ALLMONITORS)
				{
					System.out.println("ALLMONITORS");
					f.setUndecorated(true);
					f.setResizable(false);
					Rectangle allMonitors = coffeeSaint.getDimensionsOverAllMonitors();
					f.setLocation(allMonitors.x, allMonitors.y);
					f.setSize(allMonitors.width, allMonitors.height);
				}
				else
				{
					//					f.setExtendedState(f.getExtendedState() | JFrame.MAXIMIZED_BOTH);

					if (config.getFullscreen() == FullScreenMode.UNDECORATED)
					{
						System.out.println("UNDECORATED");
						f.setUndecorated(true);
						f.setResizable(false);
					}
					else
					{
						System.out.println("WINDOWED");
					}
				}

				f.setContentPane(gui);

				RepaintManager.currentManager(gui).setDoubleBufferingEnabled(config.getDoubleBuffering());

				System.out.println("Initial paint");

				f.setTitle(getVersion());
				setIcon(coffeeSaint, f);

				f.setVisible(true);

				f.addWindowListener(new FrameListener(config, coffeeSaint));
			}

			if (config.getHTTPServerListenPort() != -1)
			{
				System.out.println("Start HTTP server");
				Thread httpServer = new Thread(new HTTPServer(config, coffeeSaint, statistics, gui));
				httpServer.setPriority(Thread.MAX_PRIORITY);
				httpServer.start();
			}

			if (config.getRunGui())
			{
				System.out.println("Start gui loop");
				gui.guiLoop();
			}
			else
			{
				System.out.println("Start daemon loop");
				daemonLoop(coffeeSaint, config);
			}
		}
		catch(Exception e)
		{
			showException(e);
		}
	}