package de.htw.vs.shell;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.SystemUtils;
import org.springframework.shell.Bootstrap;

import de.htw.vs.shell.util.UncaughtExceptionLogger;


public class Application {
	
	/**
	 * The name of the variable which stores the path to the base directory
	 * for this application.
	 */
	public static final String APP_HOME_VAR = "user.app.home";
	
	/**
	 * Run the application with the specified program arguments.
	 * 
	 * @param args the program arguments.
	 */
	public static void run(String[] args)  {
		registerAppHomeDirSystemVariable();
		registerUncaughtExceptionLogger();
		setConsoleCodePage();
		runSpringShellWithoutInternalCommands(args);
	}


	private static void setConsoleCodePage() {
		String charsetName = "UTF-8";
		
		if(SystemUtils.IS_OS_WINDOWS || false) {
			charsetName = "CP1252";
		} 
		
		if(!Charset.isSupported(charsetName)) {
			charsetName = Charset.defaultCharset().name();
		}
		
		try {
			System.setProperty("console.encoding", charsetName);
			System.setOut(new PrintStream(System.out, true, charsetName));
			System.setErr(new PrintStream(System.out, true, charsetName));
		} catch(UnsupportedEncodingException ex) {}
	}

	
	private static void registerAppHomeDirSystemVariable() {
		File pathToLibDir = new File(Application.class.getProtectionDomain().getCodeSource().getLocation().getPath());
		System.setProperty(APP_HOME_VAR, new File(pathToLibDir.getParent()).getParent());
	}

	private static void registerUncaughtExceptionLogger() {
		Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionLogger());
	}
	
	private static void runSpringShellWithoutInternalCommands(String[] args) {
		try {
			Bootstrap.main(ArrayUtils.addAll(args, "--disableInternalCommands"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}

@@ -32,163 +32,55 @@
 ort 
-java.io.File;%0D%0Aimport java.io.IOException;%0D%0Aimport java.io.PrintStream;%0D%0Aimport java.io.UnsupportedEncodingException;%0D%0Aimport java.nio.charset.Charset
+de.htw.vs.shell.util.UncaughtExceptionLogger
 ;%0D%0A
-%0D%0A
 impo
@@ -224,52 +224,158 @@
 ort 
-de.htw.vs.shell.util.UncaughtExceptionLogger
+java.io.File;%0D%0Aimport java.io.IOException;%0D%0Aimport java.io.PrintStream;%0D%0Aimport java.io.UnsupportedEncodingException;%0D%0Aimport java.nio.charset.Charset
 ;%0D%0A%0D
@@ -405,17 +405,16 @@
 tion %7B%0D%0A
-%09
 %0D%0A%09/**%0D%0A
@@ -581,17 +581,16 @@
 home%22;%0D%0A
-%09
 %0D%0A%09/**%0D%0A
@@ -655,17 +655,16 @@
 ts.%0D%0A%09 *
- 
 %0D%0A%09 * @p
@@ -982,18 +982,16 @@
 TF-8%22;%0D%0A
-%09%09
 %0D%0A%09%09if(S
@@ -1018,17 +1018,8 @@
 DOWS
- %7C%7C false
 ) %7B%0D
@@ -1054,13 +1054,10 @@
 %0A%09%09%7D
- 
 %0D%0A
-%09%09
 %0D%0A%09%09
@@ -1153,18 +1153,16 @@
 ;%0D%0A%09%09%7D%0D%0A
-%09%09
 %0D%0A%09%09try 
@@ -1320,35 +1320,35 @@
 ntStream(System.
-out
+err
 , true, charsetN
@@ -1408,17 +1408,16 @@
 %0D%0A%09%7D%0D%0A%0D%0A
-%09
 %0D%0A%09priva
@@ -1813,17 +1813,16 @@
 );%0D%0A%09%7D%0D%0A
-%09
 %0D%0A%09priva
@@ -2038,14 +2038,13 @@
 %09%7D%0D%0A%09%7D%0D%0A
-%09
 %0D%0A%7D%0D%0A

Enable UTF-8 encoding for unix and CP1252 encoding for windows.