package com.dairymoose.modernlife.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/core/ModernLifeConfigPrepopulator.class */
public class ModernLifeConfigPrepopulator {
    private static final Logger LOGGER = LogManager.getLogger();

    public void parseFile(String modName, String fileSuffix, IConfigOverride... configOverride) {
        String key;
        if (modName == null || fileSuffix == null) {
            return;
        }
        if (!fileSuffix.endsWith(".toml")) {
            fileSuffix = fileSuffix + ".toml";
        }
        String finalPath = "config/" + modName + "-" + fileSuffix;
        File f = new File(finalPath);
        try {
            FileReader fr = new FileReader(f);
            BufferedReader br = new BufferedReader(fr);
            while (true) {
                String line = br.readLine();
                if (line != null) {
                    String trimmed = line.trim();
                    if (trimmed != null && trimmed.length() != 0) {
                        for (IConfigOverride configSpec : configOverride) {
                            if (configSpec != null && (key = configSpec.getKey()) != null && trimmed.startsWith(key)) {
                                ModernLifeCommon.LOGGER.debug("key match: " + key);
                                String[] keyValue = trimmed.split("=");
                                if (keyValue.length == 2) {
                                    String value = keyValue[1];
                                    String trimmedValue = value.trim();
                                    Integer configValue = null;
                                    try {
                                        configValue = Integer.valueOf(trimmedValue);
                                    } catch (NumberFormatException e) {
                                    }
                                    if (configValue != null) {
                                        ModernLifeCommon.LOGGER.debug("value = " + configValue);
                                        configSpec.setOverride(configValue);
                                    }
                                }
                            }
                        }
                    }
                } else {
                    fr.close();
                    return;
                }
            }
        } catch (IOException e2) {
            ModernLifeCommon.LOGGER.error("File not found", e2);
        }
    }
}
