package org.springframework.data.rest.shell.commands;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.shell.core.JLineShellComponent;
import org.springframework.shell.event.ShellStatus;
import org.springframework.shell.event.ShellStatusListener;
import org.springframework.stereotype.Component;

/**
 * @author Jon Brisbin
 */
@Component
public class DotRcReader implements InitializingBean, ShellStatusListener {

  @Autowired
  private JLineShellComponent shell;
  private boolean readDotRc = false;

  @Override public void afterPropertiesSet() throws Exception {
    shell.addShellStatusListener(this);
  }

  @Override public void onShellStatusChange(ShellStatus oldStatus, ShellStatus newStatus) {
    if(oldStatus.getStatus() == ShellStatus.Status.STARTED
        && newStatus.getStatus() == ShellStatus.Status.USER_INPUT) {
      if(!readDotRc) {
        try {
          readDotRc();
        } catch(Exception e) {
          throw new IllegalStateException(e.getMessage(), e);
        }
      }
    }
  }

  private void sourceFile(File f) throws IOException {
    BufferedReader dotRc = new BufferedReader(new FileReader(f));
    String line;
    while(null != (line = dotRc.readLine())) {
      shell.executeCommand(line);
    }
  }

  private void readDotRc() throws Exception {
    String homeDir = System.getenv("HOME");
    File restShellInitDir = new File(homeDir + File.separator + ".rest-shell");
    if(restShellInitDir.exists() && restShellInitDir.isDirectory()) {
      File[] files = restShellInitDir.listFiles();
      if(null == files) {
        return;
      }
      for(File f : files) {
        sourceFile(f);
      }
      readDotRc = true;
    }
  }

}


@@ -160,67 +160,8 @@
 n;%0A%0A
-import org.springframework.beans.factory.InitializingBean;%0A
 impo
@@ -250,38 +250,30 @@
 ork.
-shell.core.JLineShellComponent
+context.SmartLifecycle
 ;%0Aim
@@ -307,85 +307,32 @@
 ell.
-event.ShellStatus;%0Aimport org.springframework.shell.event.ShellStatusListener
+core.JLineShellComponent
 ;%0Aim
@@ -461,45 +461,22 @@
 nts 
-InitializingBean, ShellStatusListener
+SmartLifecycle
  %7B%0A%0A
@@ -586,345 +586,169 @@
 lic 
-void afterPropertiesSet() throws Exception %7B%0A    shell.addShellStatusListener(this);%0A  %7D%0A%0A  @Override public void onShellStatusChange(ShellStatus oldStatus, ShellStatus newStatus) %7B%0A    if(oldStatus.getStatus() == ShellStatus.Status.STARTED%0A        && newStatus.getStatus() == ShellStatus.Status.USER_INPUT) %7B%0A      if(!readDotRc
+boolean isAutoStartup() %7B%0A    return true;%0A  %7D%0A%0A  @Override public void stop(Runnable callback) %7B%0A    callback.run();%0A  %7D%0A%0A  @Override public void start(
 ) %7B%0A    
     
@@ -743,20 +743,16 @@
 ) %7B%0A    
-  
-  
 try %7B%0A  
@@ -755,20 +755,16 @@
 %7B%0A      
-  
-  
 readDotR
@@ -768,20 +768,16 @@
 otRc();%0A
-    
     %7D ca
@@ -801,20 +801,16 @@
 %7B%0A      
- 
-   
 throw ne
@@ -861,27 +861,186 @@
     
-  
+%7D%0A  %7D%0A%0A  @Override public void stop() %7B%0A
   %7D%0A
+%0A
   
-  
+@Override public boolean isRunning() %7B%0A    return readDotRc;%0A
   %7D%0A
+%0A
   
-  %7D
+@Override public int getPhase() %7B%0A    return Integer.MAX_VALUE;
 %0A  %7D

Fix for bug where DotRcReader wasn't calling the callback, resulting in a hang when running non-interactively.