package org.jerrymouse.gaes.mavenPlugin.install;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;


public class LinuxEnvironment extends Environment {

	@Override
	public boolean setEnv(String key, String prop) {
		if (getEnv(key).equals(prop)) {
			return true;
		}
		File bashrc = new File(getEnv("HOME") + "/.bashrc");
		String appendInfo = "export " + key + "=" + prop + "\n";
		appendToFile(appendInfo, bashrc);
		if (getEnv(key).equals(prop))
			return true;
		return false;
	}

	private void appendToFile(String str, File file) {
		FileOutputStream stream;// provides file access
		OutputStreamWriter writer;// writes to the file
		try {
			stream = new FileOutputStream(file, true);
			writer = new OutputStreamWriter(stream);
			writer.write(str);
			writer.close();
			stream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}// appendToFile
}
