package com.atlassian.bitbucket.mesh.config;

import org.springframework.core.env.Environment;

import javax.annotation.Nonnull;

class DefaultProcessConfig implements ProcessConfig {

    private final Environment environment;

    private volatile Settings current;

    DefaultProcessConfig(Environment environment) {
        this.environment = environment;

        current = new Settings(environment);
    }

    @Override
    public int getCommandLineSize() {
        return current.commandLineSize;
    }

    @Override
    public int getEnvironmentVariableSize() {
        return current.environmentVariableSize;
    }

    @Nonnull
    @Override
    public String getSshBinary() {
        return current.sshBinary;
    }

    void reload() {
        current = new Settings(environment);
    }

    private static class Settings {

        private final int commandLineSize;
        private final int environmentVariableSize;
        private final String sshBinary;

        Settings(Environment environment) {
            commandLineSize = Math.max(8000, environment.getProperty("os.limits.commandline", Integer.class, 32000));
            environmentVariableSize = Math.max(8000,
                    environment.getProperty("os.limits.environmentvariable", Integer.class, 16000));
            sshBinary = environment.getProperty("ssh.binary", "ssh");
        }
    }
}
