#!/usr/bin/env ruby

require_relative "../lib/2pass"

def help_message
  puts <<~HELP
    Usage:
      2pass add <vault_name> <id> <value> [env]    - Add new secret. env is optional
      2pass get <vault_name> <id> [env]            - Get content by ID from the specified vault. env is optional
      2pass list <vault_name> [env]                - List the content of the specified vault as key=value pairs. env is optional
      2pass list <vault_name> [env] --json         - List the content of the specified vault as JSON. env is optional
      2pass list <vault_name> [env] --dotenv       - List the content of the specified vault as key=value pairs. env is optional
      2pass link <vault_name> <target_path> [env]  - Create a symlink for an existing vault. Useful when the vault is stored in a synced place (iCloud, Dropbox, etc.). env is optional
      2pass update                                  - Update 2pass to the latest RubyGems release
      2pass -h                                      - Display this help message
  HELP
end

options = {}
OptionParser.new do |opts|
  opts.banner = "Usage: 2pass [options]"

  opts.on("-h", "--help", "Display this help message") do
    options[:help] = true
  end
  opts.on("-v", "--version", "Display the version") do
    options[:version] = true
  end
  opts.on("--json", "Use JSON output format") do
    options[:format] = :json
  end
  opts.on("--dotenv", "Use key=value output format") do
    options[:format] = :dotenv
  end
end.parse!

if options[:help]
  help_message
  exit
end

if options[:version]
  puts TwoPass::VERSION
  exit
end

if ARGV.empty?
  help_message
  exit(1)
end

command, *args = ARGV

begin
  case command&.to_sym
  when :get
    if args.length < 2 || args.length > 3
      help_message
      exit(1)
    end
    vault_name, id, env = args[0], args[1], args[2]
    puts TwoPass.get_secret(vault_name, id, env: env)
  when :add
    if args.length < 3 || args.length > 4
      help_message
      exit(1)
    end
    vault_name, id, value, env = args[0], args[1], args[2], args[3]
    TwoPass.add_secret(vault_name, id, value, env: env)
  when :list
    if args.length < 1 || args.length > 2
      help_message
      exit(1)
    end
    vault_name, env = args[0], args[1]
    if options[:format] == :json
      puts TwoPass.list_content(vault_name, env: env, format: :json)
    elsif options[:format] == :dotenv
      puts TwoPass.list_content(vault_name, env: env, format: :dotenv)
    else
      puts TwoPass.list_content(vault_name, env: env)
    end
    exit(0)
  when :link
    if args.length < 2 || args.length > 3
      help_message
      exit(1)
    end
    vault_name, target_path, env = args[0], args[1], args[2]
    TwoPass.create_symlink(vault_name, target_path, env: env)
  when :update
    if args.any?
      help_message
      exit(1)
    end
    puts TwoPass.update_cli
  when :help
    help_message
  else
    help_message
    exit(1)
  end
rescue => e
  STDERR.puts e.message

  if ENV["DEBUG"]
    STDERR.puts
    STDERR.puts e.backtrace
  end

  exit(1)
end
