package com.senseidb.clue;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URI;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.Version;

import com.senseidb.clue.commands.ClueCommand;
import com.senseidb.clue.commands.HelpCommand;

public class ClueApplication {
  
  private final ClueContext ctx;
  private final ClueCommand helpCommand;
  
  public ClueApplication(String idxLocation, ClueConfiguration config, boolean interactiveMode) throws Exception{
    Directory dir = config.getDirBuilder().build(new URI(idxLocation));
    if (!DirectoryReader.indexExists(dir)){
      System.out.println("lucene index does not exist at: "+idxLocation);
      System.exit(1);
    }
    IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_47, new StandardAnalyzer(Version.LUCENE_47));    
    ctx = new ClueContext(dir, config, writerConfig, interactiveMode);
    helpCommand = ctx.getCommand(HelpCommand.CMD_NAME);
  }
  
  public void handleCommand(String cmdName, String[] args, PrintStream out){
    ClueCommand cmd = ctx.getCommand(cmdName);
    if (cmd == null){
      out.println(cmdName+" is not supported:");
      cmd = helpCommand;
    }
    try{
      cmd.execute(args, out);
    }
    catch(Exception e){
      e.printStackTrace();
    }
  }
  
  public static void main(String[] args) throws Exception {
    if (args.length < 1){
      System.out.println("usage: <index location> <command> <command args>");
      System.exit(1);
    }
    
    ClueConfiguration config = ClueConfiguration.load();
    
    String idxLocation = args[0];
    
    ClueApplication app = null;
    
    if (args.length > 1){
      String cmd = args[1];
      if ("readonly".equalsIgnoreCase(cmd)) {
        if (args.length > 2) {
          cmd = args[2];
          app = new ClueApplication(idxLocation, config, false);
          String[] cmdArgs;
          cmdArgs = new String[args.length - 3];
          System.arraycopy(args, 3, cmdArgs, 0, cmdArgs.length);
          app.ctx.setReadOnlyMode(true);
          app.handleCommand(cmd, cmdArgs, System.out);
        }
      }
      else {
        app = new ClueApplication(idxLocation, config, false);
        String[] cmdArgs;
        cmdArgs = new String[args.length - 2];
        System.arraycopy(args, 2, cmdArgs, 0, cmdArgs.length);
        app.handleCommand(cmd, cmdArgs, System.out);
      }
      return;
    }
    app = new ClueApplication(idxLocation, config, true);
    BufferedReader inReader = new BufferedReader(new InputStreamReader(System.in));
    while(true){
      System.out.print("> ");
      String line = inReader.readLine();
      if (line == null || line.isEmpty()) continue;
      line = line.trim();
      String[] parts = line.split("\\s");
      if (parts.length > 0){
        String cmd = parts[0];
        String[] cmdArgs = new String[parts.length - 1];
        System.arraycopy(parts, 1, cmdArgs, 0, cmdArgs.length);
        app.handleCommand(cmd, cmdArgs, System.out);
      }
    }
        
  }
}

@@ -44,32 +44,60 @@
 BufferedReader;%0A
+import java.io.IOException;%0A
 import java.io.I
@@ -106,32 +106,32 @@
 utStreamReader;%0A
-
 import java.io.P
@@ -610,24 +610,343 @@
 mand;%0A  %0A  p
+rivate static ClueConfiguration config;%0A  %0A  static %7B%0A    try %7B%0A      config = ClueConfiguration.load();%0A    %7D catch (Exception e) %7B%0A      throw new RuntimeException(e);%0A    %7D%0A  %7D%0A  %0A  public ClueContext getContext() %7B%0A    return ctx;%0A  %7D%0A  %0A  public ClueConfiguration getConfiguration() %7B%0A    return config;%0A  %7D%0A  %0A  p
 ublic ClueAp
@@ -977,34 +977,8 @@
 tion
-, ClueConfiguration config
 , bo
@@ -1821,32 +1821,619 @@
 );%0A    %7D%0A  %7D%0A  %0A
+  public void run() throws IOException %7B%0A    BufferedReader inReader = new BufferedReader(new InputStreamReader(System.in));%0A    while(true)%7B%0A      System.out.print(%22%3E %22);%0A      String line = inReader.readLine();%0A      if (line == null %7C%7C line.isEmpty()) continue;%0A      line = line.trim();%0A      String%5B%5D parts = line.split(%22%5C%5Cs%22);%0A      if (parts.length %3E 0)%7B%0A        String cmd = parts%5B0%5D;%0A        String%5B%5D cmdArgs = new String%5Bparts.length - 1%5D;%0A        System.arraycopy(parts, 1, cmdArgs, 0, cmdArgs.length);%0A        handleCommand(cmd, cmdArgs, System.out);%0A      %7D%0A    %7D%0A  %7D%0A  %0A  %0A
   public static 
@@ -2617,70 +2617,8 @@
    %0A
-    ClueConfiguration config = ClueConfiguration.load();%0A    %0A
     
@@ -2889,32 +2889,24 @@
 idxLocation,
- config,
  false);%0A   
@@ -3217,24 +3217,16 @@
 ocation,
- config,
  false);
@@ -3485,24 +3485,16 @@
 ocation,
- config,
  true);%0A
@@ -3501,552 +3501,18 @@
     
-BufferedReader inReader = new BufferedReader(new InputStreamReader(System.in));%0A    while(true)%7B%0A      System.out.print(%22%3E %22);%0A      String line = inReader.readLine();%0A      if (line == null %7C%7C line.isEmpty()) continue;%0A      line = line.trim();%0A      String%5B%5D parts = line.split(%22%5C%5Cs%22);%0A      if (parts.length %3E 0)%7B%0A        String cmd = parts%5B0%5D;%0A        String%5B%5D cmdArgs = new String%5Bparts.length - 1%5D;%0A        System.arraycopy(parts, 1, cmdArgs, 0, cmdArgs.length);%0A        app.handleCommand(cmd, cmdArgs, System.out);%0A      %7D%0A    %7D%0A        
+app.run();
 %0A  %7D

clean up