/
area/
classes/net/sourceforge/pain/logic/
classes/net/sourceforge/pain/logic/event/
classes/net/sourceforge/pain/logic/fn/util/
classes/net/sourceforge/pain/network/console/
classes/net/sourceforge/pain/plugin/
classes/net/sourceforge/pain/plugin/reset/
classes/net/sourceforge/pain/plugin/shutdown/
classes/net/sourceforge/pain/plugin/social/
classest/net/sourceforge/pain/db/data/
doc/
doc/paindb/resources/
src/net/sourceforge/pain/logic/
src/net/sourceforge/pain/logic/event/
src/net/sourceforge/pain/logic/fn/util/
src/net/sourceforge/pain/network/console/
src/net/sourceforge/pain/network/console/telnet/
src/net/sourceforge/pain/plugin/
src/net/sourceforge/pain/plugin/command/
src/net/sourceforge/pain/plugin/reset/
src/net/sourceforge/pain/plugin/shutdown/
src/net/sourceforge/pain/plugin/social/
src/net/sourceforge/pain/util/
tests/
tests/net/sourceforge/pain/db/data/
package net.sourceforge.pain.logic.event.console.command;

import net.sourceforge.pain.*;
import net.sourceforge.pain.util.*;
import net.sourceforge.pain.logic.event.console.*;
import net.sourceforge.pain.logic.fn.*;
import net.sourceforge.pain.plugin.*;
import net.sourceforge.pain.plugin.shutdown.*;


public final class Shutdown extends GrantedCommand {

	final static String SHUTDOWN_PLUGIN_NAME = "shutdown.ShutdownTimer";

	public void processCommand() throws Exception {
		if (commandParams == null) {
			showUsage();
			return;
		}
		if ("EMERGENCY".equals(commandParams)) { // if there are some problems with plugins or code..
			Core.getTime().stopTimer();
		} else {
			PluginManager plm = Core.getPluginManager();
			ShutdownTimer shutTimer = (ShutdownTimer) plm.getPlugin(SHUTDOWN_PLUGIN_NAME);

			if (shutTimer == null) {
				MessageOutFn.outln(console, "ShutdownTimer plugin is not loaded!");
				return;
			}

			commandParams = commandParams.toUpperCase();
			final boolean wasRunned = shutTimer.isShutdownInProcess();
			Log.debug("shutdown was runned:" + wasRunned);
			if ("STOP".equals(commandParams) || "CANCEL".equals(commandParams)) {
				if (wasRunned) {
					shutTimer.cancel(true);
				} else {
					MessageOutFn.outln(console, "Shutdown was not runned!");
					return;
				}
			} else if ("NOW".equals(commandParams)) {
				shutTimer.now();
			} else if ("TIME".equals(commandParams)) {
				if (!wasRunned) {
					MessageOutFn.outln(console, "Shutdown was not runned!");
					return;
				}
				MessageOutFn.outln(console, "Shutdown time " + shutTimer.getTimeBeforeShutdown() + " sec.");
			} else {
				int minutes;
				try {
					minutes = Integer.parseInt(commandParams);
				} catch (Exception e) {
					showUsage();
					return;
				}
				minutes = Math.max(minutes, 0);
				shutTimer.reset(minutes * 60, !wasRunned);
				if (wasRunned) {
					MessageOutFn.outln(console, "Shutdown time changed");
				}
			}

		}
	}

	private void showUsage() {
		MessageOutFn.outln(console, command.name + ": specify time in minutes to shutdown, 'NOW' for instant shutdown or 'CANCEL' to stop");
	}
}