/*
 * Licensed to the University of California, Berkeley under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package tachyon.shell;

import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.annotation.concurrent.NotThreadSafe;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.lang.StringUtils;
import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;

import tachyon.Constants;
import tachyon.client.file.FileSystem;
import tachyon.conf.TachyonConf;
import tachyon.shell.command.TfsShellCommand;
import tachyon.util.CommonUtils;

/**
 * Class for handling command line inputs.
 */
@NotThreadSafe
public class TfsShell implements Closeable {
  private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);
  private static final HashMap<String, String[]> CMD_ALIAS = new HashMap<String, String[]>() { {
      put("chgrpr", new String[] {"chgrp", "-R"});
      put("chmodr", new String[] {"chmod", "-R"});
      put("chownr", new String[] {"chown", "-R"});
      // TODO(chaomin): also add "lsr", "rmr" as alias.
    } };

  /**
   * Main method, starts a new TfsShell.
   *
   * @param argv [] Array of arguments given by the user's input from the terminal
   * @throws IOException if closing the shell fails
   */
  public static void main(String[] argv) throws IOException {
    TfsShell shell = new TfsShell(new TachyonConf());
    int ret;
    try {
      ret = shell.run(argv);
    } finally {
      shell.close();
    }
    System.exit(ret);
  }

  private final Map<String, TfsShellCommand> mCommands = Maps.newHashMap();
  private final TachyonConf mTachyonConf;
  private final FileSystem mFileSystem;

  /**
   * @param tachyonConf the configuration for Tachyon
   */
  public TfsShell(TachyonConf tachyonConf) {
    mTachyonConf = tachyonConf;
    mFileSystem = FileSystem.Factory.get();
    loadCommands();
  }

  @Override
  public void close() throws IOException {
  }

  /**
   * Uses reflection to get all the {@link TfsShellCommand} classes and store them in a map.
   */
  private void loadCommands() {
    String pkgName = TfsShellCommand.class.getPackage().getName();
    Reflections reflections = new Reflections(pkgName);
    for (Class<? extends TfsShellCommand> cls : reflections.getSubTypesOf(TfsShellCommand.class)) {
      // Only instantiate a concrete class
      if (!Modifier.isAbstract(cls.getModifiers())) {
        TfsShellCommand cmd;
        try {
          cmd = CommonUtils.createNewClassInstance(cls,
              new Class[] { TachyonConf.class, FileSystem.class },
              new Object[] { mTachyonConf, mFileSystem });
        } catch (Exception e) {
          throw Throwables.propagate(e);
        }
        mCommands.put(cmd.getCommandName(), cmd);
      }
    }
  }

  /**
   * Gets the replacement command for alias.
   *
   * @param command name
   * @return replacement command if cmd is an alias
   */
  private String[] getReplacementCmd(String cmd) {
    if (CMD_ALIAS.containsKey(cmd)) {
      return CMD_ALIAS.get(cmd);
    } else {
      return null;
    }
  }

  /**
   * Method which prints the method to use all the commands.
   */
  private void printUsage() {
    System.out.println("Usage: java TfsShell");
    SortedSet<String> sortedCmds = new TreeSet<String>(mCommands.keySet());
    for (String cmd : sortedCmds) {
      System.out.format("%-60s%-95s%n", "       [" + mCommands.get(cmd).getUsage() + "]   ",
          mCommands.get(cmd).getDescription());
    }
  }

  /**
   * Method which determines how to handle the user's request, will display usage help to the user
   * if command format is incorrect.
   *
   * @param argv [] Array of arguments given by the user's input from the terminal
   * @return 0 if command is successful, -1 if an error occurred
   */
  public int run(String... argv) {
    if (argv.length == 0) {
      printUsage();
      return -1;
    }

    // Sanity check on the number of arguments
    String cmd = argv[0];
    TfsShellCommand command = mCommands.get(cmd);

    if (command == null) { // Unknown command (we didn't find the cmd in our dict)
      String[] replacementCmd = getReplacementCmd(cmd);
      if (replacementCmd == null) {
        System.out.println(cmd + " is an unknown command.\n");
        printUsage();
        return -1;
      }
      // Handle command alias.
      String[] replacementArgv = new String[replacementCmd.length + argv.length - 1];
      for (int i = 0; i < replacementCmd.length; ++ i) {
        replacementArgv[i] = replacementCmd[i];
      }
      for (int i = 1; i < argv.length; ++ i) {
        replacementArgv[i + replacementCmd.length - 1] = argv[i];
      }
      return run(replacementArgv);
    }

    String[] args = Arrays.copyOfRange(argv, 1, argv.length);
    CommandLine cmdline = command.parseAndValidateArgs(args);
    if (cmdline == null) {
      printUsage();
      return -1;
    }

    // Handle the command
    try {
      command.run(cmdline);
      return 0;
    } catch (IOException e) {
      System.out.println(e.getMessage());
      LOG.error("Error running " + StringUtils.join(argv, " "), e);
      return -1;
    }
  }
}

@@ -5289,25 +5289,291 @@
 ommand alias
-.
+, and print out WARNING message for deprecated cmd.%0A      String deprecatedMsg = %22WARNING: %22 + cmd + %22 is deprecated. Please use %22%0A                             + replacementCmd%5B0%5D + %22 instead.%22;%0A      System.out.println(deprecatedMsg);%0A      LOG.warn(deprecatedMsg);%0A
 %0A      Strin

Print a warning msg for deprecated command.