package electrosphere.game.server.db;

import electrosphere.logger.LoggerInterface;
import electrosphere.util.FileUtils;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author satellite
 */
public class DatabaseUtils {
    public static boolean initCentralDBFile(String path){
        String sanitizedPath = "." + FileUtils.sanitizeFilePath(path);
        if(!FileUtils.checkFileExists(sanitizedPath)){
            return false;
        }
        String dbFilePath = sanitizedPath + "/central.db";
        DatabaseController controller = new DatabaseController();
        controller.connect(dbFilePath);
        String rawScript = "";
        try {
            rawScript = FileUtils.getSQLScriptFileAsString("createTables.sql");
        } catch (IOException ex) {
            LoggerInterface.loggerEngine.ERROR("Failure reading create db script", ex);
            return false;
        }
        String[] scriptLines = rawScript.split("\n");
        for(String line : scriptLines){
            if(line.length() > 1 && !line.startsWith("--")){
                System.out.println("EXECUTE: " + line);
                controller.executeStatement(line);
            }
        }
        controller.disconnect();
        return true;
    }
}
