package com.atlassian.stash.internal.scm.git.transcode;

import com.atlassian.stash.internal.scm.git.GitCommandBuilderConfigurer;
import com.atlassian.bitbucket.scm.git.command.GitScmCommandBuilder;

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.
 *
 * @since 3.1
 */
public class TranscodeGitCommandBuilderConfigurer implements GitCommandBuilderConfigurer {

    //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 TranscodeGitCommandBuilderConfigurer(TranscodeServer transcodeServer) {
        this.transcodeServer = transcodeServer;
    }

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