package com.atlassian.bitbucket.mesh.git.transcode;

import com.atlassian.bitbucket.mesh.git.builder.GitProcessBuilder;
import com.atlassian.bitbucket.mesh.git.builder.GitProcessBuilderConfigurer;

import javax.annotation.Nonnull;

/**
 * Applies {@link #HOST STASH_GIT_TRANSCODE_HOST} and {@link #PORT STASH_GIT_TRANSCODE_PORT} environment variables
 * when the {@link TranscodeServer} is running.
 */
public class TranscodeGitProcessBuilderConfigurer implements GitProcessBuilderConfigurer {

    //These variables are used in transcode.pl; they cannot be changed here without changing both places
    private static final String HOST = "STASH_GIT_TRANSCODE_HOST";
    private static final String PORT = "STASH_GIT_TRANSCODE_PORT";

    private final TranscodeServer transcodeServer;

    public TranscodeGitProcessBuilderConfigurer(TranscodeServer transcodeServer) {
        this.transcodeServer = transcodeServer;
    }

    @Override
    public void configure(@Nonnull GitProcessBuilder builder) {
        if (transcodeServer.isRunning()) {
            builder.withEnvironment(HOST, transcodeServer.getAddress())
                    .withEnvironment(PORT, Integer.toString(transcodeServer.getPort()));
        }
    }
}
