/*
 * @(#)Config.java
 *
 * This file is part of webCDwriter - Network CD Writing.
 *
 * Copyright (C) 1999-2004 Jörg P. M. Haeger
 *
 * webCDwriter is free software. See CDcreator.java for details.
 */

import java.io.*;

/**
 *
 * @version 20040709
 * @author Jörg P. M. Haeger
 */
class Config {
	final static File configFile = initFile();
	static File configMirror = null;
	static String log = "";

	final static String serverName = "localhost";
	final static int serverPortNo = 12411;

	final static String
		country = "webCDcreator.country",
		language = "webCDcreator.language",
		height = "webCDcreator.MainWin.height",
		width = "webCDcreator.MainWin.width",
		x = "webCDcreator.MainWin.x",
		y = "webCDcreator.MainWin.y",
		JAVAWS142 = "webCDcreator.javaws142Workaround",
		MEDIUM = "webCDcreator.medium",
		onStartup = "webCDcreator.NewProjectDialog.onStartup",
		SOURCES = "webCDcreator.sources",
		VERIFY = "webCDcreator.verify";

	private static void copy(File from, File to) throws IOException {
		InputStream in = new FileInputStream(from);
		OutputStream out = new FileOutputStream(to);
		while (true) {
			byte[] bs = new byte[4 * 1024];
			int n = in.read(bs);
			if (n <= 0)
				break;
			out.write(bs, 0, n);
		}
		out.close();
		in.close();
	}

	/**
	 *
	 */
	static String get(String key) {
		try {
			BufferedReader reader
				= new BufferedReader(
					new FileReader(configFile));
			while (true) {
				String line = reader.readLine();
				if (line == null)
					break;
				int p0 = line.indexOf("=");
				if (p0 > 0) {
					if (line.substring(0, p0).equals(key))
						return line.substring(p0 + 1);
				}
			}
		}
		catch (IOException e) { }
		if (key.equals(width))
			return "640";
		if (key.equals(height))
			return "480";
		if (key.equals(x))
			return "-1";
		if (key.equals(y))
			return "-1";
		if (key.equals(JAVAWS142)
				|| key.equals(onStartup)
				|| key.equals(VERIFY))
			return "true";
		if (key.equals(MEDIUM))
			return Mode.CDR74;
		return "";
	}

	/**
	 *
	 */
	static boolean getBool(String key) {
		return new Boolean(get(key)).booleanValue();
	}

	/**
	 *
	 */
	static int getInt(String key) {
		try {
			return Integer.parseInt(get(key));
		}
		catch (NumberFormatException e) {
			return -1;
		}
	}

	private static File initFile() {
		String home = System.getProperty("user.home");
		File file = new File(home, ".webCDcreator");
		if (isUnix())
			return file;

		// Windows
		File winFile = new File(home, "webCDcreator.txt");
		if (file.exists())
			file.renameTo(winFile);
		return winFile;
	}

	/**
	 *
	 */
	static boolean isUnix() {
		return File.separatorChar == '/';
	}

	static void mirror() {
		String home = System.getProperty("user.home");
		if (!home.startsWith("P:") && new File("P:\\").exists()) {
			log += " P";
			configMirror = new File("P:\\", "webCDcreator.txt");
			if (!configFile.exists() && configMirror.exists())
				try {
					log += " M1";
					copy(configMirror, configFile);
					log += " M2";
				}
				catch (IOException e) { }
		}
		log += " " + configFile.getPath();
		log += " " + get(width);
		log += " " + get(SOURCES);
	}

	/**
	 *
	 */
	static void set(String key, String value) {
		String old = get(key);
		if (old != null && old.equals(value))
			return;
		if (CDcreator.isNetscapeVM())
			return;
		Version.debug("Config", "set(" + key + ", " + value + ")");
		try {
			BufferedReader reader
				= new BufferedReader(
					new FileReader(configFile));
			File tmpFile = new File(configFile.getParent(),
				configFile.getName() + ".tmp");
			PrintWriter writer
				= new PrintWriter(
					new FileOutputStream(tmpFile));
			boolean found = false;
			while (true) {
				String line = reader.readLine();
				if (line == null)
					break;
				int p0 = line.indexOf("=");
				if (p0 > 0) {
					if (line.substring(0, p0).equals(key)) {
						line = line.substring(0, p0 + 1) + value;
						found = true;
					}
				}
				writer.println(line);
			}
			reader.close();
			if (!found)
				writer.println(key + "=" + value);
			writer.close();
			if (!tmpFile.renameTo(configFile)) {
				copy(tmpFile, configFile);
				tmpFile.delete();
			}
			if (configMirror != null)
				try {
					log += " M3";
					copy(configFile, configMirror);
					log += " M4";
				}
				catch (IOException e) { }
		}
		catch (IOException e) {
			Version.debug("Config", "ERR " + e);
		}
	}

	/**
	 *
	 */
	static void set(String key, boolean value) {
		set(key, "" + value);
	}

	/**
	 *
	 */
	static void set(String key, int value) {
		set(key, "" + value);
	}
}
