#!/usr/bin/env ruby
# encoding: UTF-8

# resolve bin path, ignoring symlinks
require 'pathname'
bin_file = Pathname.new(__FILE__).realpath

# add self to libpath
$:.unshift File.expand_path('../../lib', bin_file)

require 'kontena_cli'
STDOUT.sync = true

begin
  Kontena::PluginManager.instance.init
  Kontena::MainCommand.run
rescue Excon::Errors::SocketError => exc
  if exc.message.include?('Unable to verify certificate')
    $stderr.puts " [#{Kontena.pastel.red('error')}] The server uses a certificate signed by an unknown authority."
    $stderr.puts "         You can trust this server by copying server CA pem file to: #{Kontena.pastel.yellow("~/.kontena/certs/<hostname>.pem")}"
    $stderr.puts "         Protip: you can bypass the certificate check by setting #{Kontena.pastel.yellow('SSL_IGNORE_ERRORS=true')} env variable, but any data you send to the server could be intercepted by others."
    abort
  else
    abort(exc.message)
  end
rescue Kontena::Errors::StandardError => exc
  raise exc if ENV['DEBUG']
  $stderr.puts " [#{Kontena.pastel.red('error')}] #{exc.message}"
  abort
rescue Errno::EPIPE
  # If user is piping the command outputs to some other command that might exit before CLI has outputted everything
  abort
rescue => exc
  raise exc if ENV['DEBUG']
  $stderr.puts " [#{Kontena.pastel.red('error')}] #{exc.message}"
  $stderr.puts "         Rerun the command with environment DEBUG=true set to get the full exception"
  abort
end
