#!/usr/bin/env ruby-2.5

unless ENV['SSH_CONNECTION']
  puts "Only ssh allowed"
  exit
end

original_cmd = ENV.delete('SSH_ORIGINAL_COMMAND')

require_relative '../lib/gitlab_init'

#
#
# GitLab shell, invoked from ~/.ssh/authorized_keys or from an
# AuthorizedPrincipalsCommand in the key-less SSH CERT mode.
#
#
require File.join(ROOT_PATH, 'lib', 'gitlab_shell')

# We must match e.g. "key-12345" anywhere on the command-line. See
# https://gitlab.com/gitlab-org/gitlab-shell/issues/145
who = /\b(?:(?:key)-[0-9]+|username-\S+)\b/.match(ARGV.join(' ')).to_s

if GitlabShell.new(who).exec(original_cmd)
  exit 0
else
  exit 1
end
