/*
 * LOGICAL-PARADOX.ORG
 * Copyright (C)2005 satoshi akabane(akabane@logical-paradox.org)
 * $Id: dac.java,v 1.2 2005/09/07 13:38:06 rampil Exp $
 */
package org.logical_paradox.vsan.tools.console.command;

import java.io.OutputStream;
import java.io.PrintWriter;

import org.logical_paradox.shell.Argument;
import org.logical_paradox.shell.Command;
import org.logical_paradox.shell.Option;
import org.logical_paradox.shell.ResultCode;
import org.logical_paradox.shell.ShellEnv;
import org.logical_paradox.shell.exception.CommandExecutionException;
import org.logical_paradox.shell.exception.TerminationException;
import org.logical_paradox.vsan.tools.console.DacConsoleConstant;

/**
 * <pre>
 * ■dac
 * 
 * 【動作仕様】
 * DACの所在位置を表示または変更します。
 * 
 * 【書式】
 * dac [ホストアドレス]
 * 
 * 【例】
 * dac (現在のDAC設定を表示します)
 * dac localhost:9876 (DAC設定をlocalhost:9876に変更します)
 * </pre>
 * 
 * @author satoshi akabane@logical-paradox.org
 * @version $Revision: 1.2 $
 */
public class dac implements Command {
	public static final String USAGE = "dac [<hostname>[:<port>]]";

	public String usage() {
		return USAGE;
	}

	public ResultCode execute(OutputStream o, ShellEnv env, Argument a) throws CommandExecutionException, TerminationException {
		if(a == null) {
			a = new Argument();
		}
		String[] args = a.getArguments();
		Option[] opts = (Option[])a.getOptions();

		if(args.length > 1 || opts.length > 0) {
			// 引数の数が不正
			throw new CommandExecutionException();
		}

		if(args.length == 0) {
			// ホスト設定なしの場合
			PrintWriter pw = new PrintWriter(o);
			String dachost = env.getProperty(DacConsoleConstant.ENV_DAC);
			if(dachost == null) {
				dachost = DacConsoleConstant.DACC_UNDEFINED_HOST;
			}
			pw.println(dachost);
			pw.flush();
			pw.close();
		} else {
			// ホスト設定ありの場合
			// 環境変数に対してDACの設定をする
			env.setProperty(DacConsoleConstant.ENV_DAC, args[0]);
		}
		return new ResultCode(ResultCode.STATUS_OK);
	}

}
