import java.io.*;

class configuration extends database{
    
    final String ConfigFileName = "questionnaire.conf";
    char csvSeparator = ',';
    String RootDirectory, IndexFileName, QuestionsFileName, ResponsesFileName;
    
    public configuration() throws IOException{
        
        for (int r = 0; r < get_total_lines(ConfigFileName); r++){
            String l = get_line(ConfigFileName, r);
            if (l.indexOf("#") != -1)
                parseConfigLine(l.substring(0, l.indexOf("#")));
            else
                parseConfigLine(l);
        }
        
        checkConfiguration();
    }
    
    public void parseConfigLine(String s){
     
        if (s.indexOf("=") != -1){
            if (removeSpaces(s.substring(0, s.indexOf("="))).compareTo("RootDirectory") == 0)
                RootDirectory = removeSpaces(s.substring(s.indexOf("=")+1, s.lastIndexOf(";")));
            else if (removeSpaces(s.substring(0, s.indexOf("="))).compareTo("IndexFileName") == 0)
                IndexFileName = removeSpaces(s.substring(s.indexOf("=")+1, s.lastIndexOf(";")));
            else if (removeSpaces(s.substring(0, s.indexOf("="))).compareTo("QuestionsFileName") == 0) 
                QuestionsFileName = removeSpaces(s.substring(s.indexOf("=")+1, s.lastIndexOf(";")));
            else if (removeSpaces(s.substring(0, s.indexOf("="))).compareTo("ResponsesFileName") == 0) 
                ResponsesFileName = removeSpaces(s.substring(s.indexOf("=")+1, s.lastIndexOf(";")));
            else
                csvSeparator = removeSpaces(s.substring(s.indexOf("=")+1, s.lastIndexOf(";"))).charAt(1);
                 
        }
            
    }
    
    private String removeSpaces(String s){
        
        String s1 = new String();
        for (int r = 0; r < s.length(); r++){
            char c = s.charAt(r);
            if ((s.substring(r, r+1).compareTo(" ") != 0) && (s.substring(r, r+1).compareTo("	") != 0))
                s1 += s.substring(r, r+1);
        }
        
        return s1;
    }
    
    private void checkConfiguration(){
        if (QuestionsFileName == null){
            QuestionsFileName = "questions.dat";
            System.err.println("Misconfiguration: default questions file loaded.");
        }
        if (IndexFileName == null){
            IndexFileName = "questionsIndex.dat";
            System.err.println("Misconfiguration: default questions index file loaded.");
        }
        if (ResponsesFileName == null){
            ResponsesFileName = "responses.dat";
            System.err.println("Misconfiguration: default responses file loaded.");
        }
        if (RootDirectory == null){
            RootDirectory = "";
            System.err.println("Misconfiguration: default root directory loaded.");
        }
        
    }
    
}
