/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.integration.tool.portfolio;

import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opengamma.OpenGammaRuntimeException;
import com.opengamma.component.tool.AbstractTool;
import com.opengamma.integration.tool.IntegrationToolContext;
import com.opengamma.master.portfolio.ManageablePortfolio;
import com.opengamma.master.portfolio.PortfolioDocument;
import com.opengamma.master.portfolio.PortfolioSearchRequest;
import com.opengamma.master.portfolio.PortfolioSearchResult;
import com.opengamma.scripts.Scriptable;

/**
 * Tool to aggregate portfolios
 */
@Scriptable
public class PortfolioCopyMoveTool extends AbstractTool<IntegrationToolContext> {

  private static final Logger s_logger = LoggerFactory.getLogger(PortfolioCopyMoveTool.class);

  private static final String ORIGINAL_PORTFOLIO_NAME = "n";
  private static final String NEW_PORTFOLIO_NAME = "m";
  private static final String COPY_OPT = "d";

  /**
   * Runs the tool.
   *
   * @param args  empty arguments
   */
  public static void main(String[] args) {  // CSIGNORE
    new PortfolioCopyMoveTool().initAndRun(args, IntegrationToolContext.class);
  }

  @Override
  protected void doRun() {
    PortfolioSearchRequest portfolioSearchRequest = new PortfolioSearchRequest();
    portfolioSearchRequest.setName(getCommandLine().getOptionValue(ORIGINAL_PORTFOLIO_NAME));
    PortfolioSearchResult portfolioSearchResult = getToolContext().getPortfolioMaster().search(portfolioSearchRequest);

    if (portfolioSearchResult.getDocuments().size() == 0) {
      throw new OpenGammaRuntimeException("No matching portfolio could be found, will not proceed.");
    } else if (portfolioSearchResult.getDocuments().size() > 1) {
      throw new OpenGammaRuntimeException("More than one portfolio matches, will not proceed.");
    } else {
      PortfolioDocument portfolioDocument = portfolioSearchResult.getFirstDocument();
      PortfolioSearchRequest portfolioSearchRequest1 = new PortfolioSearchRequest();
      portfolioSearchRequest1.setName(getCommandLine().getOptionValue(NEW_PORTFOLIO_NAME));
      PortfolioSearchResult portfolioSearchResult1 = getToolContext().getPortfolioMaster().search(portfolioSearchRequest1);
      if (portfolioSearchResult1.getDocuments().size() != 0) {
        throw new OpenGammaRuntimeException("A portfolio with the specified new name already exists, will not proceed.");
      }
      if (getCommandLine().hasOption(COPY_OPT)) {
        // Copy - check if new portfolio name already in use
        portfolioDocument.getPortfolio().setName(getCommandLine().getOptionValue(NEW_PORTFOLIO_NAME));
        getToolContext().getPortfolioMaster().add(portfolioDocument);
      } else {
        // Move
        portfolioDocument.getPortfolio().setName(getCommandLine().getOptionValue(NEW_PORTFOLIO_NAME));
        getToolContext().getPortfolioMaster().update(portfolioDocument);
      }
    }
  }

  protected Options createOptions(boolean contextProvided) {
    Options options = super.createOptions(contextProvided);

    Option origNameOption = new Option(
        ORIGINAL_PORTFOLIO_NAME, "origname", true, "The name of the OpenGamma portfolio to copy or rename");
    origNameOption.setRequired(true);
    options.addOption(origNameOption);

    Option newNameOption = new Option(
        NEW_PORTFOLIO_NAME, "newname", true, "The new name of the OpenGamma portfolio");
    newNameOption.setRequired(true);
    options.addOption(newNameOption);

    Option copyOption = new Option(
        COPY_OPT, "duplicate", false, "If specified, copy the portfolio instead of moving it (but share referenced positions and securities)");
    copyOption.setRequired(false);
    options.addOption(copyOption);

    return options;
  }
}

@@ -260,111 +260,9 @@
 tion
-Builder;%0Aimport org.apache.commons.cli.Options;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
+s
 ;%0A%0Ai
@@ -424,67 +424,8 @@
 xt;%0A
-import com.opengamma.master.portfolio.ManageablePortfolio;%0A
 impo
@@ -781,104 +781,8 @@
  %7B%0A%0A
-  private static final Logger s_logger = LoggerFactory.getLogger(PortfolioCopyMoveTool.class);%0A%0A
   pr
@@ -1067,24 +1067,42 @@
 CSIGNORE%0A   
+ boolean success =
  new Portfol
@@ -1166,16 +1166,50 @@
 class);%0A
+    System.exit(success ? 0 : 1);%0A
   %7D%0A%0A  @

Exit after tool has run