/*
 * Licensed to the Apache Software Foundation (ASF) 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 org.apache.accumulo.shell.commands;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import org.apache.accumulo.core.client.AccumuloException;
import org.apache.accumulo.core.client.AccumuloSecurityException;
import org.apache.accumulo.core.client.TableNotFoundException;
import org.apache.accumulo.shell.Shell;
import org.apache.accumulo.shell.Shell.Command;
import org.apache.accumulo.shell.Token;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;

public class ImportDirectoryCommand extends Command {

  @Override
  public String description() {
    return "bulk imports an entire directory of data files into an existing table."
        + " The table is either passed with the -t tablename opt, or into to the current"
        + " table if the -t option is not provided. The boolean argument determines if accumulo"
        + " sets the time. Passing 3 arguments will use the old bulk import.  The new bulk"
        + " import only takes 2 arguments: <directory> true|false";
  }

  @SuppressWarnings("deprecation")
  @Override
  public int execute(final String fullCommand, final CommandLine cl, final Shell shellState)
      throws IOException, AccumuloException, AccumuloSecurityException, TableNotFoundException {

    final String tableName = OptUtil.getTableOpt(cl, shellState);

    String[] args = cl.getArgs();
    String dir = args[0];
    boolean setTime;

    // new bulk import only takes 2 args
    if (args.length == 2) {
      setTime = Boolean.parseBoolean(cl.getArgs()[1]);
      shellState.getAccumuloClient().tableOperations().importDirectory(dir).to(tableName)
          .tableTime(setTime).load();
    } else if (args.length == 3) {
      // warn using deprecated bulk import
      Shell.log.warn(
          "Deprecated since 2.0.0. New bulk import technique does not take a failure directory "
              + "as an argument.");
      String failureDir = args[1];
      setTime = Boolean.parseBoolean(cl.getArgs()[2]);
      shellState.getAccumuloClient().tableOperations().importDirectory(tableName, dir, failureDir,
          setTime);
      return 0;
    } else {
      shellState.printException(
          new IllegalArgumentException(String.format("Expected 2 or 3 arguments. There %s %d.",
              args.length == 1 ? "was" : "were", args.length)));
      printHelp(shellState);
      return 1;
    }

    return 0;
  }

  @Override
  public int numArgs() {
    return Shell.NO_FIXED_ARG_LENGTH_CHECK;
  }

  @Override
  public String usage() {
    return getName() + "[-t tablename] <directory> [failureDirectory] true|false";
  }

  @Override
  public Options getOptions() {
    final Options opts = super.getOptions();
    opts.addOption(OptUtil.tableOpt("name of the table to import files into"));
    return opts;
  }

  @Override
  public void registerCompletion(final Token root,
      final Map<CompletionSet,Set<String>> completionSet) {
    registerCompletionForTables(root, completionSet);
  }

}

@@ -2199,52 +2199,142 @@
     
-String dir = args%5B0%5D;%0A    boolean setTime;%0A%0A
+boolean setTime;%0A    String dir = args.length %3E 0 ? args%5B0%5D : %22%22;%0A    int status = 0;%0A%0A    switch (args.length) %7B%0A      case 2: %7B%0A    
     
@@ -2376,34 +2376,8 @@
 s%0A  
-  if (args.length == 2) %7B%0A
     
@@ -2423,24 +2423,26 @@
 Args()%5B1%5D);%0A
+  
       shellS
@@ -2529,16 +2529,18 @@
         
+  
 .tableTi
@@ -2567,39 +2567,45 @@
     
-%7D else if (args.length == 3)
+    break;%0A      %7D%0A      case 3:
  %7B%0A
+  
     
@@ -2649,16 +2649,18 @@
 t%0A      
+  
 Shell.lo
@@ -2667,16 +2667,18 @@
 g.warn(%0A
+  
         
@@ -2766,16 +2766,18 @@
 ctory %22%0A
+  
         
@@ -2806,24 +2806,26 @@
 t.%22);%0A      
+  
 String failu
@@ -2841,16 +2841,18 @@
 rgs%5B1%5D;%0A
+  
       se
@@ -2894,24 +2894,26 @@
 Args()%5B2%5D);%0A
+  
       shellS
@@ -3005,24 +3005,26 @@
 ,%0A          
+  
 setTime);%0A  
@@ -3031,31 +3031,44 @@
     
-return 0;%0A    %7D else
+  break;%0A      %7D%0A      default:
  %7B%0A
+  
     
@@ -3096,16 +3096,18 @@
 eption(%0A
+  
         
@@ -3208,16 +3208,18 @@
         
+  
 args.len
@@ -3257,24 +3257,26 @@
 .length)));%0A
+  
       printH
@@ -3302,17 +3302,44 @@
     
-return 1;
+  status = 1;%0A        break;%0A      %7D
 %0A   
@@ -3353,17 +3353,22 @@
  return 
-0
+status
 ;%0A  %7D%0A%0A 

Fix ArrayIndexOutOfBoundsException in importdir (#1571)