package kamicharserver;

import java.io.*;
import java.util.Scanner;
import java.util.HashMap;

public final class KamiConfig {

    private File fFile = null;

    public KamiConfig() {
        fFile = new File("config.cfg");
        if (!fFile.exists()) {
            fFile = new File("./dist/config.cfg");
            if (!fFile.exists()) {
                fFile = new File("../config.cfg");
            }
        }

    }

    public KamiConfig(String file) {
        fFile = new File(file);
        if (!fFile.exists()) {
            fFile = new File("./dist/" + file);
            if (!fFile.exists()) {
                fFile = new File("../" + file);
            }
        }

    }

    public final void readConfig(HashMap hm) {
        Scanner scanner = null;
        String[] vars = null;
        try {
            scanner = new Scanner(fFile);
            while (scanner.hasNextLine()) {
                vars = scanner.nextLine().split("=");
                if (vars != null && vars.length > 1) {
                    hm.put(vars[0].trim(), vars[1].trim());
                }
            }
        } catch (FileNotFoundException ex) {
            System.out.println("Config.cfg not found! (" + fFile.getAbsolutePath() + ")");
            System.exit(-1);
        }
    }
}
