#!/usr/bin/env ruby
require 'optparse'
require_relative 'run'

PROGNAME = 'vendor-gitaly-proto'
USAGE = "Usage: #{PROGNAME} [--fork GITALY_PROTO_FORK_REPO] REVISION"
ORIGIN = 'gitlab.com/gitlab-org/gitaly-proto'

def main(revision, repo:)
  if repo == ORIGIN
    run!(%W[go mod edit -dropreplace=#{ORIGIN}])
    run!(%W[go get #{ORIGIN}@#{revision}])
  else
    run!(%W[go mod edit -replace=#{ORIGIN}=#{repo}@#{revision}])
  end

  run!(%w[go mod tidy])
end

options = ARGV.getopts(nil, 'fork:')
abort USAGE unless ARGV.count == 1

main(ARGV.first, repo: options['fork'] || ORIGIN)
