package org.greenstone.gatherer.util;

import java.io.*;
import java.util.*;

public class GSDLSiteConfig
    extends LinkedHashMap {
    private File gsdlsite_cfg;
    private File glisite_cfg;
    private String autoenter_initial;
    private String start_browser_initial;
    
    static final private String AUTOENTER = "autoenter";
    static final private String COLON = ":";
    static final private String ENTERLIB = "enterlib";
    static final private String FALSE = "0";
    static final private String GLISITE_CFG = "glisite.cfg";
    static final private String GSDL = "gsdl";
    static final private String GSDLSITE_CFG = "gsdlsite.cfg";
    static final private String LOCAL_HOST = "http://localhost"; 
    static final private String PORTNUMBER = "portnumber";
    static final private String SEPARATOR = "/";
    static final private String SPECIFIC_CONFIG = "--config=";
    static final private String STARTBROWSER = "start_browser";
    static final private String TRUE = "1";
    static final private String URL = "url";

    public GSDLSiteConfig(File server_exe) {
	debug("New GSDLSiteConfig for: " + server_exe.getAbsolutePath());
	gsdlsite_cfg = new File(server_exe.getParentFile(), GSDLSITE_CFG);
	glisite_cfg = new File(server_exe.getParentFile(), GLISITE_CFG);
	autoenter_initial = null;
	start_browser_initial = null;
	if(gsdlsite_cfg.exists()) {
	    debug("Load: " + gsdlsite_cfg.getAbsolutePath());
	    clear();
	    try {
		BufferedReader in = new BufferedReader(new FileReader(gsdlsite_cfg));
		String line = null;
		while((line = in.readLine()) != null) {
		    String key = null;
		    String value = null;
		    int index = -1;
		    if((index = line.indexOf("=")) != -1 && line.length() >= index + 1) {
			key = line.substring(0, index);
			value = line.substring(index + 1);
		    }
		    else {
			key = line;
		    }
		    put(key, value);
		}
		in.close();
	    }
	    catch (Exception error) {
		error.printStackTrace();
	    }
	}
	else {
	    debug("No GSDLsite.cfg file can be found!");
	}
    }

    public boolean exists() {
	return gsdlsite_cfg.exists();
    }

    public String getLocalHostURL() {
	StringBuffer url = new StringBuffer(LOCAL_HOST);
	url.append(COLON);
	url.append((String)get(PORTNUMBER));
	String enterlib = (String)get(ENTERLIB);
	if(enterlib == null || enterlib.length() == 0) {
	    // Use the default /gsdl and hope for the best.
	    url.append(SEPARATOR);
	    url.append(GSDL);
	}
	else {
	    if(!enterlib.startsWith(SEPARATOR)) {
		url.append(SEPARATOR);
	    }
	    url.append(enterlib);
	}
	enterlib = null;
	debug("Found Local Library Address: " + url.toString());
	return url.toString();
    }

    public String getSiteConfigFilename() {
	return SPECIFIC_CONFIG + glisite_cfg.getAbsolutePath();
    }

    public String getURL() {
	// URL is made from url and portnumber
	String url = (String) get(URL);
	if(url != null) {
	    StringBuffer temp = new StringBuffer(url);
	    temp.append(COLON);
	    temp.append((String)get(PORTNUMBER));
	    String enterlib = (String)get(ENTERLIB);
	    if(enterlib == null || enterlib.length() == 0) {
		// Use the default /gsdl and hope for the best.
		temp.append(SEPARATOR);
		temp.append(GSDL);
	    }
	    else {
		if(!enterlib.startsWith(SEPARATOR)) {
		    temp.append(SEPARATOR);
		}
		temp.append(enterlib);
	    }
	    enterlib = null;
	    url = temp.toString();
	}
	debug("Found Local Library Address: " + url);
	return url;
    }

    public void load() {
	if(glisite_cfg.exists()) {
	    debug("Load: " + glisite_cfg.getAbsolutePath());
	    clear();
	    try {
		BufferedReader in = new BufferedReader(new FileReader(glisite_cfg));
		String line = null;
		while((line = in.readLine()) != null) {
		    String key = null;
		    String value = null;
		    int index = -1;
		    if((index = line.indexOf("=")) != -1 && line.length() >= index + 1) {
			key = line.substring(0, index);
			value = line.substring(index + 1);
		    }
		    else {
			key = line;
		    }
		    put(key, value);
		}
		in.close();
	    }
	    catch (Exception error) {
		error.printStackTrace();
	    }
	}
	else {
	    debug("No GSDLsite.cfg file can be found!");
	}
    }

    /** Restore the autoenter value to its initial value, and remove url if present. */
    public void restore() {
	if(glisite_cfg != null) {
	    // Delete the file
	    glisite_cfg.delete();
	}
	else {
	    debug("Restore Initial Settings");
	    put(AUTOENTER, autoenter_initial);
	    put(STARTBROWSER, start_browser_initial);
	    remove(URL);
	    save();
	}
    }

    public void set() {
	debug("Set Session Settings");
	if(autoenter_initial == null) {
	    autoenter_initial = (String) get(AUTOENTER);
	    debug("Remember autoenter was: " + autoenter_initial);
	}
	put(AUTOENTER, TRUE);
	if(start_browser_initial == null) {
	    start_browser_initial = (String) get(STARTBROWSER);
	    debug("Remember start_browser was: " + start_browser_initial);
	}
	put(STARTBROWSER, FALSE);
	save();
    }

    private void debug(String message) {
	///ystem.err.println(message);
    }

    private void save() {
	//debug("Save: " + gsdlsite_cfg.getAbsolutePath());
	debug("Save: " + glisite_cfg.getAbsolutePath());
	try {
	    //BufferedWriter out = new BufferedWriter(new FileWriter(gsdlsite_cfg, false));
	    BufferedWriter out = new BufferedWriter(new FileWriter(glisite_cfg, false));
	    for(Iterator keys = keySet().iterator(); keys.hasNext(); ) {
		String key = (String) keys.next();
		String value = (String) get(key);
		out.write(key, 0, key.length());
		if(value != null) {
		    out.write('=');
		    out.write(value, 0, value.length());
		}
		out.newLine();
	    }
	    out.flush();
	    out.close();
	}
	catch (Exception error) {
	    error.printStackTrace();
	}
    }
}
