#!/usr/bin/env ruby

# frozen_string_literal: true

$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
$stdout.sync = true

require 'chronic_duration'
require 'down/http'
require 'gpt_common'
require 'gpt_logger'
require 'import_project'
require 'optimist'
require 'pathname'
require 'time'
require 'uri'

opts = Optimist.options do
  banner "Usage: import-project [options]"
  banner "\nImports a GitLab Project tarball (local or remote) into the specified environment.\nDefaults to importing the gitlabhq project from a remote filestore to the specified environment, group and project namespace."
  banner "\nOptions:"
  opt :environment_url, "Full URL for the environment to import to.", short: :none, type: :string, required: true
  opt :project_tarball, "Location of project tarball to import. Can be local or remote.", short: :none, type: :string, default: 'https://gitlab.com/gitlab-org/quality/performance-data/raw/main/projects_export/gitlabhq_export_16.0.0.tar.gz'
  opt :namespace, "The ID or path of the namespace that the project will be imported to, such as a Group.", short: :none, type: :string
  opt :project_name, "Name for project. Can be also be a combined path and name if required.", short: :none, type: :string, required: true
  opt :storage_name, "Name for target repository storage (Gitaly).", short: :none, type: :string, default: 'default'
  opt :with_cleanup, "Remove the project after the import is finished", short: :none, type: :boolean, default: false
  opt :unique_name, "Add timestamp to the project name to avoid uniqueness violations in case of multiple imports into the same group", short: :none, type: :boolean, default: false
  opt :help, 'Show help message'
  banner "\nEnvironment Variables:"
  banner "  ACCESS_TOKEN             A valid GitLab Personal Access Token for the specified environment. The token should have admin access and all permissions set. (Default: nil)"
  banner "\nExamples:"
  banner "  import-project --environment-url 10k.testbed.gitlab.net"
  banner "  import-project --environment-url localhost:3000 --project-tarball /home/user/test-project.tar.gz --namespace test-group --project-name test-project"
  banner "  import-project --with-cleanup --unique-name --environment-url https://staging.gitlab.com/ --project-tarball /home/user/test-project.tar.gz --namespace test-group --project-name test-project"
end

raise 'Environment Variable ACCESS_TOKEN must be set to proceed. See command help for more info' unless ENV['ACCESS_TOKEN']

# GPT logger setup
results_home = ENV['GPT_DOCKER_RESULTS_DIR'] || Pathname.new(File.expand_path('../results', __dir__)).relative_path_from(Dir.pwd)
results_file_prefix = "#{URI(opts[:environment_url]).host}_#{Time.now.strftime('%Y-%m-%d_%H%M%S')}"
ENV['GPT_LOGGER_PATH'] = "#{results_home}/import-project_#{results_file_prefix}.log"

# Import project
project_name = opts[:unique_name] ? "#{opts[:project_name]}_#{Time.now.to_i}" : opts[:project_name]
import_project = ImportProject.new(env_url: opts[:environment_url], project_tarball: opts[:project_tarball])
proj_tarball_file = import_project.setup_tarball(project_tarball: opts[:project_tarball])
import_project.import_project(proj_tarball_file:, project_name:, namespace: opts[:namespace], storage_name: opts[:storage_name], with_cleanup: opts[:with_cleanup])
